Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: content/child/webthread_impl.cc

Issue 922733002: scheduler: Implement task observers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Build fix. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/child/webthread_impl.h ('k') | content/content_renderer.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 AddTaskObserverInternal(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 RemoveTaskObserverInternal(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) 65 void WebThreadBase::AddTaskObserverInternal(
60 : thread_(new base::Thread(name)) { 66 base::MessageLoop::TaskObserver* observer) {
61 thread_->Start(); 67 base::MessageLoop::current()->AddTaskObserver(observer);
68 }
69
70 void WebThreadBase::RemoveTaskObserverInternal(
71 base::MessageLoop::TaskObserver* observer) {
72 base::MessageLoop::current()->RemoveTaskObserver(observer);
62 } 73 }
63 74
64 // RunWebThreadTask takes the ownership of |task| from base::Closure and 75 // RunWebThreadTask takes the ownership of |task| from base::Closure and
65 // deletes it on the first invocation of the closure for thread-safety. 76 // deletes it on the first invocation of the closure for thread-safety.
66 // base::Closure made from RunWebThreadTask is copyable but Closure::Run 77 // base::Closure made from RunWebThreadTask is copyable but Closure::Run
67 // should be called at most only once. 78 // should be called at most only once.
68 // This is because WebThread::Task can contain RefPtr to a 79 // This is because WebThread::Task can contain RefPtr to a
69 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain 80 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain
70 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here, 81 // RefPtr to WebKit's StringImpl), and if we don't delete |task| here,
71 // it causes a race condition as follows: 82 // it causes a race condition as follows:
72 // [A] In task->run(), more RefPtr's to the refcounted object can be created, 83 // [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 84 // 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. 85 // RefPtr's (as intended) on the thread where the task is executed.
75 // [B] However, base::Closure still retains the ownership of WebThread::Task 86 // [B] However, base::Closure still retains the ownership of WebThread::Task
76 // even after RunWebThreadTask is called. 87 // even after RunWebThreadTask is called.
77 // When base::Closure is deleted, WebThread::Task is deleted and the 88 // When base::Closure is deleted, WebThread::Task is deleted and the
78 // reference counter of the object is decreased by one, possibly from a 89 // reference counter of the object is decreased by one, possibly from a
79 // different thread from [A], which is a race condition. 90 // different thread from [A], which is a race condition.
80 // Taking the ownership of |task| here by using scoped_ptr and base::Passed 91 // 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. 92 // removes the reference counter modification of [B] and the race condition.
82 // When the closure never runs at all, the corresponding WebThread::Task is 93 // 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 94 // 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 95 // are no reference counter modification like [A] (because task->run() is not
85 // executed), so there are no race conditions. 96 // executed), so there are no race conditions.
86 // See https://crbug.com/390851 for more details. 97 // See https://crbug.com/390851 for more details.
87 static void RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { 98 //
88 task->run(); 99 // static
100 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) {
101 task->run();
89 } 102 }
90 103
91 void WebThreadImpl::postTask(Task* task) { 104 void WebThreadBase::postTask(const blink::WebTraceLocation& location,
92 postDelayedTask(task, 0);
93 }
94
95 void WebThreadImpl::postTask(const blink::WebTraceLocation& location,
96 Task* task) { 105 Task* task) {
97 postDelayedTask(location, task, 0); 106 postDelayedTask(location, task, 0);
98 } 107 }
99 108
100 void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) { 109 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, 110 Task* task,
109 long long delay_ms) { 111 long long delay_ms) {
110 tracked_objects::Location location(web_location.functionName(), 112 tracked_objects::Location location(web_location.functionName(),
111 web_location.fileName(), -1, nullptr); 113 web_location.fileName(), -1, nullptr);
112 thread_->message_loop()->PostDelayedTask( 114 TaskRunner()->PostDelayedTask(
113 location, 115 location,
114 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), 116 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
115 base::TimeDelta::FromMilliseconds(delay_ms)); 117 base::TimeDelta::FromMilliseconds(delay_ms));
116 } 118 }
117 119
118 void WebThreadImpl::enterRunLoop() { 120 void WebThreadBase::enterRunLoop() {
119 CHECK(isCurrentThread()); 121 CHECK(isCurrentThread());
120 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting. 122 CHECK(!MessageLoop()->is_running()); // We don't support nesting.
121 thread_->message_loop()->Run(); 123 MessageLoop()->Run();
122 } 124 }
123 125
124 void WebThreadImpl::exitRunLoop() { 126 void WebThreadBase::exitRunLoop() {
125 CHECK(isCurrentThread()); 127 CHECK(isCurrentThread());
126 CHECK(thread_->message_loop()->is_running()); 128 CHECK(MessageLoop()->is_running());
127 thread_->message_loop()->Quit(); 129 MessageLoop()->Quit();
128 } 130 }
129 131
130 bool WebThreadImpl::isCurrentThread() const { 132 bool WebThreadBase::isCurrentThread() const {
131 return thread_->thread_id() == base::PlatformThread::CurrentId(); 133 return TaskRunner()->BelongsToCurrentThread();
132 } 134 }
133 135
134 blink::PlatformThreadId WebThreadImpl::threadId() const { 136 blink::PlatformThreadId WebThreadImpl::threadId() const {
135 return thread_->thread_id(); 137 return thread_->thread_id();
136 } 138 }
137 139
140 WebThreadImpl::WebThreadImpl(const char* name)
141 : thread_(new base::Thread(name)) {
142 thread_->Start();
143 }
144
138 WebThreadImpl::~WebThreadImpl() { 145 WebThreadImpl::~WebThreadImpl() {
139 thread_->Stop(); 146 thread_->Stop();
140 } 147 }
141 148
149 base::MessageLoop* WebThreadImpl::MessageLoop() const {
150 return thread_->message_loop();
151 }
152
153 base::SingleThreadTaskRunner* WebThreadImpl::TaskRunner() const {
154 return thread_->message_loop_proxy().get();
155 }
156
142 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( 157 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
143 scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner) 158 scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner)
144 : owning_thread_task_runner_(owning_thread_task_runner), 159 : owning_thread_task_runner_(owning_thread_task_runner),
145 thread_id_(base::PlatformThread::CurrentId()) { 160 thread_id_(base::PlatformThread::CurrentId()) {
146 } 161 }
147 162
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 { 163 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const {
196 return thread_id_; 164 return thread_id_;
197 } 165 }
198 166
199 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {} 167 WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {
168 }
169
170 base::MessageLoop* WebThreadImplForMessageLoop::MessageLoop() const {
171 DCHECK(isCurrentThread());
172 return base::MessageLoop::current();
173 }
174
175 base::SingleThreadTaskRunner* WebThreadImplForMessageLoop::TaskRunner() const {
176 return owning_thread_task_runner_.get();
177 }
200 178
201 } // namespace content 179 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webthread_impl.h ('k') | content/content_renderer.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698