Chromium Code Reviews| Index: mojo/common/handle_watcher.cc |
| diff --git a/mojo/common/handle_watcher.cc b/mojo/common/handle_watcher.cc |
| index 150305c00f264a9bcf958c693b4de99989b72ce6..3fad4f8479500e80706038d26d9cef05120160f7 100644 |
| --- a/mojo/common/handle_watcher.cc |
| +++ b/mojo/common/handle_watcher.cc |
| @@ -8,12 +8,12 @@ |
| #include "base/atomic_sequence_num.h" |
| #include "base/bind.h" |
| -#include "base/lazy_instance.h" |
| #include "base/memory/weak_ptr.h" |
| #include "base/message_loop/message_loop.h" |
| #include "base/message_loop/message_loop_proxy.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 +27,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 +155,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 +174,7 @@ class WatcherThreadManager { |
| void StopWatching(WatcherID watcher_id); |
| private: |
| - friend struct base::DefaultLazyInstanceTraits<WatcherThreadManager>; |
| - |
| WatcherThreadManager(); |
| - ~WatcherThreadManager(); |
| base::Thread thread_; |
| @@ -184,10 +185,25 @@ class WatcherThreadManager { |
| DISALLOW_COPY_AND_ASSIGN(WatcherThreadManager); |
| }; |
| +struct WatcherThreadManagerData : EnvironmentData::Data { |
| + scoped_ptr<WatcherThreadManager> thread_manager; |
| +}; |
| + |
| +WatcherThreadManager::~WatcherThreadManager() { |
| + thread_.Stop(); |
| +} |
| + |
| WatcherThreadManager* WatcherThreadManager::GetInstance() { |
| - static base::LazyInstance<WatcherThreadManager> instance = |
| - LAZY_INSTANCE_INITIALIZER; |
| - return &instance.Get(); |
| + WatcherThreadManagerData* data = static_cast<WatcherThreadManagerData*>( |
|
darin (slow to review)
2014/03/31 23:21:55
it looks like you have to worry about multiple thr
sky
2014/03/31 23:54:40
Done.
|
| + 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 +245,6 @@ WatcherThreadManager::WatcherThreadManager() |
| thread_.StartWithOptions(thread_options); |
| } |
| -WatcherThreadManager::~WatcherThreadManager() { |
| - thread_.Stop(); |
| -} |
| - |
| } // namespace |
| // HandleWatcher::StartState --------------------------------------------------- |