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_waiters2) c->_c_waiters2--;
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 (m->_m_type && (m->_m_lock&INT_MAX) != pthread_self()->tid)
43 if (ts && ts->tv_nsec >= 1000000000UL)
48 a_inc(&c->_c_waiters);
50 if (c->_c_mutex != (void *)-1) {
52 while (a_swap(&c->_c_lock, 1))
53 __wait(&c->_c_lock, &c->_c_lockwait, 1, 1);
55 a_store(&c->_c_lock, 0);
56 if (c->_c_lockwait) __wake(&c->_c_lock, 1, 1);
61 pthread_mutex_unlock(m);
63 do e = __timedwait(&c->_c_seq, seq, c->_c_clock, ts, cleanup, &cm, 0);
64 while (c->_c_seq == seq && (!e || e==EINTR));
65 if (e == EINTR) e = 0;
69 if ((r=pthread_mutex_lock(m))) return r;