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 /chrome | |
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 'chrome')
-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 |
5 files changed, 36 insertions, 30 deletions
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_; |