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
8 #include "content/child/webthread_impl.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/pending_task.h"
14 #include "base/threading/platform_thread.h"
18 WebThreadBase::WebThreadBase() {}
19 WebThreadBase::~WebThreadBase() {}
21 class WebThreadBase::TaskObserverAdapter
22 : public base::MessageLoop::TaskObserver
{
24 TaskObserverAdapter(WebThread::TaskObserver
* observer
)
25 : observer_(observer
) {}
27 void WillProcessTask(const base::PendingTask
& pending_task
) override
{
28 observer_
->willProcessTask();
31 void DidProcessTask(const base::PendingTask
& pending_task
) override
{
32 observer_
->didProcessTask();
36 WebThread::TaskObserver
* observer_
;
39 void WebThreadBase::addTaskObserver(TaskObserver
* observer
) {
40 CHECK(isCurrentThread());
41 std::pair
<TaskObserverMap::iterator
, bool> result
= task_observer_map_
.insert(
42 std::make_pair(observer
, static_cast<TaskObserverAdapter
*>(NULL
)));
44 result
.first
->second
= new TaskObserverAdapter(observer
);
45 base::MessageLoop::current()->AddTaskObserver(result
.first
->second
);
48 void WebThreadBase::removeTaskObserver(TaskObserver
* observer
) {
49 CHECK(isCurrentThread());
50 TaskObserverMap::iterator iter
= task_observer_map_
.find(observer
);
51 if (iter
== task_observer_map_
.end())
53 base::MessageLoop::current()->RemoveTaskObserver(iter
->second
);
55 task_observer_map_
.erase(iter
);
58 WebThreadImpl::WebThreadImpl(const char* name
)
59 : thread_(new base::Thread(name
)) {
63 // RunWebThreadTask takes the ownership of |task| from base::Closure and
64 // deletes it on the first invocation of the closure for thread-safety.
65 // base::Closure made from RunWebThreadTask is copyable but Closure::Run
66 // should be called at most only once.
67 // This is because WebThread::Task can contain RefPtr to a
68 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain
69 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here,
70 // it causes a race condition as follows:
71 // [A] In task->run(), more RefPtr's to the refcounted object can be created,
72 // and the reference counter of the object can be modified via these
73 // RefPtr's (as intended) on the thread where the task is executed.
74 // [B] However, base::Closure still retains the ownership of WebThread::Task
75 // even after RunWebThreadTask is called.
76 // When base::Closure is deleted, WebThread::Task is deleted and the
77 // reference counter of the object is decreased by one, possibly from a
78 // different thread from [A], which is a race condition.
79 // Taking the ownership of |task| here by using scoped_ptr and base::Passed
80 // removes the reference counter modification of [B] and the race condition.
81 // When the closure never runs at all, the corresponding WebThread::Task is
82 // destructed when base::Closure is deleted (like [B]). In this case, there
83 // are no reference counter modification like [A] (because task->run() is not
84 // executed), so there are no race conditions.
85 // See https://crbug.com/390851 for more details.
86 static void RunWebThreadTask(scoped_ptr
<blink::WebThread::Task
> task
) {
90 void WebThreadImpl::postTask(Task
* task
) {
91 thread_
->message_loop()->PostTask(
93 base::Bind(RunWebThreadTask
, base::Passed(scoped_ptr
<Task
>(task
))));
96 void WebThreadImpl::postDelayedTask(Task
* task
, long long delay_ms
) {
97 thread_
->message_loop()->PostDelayedTask(
99 base::Bind(RunWebThreadTask
, base::Passed(scoped_ptr
<Task
>(task
))),
100 base::TimeDelta::FromMilliseconds(delay_ms
));
103 void WebThreadImpl::enterRunLoop() {
104 CHECK(isCurrentThread());
105 CHECK(!thread_
->message_loop()->is_running()); // We don't support nesting.
106 thread_
->message_loop()->Run();
109 void WebThreadImpl::exitRunLoop() {
110 CHECK(isCurrentThread());
111 CHECK(thread_
->message_loop()->is_running());
112 thread_
->message_loop()->Quit();
115 bool WebThreadImpl::isCurrentThread() const {
116 return thread_
->thread_id() == base::PlatformThread::CurrentId();
119 blink::PlatformThreadId
WebThreadImpl::threadId() const {
120 return thread_
->thread_id();
123 WebThreadImpl::~WebThreadImpl() {
127 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
128 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
)
129 : main_thread_task_runner_(main_thread_task_runner
),
130 thread_id_(base::PlatformThread::CurrentId()) {}
132 void WebThreadImplForMessageLoop::postTask(Task
* task
) {
133 main_thread_task_runner_
->PostTask(
135 base::Bind(RunWebThreadTask
, base::Passed(make_scoped_ptr(task
))));
138 void WebThreadImplForMessageLoop::postDelayedTask(Task
* task
,
139 long long delay_ms
) {
140 main_thread_task_runner_
->PostDelayedTask(
142 base::Bind(RunWebThreadTask
, base::Passed(make_scoped_ptr(task
))),
143 base::TimeDelta::FromMilliseconds(delay_ms
));
146 void WebThreadImplForMessageLoop::enterRunLoop() {
147 CHECK(isCurrentThread());
148 // We don't support nesting.
149 CHECK(!base::MessageLoop::current()->is_running());
150 base::MessageLoop::current()->Run();
153 void WebThreadImplForMessageLoop::exitRunLoop() {
154 CHECK(isCurrentThread());
155 CHECK(base::MessageLoop::current()->is_running());
156 base::MessageLoop::current()->Quit();
159 bool WebThreadImplForMessageLoop::isCurrentThread() const {
160 return main_thread_task_runner_
->BelongsToCurrentThread();
163 blink::PlatformThreadId
WebThreadImplForMessageLoop::threadId() const {
167 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {}
169 } // namespace content