OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // A condition variable class (to be used with |mojo::system::Mutex|). | |
6 | |
7 #ifndef MOJO_EDK_SYSTEM_COND_VAR_H_ | |
8 #define MOJO_EDK_SYSTEM_COND_VAR_H_ | |
9 | |
10 #include <pthread.h> | |
11 #include <stdint.h> | |
12 | |
13 #include "mojo/edk/system/thread_annotations.h" | |
14 #include "mojo/public/cpp/system/macros.h" | |
15 | |
16 namespace mojo { | |
17 namespace system { | |
18 | |
19 class Mutex; | |
20 | |
21 class CondVar { | |
22 public: | |
23 CondVar(); | |
24 ~CondVar(); | |
25 | |
26 // Atomically releases |*mutex| (which must be held) and blocks on this | |
27 // condition variable, unlocking and reacquiring |*mutex| when: | |
28 // * |SignalAll()| is called, | |
29 // * |Signal()| is called and this thread is scheduled to be the next to be | |
30 // unblocked, or | |
31 // * whenever (spuriously, e.g., due to |EINTR|). | |
32 // To deal with spurious wakeups, wait using a loop (with |my_mutex| held): | |
33 // while (!<my_condition>) | |
34 // cv.Wait(&my_mutex); | |
35 void Wait(Mutex* mutex) MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex); | |
36 | |
37 // Like |Wait()|, but will also unblock when |timeout_microseconds| have | |
38 // elapsed without this condition variable being signaled. Returns true on | |
39 // timeout; this is somewhat counterintuitive, but the false case is | |
40 // non-specific: the condition variable may or may not have been signaled and | |
41 // |timeout_microseconds| may or may not have already elapsed (spurious | |
42 // wakeups are possible). | |
43 // TODO(vtl): A version with an absolute deadline time would be more efficient | |
44 // for users who want to wait to be signaled or a timeout to have definitely | |
45 // elapsed. With this API, users have to recalculate the timeout when they | |
46 // detect a spurious wakeup. | |
47 bool WaitWithTimeout(Mutex* mutex, uint64_t timeout_microseconds) | |
48 MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex); | |
49 | |
50 // Signals this condition variable, waking at least one waiting thread if | |
51 // there are any. | |
52 void Signal(); | |
53 | |
54 // Signals this condition variable, waking all waiting threads. | |
55 void SignalAll(); | |
56 | |
57 private: | |
58 pthread_cond_t impl_; | |
59 | |
60 MOJO_DISALLOW_COPY_AND_ASSIGN(CondVar); | |
61 }; | |
62 | |
63 } // namespace system | |
64 } // namespace mojo | |
65 | |
66 #endif // MOJO_EDK_SYSTEM_COND_VAR_H_ | |
OLD | NEW |