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

Unified Diff: mojo/edk/system/watcher_set.cc

Issue 1748503002: [mojo-edk] Add MojoWatch and MojoCancelWatch APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 10 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
Index: mojo/edk/system/watcher_set.cc
diff --git a/mojo/edk/system/watcher_set.cc b/mojo/edk/system/watcher_set.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a90020b0de8eb932696666c735e67b55b04f9c4e
--- /dev/null
+++ b/mojo/edk/system/watcher_set.cc
@@ -0,0 +1,58 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/edk/system/watcher_set.h"
+
+#include "mojo/public/c/system/types.h"
+
+namespace mojo {
+namespace edk {
+
+WatcherSet::WatcherSet() {}
+
+WatcherSet::~WatcherSet() {}
+
+void WatcherSet::NotifyForStateChange(const HandleSignalsState& state) {
+ for (const auto& entry : watchers_) {
+ entry.second->NotifyForStateChange(
+ static_cast<MojoHandleSignalsState>(state));
+ }
+}
+
+void WatcherSet::NotifyClosed() {
+ for (const auto& entry : watchers_)
+ entry.second->NotifyClosed();
+}
+
+MojoResult WatcherSet::Add(MojoHandleSignals signals,
+ const Watcher::WatchCallback& callback,
+ uintptr_t context,
+ const HandleSignalsState& current_state) {
+ auto it = watchers_.find(context);
+ if (it != watchers_.end())
+ return MOJO_RESULT_ALREADY_EXISTS;
+
+ if (!current_state.can_satisfy(signals))
+ return MOJO_RESULT_FAILED_PRECONDITION;
+
+ scoped_refptr<Watcher> watcher(new Watcher(signals, callback));
+ watchers_.insert(std::make_pair(context, watcher));
+
+ watcher->NotifyForStateChange(current_state);
+
+ return MOJO_RESULT_OK;
+}
+
+MojoResult WatcherSet::Remove(uintptr_t context) {
+ auto it = watchers_.find(context);
+ if (it == watchers_.end())
+ return MOJO_RESULT_INVALID_ARGUMENT;
+
+ it->second->Cancel();
Anand Mistry (off Chromium) 2016/03/01 07:30:40 Hmm. There's a potential deadlock here. If on thre
Ken Rockot(use gerrit already) 2016/03/01 08:17:44 Good catch. I've changed this so that cancellation
+ watchers_.erase(it);
+ return MOJO_RESULT_OK;
+}
+
+} // namespace edk
+} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698