| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 8 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 | 13 |
| 14 namespace { |
| 15 |
| 16 // 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 // MessageLoop::Quit() is called directly, which is unexpected when using a |
| 19 // Thread to setup and run a MessageLoop. |
| 20 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool( |
| 21 base::LINKER_INITIALIZED); |
| 22 |
| 23 } // namespace |
| 24 |
| 14 // This task is used to trigger the message loop to exit. | 25 // This task is used to trigger the message loop to exit. |
| 15 class ThreadQuitTask : public Task { | 26 class ThreadQuitTask : public Task { |
| 16 public: | 27 public: |
| 17 virtual void Run() { | 28 virtual void Run() { |
| 18 MessageLoop::current()->Quit(); | 29 MessageLoop::current()->Quit(); |
| 19 Thread::SetThreadWasQuitProperly(true); | 30 Thread::SetThreadWasQuitProperly(true); |
| 20 } | 31 } |
| 21 }; | 32 }; |
| 22 | 33 |
| 23 // Used to pass data to ThreadMain. This structure is allocated on the stack | 34 // Used to pass data to ThreadMain. This structure is allocated on the stack |
| (...skipping 17 matching lines...) Expand all Loading... |
| 41 thread_(0), | 52 thread_(0), |
| 42 message_loop_(NULL), | 53 message_loop_(NULL), |
| 43 thread_id_(kInvalidThreadId), | 54 thread_id_(kInvalidThreadId), |
| 44 name_(name) { | 55 name_(name) { |
| 45 } | 56 } |
| 46 | 57 |
| 47 Thread::~Thread() { | 58 Thread::~Thread() { |
| 48 Stop(); | 59 Stop(); |
| 49 } | 60 } |
| 50 | 61 |
| 51 namespace { | |
| 52 | |
| 53 // We use this thread-local variable to record whether or not a thread exited | |
| 54 // because its Stop method was called. This allows us to catch cases where | |
| 55 // MessageLoop::Quit() is called directly, which is unexpected when using a | |
| 56 // Thread to setup and run a MessageLoop. | |
| 57 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool( | |
| 58 base::LINKER_INITIALIZED); | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 void Thread::SetThreadWasQuitProperly(bool flag) { | |
| 63 lazy_tls_bool.Pointer()->Set(flag); | |
| 64 } | |
| 65 | |
| 66 bool Thread::GetThreadWasQuitProperly() { | |
| 67 bool quit_properly = true; | |
| 68 #ifndef NDEBUG | |
| 69 quit_properly = lazy_tls_bool.Pointer()->Get(); | |
| 70 #endif | |
| 71 return quit_properly; | |
| 72 } | |
| 73 | |
| 74 bool Thread::Start() { | 62 bool Thread::Start() { |
| 75 return StartWithOptions(Options()); | 63 return StartWithOptions(Options()); |
| 76 } | 64 } |
| 77 | 65 |
| 78 bool Thread::StartWithOptions(const Options& options) { | 66 bool Thread::StartWithOptions(const Options& options) { |
| 79 DCHECK(!message_loop_); | 67 DCHECK(!message_loop_); |
| 80 | 68 |
| 81 SetThreadWasQuitProperly(false); | 69 SetThreadWasQuitProperly(false); |
| 82 | 70 |
| 83 StartupData startup_data(options); | 71 StartupData startup_data(options); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 return; | 121 return; |
| 134 | 122 |
| 135 stopping_ = true; | 123 stopping_ = true; |
| 136 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); | 124 message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); |
| 137 } | 125 } |
| 138 | 126 |
| 139 void Thread::Run(MessageLoop* message_loop) { | 127 void Thread::Run(MessageLoop* message_loop) { |
| 140 message_loop->Run(); | 128 message_loop->Run(); |
| 141 } | 129 } |
| 142 | 130 |
| 131 void Thread::SetThreadWasQuitProperly(bool flag) { |
| 132 lazy_tls_bool.Pointer()->Set(flag); |
| 133 } |
| 134 |
| 135 bool Thread::GetThreadWasQuitProperly() { |
| 136 bool quit_properly = true; |
| 137 #ifndef NDEBUG |
| 138 quit_properly = lazy_tls_bool.Pointer()->Get(); |
| 139 #endif |
| 140 return quit_properly; |
| 141 } |
| 142 |
| 143 void Thread::ThreadMain() { | 143 void Thread::ThreadMain() { |
| 144 { | 144 { |
| 145 // The message loop for this thread. | 145 // The message loop for this thread. |
| 146 MessageLoop message_loop(startup_data_->options.message_loop_type); | 146 MessageLoop message_loop(startup_data_->options.message_loop_type); |
| 147 | 147 |
| 148 // Complete the initialization of our Thread object. | 148 // Complete the initialization of our Thread object. |
| 149 thread_id_ = PlatformThread::CurrentId(); | 149 thread_id_ = PlatformThread::CurrentId(); |
| 150 PlatformThread::SetName(name_.c_str()); | 150 PlatformThread::SetName(name_.c_str()); |
| 151 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. | 151 ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
| 152 message_loop.set_thread_name(name_); | 152 message_loop.set_thread_name(name_); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 171 | 171 |
| 172 // We can't receive messages anymore. | 172 // We can't receive messages anymore. |
| 173 message_loop_ = NULL; | 173 message_loop_ = NULL; |
| 174 message_loop_proxy_ = NULL; | 174 message_loop_proxy_ = NULL; |
| 175 } | 175 } |
| 176 CleanUpAfterMessageLoopDestruction(); | 176 CleanUpAfterMessageLoopDestruction(); |
| 177 thread_id_ = kInvalidThreadId; | 177 thread_id_ = kInvalidThreadId; |
| 178 } | 178 } |
| 179 | 179 |
| 180 } // namespace base | 180 } // namespace base |
| OLD | NEW |