1 #include "pthread_impl.h"
8 static void unwait(pthread_cond_t *c, pthread_mutex_t *m)
10 /* Removing a waiter is non-trivial if we could be using requeue
11 * based broadcast signals, due to mutex access issues, etc. */
13 if (c->_c_mutex == (void *)-1) {
14 a_dec(&c->_c_waiters);
18 while (a_swap(&c->_c_lock, 1))
19 __wait(&c->_c_lock, &c->_c_lockwait, 1, 1);
21 if (c->_c_waiters) c->_c_waiters--;
22 else a_dec(&m->_m_waiters);
24 a_store(&c->_c_lock, 0);
25 if (c->_c_lockwait) __wake(&c->_c_lock, 1, 1);
28 static void cleanup(void *p)
32 pthread_mutex_lock(cm->m);
35 int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, const struct timespec *ts)
37 struct cm cm = { .c=c, .m=m };
40 if (ts && ts->tv_nsec >= 1000000000UL)
45 if (c->_c_mutex == (void *)-1) {
46 a_inc(&c->_c_waiters);
49 while (a_swap(&c->_c_lock, 1))
50 __wait(&c->_c_lock, &c->_c_lockwait, 1, 1);
52 a_store(&c->_c_lock, 0);
53 if (c->_c_lockwait) __wake(&c->_c_lock, 1, 1);
58 if ((r=pthread_mutex_unlock(m))) return r;
60 do e = __timedwait(&c->_c_seq, seq, c->_c_clock, ts, cleanup, &cm, 0);
61 while (c->_c_seq == seq && (!e || e==EINTR));
62 if (e == EINTR) e = 0;
66 if ((r=pthread_mutex_lock(m))) return r;