Index: mojo/edk/system/watcher_set.h |
diff --git a/mojo/edk/system/watcher_set.h b/mojo/edk/system/watcher_set.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..efbe82c2a2746002b794b73006e27a543620abcd |
--- /dev/null |
+++ b/mojo/edk/system/watcher_set.h |
@@ -0,0 +1,79 @@ |
+// 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. |
+ |
+#ifndef MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
+#define MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
+ |
+#include <unordered_map> |
+ |
+#include "base/callback.h" |
+#include "mojo/edk/system/dispatcher.h" |
+#include "mojo/edk/system/handle_signals_state.h" |
+#include "mojo/public/c/system/types.h" |
+ |
+namespace mojo { |
+namespace edk { |
+ |
+// A WatcherSet maintains a set of callbacks to be notified in the event of |
+// handle state changes. Note that callbacks are never run directly but are |
+// instead attached as finalizers on a RequestContext. This ensures that the |
+// callbacks can safely reenter the EDK. |
+class WatcherSet { |
+ public: |
+ WatcherSet(); |
+ ~WatcherSet(); |
+ |
+ // Notifies any stored watchers of a state change if they're watching for some |
+ // signals that are now satisfied or permanently unsatisfiable. Any |
+ // notifications to dispatch are added to |request_context|'s list of |
+ // finalizers to ensure that callbacks can safely re-enter the EDK. |
+ void NotifyOfStateChange(const HandleSignalsState& state, |
+ Dispatcher::RequestContext* request_context); |
+ |
+ // Cancels and removes all watchers. All removed watchers will have their |
+ // callbacks invoked on |request_context| finalization to notify the watcher |
+ // of cancellation. |
+ void CancelAll(Dispatcher::RequestContext* request_context); |
+ |
+ // Adds a new watcher to watch for signals in |signals| to be satisfied or |
+ // unsatisfiable. |current_state| is the current signals state of the |
+ // handle being watched. |
+ // |
+ // If the handle currently satisfies any signals in |signals|, the callback |
+ // will be invoked on |request_context| finalization. |
+ MojoResult Add(MojoHandleSignals signals, |
+ const Dispatcher::WatchCallback& callback, |
+ uintptr_t context, |
+ const HandleSignalsState& current_state, |
+ Dispatcher::RequestContext* request_context); |
+ |
+ MojoResult Remove(uintptr_t context); |
+ |
+ private: |
+ struct Watcher { |
+ Watcher(MojoHandleSignals signals, |
+ const Dispatcher::WatchCallback& callback); |
+ ~Watcher(); |
+ |
+ MojoHandleSignals signals; |
+ Dispatcher::WatchCallback callback; |
+ }; |
+ |
+ // If |current_state| satisfies signals in which |watcher| is interested, a |
+ // finalizer is added to |request_context| which invokes the watcher's |
+ // callback. |
+ void MaybeNotifyWatcher(const Watcher& watcher, |
+ const MojoHandleSignalsState& current_state, |
+ Dispatcher::RequestContext* request_context); |
+ |
+ // A map of watchers keyed on context value. |
+ std::unordered_map<uintptr_t, Watcher> watchers_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WatcherSet); |
+}; |
+ |
+} // namespace edk |
+} // namespace mojo |
+ |
+#endif // MOJO_EDK_SYSTEM_WATCHER_SET_H_ |