| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/thread.h" | 5 #include "base/thread.h" |
| 6 | 6 |
| 7 #include "base/singleton.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/thread_local.h" | 9 #include "base/thread_local.h" |
| 10 #include "base/waitable_event.h" | 10 #include "base/waitable_event.h" |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 | 13 |
| 14 // This task is used to trigger the message loop to exit. | 14 // This task is used to trigger the message loop to exit. |
| 15 class ThreadQuitTask : public Task { | 15 class ThreadQuitTask : public Task { |
| 16 public: | 16 public: |
| 17 virtual void Run() { | 17 virtual void Run() { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 37 thread_(0), | 37 thread_(0), |
| 38 message_loop_(NULL), | 38 message_loop_(NULL), |
| 39 thread_id_(0), | 39 thread_id_(0), |
| 40 name_(name) { | 40 name_(name) { |
| 41 } | 41 } |
| 42 | 42 |
| 43 Thread::~Thread() { | 43 Thread::~Thread() { |
| 44 Stop(); | 44 Stop(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 namespace { |
| 48 |
| 47 // We use this thread-local variable to record whether or not a thread exited | 49 // We use this thread-local variable to record whether or not a thread exited |
| 48 // because its Stop method was called. This allows us to catch cases where | 50 // because its Stop method was called. This allows us to catch cases where |
| 49 // MessageLoop::Quit() is called directly, which is unexpected when using a | 51 // MessageLoop::Quit() is called directly, which is unexpected when using a |
| 50 // Thread to setup and run a MessageLoop. | 52 // Thread to setup and run a MessageLoop. |
| 51 namespace { | 53 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool( |
| 52 | 54 base::LINKER_INITIALIZED); |
| 53 // Use a differentiating type to make sure we don't share our boolean we any | |
| 54 // other Singleton<ThreadLocalBoolean>'s. | |
| 55 struct ThreadExitedDummyDiffType { }; | |
| 56 typedef Singleton<ThreadLocalBoolean, | |
| 57 DefaultSingletonTraits<ThreadLocalBoolean>, | |
| 58 ThreadExitedDummyDiffType> ThreadExitedSingleton; | |
| 59 | 55 |
| 60 } // namespace | 56 } // namespace |
| 61 | 57 |
| 62 void Thread::SetThreadWasQuitProperly(bool flag) { | 58 void Thread::SetThreadWasQuitProperly(bool flag) { |
| 63 ThreadExitedSingleton::get()->Set(flag); | 59 lazy_tls_bool.Pointer()->Set(flag); |
| 64 } | 60 } |
| 65 | 61 |
| 66 bool Thread::GetThreadWasQuitProperly() { | 62 bool Thread::GetThreadWasQuitProperly() { |
| 67 bool quit_properly = true; | 63 bool quit_properly = true; |
| 68 #ifndef NDEBUG | 64 #ifndef NDEBUG |
| 69 quit_properly = ThreadExitedSingleton::get()->Get(); | 65 quit_properly = lazy_tls_bool.Pointer()->Get(); |
| 70 #endif | 66 #endif |
| 71 return quit_properly; | 67 return quit_properly; |
| 72 } | 68 } |
| 73 | 69 |
| 74 bool Thread::Start() { | 70 bool Thread::Start() { |
| 75 return StartWithOptions(Options()); | 71 return StartWithOptions(Options()); |
| 76 } | 72 } |
| 77 | 73 |
| 78 bool Thread::StartWithOptions(const Options& options) { | 74 bool Thread::StartWithOptions(const Options& options) { |
| 79 DCHECK(!message_loop_); | 75 DCHECK(!message_loop_); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 CleanUp(); | 159 CleanUp(); |
| 164 | 160 |
| 165 // Assert that MessageLoop::Quit was called by ThreadQuitTask. | 161 // Assert that MessageLoop::Quit was called by ThreadQuitTask. |
| 166 DCHECK(GetThreadWasQuitProperly()); | 162 DCHECK(GetThreadWasQuitProperly()); |
| 167 | 163 |
| 168 // We can't receive messages anymore. | 164 // We can't receive messages anymore. |
| 169 message_loop_ = NULL; | 165 message_loop_ = NULL; |
| 170 } | 166 } |
| 171 | 167 |
| 172 } // namespace base | 168 } // namespace base |
| OLD | NEW |