OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
| 6 #define MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
| 7 |
| 8 #include <unordered_map> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "mojo/edk/system/dispatcher.h" |
| 12 #include "mojo/edk/system/handle_signals_state.h" |
| 13 #include "mojo/public/c/system/types.h" |
| 14 |
| 15 namespace mojo { |
| 16 namespace edk { |
| 17 |
| 18 // A WatcherSet maintains a set of callbacks to be notified in the event of |
| 19 // handle state changes. Note that callbacks are never run directly but are |
| 20 // instead attached as finalizers on a RequestContext. This ensures that the |
| 21 // callbacks can safely reenter the EDK. |
| 22 class WatcherSet { |
| 23 public: |
| 24 WatcherSet(); |
| 25 ~WatcherSet(); |
| 26 |
| 27 // Notifies any stored watchers of a state change if they're watching for some |
| 28 // signals that are now satisfied or permanently unsatisfiable. Any |
| 29 // notifications to dispatch are added to |request_context|'s list of |
| 30 // finalizers to ensure that callbacks can safely re-enter the EDK. |
| 31 void NotifyOfStateChange(const HandleSignalsState& state, |
| 32 Dispatcher::RequestContext* request_context); |
| 33 |
| 34 // Cancels and removes all watchers. All removed watchers will have their |
| 35 // callbacks invoked on |request_context| finalization to notify the watcher |
| 36 // of cancellation. |
| 37 void CancelAll(Dispatcher::RequestContext* request_context); |
| 38 |
| 39 // Adds a new watcher to watch for signals in |signals| to be satisfied or |
| 40 // unsatisfiable. |current_state| is the current signals state of the |
| 41 // handle being watched. |
| 42 // |
| 43 // If the handle currently satisfies any signals in |signals|, the callback |
| 44 // will be invoked on |request_context| finalization. |
| 45 MojoResult Add(MojoHandleSignals signals, |
| 46 const Dispatcher::WatchCallback& callback, |
| 47 uintptr_t context, |
| 48 const HandleSignalsState& current_state, |
| 49 Dispatcher::RequestContext* request_context); |
| 50 |
| 51 MojoResult Remove(uintptr_t context); |
| 52 |
| 53 private: |
| 54 struct Watcher { |
| 55 Watcher(MojoHandleSignals signals, |
| 56 const Dispatcher::WatchCallback& callback); |
| 57 ~Watcher(); |
| 58 |
| 59 MojoHandleSignals signals; |
| 60 Dispatcher::WatchCallback callback; |
| 61 }; |
| 62 |
| 63 // If |current_state| satisfies signals in which |watcher| is interested, a |
| 64 // finalizer is added to |request_context| which invokes the watcher's |
| 65 // callback. |
| 66 void MaybeNotifyWatcher(const Watcher& watcher, |
| 67 const MojoHandleSignalsState& current_state, |
| 68 Dispatcher::RequestContext* request_context); |
| 69 |
| 70 // A map of watchers keyed on context value. |
| 71 std::unordered_map<uintptr_t, Watcher> watchers_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(WatcherSet); |
| 74 }; |
| 75 |
| 76 } // namespace edk |
| 77 } // namespace mojo |
| 78 |
| 79 #endif // MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
OLD | NEW |