Index: ui/aura/env.cc |
diff --git a/ui/aura/env.cc b/ui/aura/env.cc |
index 6fa86da46270299fc470ab318286c42ae32ddba6..c850f03ac2819adb1d1309cbd6ccf7098cb331c7 100644 |
--- a/ui/aura/env.cc |
+++ b/ui/aura/env.cc |
@@ -4,6 +4,8 @@ |
#include "ui/aura/env.h" |
+#include "base/lazy_instance.h" |
+#include "base/threading/thread_local.h" |
#include "ui/aura/env_observer.h" |
#include "ui/aura/input_state_lookup.h" |
#include "ui/events/event_target_iterator.h" |
@@ -11,41 +13,34 @@ |
namespace aura { |
-// static |
-Env* Env::instance_ = NULL; |
+namespace { |
-//////////////////////////////////////////////////////////////////////////////// |
-// Env, public: |
+// Env is thread local so that aura may be used on multiple threads. |
+base::LazyInstance<base::ThreadLocalPointer<Env> >::Leaky lazy_tls_ptr = |
+ LAZY_INSTANCE_INITIALIZER; |
-Env::Env() |
- : mouse_button_flags_(0), |
- is_touch_down_(false), |
- input_state_lookup_(InputStateLookup::Create().Pass()) { |
-} |
+} // namespace |
-Env::~Env() { |
- FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv()); |
-} |
+//////////////////////////////////////////////////////////////////////////////// |
+// Env, public: |
-//static |
+// static |
void Env::CreateInstance(bool create_event_source) { |
- if (!instance_) { |
- instance_ = new Env; |
- instance_->Init(create_event_source); |
- } |
+ if (!lazy_tls_ptr.Pointer()->Get()) |
+ (new Env())->Init(create_event_source); |
} |
// static |
Env* Env::GetInstance() { |
- DCHECK(instance_) << "Env::CreateInstance must be called before getting " |
- "the instance of Env."; |
- return instance_; |
+ Env* env = lazy_tls_ptr.Pointer()->Get(); |
+ DCHECK(env) << "Env::CreateInstance must be called before getting the " |
+ "instance of Env."; |
+ return env; |
} |
// static |
void Env::DeleteInstance() { |
- delete instance_; |
- instance_ = NULL; |
+ delete lazy_tls_ptr.Pointer()->Get(); |
} |
void Env::AddObserver(EnvObserver* observer) { |
@@ -64,6 +59,20 @@ bool Env::IsMouseButtonDown() const { |
//////////////////////////////////////////////////////////////////////////////// |
// Env, private: |
+Env::Env() |
+ : mouse_button_flags_(0), |
+ is_touch_down_(false), |
+ input_state_lookup_(InputStateLookup::Create().Pass()) { |
+ DCHECK(lazy_tls_ptr.Pointer()->Get() == NULL); |
+ lazy_tls_ptr.Pointer()->Set(this); |
+} |
+ |
+Env::~Env() { |
+ FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv()); |
+ DCHECK_EQ(this, lazy_tls_ptr.Pointer()->Get()); |
+ lazy_tls_ptr.Pointer()->Set(NULL); |
+} |
+ |
void Env::Init(bool create_event_source) { |
if (create_event_source && !ui::PlatformEventSource::GetInstance()) |
event_source_ = ui::PlatformEventSource::CreateDefault(); |