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

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

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