Index: mojo/public/cpp/bindings/lib/sync_handle_registry.cc |
diff --git a/mojo/public/cpp/bindings/lib/sync_handle_watcher.cc b/mojo/public/cpp/bindings/lib/sync_handle_registry.cc |
similarity index 71% |
copy from mojo/public/cpp/bindings/lib/sync_handle_watcher.cc |
copy to mojo/public/cpp/bindings/lib/sync_handle_registry.cc |
index 13ab4918c915d686f118bdf7aaaf8c521851ca50..3d88ec96da7c8270d92c5855de9118da103dc9b2 100644 |
--- a/mojo/public/cpp/bindings/lib/sync_handle_watcher.cc |
+++ b/mojo/public/cpp/bindings/lib/sync_handle_registry.cc |
@@ -2,7 +2,7 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "mojo/public/cpp/bindings/lib/sync_handle_watcher.h" |
+#include "mojo/public/cpp/bindings/lib/sync_handle_registry.h" |
#include "base/lazy_instance.h" |
#include "base/logging.h" |
@@ -14,25 +14,24 @@ namespace mojo { |
namespace internal { |
namespace { |
-base::LazyInstance<base::ThreadLocalPointer<SyncHandleWatcher>> |
+base::LazyInstance<base::ThreadLocalPointer<SyncHandleRegistry>> |
g_current_sync_handle_watcher = LAZY_INSTANCE_INITIALIZER; |
} // namespace |
// static |
-SyncHandleWatcher* SyncHandleWatcher::current() { |
- SyncHandleWatcher* result = g_current_sync_handle_watcher.Pointer()->Get(); |
+SyncHandleRegistry* SyncHandleRegistry::current() { |
+ SyncHandleRegistry* result = g_current_sync_handle_watcher.Pointer()->Get(); |
if (!result) { |
- // This object will be destroyed when the current message loop goes away. |
- result = new SyncHandleWatcher(); |
+ result = new SyncHandleRegistry(); |
DCHECK_EQ(result, g_current_sync_handle_watcher.Pointer()->Get()); |
} |
return result; |
} |
-bool SyncHandleWatcher::RegisterHandle(const Handle& handle, |
- MojoHandleSignals handle_signals, |
- const HandleCallback& callback) { |
+bool SyncHandleRegistry::RegisterHandle(const Handle& handle, |
+ MojoHandleSignals handle_signals, |
+ const HandleCallback& callback) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
if (ContainsKey(handles_, handle)) |
@@ -47,19 +46,19 @@ bool SyncHandleWatcher::RegisterHandle(const Handle& handle, |
return true; |
} |
-void SyncHandleWatcher::UnregisterHandle(const Handle& handle) { |
+void SyncHandleRegistry::UnregisterHandle(const Handle& handle) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
- DCHECK(ContainsKey(handles_, handle)); |
+ if (!ContainsKey(handles_, handle)) |
+ return; |
MojoResult result = |
MojoRemoveHandle(wait_set_handle_.get().value(), handle.value()); |
DCHECK_EQ(MOJO_RESULT_OK, result); |
- |
handles_.erase(handle); |
} |
-bool SyncHandleWatcher::WatchAllHandles(const bool* should_stop[], |
- size_t count) { |
+bool SyncHandleRegistry::WatchAllHandles(const bool* should_stop[], |
+ size_t count) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
MojoResult result; |
@@ -67,7 +66,10 @@ bool SyncHandleWatcher::WatchAllHandles(const bool* should_stop[], |
MojoHandle ready_handle; |
MojoResult ready_handle_result; |
- while (true) { |
+ // This object may be destroyed during a callback. So we have to preserve |
+ // the boolean. |
+ scoped_refptr<base::RefCountedData<bool>> destroyed = destroyed_; |
+ while (!destroyed->data) { |
for (size_t i = 0; i < count; ++i) |
if (*should_stop[i]) |
return true; |
@@ -91,10 +93,11 @@ bool SyncHandleWatcher::WatchAllHandles(const bool* should_stop[], |
iter->second.Run(ready_handle_result); |
}; |
- return true; |
+ return false; |
} |
-SyncHandleWatcher::SyncHandleWatcher() { |
+SyncHandleRegistry::SyncHandleRegistry() |
+ : destroyed_(new base::RefCountedData<bool>(false)) { |
MojoHandle handle; |
MojoResult result = MojoCreateWaitSet(&handle); |
CHECK_EQ(MOJO_RESULT_OK, result); |
@@ -107,17 +110,18 @@ SyncHandleWatcher::SyncHandleWatcher() { |
base::MessageLoop::current()->AddDestructionObserver(this); |
} |
-SyncHandleWatcher::~SyncHandleWatcher() { |
+SyncHandleRegistry::~SyncHandleRegistry() { |
DCHECK(thread_checker_.CalledOnValidThread()); |
- DCHECK(handles_.empty()); |
+ destroyed_->data = true; |
g_current_sync_handle_watcher.Pointer()->Set(nullptr); |
} |
-void SyncHandleWatcher::WillDestroyCurrentMessageLoop() { |
+void SyncHandleRegistry::WillDestroyCurrentMessageLoop() { |
DCHECK(thread_checker_.CalledOnValidThread()); |
DCHECK_EQ(this, g_current_sync_handle_watcher.Pointer()->Get()); |
base::MessageLoop::current()->RemoveDestructionObserver(this); |
+ |
delete this; |
} |