OLD | NEW |
(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_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ |
| 6 #define MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "mojo/public/c/system/types.h" |
| 14 #include "mojo/public/cpp/system/handle.h" |
| 15 #include "mojo/public/cpp/system/system_export.h" |
| 16 |
| 17 namespace mojo { |
| 18 |
| 19 // WaitSet provides an efficient means of blocking a thread on any number of |
| 20 // Mojo handles changing state in some interesting way. |
| 21 // |
| 22 // 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 |
| 24 // added or removed from the set. A blocking wait operation (see the Wait() |
| 25 // method below) can then be performed multiple times for the same set of |
| 26 // handles with minimal additional setup per call. |
| 27 // |
| 28 // WaitSet is NOT thread-safe, so naturally handles may not be added to or |
| 29 // removed from the set while waiting. |
| 30 class MOJO_CPP_SYSTEM_EXPORT WaitSet { |
| 31 public: |
| 32 WaitSet(); |
| 33 ~WaitSet(); |
| 34 |
| 35 // 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 |
| 37 // in |signals| becomes satisfied on |handle| or all of them become |
| 38 // permanently unsatisfiable. |
| 39 // |
| 40 // Return values: |
| 41 // |MOJO_RESULT_OK| if |handle| has been successfully added. |
| 42 // |MOJO_RESULT_ALREADY_EXISTS| if |handle| is already in this WaitSet. |
| 43 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. |
| 44 MojoResult AddHandle(Handle handle, MojoHandleSignals signals); |
| 45 |
| 46 // Removes |handle| from the set of handles to wait on. Future calls to |
| 47 // Wait() will be unaffected by the state of this handle. |
| 48 // |
| 49 // Return values: |
| 50 // |MOJO_RESULT_OK| if |handle| has been successfully removed. |
| 51 // |MOJO_RESULT_NOT_FOUND| if |handle| was not in the set. |
| 52 MojoResult RemoveHandle(Handle handle); |
| 53 |
| 54 // Waits on the current set of handles for one or more of them to meet the |
| 55 // conditions which were specified when they were added via AddHandle() above. |
| 56 // |
| 57 // |*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 |
| 59 // be non-null). If |signals_states| is non-null it must also point to enough |
| 60 // storage for |*num_ready_handles| MojoHandleSignalsState structures. |
| 61 // |
| 62 // Upon return, |*num_ready_handles| will contain the total number of handles |
| 63 // whose information is stored in the given output buffers. |
| 64 // |
| 65 // Every entry in |ready_handles| on output corresponds to one of the handles |
| 66 // whose signaling state termianted the Wait() operation. Every corresponding |
| 67 // entry in |ready_results| indicates the status of a ready handle according |
| 68 // to the following result codes: |
| 69 // |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 |
| 71 // become permanently unsatisfiable. |
| 72 // |MOJO_RESULT_CANCELLED| if the handle has been closed from another |
| 73 // thread. NOTE: It is important to recognize that this means the |
| 74 // corresponding value in |ready_handles| is either invalid, or valid |
| 75 // but referring to a different handle (i.e. has already been reused) by |
| 76 // the time Wait() returns. The handle in question is automatically |
| 77 // removed from the WaitSet. |
| 78 void Wait(size_t* num_ready_handles, |
| 79 Handle* ready_handles, |
| 80 MojoResult* ready_results, |
| 81 MojoHandleSignalsState* signals_states = nullptr); |
| 82 |
| 83 private: |
| 84 class State; |
| 85 |
| 86 // Thread-safe state associated with this WaitSet. Used to aggregate |
| 87 // notifications from watched handles. |
| 88 scoped_refptr<State> state_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(WaitSet); |
| 91 }; |
| 92 |
| 93 } // namespace mojo |
| 94 |
| 95 #endif // MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ |
OLD | NEW |