diff options
-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 | ||||
-rw-r--r-- | chrome/common/ipc_sync_channel.cc | 21 | ||||
-rw-r--r-- | chrome/common/notification_service.cc | 15 | ||||
-rw-r--r-- | chrome/common/notification_service.h | 9 | ||||
-rw-r--r-- | chrome/renderer/render_thread.cc | 14 | ||||
-rw-r--r-- | chrome/renderer/render_thread.h | 7 |
9 files changed, 63 insertions, 61 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); diff --git a/chrome/common/ipc_sync_channel.cc b/chrome/common/ipc_sync_channel.cc index 83beec0..fec8965 100644 --- a/chrome/common/ipc_sync_channel.cc +++ b/chrome/common/ipc_sync_channel.cc @@ -6,8 +6,9 @@ #include "chrome/common/ipc_sync_channel.h" +#include "base/lazy_instance.h" #include "base/logging.h" -#include "base/thread_local_storage.h" +#include "base/thread_local.h" #include "chrome/common/child_process.h" #include "chrome/common/ipc_logging.h" #include "chrome/common/ipc_sync_message.h" @@ -31,9 +32,7 @@ namespace IPC { // SyncChannel objects on the same thread (since one object can receive a // sync message while another one is blocked). -// Holds a pointer to the per-thread ReceivedSyncMsgQueue object. -// TODO(evanm): this shouldn't rely on static initialization. -static TLSSlot g_tls_index; +class SyncChannel::ReceivedSyncMsgQueue; class SyncChannel::ReceivedSyncMsgQueue : public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> { @@ -45,10 +44,10 @@ class SyncChannel::ReceivedSyncMsgQueue : } ~ReceivedSyncMsgQueue() { - DCHECK(g_tls_index.Get()); + DCHECK(lazy_tls_ptr_.Pointer()->Get()); DCHECK(MessageLoop::current() == listener_message_loop_); CloseHandle(blocking_event_); - g_tls_index.Set(NULL); + lazy_tls_ptr_.Pointer()->Set(NULL); } // Called on IPC thread when a synchronous message or reply arrives. @@ -155,6 +154,10 @@ class SyncChannel::ReceivedSyncMsgQueue : HANDLE blocking_event() { return blocking_event_; } MessageLoop* listener_message_loop() { return listener_message_loop_; } + // Holds a pointer to the per-thread ReceivedSyncMsgQueue object. + static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> > + lazy_tls_ptr_; + private: // Called on the ipc thread to check if we can unblock any current Send() // calls based on a queued reply. @@ -203,6 +206,8 @@ class SyncChannel::ReceivedSyncMsgQueue : std::vector<Reply> received_replies_; }; +base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> > + SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_(base::LINKER_INITIALIZED); SyncChannel::SyncContext::SyncContext( Channel::Listener* listener, @@ -213,13 +218,13 @@ SyncChannel::SyncContext::SyncContext( reply_deserialize_result_(false) { // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple // SyncChannel objects that can block the same thread). - received_sync_msgs_ = static_cast<ReceivedSyncMsgQueue*>(g_tls_index.Get()); + received_sync_msgs_ = ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Get(); if (!received_sync_msgs_) { // Stash a pointer to the listener thread's ReceivedSyncMsgQueue, as we // need to be able to access it in the IPC thread. received_sync_msgs_ = new ReceivedSyncMsgQueue(); - g_tls_index.Set(received_sync_msgs_); + ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(received_sync_msgs_); } // Addref manually so that we can ensure destruction on the listener thread diff --git a/chrome/common/notification_service.cc b/chrome/common/notification_service.cc index 5bf54c3..dccadbe 100644 --- a/chrome/common/notification_service.cc +++ b/chrome/common/notification_service.cc @@ -4,9 +4,16 @@ #include "chrome/common/notification_service.h" -// TODO(evanm): This shouldn't depend on static initialization. +#include "base/lazy_instance.h" +#include "base/thread_local.h" + +static base::LazyInstance<base::ThreadLocalPointer<NotificationService> > + lazy_tls_ptr(base::LINKER_INITIALIZED); + // static -TLSSlot NotificationService::tls_index_; +NotificationService* NotificationService::current() { + return lazy_tls_ptr.Pointer()->Get(); +} // static bool NotificationService::HasKey(const NotificationSourceMap& map, @@ -20,7 +27,7 @@ NotificationService::NotificationService() { memset(observer_counts_, 0, sizeof(observer_counts_)); #endif - tls_index_.Set(this); + lazy_tls_ptr.Pointer()->Set(this); } void NotificationService::AddObserver(NotificationObserver* observer, @@ -94,7 +101,7 @@ void NotificationService::Notify(NotificationType type, NotificationService::~NotificationService() { - tls_index_.Set(NULL); + lazy_tls_ptr.Pointer()->Set(NULL); #ifndef NDEBUG for (int i = 0; i < NOTIFICATION_TYPE_COUNT; i++) { diff --git a/chrome/common/notification_service.h b/chrome/common/notification_service.h index 60fd77e..170baa6 100644 --- a/chrome/common/notification_service.h +++ b/chrome/common/notification_service.h @@ -12,7 +12,6 @@ #include <map> #include "base/observer_list.h" -#include "base/thread_local_storage.h" #include "base/values.h" #include "chrome/common/notification_details.h" #include "chrome/common/notification_source.h" @@ -24,9 +23,7 @@ class NotificationService { public: // Returns the NotificationService object for the current thread, or NULL if // none. - static NotificationService* current() { - return static_cast<NotificationService *>(tls_index_.Get()); - } + static NotificationService* current(); // Normally instantiated when the thread is created. Not all threads have // a NotificationService. Only one instance should be created per thread. @@ -91,10 +88,6 @@ class NotificationService { // a simple array is probably the fastest way to dispatch. NotificationSourceMap observers_[NOTIFICATION_TYPE_COUNT]; - // The thread local storage index, used for getting the current thread's - // instance. - static TLSSlot tls_index_; - #ifndef NDEBUG // Used to check to see that AddObserver and RemoveObserver calls are // balanced. diff --git a/chrome/renderer/render_thread.cc b/chrome/renderer/render_thread.cc index fb70dd4..97f71b5 100644 --- a/chrome/renderer/render_thread.cc +++ b/chrome/renderer/render_thread.cc @@ -7,7 +7,9 @@ #include "chrome/renderer/render_thread.h" +#include "base/lazy_instance.h" #include "base/shared_memory.h" +#include "base/thread_local.h" #include "chrome/common/ipc_logging.h" #include "chrome/common/notification_service.h" #include "chrome/plugin/plugin_channel.h" @@ -22,13 +24,17 @@ static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */; // V8 needs a 1MB stack size. static const size_t kStackSize = 1024 * 1024; -// TODO(evanm): don't rely on static initialization. -// static -TLSSlot RenderThread::tls_index_; +static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > + lazy_tls_ptr(base::LINKER_INITIALIZED); //----------------------------------------------------------------------------- // Methods below are only called on the owner's thread: +// static +RenderThread* RenderThread::current() { + return lazy_tls_ptr.Pointer()->Get(); +} + RenderThread::RenderThread(const std::wstring& channel_name) : Thread("Chrome_RenderThread"), channel_name_(channel_name), @@ -99,7 +105,7 @@ void RenderThread::Init() { IPC::Channel::MODE_CLIENT, this, NULL, owner_loop_, true, RenderProcess::GetShutDownEvent())); - tls_index_.Set(this); + lazy_tls_ptr.Pointer()->Set(this); // The renderer thread should wind-up COM. CoInitialize(0); diff --git a/chrome/renderer/render_thread.h b/chrome/renderer/render_thread.h index 89b203f..50e022a 100644 --- a/chrome/renderer/render_thread.h +++ b/chrome/renderer/render_thread.h @@ -9,7 +9,6 @@ #include "base/shared_memory.h" #include "base/task.h" #include "base/thread.h" -#include "base/thread_local_storage.h" #include "chrome/common/ipc_sync_channel.h" #include "chrome/common/message_router.h" @@ -48,9 +47,7 @@ class RenderThread : public IPC::Channel::Listener, void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter); // The RenderThread instance for the current thread. - static RenderThread* current() { - return static_cast<RenderThread*>(tls_index_.Get()); - } + static RenderThread* current(); VisitedLinkSlave* visited_link_slave() const { return visited_link_slave_; } @@ -94,8 +91,6 @@ class RenderThread : public IPC::Channel::Listener, // decisions about how to allocation resources using current information. void InformHostOfCacheStats(); - static TLSSlot tls_index_; - // The message loop used to run tasks on the thread that started this thread. MessageLoop* owner_loop_; |