Index: mojo/common/handle_watcher.cc |
diff --git a/mojo/common/handle_watcher.cc b/mojo/common/handle_watcher.cc |
index 150305c00f264a9bcf958c693b4de99989b72ce6..a95669c5803056037723de81df2a84e391fd7881 100644 |
--- a/mojo/common/handle_watcher.cc |
+++ b/mojo/common/handle_watcher.cc |
@@ -12,8 +12,10 @@ |
#include "base/memory/weak_ptr.h" |
#include "base/message_loop/message_loop.h" |
#include "base/message_loop/message_loop_proxy.h" |
+#include "base/synchronization/lock.h" |
#include "base/threading/thread.h" |
#include "base/time/time.h" |
+#include "mojo/common/environment_data.h" |
#include "mojo/common/message_pump_mojo.h" |
#include "mojo/common/message_pump_mojo_handler.h" |
#include "mojo/common/time_helper.h" |
@@ -27,6 +29,8 @@ namespace { |
const char kWatcherThreadName[] = "handle-watcher-thread"; |
+const char kWatcherThreadManagerKey[] = "watcher-thread-manager"; |
+ |
// TODO(sky): this should be unnecessary once MessageLoop has been refactored. |
MessagePumpMojo* message_pump_mojo = NULL; |
@@ -153,6 +157,8 @@ void WatcherBackend::OnHandleError(const Handle& handle, MojoResult result) { |
// to be ready. All requests are handled by WatcherBackend. |
class WatcherThreadManager { |
public: |
+ ~WatcherThreadManager(); |
+ |
// Returns the shared instance. |
static WatcherThreadManager* GetInstance(); |
@@ -170,10 +176,7 @@ class WatcherThreadManager { |
void StopWatching(WatcherID watcher_id); |
private: |
- friend struct base::DefaultLazyInstanceTraits<WatcherThreadManager>; |
- |
WatcherThreadManager(); |
- ~WatcherThreadManager(); |
base::Thread thread_; |
@@ -184,10 +187,29 @@ class WatcherThreadManager { |
DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager); |
}; |
+struct WatcherThreadManagerData : EnvironmentData::Data { |
+ scoped_ptr<WatcherThreadManager> thread_manager; |
+}; |
+ |
+WatcherThreadManager::~WatcherThreadManager() { |
+ thread_.Stop(); |
+} |
+ |
+static base::LazyInstance<base::Lock> thread_lookup_lock = |
+ LAZY_INSTANCE_INITIALIZER; |
+ |
WatcherThreadManager* WatcherThreadManager::GetInstance() { |
- static base::LazyInstance<WatcherThreadManager> instance = |
- LAZY_INSTANCE_INITIALIZER; |
- return &instance.Get(); |
+ base::AutoLock auto_lock(thread_lookup_lock.Get()); |
+ WatcherThreadManagerData* data = static_cast<WatcherThreadManagerData*>( |
+ EnvironmentData::GetInstance()->GetData(kWatcherThreadManagerKey)); |
+ if (!data) { |
+ data = new WatcherThreadManagerData; |
+ data->thread_manager.reset(new WatcherThreadManager); |
+ EnvironmentData::GetInstance()->SetData( |
+ kWatcherThreadManagerKey, |
+ scoped_ptr<EnvironmentData::Data>(data)); |
+ } |
+ return data->thread_manager.get(); |
} |
WatcherID WatcherThreadManager::StartWatching( |
@@ -229,10 +251,6 @@ WatcherThreadManager::WatcherThreadManager() |
thread_.StartWithOptions(thread_options); |
} |
-WatcherThreadManager::~WatcherThreadManager() { |
- thread_.Stop(); |
-} |
- |
} // namespace |
// HandleWatcher::StartState --------------------------------------------------- |