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

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

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: rebase 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_WATCH_H_
6 #define MOJO_EDK_SYSTEM_WATCH_H_
7
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/synchronization/lock.h"
11 #include "mojo/edk/system/atomic_flag.h"
12 #include "mojo/edk/system/handle_signals_state.h"
13
14 namespace mojo {
15 namespace edk {
16
17 class Dispatcher;
18 class WatcherDispatcher;
19
20 // Encapsulates the state associated with a single watch context within a
21 // watcher.
22 //
23 // Every Watch has its own cancellation state, and is captured by RequestContext
24 // notification finalizers to avoid redundant context resolution during
25 // finalizer execution.
26 class Watch : public base::RefCountedThreadSafe<Watch> {
27 public:
28 // Constructs a Watch which represents a watch within |watcher| associated
29 // with |context|, watching |dispatcher| for |signals|.
30 Watch(const scoped_refptr<WatcherDispatcher>& watcher,
31 const scoped_refptr<Dispatcher>& dispatcher,
32 uintptr_t context,
33 MojoHandleSignals signals);
34
35 // Notifies the Watch of a potential state change.
36 //
37 // If |allowed_to_call_callback| is true, this may add a notification
38 // finalizer to the current RequestContext to invoke the watcher's callback
39 // with this watch's context. See return values below.
40 //
41 // This is called directly by WatcherDispatcher whenever the Watch's observed
42 // dispatcher notifies the WatcherDispatcher of a state change a state change.
yzshen1 2017/03/13 20:21:18 nit: "a state change" occurs twice.
Ken Rockot(use gerrit already) 2017/03/13 22:00:10 Fixed
43 //
44 // Returns |true| if the Watch entered or remains in a ready state as a result
45 // of the state change. If |allowed_to_call_callback| was true in this case,
46 // the Watch will have also attached a notification finalizer to the current
47 // RequestContext.
48 //
49 // Returns |false| if the
50 bool NotifyState(const HandleSignalsState& state,
51 bool allowed_to_call_callback);
52
53 // Notifies the watch of cancellation ASAP. This will always be the last
54 // notification sent for the watch.
55 void Cancel();
56
57 // Finalizer method for RequestContexts. This method is invoked once for every
58 // notification finalizer added to a RequestContext by this object. This calls
59 // down into the WatcherDispatcher to do the actual notification call.
60 void InvokeCallback(MojoResult result,
61 const HandleSignalsState& state,
62 MojoWatcherNotificationFlags flags);
63
64 const scoped_refptr<Dispatcher>& dispatcher() const { return dispatcher_; }
65 uintptr_t context() const { return context_; }
66
67 MojoResult last_known_result() const {
68 AssertWatcherLockAcquired();
69 return last_known_result_;
70 }
71
72 MojoHandleSignalsState last_known_signals_state() const {
73 AssertWatcherLockAcquired();
74 return last_known_signals_state_;
75 }
76
77 bool ready() const {
78 AssertWatcherLockAcquired();
79 return last_known_result_ == MOJO_RESULT_OK ||
80 last_known_result_ == MOJO_RESULT_FAILED_PRECONDITION;
81 }
82
83 private:
84 friend class base::RefCountedThreadSafe<Watch>;
85
86 ~Watch();
87
88 #if DCHECK_IS_ON()
89 void AssertWatcherLockAcquired() const;
90 #else
91 void AssertWatcherLockAcquired() const {}
92 #endif
93
94 const scoped_refptr<WatcherDispatcher> watcher_;
95 const scoped_refptr<Dispatcher> dispatcher_;
96 const uintptr_t context_;
97 const MojoHandleSignals signals_;
98
99 // The result code with which this Watch would notify if currently armed,
100 // based on the last known signaling state of |dispatcher_|. Guarded by the
101 // owning WatcherDispatcher's lock.
102 MojoResult last_known_result_ = MOJO_RESULT_UNKNOWN;
103
104 // The last known signaling state of |dispatcher_|. Guarded by the owning
105 // WatcherDispatcher's lock.
106 MojoHandleSignalsState last_known_signals_state_ = {0, 0};
107
108 // Guards |is_cancelled_| below and mutually excludes individual watch
109 // notification executions for this same watch context.
110 //
111 // Note that this should only be acquired from a RequestContext finalizer to
112 // ensure that no other internal locks are already held.
113 base::Lock notification_lock_;
114
115 // Guarded by |notification_lock_|.
116 bool is_cancelled_ = false;
117
118 DISALLOW_COPY_AND_ASSIGN(Watch);
119 };
120
121 } // namespace edk
122 } // namespace mojo
123
124 #endif // MOJO_EDK_SYSTEM_WATCH_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698