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

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

Powered by Google App Engine
This is Rietveld 408576698