OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // An implementation of WebThread in terms of base::MessageLoop and |
| 6 // base::Thread |
| 7 |
| 8 #include "content/child/webthread_base.h" |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/pending_task.h" |
| 13 #include "base/threading/platform_thread.h" |
| 14 #include "content/child/scheduler/single_thread_idle_task_runner.h" |
| 15 #include "third_party/WebKit/public/platform/WebTraceLocation.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 class WebThreadBase::TaskObserverAdapter |
| 20 : public base::MessageLoop::TaskObserver { |
| 21 public: |
| 22 TaskObserverAdapter(WebThread::TaskObserver* observer) |
| 23 : observer_(observer) {} |
| 24 |
| 25 void WillProcessTask(const base::PendingTask& pending_task) override { |
| 26 observer_->willProcessTask(); |
| 27 } |
| 28 |
| 29 void DidProcessTask(const base::PendingTask& pending_task) override { |
| 30 observer_->didProcessTask(); |
| 31 } |
| 32 |
| 33 private: |
| 34 WebThread::TaskObserver* observer_; |
| 35 }; |
| 36 |
| 37 WebThreadBase::WebThreadBase() { |
| 38 } |
| 39 |
| 40 WebThreadBase::~WebThreadBase() { |
| 41 for (auto& observer_entry : task_observer_map_) { |
| 42 delete observer_entry.second; |
| 43 } |
| 44 } |
| 45 |
| 46 void WebThreadBase::addTaskObserver(TaskObserver* observer) { |
| 47 CHECK(isCurrentThread()); |
| 48 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert( |
| 49 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL))); |
| 50 if (result.second) |
| 51 result.first->second = new TaskObserverAdapter(observer); |
| 52 AddTaskObserverInternal(result.first->second); |
| 53 } |
| 54 |
| 55 void WebThreadBase::removeTaskObserver(TaskObserver* observer) { |
| 56 CHECK(isCurrentThread()); |
| 57 TaskObserverMap::iterator iter = task_observer_map_.find(observer); |
| 58 if (iter == task_observer_map_.end()) |
| 59 return; |
| 60 RemoveTaskObserverInternal(iter->second); |
| 61 delete iter->second; |
| 62 task_observer_map_.erase(iter); |
| 63 } |
| 64 |
| 65 void WebThreadBase::AddTaskObserverInternal( |
| 66 base::MessageLoop::TaskObserver* observer) { |
| 67 base::MessageLoop::current()->AddTaskObserver(observer); |
| 68 } |
| 69 |
| 70 void WebThreadBase::RemoveTaskObserverInternal( |
| 71 base::MessageLoop::TaskObserver* observer) { |
| 72 base::MessageLoop::current()->RemoveTaskObserver(observer); |
| 73 } |
| 74 |
| 75 // RunWebThreadTask takes the ownership of |task| from base::Closure and |
| 76 // deletes it on the first invocation of the closure for thread-safety. |
| 77 // base::Closure made from RunWebThreadTask is copyable but Closure::Run |
| 78 // should be called at most only once. |
| 79 // This is because WebThread::Task can contain RefPtr to a |
| 80 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain |
| 81 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here, |
| 82 // it causes a race condition as follows: |
| 83 // [A] In task->run(), more RefPtr's to the refcounted object can be created, |
| 84 // and the reference counter of the object can be modified via these |
| 85 // RefPtr's (as intended) on the thread where the task is executed. |
| 86 // [B] However, base::Closure still retains the ownership of WebThread::Task |
| 87 // even after RunWebThreadTask is called. |
| 88 // When base::Closure is deleted, WebThread::Task is deleted and the |
| 89 // reference counter of the object is decreased by one, possibly from a |
| 90 // different thread from [A], which is a race condition. |
| 91 // Taking the ownership of |task| here by using scoped_ptr and base::Passed |
| 92 // removes the reference counter modification of [B] and the race condition. |
| 93 // When the closure never runs at all, the corresponding WebThread::Task is |
| 94 // destructed when base::Closure is deleted (like [B]). In this case, there |
| 95 // are no reference counter modification like [A] (because task->run() is not |
| 96 // executed), so there are no race conditions. |
| 97 // See https://crbug.com/390851 for more details. |
| 98 // |
| 99 // static |
| 100 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { |
| 101 task->run(); |
| 102 } |
| 103 |
| 104 // static |
| 105 void WebThreadBase::RunWebThreadIdleTask( |
| 106 scoped_ptr<blink::WebThread::IdleTask> idle_task, |
| 107 base::TimeTicks deadline) { |
| 108 idle_task->run((deadline - base::TimeTicks()).InSecondsF()); |
| 109 } |
| 110 |
| 111 void WebThreadBase::postTask(const blink::WebTraceLocation& location, |
| 112 Task* task) { |
| 113 postDelayedTask(location, task, 0); |
| 114 } |
| 115 |
| 116 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location, |
| 117 Task* task, |
| 118 long long delay_ms) { |
| 119 tracked_objects::Location location(web_location.functionName(), |
| 120 web_location.fileName(), -1, nullptr); |
| 121 TaskRunner()->PostDelayedTask( |
| 122 location, |
| 123 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), |
| 124 base::TimeDelta::FromMilliseconds(delay_ms)); |
| 125 } |
| 126 |
| 127 void WebThreadBase::postIdleTask(const blink::WebTraceLocation& web_location, |
| 128 IdleTask* idle_task) { |
| 129 tracked_objects::Location location(web_location.functionName(), |
| 130 web_location.fileName(), -1, nullptr); |
| 131 IdleTaskRunner()->PostIdleTask( |
| 132 location, base::Bind(&WebThreadBase::RunWebThreadIdleTask, |
| 133 base::Passed(make_scoped_ptr(idle_task)))); |
| 134 } |
| 135 |
| 136 void WebThreadBase::postIdleTaskAfterWakeup( |
| 137 const blink::WebTraceLocation& web_location, |
| 138 IdleTask* idle_task) { |
| 139 tracked_objects::Location location(web_location.functionName(), |
| 140 web_location.fileName(), -1, nullptr); |
| 141 IdleTaskRunner()->PostIdleTaskAfterWakeup( |
| 142 location, base::Bind(&WebThreadBase::RunWebThreadIdleTask, |
| 143 base::Passed(make_scoped_ptr(idle_task)))); |
| 144 } |
| 145 |
| 146 void WebThreadBase::enterRunLoop() { |
| 147 CHECK(isCurrentThread()); |
| 148 CHECK(MessageLoop()); |
| 149 CHECK(!MessageLoop()->is_running()); // We don't support nesting. |
| 150 MessageLoop()->Run(); |
| 151 } |
| 152 |
| 153 void WebThreadBase::exitRunLoop() { |
| 154 CHECK(isCurrentThread()); |
| 155 CHECK(MessageLoop()); |
| 156 CHECK(MessageLoop()->is_running()); |
| 157 MessageLoop()->Quit(); |
| 158 } |
| 159 |
| 160 bool WebThreadBase::isCurrentThread() const { |
| 161 return TaskRunner()->BelongsToCurrentThread(); |
| 162 } |
| 163 |
| 164 } // namespace content |
OLD | NEW |