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