OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // Provides classes with functionality analogous to (but much more limited than) | 5 // Provides classes with functionality analogous to (but much more limited than) |
6 // Chromium's |base::WaitableEvent|, which in turn provides functionality | 6 // Chromium's |base::WaitableEvent|, which in turn provides functionality |
7 // analogous to Windows's Event. (Unlike these two, we have separate types for | 7 // analogous to Windows's Event. (Unlike these two, we have separate types for |
8 // the manual- and auto-reset versions.) | 8 // the manual- and auto-reset versions.) |
9 | 9 |
10 #ifndef MOJO_EDK_SYSTEM_WAITABLE_EVENT_H_ | 10 #ifndef MOJO_EDK_SYSTEM_WAITABLE_EVENT_H_ |
11 #define MOJO_EDK_SYSTEM_WAITABLE_EVENT_H_ | 11 #define MOJO_EDK_SYSTEM_WAITABLE_EVENT_H_ |
12 | 12 |
13 #include "mojo/edk/system/cond_var.h" | 13 #include "mojo/edk/util/cond_var.h" |
14 #include "mojo/edk/system/mutex.h" | 14 #include "mojo/edk/util/mutex.h" |
15 #include "mojo/edk/system/thread_annotations.h" | 15 #include "mojo/edk/util/thread_annotations.h" |
16 #include "mojo/public/cpp/system/macros.h" | 16 #include "mojo/public/cpp/system/macros.h" |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
19 namespace system { | 19 namespace system { |
20 | 20 |
21 // AutoResetWaitableEvent ------------------------------------------------------ | 21 // AutoResetWaitableEvent ------------------------------------------------------ |
22 | 22 |
23 // An event that can be signaled and waited on. This version automatically | 23 // An event that can be signaled and waited on. This version automatically |
24 // returns to the unsignaled state after unblocking one waiter. (This is similar | 24 // returns to the unsignaled state after unblocking one waiter. (This is similar |
25 // to Windows's auto-reset Event, which is also imitated by Chromium's | 25 // to Windows's auto-reset Event, which is also imitated by Chromium's |
(...skipping 29 matching lines...) Expand all Loading... |
55 // without being signaled in which case it returns true (otherwise, it returns | 55 // without being signaled in which case it returns true (otherwise, it returns |
56 // false). | 56 // false). |
57 bool WaitWithTimeout(uint64_t timeout_microseconds); | 57 bool WaitWithTimeout(uint64_t timeout_microseconds); |
58 | 58 |
59 // Returns whether this event is in a signaled state or not. For use in tests | 59 // Returns whether this event is in a signaled state or not. For use in tests |
60 // only (in general, this is racy). Note: Unlike | 60 // only (in general, this is racy). Note: Unlike |
61 // |base::WaitableEvent::IsSignaled()|, this doesn't reset the signaled state. | 61 // |base::WaitableEvent::IsSignaled()|, this doesn't reset the signaled state. |
62 bool IsSignaledForTest(); | 62 bool IsSignaledForTest(); |
63 | 63 |
64 private: | 64 private: |
65 CondVar cv_; | 65 util::CondVar cv_; |
66 Mutex mutex_; | 66 util::Mutex mutex_; |
67 | 67 |
68 // True if this event is in the signaled state. | 68 // True if this event is in the signaled state. |
69 bool signaled_ MOJO_GUARDED_BY(mutex_) = false; | 69 bool signaled_ MOJO_GUARDED_BY(mutex_) = false; |
70 | 70 |
71 MOJO_DISALLOW_COPY_AND_ASSIGN(AutoResetWaitableEvent); | 71 MOJO_DISALLOW_COPY_AND_ASSIGN(AutoResetWaitableEvent); |
72 }; | 72 }; |
73 | 73 |
74 // ManualResetWaitableEvent ---------------------------------------------------- | 74 // ManualResetWaitableEvent ---------------------------------------------------- |
75 | 75 |
76 // An event that can be signaled and waited on. This version remains signaled | 76 // An event that can be signaled and waited on. This version remains signaled |
(...skipping 20 matching lines...) Expand all Loading... |
97 // Like |Wait()|, but with a timeout. Also unblocks if |timeout_microseconds| | 97 // Like |Wait()|, but with a timeout. Also unblocks if |timeout_microseconds| |
98 // without being signaled in which case it returns true (otherwise, it returns | 98 // without being signaled in which case it returns true (otherwise, it returns |
99 // false). | 99 // false). |
100 bool WaitWithTimeout(uint64_t timeout_microseconds); | 100 bool WaitWithTimeout(uint64_t timeout_microseconds); |
101 | 101 |
102 // Returns whether this event is in a signaled state or not. For use in tests | 102 // Returns whether this event is in a signaled state or not. For use in tests |
103 // only (in general, this is racy). | 103 // only (in general, this is racy). |
104 bool IsSignaledForTest(); | 104 bool IsSignaledForTest(); |
105 | 105 |
106 private: | 106 private: |
107 CondVar cv_; | 107 util::CondVar cv_; |
108 Mutex mutex_; | 108 util::Mutex mutex_; |
109 | 109 |
110 // True if this event is in the signaled state. | 110 // True if this event is in the signaled state. |
111 bool signaled_ MOJO_GUARDED_BY(mutex_) = false; | 111 bool signaled_ MOJO_GUARDED_BY(mutex_) = false; |
112 | 112 |
113 // While |CondVar::SignalAll()| (|pthread_cond_broadcast()|) will wake all | 113 // While |CondVar::SignalAll()| (|pthread_cond_broadcast()|) will wake all |
114 // waiting threads, one has to deal with spurious wake-ups. Checking | 114 // waiting threads, one has to deal with spurious wake-ups. Checking |
115 // |signaled_| isn't sufficient, since another thread may have been awoken and | 115 // |signaled_| isn't sufficient, since another thread may have been awoken and |
116 // (manually) reset |signaled_|. This is a counter that is incremented in | 116 // (manually) reset |signaled_|. This is a counter that is incremented in |
117 // |Signal()| before calling |CondVar::SignalAll()|. A waiting thread knows it | 117 // |Signal()| before calling |CondVar::SignalAll()|. A waiting thread knows it |
118 // was awoken if |signal_id_| is different from when it started waiting. | 118 // was awoken if |signal_id_| is different from when it started waiting. |
119 unsigned signal_id_ MOJO_GUARDED_BY(mutex_) = 0u; | 119 unsigned signal_id_ MOJO_GUARDED_BY(mutex_) = 0u; |
120 | 120 |
121 MOJO_DISALLOW_COPY_AND_ASSIGN(ManualResetWaitableEvent); | 121 MOJO_DISALLOW_COPY_AND_ASSIGN(ManualResetWaitableEvent); |
122 }; | 122 }; |
123 | 123 |
124 } // namespace system | 124 } // namespace system |
125 } // namespace mojo | 125 } // namespace mojo |
126 | 126 |
127 #endif // MOJO_EDK_SYSTEM_WAITABLE_EVENT_H_ | 127 #endif // MOJO_EDK_SYSTEM_WAITABLE_EVENT_H_ |
OLD | NEW |