| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 9 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
| 10 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // We use this thread-local variable to record whether or not a thread exited | 17 // We use this thread-local variable to record whether or not a thread exited |
| 17 // because its Stop method was called. This allows us to catch cases where | 18 // because its Stop method was called. This allows us to catch cases where |
| 18 // MessageLoop::Quit() is called directly, which is unexpected when using a | 19 // MessageLoop::Quit() is called directly, which is unexpected when using a |
| 19 // Thread to setup and run a MessageLoop. | 20 // Thread to setup and run a MessageLoop. |
| 20 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool = | 21 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool = |
| 21 LAZY_INSTANCE_INITIALIZER; | 22 LAZY_INSTANCE_INITIALIZER; |
| 22 | 23 |
| 23 } // namespace | 24 } // namespace |
| 24 | 25 |
| 25 // This task is used to trigger the message loop to exit. | 26 // This is used to trigger the message loop to exit. |
| 26 class ThreadQuitTask : public Task { | 27 void ThreadQuitHelper() { |
| 27 public: | 28 MessageLoop::current()->Quit(); |
| 28 virtual void Run() { | 29 Thread::SetThreadWasQuitProperly(true); |
| 29 MessageLoop::current()->Quit(); | 30 } |
| 30 Thread::SetThreadWasQuitProperly(true); | |
| 31 } | |
| 32 }; | |
| 33 | 31 |
| 34 // Used to pass data to ThreadMain. This structure is allocated on the stack | 32 // Used to pass data to ThreadMain. This structure is allocated on the stack |
| 35 // from within StartWithOptions. | 33 // from within StartWithOptions. |
| 36 struct Thread::StartupData { | 34 struct Thread::StartupData { |
| 37 // We get away with a const reference here because of how we are allocated. | 35 // We get away with a const reference here because of how we are allocated. |
| 38 const Thread::Options& options; | 36 const Thread::Options& options; |
| 39 | 37 |
| 40 // Used to synchronize thread startup. | 38 // Used to synchronize thread startup. |
| 41 WaitableEvent event; | 39 WaitableEvent event; |
| 42 | 40 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 // We should only be called on the same thread that started us. | 112 // We should only be called on the same thread that started us. |
| 115 | 113 |
| 116 // Reading thread_id_ without a lock can lead to a benign data race | 114 // Reading thread_id_ without a lock can lead to a benign data race |
| 117 // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer. | 115 // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer. |
| 118 DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_), PlatformThread::CurrentId()); | 116 DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_), PlatformThread::CurrentId()); |
| 119 | 117 |
| 120 if (stopping_ || !message_loop_) | 118 if (stopping_ || !message_loop_) |
| 121 return; | 119 return; |
| 122 | 120 |
| 123 stopping_ = true; | 121 stopping_ = true; |
| 124 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); | 122 message_loop_->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
| 125 } | 123 } |
| 126 | 124 |
| 127 void Thread::Run(MessageLoop* message_loop) { | 125 void Thread::Run(MessageLoop* message_loop) { |
| 128 message_loop->Run(); | 126 message_loop->Run(); |
| 129 } | 127 } |
| 130 | 128 |
| 131 void Thread::SetThreadWasQuitProperly(bool flag) { | 129 void Thread::SetThreadWasQuitProperly(bool flag) { |
| 132 lazy_tls_bool.Pointer()->Set(flag); | 130 lazy_tls_bool.Pointer()->Set(flag); |
| 133 } | 131 } |
| 134 | 132 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 158 | 156 |
| 159 startup_data_->event.Signal(); | 157 startup_data_->event.Signal(); |
| 160 // startup_data_ can't be touched anymore since the starting thread is now | 158 // startup_data_ can't be touched anymore since the starting thread is now |
| 161 // unlocked. | 159 // unlocked. |
| 162 | 160 |
| 163 Run(message_loop_); | 161 Run(message_loop_); |
| 164 | 162 |
| 165 // Let the thread do extra cleanup. | 163 // Let the thread do extra cleanup. |
| 166 CleanUp(); | 164 CleanUp(); |
| 167 | 165 |
| 168 // Assert that MessageLoop::Quit was called by ThreadQuitTask. | 166 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
| 169 DCHECK(GetThreadWasQuitProperly()); | 167 DCHECK(GetThreadWasQuitProperly()); |
| 170 | 168 |
| 171 // We can't receive messages anymore. | 169 // We can't receive messages anymore. |
| 172 message_loop_ = NULL; | 170 message_loop_ = NULL; |
| 173 } | 171 } |
| 174 thread_id_ = kInvalidThreadId; | 172 thread_id_ = kInvalidThreadId; |
| 175 } | 173 } |
| 176 | 174 |
| 177 } // namespace base | 175 } // namespace base |
| OLD | NEW |