1 Conditional Variable pseudocode.
2 ================================
4 int pthread_cond_timedwait (pthread_cond_t *cv, pthread_mutex_t *mutex);
5 int pthread_cond_signal (pthread_cond_t *cv);
6 int pthread_cond_broadcast (pthread_cond_t *cv);
8 struct pthread_cond_t {
10 unsigned int cond_lock;
16 Total number of threads using the conditional variable.
20 sequence number for next wakeup.
24 sequence number of last woken thread.
26 uint32_t broadcast_seq;
41 cleanup_handler(cv_data)
46 if (cv_data->bc_seq == cv->broadcast_seq) {
51 /* make sure no signal gets lost. */
52 FUTEX_WAKE(cv->wakeup_seq, ALL);
58 cond_timedwait(cv, mutex, timeout):
66 val = seq = cv->wakeup_seq;
67 cv_data.bc = cv->broadcast_seq;
74 enable_async(&cv_data);
76 ret = FUTEX_WAIT(cv->wakeup_seq, val, timeout);
82 if (bc != cv->broadcast_seq)
87 if (val != seq && cv->woken_seq != val) {
92 if (ret == TIMEDOUT) {
101 lll_unlock(cv->lock);
114 if (cv->total_seq > cv->wakeup_seq) {
116 FUTEX_WAKE(cv->wakeup_seq, 1);
119 lll_unlock(cv->lock);
126 if (cv->total_seq > cv->wakeup_seq) {
127 cv->wakeup_seq = cv->total_seq;
128 cv->woken_seq = cv->total_seq;
130 FUTEX_WAKE(cv->wakeup_seq, ALL);
133 lll_unlock(cv->lock);