Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1296)

Unified Diff: base/message_loop.cc

Issue 1660: Move a bunch of code from the old to new TLS interface. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/message_loop.h ('k') | base/thread.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/message_loop.cc
===================================================================
--- base/message_loop.cc (revision 1894)
+++ base/message_loop.cc (working copy)
@@ -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
+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)
@@ -84,7 +91,7 @@
WillDestroyCurrentMessageLoop());
// OK, now make it so that no one can find us.
- tls_index_.Set(NULL);
+ lazy_tls_ptr.Pointer()->Set(NULL);
DCHECK(!state_);
« no previous file with comments | « base/message_loop.h ('k') | base/thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698