OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/threading/thread.h" | 5 #include "base/threading/thread.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 if (message_loop_ && !stopping_) | 216 if (message_loop_ && !stopping_) |
217 return true; | 217 return true; |
218 // Otherwise check the |running_| flag, which is set to true by the new thread | 218 // Otherwise check the |running_| flag, which is set to true by the new thread |
219 // only while it is inside Run(). | 219 // only while it is inside Run(). |
220 AutoLock lock(running_lock_); | 220 AutoLock lock(running_lock_); |
221 return running_; | 221 return running_; |
222 } | 222 } |
223 | 223 |
224 void Thread::Run(RunLoop* run_loop) { | 224 void Thread::Run(RunLoop* run_loop) { |
225 // Overridable protected method to be called from our |thread_| only. | 225 // Overridable protected method to be called from our |thread_| only. |
| 226 DCHECK(id_event_.IsSignaled()); |
226 DCHECK_EQ(id_, PlatformThread::CurrentId()); | 227 DCHECK_EQ(id_, PlatformThread::CurrentId()); |
227 | 228 |
228 run_loop->Run(); | 229 run_loop->Run(); |
229 } | 230 } |
230 | 231 |
231 // static | 232 // static |
232 void Thread::SetThreadWasQuitProperly(bool flag) { | 233 void Thread::SetThreadWasQuitProperly(bool flag) { |
233 lazy_tls_bool.Pointer()->Set(flag); | 234 lazy_tls_bool.Pointer()->Set(flag); |
234 } | 235 } |
235 | 236 |
(...skipping 20 matching lines...) Expand all Loading... |
256 DCHECK(!IsRunning()); | 257 DCHECK(!IsRunning()); |
257 message_loop_ = message_loop; | 258 message_loop_ = message_loop; |
258 DCHECK(IsRunning()); | 259 DCHECK(IsRunning()); |
259 | 260 |
260 using_external_message_loop_ = true; | 261 using_external_message_loop_ = true; |
261 } | 262 } |
262 | 263 |
263 void Thread::ThreadMain() { | 264 void Thread::ThreadMain() { |
264 // First, make GetThreadId() available to avoid deadlocks. It could be called | 265 // First, make GetThreadId() available to avoid deadlocks. It could be called |
265 // any place in the following thread initialization code. | 266 // any place in the following thread initialization code. |
| 267 DCHECK(!id_event_.IsSignaled()); |
| 268 // Note: this read of |id_| while |id_event_| isn't signaled is exceptionally |
| 269 // okay because ThreadMain has an happens-after relationship with the other |
| 270 // write in StartWithOptions(). |
| 271 DCHECK_EQ(kInvalidThreadId, id_); |
266 id_ = PlatformThread::CurrentId(); | 272 id_ = PlatformThread::CurrentId(); |
267 DCHECK_NE(kInvalidThreadId, id_); | 273 DCHECK_NE(kInvalidThreadId, id_); |
268 id_event_.Signal(); | 274 id_event_.Signal(); |
269 | 275 |
270 // Complete the initialization of our Thread object. | 276 // Complete the initialization of our Thread object. |
271 PlatformThread::SetName(name_.c_str()); | 277 PlatformThread::SetName(name_.c_str()); |
272 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 278 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
273 | 279 |
274 // Lazily initialize the |message_loop| so that it can run on this thread. | 280 // Lazily initialize the |message_loop| so that it can run on this thread. |
275 DCHECK(message_loop_); | 281 DCHECK(message_loop_); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 run_loop_ = nullptr; | 331 run_loop_ = nullptr; |
326 } | 332 } |
327 | 333 |
328 void Thread::ThreadQuitHelper() { | 334 void Thread::ThreadQuitHelper() { |
329 DCHECK(run_loop_); | 335 DCHECK(run_loop_); |
330 run_loop_->QuitWhenIdle(); | 336 run_loop_->QuitWhenIdle(); |
331 SetThreadWasQuitProperly(true); | 337 SetThreadWasQuitProperly(true); |
332 } | 338 } |
333 | 339 |
334 } // namespace base | 340 } // namespace base |
OLD | NEW |