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_H_ | |
6 #define MOJO_EDK_SYSTEM_WATCHER_H_ | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/synchronization/lock.h" | |
12 #include "mojo/public/c/system/functions.h" | |
13 #include "mojo/public/c/system/types.h" | |
14 | |
15 namespace mojo { | |
16 namespace edk { | |
17 | |
18 // This object corresponds to a watch added by a single call to |MojoWatch()|. | |
19 // | |
20 // An event may occur at any time which should trigger a Watcher to run its | |
21 // callback, but the callback needs to be deferred until all EDK locks are | |
22 // released. At the same time, a watch may be cancelled at any time by | |
23 // |MojoCancelWatch()| and it is not OK for the callback to be invoked after | |
24 // that happens. | |
25 // | |
26 // Therefore a Watcher needs to have some associated thread-safe state to track | |
27 // its cancellation, which is why it's ref-counted. | |
28 class Watcher : public base::RefCountedThreadSafe<Watcher> { | |
29 public: | |
30 using WatchCallback = | |
31 base::Callback<void(MojoResult, MojoHandleSignalsState)>; | |
32 | |
33 // Constructs a new Watcher which watches for |signals| to be satisfied on a | |
34 // handle and which invokes |callback| either when one such signal is | |
35 // satisfied, or all such signals become unsatisfiable. | |
36 Watcher(MojoHandleSignals signals, const WatchCallback& callback); | |
37 | |
38 // Runs the Watcher's callback with the given arguments if it hasn't been | |
39 // cancelled yet. | |
40 void MaybeInvokeCallback(MojoResult result, MojoHandleSignalsState state); | |
41 | |
42 // Notifies the Watcher of a state change. This may result in the Watcher | |
43 // adding a finalizer to the current RequestContext to invoke its callback, | |
44 // cancellation notwithstanding. | |
45 void NotifyForStateChange(MojoHandleSignalsState signals_state); | |
46 | |
47 // Notifies the Watcher of handle closure. This always results in the Watcher | |
48 // adding a finalizer to the current RequestContext to invoke its callback, | |
49 // cancellation notwithstanding. | |
50 void NotifyClosed(); | |
51 | |
52 // Explicitly cancels the watch, guaranteeing that its callback will never be | |
53 // be invoked again. | |
54 void Cancel(); | |
55 | |
56 private: | |
57 friend class base::RefCountedThreadSafe<Watcher>; | |
58 | |
59 ~Watcher(); | |
60 | |
61 // The set of signals which are watched by this Watcher. | |
62 const MojoHandleSignals signals_; | |
63 | |
64 // The callback to invoke with a result and signal state any time signals in | |
65 // |signals_| are satisfied or become permanently unsatisfiable. | |
66 const WatchCallback callback_; | |
67 | |
68 // Guards |is_valid_|. | |
Anand Mistry (off Chromium)
2016/03/01 07:30:40
s/is_valid_/is_cancelled_/
Ken Rockot(use gerrit already)
2016/03/01 08:17:44
done
| |
69 base::Lock lock_; | |
70 | |
71 // Indicates whether the watch has been cancelled. A |Watcher| may exist for a | |
72 // brief period of time after being cancelled if it's been attached as a | |
73 // RequestContext finalizer. In such cases the callback must not be invoked, | |
74 // hence this flag. | |
75 bool is_cancelled_ = false; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(Watcher); | |
78 }; | |
79 | |
80 } // namespace edk | |
81 } // namespace mojo | |
82 | |
83 #endif // MOJO_EDK_SYSTEM_WATCHER_H_ | |
OLD | NEW |