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

Side by Side Diff: mojo/edk/system/watcher_dispatcher.h

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: . Created 3 years, 9 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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_DISPATCHER_H_
6 #define MOJO_EDK_SYSTEM_WATCHER_DISPATCHER_H_
7
8 #include <map>
9 #include <set>
10
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/synchronization/lock.h"
14 #include "mojo/edk/system/dispatcher.h"
15 #include "mojo/edk/system/handle_signals_state.h"
16 #include "mojo/edk/system/system_impl_export.h"
17 #include "mojo/public/c/system/watcher.h"
18
19 namespace mojo {
20 namespace edk {
21
22 class Watch;
23
24 // The dispatcher type which backs watcher handles.
25 class WatcherDispatcher : public Dispatcher {
26 public:
27 // Constructs a new WatcherDispatcher which invokes |callback| when a
28 // registered watch observes some relevant state change.
29 explicit WatcherDispatcher(MojoWatcherCallback callback);
30
31 // Methods used by watched dispatchers to notify watchers of events.
32 void NotifyHandleState(Dispatcher* dispatcher,
33 const HandleSignalsState& state);
34 void NotifyHandleClosed(Dispatcher* dispatcher);
35
36 // Method used by RequestContext (indirectly, via Watch) to complete
37 // notification operations from a safe stack frame to avoid reentrancy.
38 void InvokeWatchCallback(uintptr_t context,
39 MojoResult result,
40 const HandleSignalsState& state,
41 MojoWatcherNotificationFlags flags);
42
43 // Dispatcher:
44 Type GetType() const override;
45 MojoResult Close() override;
46 MojoResult WatchDispatcher(scoped_refptr<Dispatcher> dispatcher,
47 MojoHandleSignals signals,
48 uintptr_t context) override;
49 MojoResult CancelWatch(uintptr_t context) override;
50 MojoResult Arm(uint32_t* num_ready_contexts,
51 uintptr_t* ready_contexts,
52 MojoResult* ready_results,
53 MojoHandleSignalsState* ready_signals_states) override;
54
55 private:
56 class WatchedHandle;
57
58 friend class Watch;
59
60 ~WatcherDispatcher() override;
61
62 const MojoWatcherCallback callback_;
63
64 // Guards access to the fields below.
65 //
66 // NOTE: This may be acquired while holding another dispatcher's lock, as
67 // watched dispatchers call into WatcherDispatcher methods which lock this
68 // when issuing state change notifications. WatcherDispatcher must therefore
69 // take caution to NEVER acquire other dispatcher locks while this is held.
70 base::Lock lock_;
71
72 bool armed_ = false;
73 bool closed_ = false;
74
75 // Tracks all watched handle dispatchers.
76 std::map<Dispatcher*, std::unique_ptr<WatchedHandle>> watched_handles_;
77
78 // A mapping from context to Watch.
79 std::map<uintptr_t, scoped_refptr<Watch>> watches_;
80
81 // The set of all WatchedHandle elements from |watched_handles_| which are
82 // currently ready to signal. This is used for efficient arming behavior,
83 // as it allows for O(1) discovery of whether or not arming can succeed
84 // and quick determination of who's responsible if it can't.
85 std::set<WatchedHandle*> ready_handles_;
86
87 DISALLOW_COPY_AND_ASSIGN(WatcherDispatcher);
88 };
89
90 } // namespace edk
91 } // namespace mojo
92
93 #endif // MOJO_EDK_SYSTEM_WATCHER_DISPATCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698