/system/core/libutils/include/utils/Condition.h
1 // --------------------------------------------------------------------------- 2 3 // DO NOT USE: please use std::condition_variable instead. 4 5 /* 6 * Condition variable class. The implementation is system-dependent. 7 * 8 * Condition variables are paired up with mutexes. Lock the mutex, 9 * call wait(), then either re-wait() if things aren't quite what you want, 10 * or unlock the mutex and continue. All threads calling wait() must 11 * use the same mutex for a given Condition. 12 * 13 * On Android and Apple platforms, these are implemented as a simple wrapper 14 * around pthread condition variables. Care must be taken to abide by 15 * the pthreads semantics, in particular, a boolean predicate must 16 * be re-evaluated after a wake-up, as spurious wake-ups may happen. 17 */ 18 class Condition { 19 public: 20 enum { 21 PRIVATE = 0, 22 SHARED = 1 23 }; 24 25 enum WakeUpType { 26 WAKE_UP_ONE = 0, 27 WAKE_UP_ALL = 1 28 }; 29 30 Condition(); 31 explicit Condition(int type); 32 ~Condition(); 33 // Wait on the condition variable. Lock the mutex before calling. 34 // Note that spurious wake-ups may happen. 35 status_t wait(Mutex& mutex); 36 // same with relative timeout 37 status_t waitRelative(Mutex& mutex, nsecs_t reltime); 38 // Signal the condition variable, allowing one thread to continue. 39 void signal(); 40 // Signal the condition variable, allowing one or all threads to continue. 41 void signal(WakeUpType type) { 42 if (type == WAKE_UP_ONE) { 43 signal(); 44 } else { 45 broadcast(); 46 } 47 } 48 // Signal the condition variable, allowing all threads to continue. 49 void broadcast(); 50 51 private: 52 #if !defined(_WIN32) 53 pthread_cond_t mCond; 54 #else 55 void* mState; 56 #endif 57 };