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"
15 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
19 class WebThreadBase::TaskObserverAdapter
20 : public base::MessageLoop::TaskObserver
{
22 TaskObserverAdapter(WebThread::TaskObserver
* observer
)
23 : observer_(observer
) {}
25 void WillProcessTask(const base::PendingTask
& pending_task
) override
{
26 observer_
->willProcessTask();
29 void DidProcessTask(const base::PendingTask
& pending_task
) override
{
30 observer_
->didProcessTask();
34 WebThread::TaskObserver
* observer_
;
37 WebThreadBase::WebThreadBase() {
40 WebThreadBase::~WebThreadBase() {
41 for (auto& observer_entry
: task_observer_map_
) {
42 delete observer_entry
.second
;
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
)));
51 result
.first
->second
= new TaskObserverAdapter(observer
);
52 AddTaskObserverInternal(result
.first
->second
);
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())
60 RemoveTaskObserverInternal(iter
->second
);
62 task_observer_map_
.erase(iter
);
65 void WebThreadBase::AddTaskObserverInternal(
66 base::MessageLoop::TaskObserver
* observer
) {
67 base::MessageLoop::current()->AddTaskObserver(observer
);
70 void WebThreadBase::RemoveTaskObserverInternal(
71 base::MessageLoop::TaskObserver
* observer
) {
72 base::MessageLoop::current()->RemoveTaskObserver(observer
);
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.
100 void WebThreadBase::RunWebThreadTask(scoped_ptr
<blink::WebThread::Task
> task
) {
104 void WebThreadBase::postTask(const blink::WebTraceLocation
& location
,
106 postDelayedTask(location
, task
, 0);
109 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation
& web_location
,
111 long long delay_ms
) {
112 tracked_objects::Location
location(web_location
.functionName(),
113 web_location
.fileName(), -1, nullptr);
114 TaskRunner()->PostDelayedTask(
116 base::Bind(RunWebThreadTask
, base::Passed(make_scoped_ptr(task
))),
117 base::TimeDelta::FromMilliseconds(delay_ms
));
120 void WebThreadBase::enterRunLoop() {
121 CHECK(isCurrentThread());
122 CHECK(!MessageLoop()->is_running()); // We don't support nesting.
123 MessageLoop()->Run();
126 void WebThreadBase::exitRunLoop() {
127 CHECK(isCurrentThread());
128 CHECK(MessageLoop()->is_running());
129 MessageLoop()->Quit();
132 bool WebThreadBase::isCurrentThread() const {
133 return TaskRunner()->BelongsToCurrentThread();
136 blink::PlatformThreadId
WebThreadImpl::threadId() const {
137 return thread_
->thread_id();
140 WebThreadImpl::WebThreadImpl(const char* name
)
141 : thread_(new base::Thread(name
)) {
145 WebThreadImpl::~WebThreadImpl() {
149 base::MessageLoop
* WebThreadImpl::MessageLoop() const {
150 return thread_
->message_loop();
153 base::SingleThreadTaskRunner
* WebThreadImpl::TaskRunner() const {
154 return thread_
->message_loop_proxy().get();
157 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
158 scoped_refptr
<base::SingleThreadTaskRunner
> owning_thread_task_runner
)
159 : owning_thread_task_runner_(owning_thread_task_runner
),
160 thread_id_(base::PlatformThread::CurrentId()) {
163 blink::PlatformThreadId
WebThreadImplForMessageLoop::threadId() const {
167 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {
170 base::MessageLoop
* WebThreadImplForMessageLoop::MessageLoop() const {
171 DCHECK(isCurrentThread());
172 return base::MessageLoop::current();
175 base::SingleThreadTaskRunner
* WebThreadImplForMessageLoop::TaskRunner() const {
176 return owning_thread_task_runner_
.get();
179 } // namespace content