diff options
author | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-10 10:54:06 +0000 |
---|---|---|
committer | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-10 10:54:06 +0000 |
commit | f886b7bf801cbdd2d9e4b31a8f74bc7490922cd4 (patch) | |
tree | 1ab6d88512e17de10c913cc163a31a7d5b2369bd /base | |
parent | 83c7264800194b0d9ffdce49c57a3fdd5cf6a1a0 (diff) | |
download | chromium_src-f886b7bf801cbdd2d9e4b31a8f74bc7490922cd4.zip chromium_src-f886b7bf801cbdd2d9e4b31a8f74bc7490922cd4.tar.gz chromium_src-f886b7bf801cbdd2d9e4b31a8f74bc7490922cd4.tar.bz2 |
Move a bunch of code from the old to new TLS interface.
Review URL: http://codereview.chromium.org/1660
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1982 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/message_loop.cc | 28 | ||||
-rw-r--r-- | base/message_loop.h | 10 | ||||
-rw-r--r-- | base/thread.cc | 18 | ||||
-rw-r--r-- | base/thread.h | 2 |
4 files changed, 27 insertions, 31 deletions
diff --git a/base/message_loop.cc b/base/message_loop.cc index e90f0b2..3abf0c8 100644 --- a/base/message_loop.cc +++ b/base/message_loop.cc @@ -7,17 +7,16 @@ #include <algorithm> #include "base/compiler_specific.h" +#include "base/lazy_instance.h" #include "base/logging.h" #include "base/message_pump_default.h" #include "base/string_util.h" -#include "base/thread_local_storage.h" +#include "base/thread_local.h" -// a TLS index to the message loop for the current thread -// Note that if we start doing complex stuff in other static initializers -// this could cause problems. -// TODO(evanm): this shouldn't rely on static initialization. -// static -TLSSlot MessageLoop::tls_index_; +// A lazily created thread local storage for quick access to a thread's message +// loop, if one exists. This should be safe and free of static constructors. +static base::LazyInstance<base::ThreadLocalPointer<MessageLoop> > lazy_tls_ptr( + base::LINKER_INITIALIZED); //------------------------------------------------------------------------------ @@ -55,14 +54,22 @@ static LPTOP_LEVEL_EXCEPTION_FILTER GetTopSEHFilter() { //------------------------------------------------------------------------------ +// static +MessageLoop* MessageLoop::current() { + // TODO(darin): sadly, we cannot enable this yet since people call us even + // when they have no intention of using us. + //DCHECK(loop) << "Ouch, did you forget to initialize me?"; + return lazy_tls_ptr.Pointer()->Get(); +} + MessageLoop::MessageLoop(Type type) : type_(type), nestable_tasks_allowed_(true), exception_restoration_(false), state_(NULL), next_sequence_num_(0) { - DCHECK(!tls_index_.Get()) << "should only have one message loop per thread"; - tls_index_.Set(this); + DCHECK(!current()) << "should only have one message loop per thread"; + lazy_tls_ptr.Pointer()->Set(this); // TODO(darin): Choose the pump based on the requested type. #if defined(OS_WIN) @@ -83,6 +90,9 @@ MessageLoop::~MessageLoop() { FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, WillDestroyCurrentMessageLoop()); + // OK, now make it so that no one can find us. + lazy_tls_ptr.Pointer()->Set(NULL); + DCHECK(!state_); // Clean up any unprocessed tasks, but take care: deleting a task could diff --git a/base/message_loop.h b/base/message_loop.h index f4511f8..a65a2e8 100644 --- a/base/message_loop.h +++ b/base/message_loop.h @@ -16,7 +16,6 @@ #include "base/ref_counted.h" #include "base/task.h" #include "base/timer.h" -#include "base/thread_local_storage.h" #if defined(OS_WIN) // We need this to declare base::MessagePumpWin::Dispatcher, which we should @@ -201,13 +200,7 @@ class MessageLoop : public base::MessagePump::Delegate { const std::string& thread_name() const { return thread_name_; } // Returns the MessageLoop object for the current thread, or null if none. - static MessageLoop* current() { - MessageLoop* loop = static_cast<MessageLoop*>(tls_index_.Get()); - // TODO(darin): sadly, we cannot enable this yet since people call us even - // when they have no intention of using us. - //DCHECK(loop) << "Ouch, did you forget to initialize me?"; - return loop; - } + static MessageLoop* current(); // Enables or disables the recursive task processing. This happens in the case // of recursive message loops. Some unwanted message loop may occurs when @@ -347,7 +340,6 @@ class MessageLoop : public base::MessagePump::Delegate { // If message_histogram_ is NULL, this is a no-op. void HistogramEvent(int event); - static TLSSlot tls_index_; static const LinearHistogram::DescriptionPair event_descriptions_[]; static bool enable_histogrammer_; diff --git a/base/thread.cc b/base/thread.cc index 4f68546..fd076a4 100644 --- a/base/thread.cc +++ b/base/thread.cc @@ -4,7 +4,7 @@ #include "base/thread.h" -#include "base/singleton.h" +#include "base/lazy_instance.h" #include "base/string_util.h" #include "base/thread_local.h" #include "base/waitable_event.h" @@ -44,29 +44,25 @@ Thread::~Thread() { Stop(); } +namespace { + // We use this thread-local variable to record whether or not a thread exited // because its Stop method was called. This allows us to catch cases where // MessageLoop::Quit() is called directly, which is unexpected when using a // Thread to setup and run a MessageLoop. -namespace { - -// Use a differentiating type to make sure we don't share our boolean we any -// other Singleton<ThreadLocalBoolean>'s. -struct ThreadExitedDummyDiffType { }; -typedef Singleton<ThreadLocalBoolean, - DefaultSingletonTraits<ThreadLocalBoolean>, - ThreadExitedDummyDiffType> ThreadExitedSingleton; +base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool( + base::LINKER_INITIALIZED); } // namespace void Thread::SetThreadWasQuitProperly(bool flag) { - ThreadExitedSingleton::get()->Set(flag); + lazy_tls_bool.Pointer()->Set(flag); } bool Thread::GetThreadWasQuitProperly() { bool quit_properly = true; #ifndef NDEBUG - quit_properly = ThreadExitedSingleton::get()->Get(); + quit_properly = lazy_tls_bool.Pointer()->Get(); #endif return quit_properly; } diff --git a/base/thread.h b/base/thread.h index 8d08ade..93ba037 100644 --- a/base/thread.h +++ b/base/thread.h @@ -136,8 +136,6 @@ class Thread : PlatformThread::Delegate { // The name of the thread. Used for debugging purposes. std::string name_; - static TLSSlot tls_index_; - friend class ThreadQuitTask; DISALLOW_COPY_AND_ASSIGN(Thread); |