OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_EDK_SYSTEM_WATCHER_SET_H_ | 5 #ifndef MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
6 #define MOJO_EDK_SYSTEM_WATCHER_SET_H_ | 6 #define MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
7 | 7 |
8 #include <unordered_map> | 8 #include <map> |
9 | 9 |
10 #include "base/callback.h" | |
11 #include "base/macros.h" | 10 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/optional.h" |
13 #include "mojo/edk/system/handle_signals_state.h" | 13 #include "mojo/edk/system/handle_signals_state.h" |
14 #include "mojo/edk/system/watcher.h" | 14 #include "mojo/edk/system/watcher_dispatcher.h" |
15 #include "mojo/public/c/system/types.h" | |
16 | 15 |
17 namespace mojo { | 16 namespace mojo { |
18 namespace edk { | 17 namespace edk { |
19 | 18 |
20 // A WatcherSet maintains a set of Watchers attached to a single handle and | 19 // A WatcherSet maintains a set of references to WatcherDispatchers to be |
21 // keyed on an arbitrary user context. | 20 // notified when a handle changes state. |
| 21 // |
| 22 // Dispatchers which may be watched by a watcher should own a WatcherSet and |
| 23 // notify it of all relevant state changes. |
22 class WatcherSet { | 24 class WatcherSet { |
23 public: | 25 public: |
24 WatcherSet(); | 26 // |owner| is the Dispatcher who owns this WatcherSet. |
| 27 explicit WatcherSet(Dispatcher* owner); |
25 ~WatcherSet(); | 28 ~WatcherSet(); |
26 | 29 |
27 // Notifies all Watchers of a state change. | 30 // Notifies all watchers of the handle's current signals state. |
28 void NotifyForStateChange(const HandleSignalsState& state); | 31 void NotifyState(const HandleSignalsState& state); |
29 | 32 |
30 // Notifies all Watchers that their watched handle has been closed. | 33 // Notifies all watchers that this handle has been closed. |
31 void NotifyClosed(); | 34 void NotifyClosed(); |
32 | 35 |
33 // Adds a new watcher to watch for signals in |signals| to be satisfied or | 36 // Adds a new watcher+context. |
34 // unsatisfiable. |current_state| is the current signals state of the | 37 MojoResult Add(const scoped_refptr<WatcherDispatcher>& watcher, |
35 // handle being watched. | |
36 MojoResult Add(MojoHandleSignals signals, | |
37 const Watcher::WatchCallback& callback, | |
38 uintptr_t context, | 38 uintptr_t context, |
39 const HandleSignalsState& current_state); | 39 const HandleSignalsState& current_state); |
40 | 40 |
41 // Removes a watcher from the set. | 41 // Removes a watcher+context. |
42 MojoResult Remove(uintptr_t context); | 42 MojoResult Remove(WatcherDispatcher* watcher, uintptr_t context); |
43 | 43 |
44 private: | 44 private: |
45 // A map of watchers keyed on context value. | 45 using ContextSet = std::set<uintptr_t>; |
46 std::unordered_map<uintptr_t, scoped_refptr<Watcher>> watchers_; | 46 |
| 47 struct Entry { |
| 48 Entry(const scoped_refptr<WatcherDispatcher>& dispatcher); |
| 49 Entry(Entry&& other); |
| 50 ~Entry(); |
| 51 |
| 52 Entry& operator=(Entry&& other); |
| 53 |
| 54 scoped_refptr<WatcherDispatcher> dispatcher; |
| 55 ContextSet contexts; |
| 56 |
| 57 private: |
| 58 DISALLOW_COPY_AND_ASSIGN(Entry); |
| 59 }; |
| 60 |
| 61 Dispatcher* const owner_; |
| 62 std::map<WatcherDispatcher*, Entry> watchers_; |
| 63 base::Optional<HandleSignalsState> last_known_state_; |
47 | 64 |
48 DISALLOW_COPY_AND_ASSIGN(WatcherSet); | 65 DISALLOW_COPY_AND_ASSIGN(WatcherSet); |
49 }; | 66 }; |
50 | 67 |
51 } // namespace edk | 68 } // namespace edk |
52 } // namespace mojo | 69 } // namespace mojo |
53 | 70 |
54 #endif // MOJO_EDK_SYSTEM_WATCHER_SET_H_ | 71 #endif // MOJO_EDK_SYSTEM_WATCHER_SET_H_ |
OLD | NEW |