OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ | 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ |
6 #define MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ | 6 #define MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "mojo/public/c/system/types.h" | 13 #include "mojo/public/c/system/types.h" |
14 #include "mojo/public/cpp/system/handle.h" | 14 #include "mojo/public/cpp/system/handle.h" |
15 #include "mojo/public/cpp/system/system_export.h" | 15 #include "mojo/public/cpp/system/system_export.h" |
16 | 16 |
| 17 namespace base { |
| 18 class WaitableEvent; |
| 19 } |
| 20 |
17 namespace mojo { | 21 namespace mojo { |
18 | 22 |
19 // WaitSet provides an efficient means of blocking a thread on any number of | 23 // WaitSet provides an efficient means of blocking a thread on any number of |
20 // Mojo handles changing state in some interesting way. | 24 // events and Mojo handle state changes. |
21 // | 25 // |
22 // Unlike WaitMany(), which incurs some extra setup cost for every call, a | 26 // Unlike WaitMany(), which incurs some extra setup cost for every call, a |
23 // WaitSet maintains some persistent accounting of a set of handles which can be | 27 // WaitSet maintains some persistent accounting of the handles added or removed |
24 // added or removed from the set. A blocking wait operation (see the Wait() | 28 // from the set. A blocking wait operation (see the Wait() method below) can |
25 // method below) can then be performed multiple times for the same set of | 29 // then be performed multiple times for the same set of events and handles with |
26 // handles with minimal additional setup per call. | 30 // minimal additional setup per call. |
27 // | 31 // |
28 // WaitSet is NOT thread-safe, so naturally handles may not be added to or | 32 // WaitSet is NOT thread-safe, so naturally handles and events may not be added |
29 // removed from the set while waiting. | 33 // to or removed from the set while waiting. |
30 class MOJO_CPP_SYSTEM_EXPORT WaitSet { | 34 class MOJO_CPP_SYSTEM_EXPORT WaitSet { |
31 public: | 35 public: |
32 WaitSet(); | 36 WaitSet(); |
33 ~WaitSet(); | 37 ~WaitSet(); |
34 | 38 |
| 39 // Adds |event| to the set of events to wait on. If successful, any future |
| 40 // Wait() on this WaitSet will wake up if the event is signaled. |
| 41 // |
| 42 // |event| is not owned. |
| 43 // |
| 44 // Return values: |
| 45 // |MOJO_RESULT_OK| if |event| has been successfully added. |
| 46 // |MOJO_RESULT_ALREADY_EXISTS| if |event| is already in this WaitSet. |
| 47 MojoResult AddEvent(base::WaitableEvent* event); |
| 48 |
| 49 // Removes |event| from the set of events to wait on. |
| 50 // |
| 51 // Return values: |
| 52 // |MOJO_RESULT_OK| if |event| has been successfully added. |
| 53 // |MOJO_RESULT_NOT_FOUND| if |event| was not in the set. |
| 54 MojoResult RemoveEvent(base::WaitableEvent* event); |
| 55 |
35 // Adds |handle| to the set of handles to wait on. If successful, any future | 56 // Adds |handle| to the set of handles to wait on. If successful, any future |
36 // Wait() on this WaitSet will wake up in the event that one or more signals | 57 // Wait() on this WaitSet will wake up in the event that one or more signals |
37 // in |signals| becomes satisfied on |handle| or all of them become | 58 // in |signals| becomes satisfied on |handle| or all of them become |
38 // permanently unsatisfiable. | 59 // permanently unsatisfiable. |
39 // | 60 // |
40 // Return values: | 61 // Return values: |
41 // |MOJO_RESULT_OK| if |handle| has been successfully added. | 62 // |MOJO_RESULT_OK| if |handle| has been successfully added. |
42 // |MOJO_RESULT_ALREADY_EXISTS| if |handle| is already in this WaitSet. | 63 // |MOJO_RESULT_ALREADY_EXISTS| if |handle| is already in this WaitSet. |
43 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. | 64 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. |
44 MojoResult AddHandle(Handle handle, MojoHandleSignals signals); | 65 MojoResult AddHandle(Handle handle, MojoHandleSignals signals); |
45 | 66 |
46 // Removes |handle| from the set of handles to wait on. Future calls to | 67 // Removes |handle| from the set of handles to wait on. Future calls to |
47 // Wait() will be unaffected by the state of this handle. | 68 // Wait() will be unaffected by the state of this handle. |
48 // | 69 // |
49 // Return values: | 70 // Return values: |
50 // |MOJO_RESULT_OK| if |handle| has been successfully removed. | 71 // |MOJO_RESULT_OK| if |handle| has been successfully removed. |
51 // |MOJO_RESULT_NOT_FOUND| if |handle| was not in the set. | 72 // |MOJO_RESULT_NOT_FOUND| if |handle| was not in the set. |
52 MojoResult RemoveHandle(Handle handle); | 73 MojoResult RemoveHandle(Handle handle); |
53 | 74 |
54 // Waits on the current set of handles for one or more of them to meet the | 75 // Waits on the current set of handles, waking up when one more of them meets |
55 // conditions which were specified when they were added via AddHandle() above. | 76 // the signaling conditions which were specified when they were added via |
| 77 // AddHandle() above. |
56 // | 78 // |
57 // |*num_ready_handles| on input must specify the number of entries available | 79 // |*num_ready_handles| on input must specify the number of entries available |
58 // for output storage in |ready_handles| and |ready_result| (which must both | 80 // for output storage in |ready_handles| and |ready_result| (which must both |
59 // be non-null). If |signals_states| is non-null it must also point to enough | 81 // be non-null). If |signals_states| is non-null it must also point to enough |
60 // storage for |*num_ready_handles| MojoHandleSignalsState structures. | 82 // storage for |*num_ready_handles| MojoHandleSignalsState structures. |
61 // | 83 // |
62 // Upon return, |*num_ready_handles| will contain the total number of handles | 84 // Upon return, |*num_ready_handles| will contain the total number of handles |
63 // whose information is stored in the given output buffers. | 85 // whose information is stored in the given output buffers. |
64 // | 86 // |
| 87 // If |ready_event| is non-null and the Wait() was unblocked by a user event |
| 88 // signaling, the address of the event which signaled will be placed in |
| 89 // |*ready_event|. Note that this is not necessarily exclusive to one or more |
| 90 // handles also being ready. If |ready_event| is non-null and no user event |
| 91 // was signaled for this Wait(), |*ready_event| will be null upon return. |
| 92 // |
65 // Every entry in |ready_handles| on output corresponds to one of the handles | 93 // Every entry in |ready_handles| on output corresponds to one of the handles |
66 // whose signaling state termianted the Wait() operation. Every corresponding | 94 // whose signaling state termianted the Wait() operation. Every corresponding |
67 // entry in |ready_results| indicates the status of a ready handle according | 95 // entry in |ready_results| indicates the status of a ready handle according |
68 // to the following result codes: | 96 // to the following result codes: |
69 // |MOJO_RESULT_OK| one or more signals for the handle has been satisfied. | 97 // |MOJO_RESULT_OK| one or more signals for the handle has been satisfied. |
70 // |MOJO_RESULT_FAILED_PRECONDITION| all of the signals for the handle have | 98 // |MOJO_RESULT_FAILED_PRECONDITION| all of the signals for the handle have |
71 // become permanently unsatisfiable. | 99 // become permanently unsatisfiable. |
72 // |MOJO_RESULT_CANCELLED| if the handle has been closed from another | 100 // |MOJO_RESULT_CANCELLED| if the handle has been closed from another |
73 // thread. NOTE: It is important to recognize that this means the | 101 // thread. NOTE: It is important to recognize that this means the |
74 // corresponding value in |ready_handles| is either invalid, or valid | 102 // corresponding value in |ready_handles| is either invalid, or valid |
75 // but referring to a different handle (i.e. has already been reused) by | 103 // but referring to a different handle (i.e. has already been reused) by |
76 // the time Wait() returns. The handle in question is automatically | 104 // the time Wait() returns. The handle in question is automatically |
77 // removed from the WaitSet. | 105 // removed from the WaitSet. |
78 void Wait(size_t* num_ready_handles, | 106 void Wait(base::WaitableEvent** ready_event, |
| 107 size_t* num_ready_handles, |
79 Handle* ready_handles, | 108 Handle* ready_handles, |
80 MojoResult* ready_results, | 109 MojoResult* ready_results, |
81 MojoHandleSignalsState* signals_states = nullptr); | 110 MojoHandleSignalsState* signals_states = nullptr); |
82 | 111 |
83 private: | 112 private: |
84 class State; | 113 class State; |
85 | 114 |
86 // Thread-safe state associated with this WaitSet. Used to aggregate | 115 // Thread-safe state associated with this WaitSet. Used to aggregate |
87 // notifications from watched handles. | 116 // notifications from watched handles. |
88 scoped_refptr<State> state_; | 117 scoped_refptr<State> state_; |
89 | 118 |
90 DISALLOW_COPY_AND_ASSIGN(WaitSet); | 119 DISALLOW_COPY_AND_ASSIGN(WaitSet); |
91 }; | 120 }; |
92 | 121 |
93 } // namespace mojo | 122 } // namespace mojo |
94 | 123 |
95 #endif // MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ | 124 #endif // MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ |
OLD | NEW |