Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / webthread_impl.cc
blob572e5890bb53409078ac70da898081855c491450
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.
5 // An implementation of WebThread in terms of base::MessageLoop and
6 // base::Thread
8 #include "content/child/webthread_impl.h"
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 "third_party/WebKit/public/platform/WebTraceLocation.h"
16 namespace content {
18 class WebThreadBase::TaskObserverAdapter
19 : public base::MessageLoop::TaskObserver {
20 public:
21 TaskObserverAdapter(WebThread::TaskObserver* observer)
22 : observer_(observer) {}
24 void WillProcessTask(const base::PendingTask& pending_task) override {
25 observer_->willProcessTask();
28 void DidProcessTask(const base::PendingTask& pending_task) override {
29 observer_->didProcessTask();
32 private:
33 WebThread::TaskObserver* observer_;
36 WebThreadBase::WebThreadBase() {
39 WebThreadBase::~WebThreadBase() {
40 for (auto& observer_entry : task_observer_map_) {
41 delete observer_entry.second;
45 void WebThreadBase::addTaskObserver(TaskObserver* observer) {
46 CHECK(isCurrentThread());
47 std::pair<TaskObserverMap::iterator, bool> result = task_observer_map_.insert(
48 std::make_pair(observer, static_cast<TaskObserverAdapter*>(NULL)));
49 if (result.second)
50 result.first->second = new TaskObserverAdapter(observer);
51 AddTaskObserverInternal(result.first->second);
54 void WebThreadBase::removeTaskObserver(TaskObserver* observer) {
55 CHECK(isCurrentThread());
56 TaskObserverMap::iterator iter = task_observer_map_.find(observer);
57 if (iter == task_observer_map_.end())
58 return;
59 RemoveTaskObserverInternal(iter->second);
60 delete iter->second;
61 task_observer_map_.erase(iter);
64 void WebThreadBase::AddTaskObserverInternal(
65 base::MessageLoop::TaskObserver* observer) {
66 base::MessageLoop::current()->AddTaskObserver(observer);
69 void WebThreadBase::RemoveTaskObserverInternal(
70 base::MessageLoop::TaskObserver* observer) {
71 base::MessageLoop::current()->RemoveTaskObserver(observer);
74 // RunWebThreadTask takes the ownership of |task| from base::Closure and
75 // deletes it on the first invocation of the closure for thread-safety.
76 // base::Closure made from RunWebThreadTask is copyable but Closure::Run
77 // should be called at most only once.
78 // This is because WebThread::Task can contain RefPtr to a
79 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain
80 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here,
81 // it causes a race condition as follows:
82 // [A] In task->run(), more RefPtr's to the refcounted object can be created,
83 // and the reference counter of the object can be modified via these
84 // RefPtr's (as intended) on the thread where the task is executed.
85 // [B] However, base::Closure still retains the ownership of WebThread::Task
86 // even after RunWebThreadTask is called.
87 // When base::Closure is deleted, WebThread::Task is deleted and the
88 // reference counter of the object is decreased by one, possibly from a
89 // different thread from [A], which is a race condition.
90 // Taking the ownership of |task| here by using scoped_ptr and base::Passed
91 // removes the reference counter modification of [B] and the race condition.
92 // When the closure never runs at all, the corresponding WebThread::Task is
93 // destructed when base::Closure is deleted (like [B]). In this case, there
94 // are no reference counter modification like [A] (because task->run() is not
95 // executed), so there are no race conditions.
96 // See https://crbug.com/390851 for more details.
98 // static
99 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) {
100 task->run();
103 void WebThreadBase::postTask(const blink::WebTraceLocation& location,
104 Task* task) {
105 postDelayedTask(location, task, 0);
108 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location,
109 Task* task,
110 long long delay_ms) {
111 tracked_objects::Location location(web_location.functionName(),
112 web_location.fileName(), -1, nullptr);
113 TaskRunner()->PostDelayedTask(
114 location,
115 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
116 base::TimeDelta::FromMilliseconds(delay_ms));
119 void WebThreadBase::enterRunLoop() {
120 CHECK(isCurrentThread());
121 CHECK(MessageLoop());
122 CHECK(!MessageLoop()->is_running()); // We don't support nesting.
123 MessageLoop()->Run();
126 void WebThreadBase::exitRunLoop() {
127 CHECK(isCurrentThread());
128 CHECK(MessageLoop());
129 CHECK(MessageLoop()->is_running());
130 MessageLoop()->Quit();
133 bool WebThreadBase::isCurrentThread() const {
134 return TaskRunner()->BelongsToCurrentThread();
137 blink::PlatformThreadId WebThreadImpl::threadId() const {
138 return thread_->thread_id();
141 WebThreadImpl::WebThreadImpl(const char* name)
142 : thread_(new base::Thread(name)) {
143 thread_->Start();
146 WebThreadImpl::~WebThreadImpl() {
147 thread_->Stop();
150 base::MessageLoop* WebThreadImpl::MessageLoop() const {
151 return nullptr;
154 base::SingleThreadTaskRunner* WebThreadImpl::TaskRunner() const {
155 return thread_->message_loop_proxy().get();
158 } // namespace content