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

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

Issue 923973002: Refactor WebThreadImpl (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
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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 void WebThreadBase::AddTaskObserverInternal( 59 void WebThreadBase::AddTaskObserverInternal(
60 base::MessageLoop::TaskObserver* observer) { 60 base::MessageLoop::TaskObserver* observer) {
61 base::MessageLoop::current()->AddTaskObserver(observer); 61 base::MessageLoop::current()->AddTaskObserver(observer);
62 } 62 }
63 63
64 void WebThreadBase::RemoveTaskObserverInternal( 64 void WebThreadBase::RemoveTaskObserverInternal(
65 base::MessageLoop::TaskObserver* observer) { 65 base::MessageLoop::TaskObserver* observer) {
66 base::MessageLoop::current()->RemoveTaskObserver(observer); 66 base::MessageLoop::current()->RemoveTaskObserver(observer);
67 } 67 }
68 68
69 WebThreadImpl::WebThreadImpl(const char* name)
70 : thread_(new base::Thread(name)) {
71 thread_->Start();
72 }
73
74 // RunWebThreadTask takes the ownership of |task| from base::Closure and 69 // RunWebThreadTask takes the ownership of |task| from base::Closure and
75 // 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.
76 // base::Closure made from RunWebThreadTask is copyable but Closure::Run 71 // base::Closure made from RunWebThreadTask is copyable but Closure::Run
77 // should be called at most only once. 72 // should be called at most only once.
78 // This is because WebThread::Task can contain RefPtr to a 73 // This is because WebThread::Task can contain RefPtr to a
79 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain 74 // thread-unsafe-reference-counted object (e.g. WorkerThreadTask can contain
80 // 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,
81 // it causes a race condition as follows: 76 // it causes a race condition as follows:
82 // [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,
83 // 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
84 // 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.
85 // [B] However, base::Closure still retains the ownership of WebThread::Task 80 // [B] However, base::Closure still retains the ownership of WebThread::Task
86 // even after RunWebThreadTask is called. 81 // even after RunWebThreadTask is called.
87 // When base::Closure is deleted, WebThread::Task is deleted and the 82 // When base::Closure is deleted, WebThread::Task is deleted and the
88 // 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
89 // different thread from [A], which is a race condition. 84 // different thread from [A], which is a race condition.
90 // 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
91 // removes the reference counter modification of [B] and the race condition. 86 // removes the reference counter modification of [B] and the race condition.
92 // 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
93 // 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
94 // 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
95 // executed), so there are no race conditions. 90 // executed), so there are no race conditions.
96 // See https://crbug.com/390851 for more details. 91 // See https://crbug.com/390851 for more details.
97 // 92 //
98 // static 93 // static
99 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) { 94 void WebThreadBase::RunWebThreadTask(scoped_ptr<blink::WebThread::Task> task) {
100 task->run(); 95 task->run();
101 } 96 }
102 97
103 void WebThreadImpl::postTask(Task* task) { 98 void WebThreadBase::postTask(const blink::WebTraceLocation& location,
104 postDelayedTask(task, 0);
105 }
106
107 void WebThreadImpl::postTask(const blink::WebTraceLocation& location,
108 Task* task) { 99 Task* task) {
109 postDelayedTask(location, task, 0); 100 postDelayedTask(location, task, 0);
110 } 101 }
111 102
112 void WebThreadImpl::postDelayedTask(Task* task, long long delay_ms) { 103 void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location,
113 thread_->message_loop()->PostDelayedTask(
114 FROM_HERE,
115 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
116 base::TimeDelta::FromMilliseconds(delay_ms));
117 }
118
119 void WebThreadImpl::postDelayedTask(const blink::WebTraceLocation& web_location,
120 Task* task, 104 Task* task,
121 long long delay_ms) { 105 long long delay_ms) {
122 tracked_objects::Location location(web_location.functionName(), 106 tracked_objects::Location location(web_location.functionName(),
123 web_location.fileName(), -1, nullptr); 107 web_location.fileName(), -1, nullptr);
124 thread_->message_loop()->PostDelayedTask( 108 TaskRunner()->PostDelayedTask(
125 location, 109 location,
126 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))), 110 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
127 base::TimeDelta::FromMilliseconds(delay_ms)); 111 base::TimeDelta::FromMilliseconds(delay_ms));
128 } 112 }
129 113
130 void WebThreadImpl::enterRunLoop() { 114 void WebThreadBase::enterRunLoop() {
131 CHECK(isCurrentThread()); 115 CHECK(isCurrentThread());
132 CHECK(!thread_->message_loop()->is_running()); // We don't support nesting. 116 CHECK(!MessageLoop()->is_running()); // We don't support nesting.
133 thread_->message_loop()->Run(); 117 MessageLoop()->Run();
134 } 118 }
135 119
136 void WebThreadImpl::exitRunLoop() { 120 void WebThreadBase::exitRunLoop() {
137 CHECK(isCurrentThread()); 121 CHECK(isCurrentThread());
138 CHECK(thread_->message_loop()->is_running()); 122 CHECK(MessageLoop()->is_running());
139 thread_->message_loop()->Quit(); 123 MessageLoop()->Quit();
140 } 124 }
141 125
142 bool WebThreadImpl::isCurrentThread() const { 126 bool WebThreadBase::isCurrentThread() const {
143 return thread_->thread_id() == base::PlatformThread::CurrentId(); 127 return TaskRunner()->BelongsToCurrentThread();
144 } 128 }
145 129
146 blink::PlatformThreadId WebThreadImpl::threadId() const { 130 blink::PlatformThreadId WebThreadImpl::threadId() const {
147 return thread_->thread_id(); 131 return thread_->thread_id();
148 } 132 }
149 133
134 WebThreadImpl::WebThreadImpl(const char* name)
135 : thread_(new base::Thread(name)) {
136 thread_->Start();
137 }
138
150 WebThreadImpl::~WebThreadImpl() { 139 WebThreadImpl::~WebThreadImpl() {
151 thread_->Stop(); 140 thread_->Stop();
152 } 141 }
153 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
154 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop( 151 WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
155 scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner) 152 scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner)
156 : owning_thread_task_runner_(owning_thread_task_runner), 153 : owning_thread_task_runner_(owning_thread_task_runner),
157 thread_id_(base::PlatformThread::CurrentId()) { 154 thread_id_(base::PlatformThread::CurrentId()) {
158 } 155 }
159 156
160 void WebThreadImplForMessageLoop::postTask(Task* task) {
161 postDelayedTask(task, 0);
162 }
163
164 void WebThreadImplForMessageLoop::postTask(
165 const blink::WebTraceLocation& location,
166 Task* task) {
167 postDelayedTask(location, task, 0);
168 }
169
170 void WebThreadImplForMessageLoop::postDelayedTask(Task* task,
171 long long delay_ms) {
172 owning_thread_task_runner_->PostDelayedTask(
173 FROM_HERE,
174 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
175 base::TimeDelta::FromMilliseconds(delay_ms));
176 }
177
178 void WebThreadImplForMessageLoop::postDelayedTask(
179 const blink::WebTraceLocation& web_location,
180 Task* task,
181 long long delay_ms) {
182 tracked_objects::Location location(web_location.functionName(),
183 web_location.fileName(), -1, nullptr);
184 owning_thread_task_runner_->PostDelayedTask(
185 location,
186 base::Bind(RunWebThreadTask, base::Passed(make_scoped_ptr(task))),
187 base::TimeDelta::FromMilliseconds(delay_ms));
188 }
189
190 void WebThreadImplForMessageLoop::enterRunLoop() {
191 CHECK(isCurrentThread());
192 // We don't support nesting.
193 CHECK(!base::MessageLoop::current()->is_running());
194 base::MessageLoop::current()->Run();
195 }
196
197 void WebThreadImplForMessageLoop::exitRunLoop() {
198 CHECK(isCurrentThread());
199 CHECK(base::MessageLoop::current()->is_running());
200 base::MessageLoop::current()->Quit();
201 }
202
203 bool WebThreadImplForMessageLoop::isCurrentThread() const {
204 return owning_thread_task_runner_->BelongsToCurrentThread();
205 }
206
207 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const { 157 blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const {
208 return thread_id_; 158 return thread_id_;
209 } 159 }
210 160
211 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 }
212 172
213 } // namespace content 173 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webthread_impl.h ('k') | content/renderer/scheduler/webthread_impl_for_scheduler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698