1 #include "pthread_impl.h"
3 void __pthread_testcancel(void);
4 int __pthread_mutex_lock(pthread_mutex_t *);
5 int __pthread_mutex_unlock(pthread_mutex_t *);
10 * Waiter objects have automatic storage on the waiting thread, and
11 * are used in building a linked list representing waiters currently
12 * waiting on the condition variable or a group of waiters woken
13 * together by a broadcast or signal; in the case of signal, this is a
14 * degenerate list of one member.
16 * Waiter lists attached to the condition variable itself are
17 * protected by the lock on the cv. Detached waiter lists are never
18 * modified again, but can only be traversed in reverse order, and are
19 * protected by the "barrier" locks in each node, which are unlocked
20 * in turn to control wake order.
22 * Since process-shared cond var semantics do not necessarily allow
23 * one thread to see another's automatic storage (they may be in
24 * different processes), the waiter list is not used for the
25 * process-shared case, but the structure is still used to store data
26 * needed by the cancellation cleanup handler.
30 struct waiter *prev, *next;
31 int state, barrier, mutex_ret;
33 pthread_mutex_t *mutex;
38 /* Self-synchronized-destruction-safe lock functions */
40 static inline void lock(volatile int *l)
44 do __wait(l, 0, 2, 1);
45 while (a_cas(l, 0, 2));
49 static inline void unlock(volatile int *l)
55 static inline void unlock_requeue(volatile int *l, volatile int *r, int w)
58 if (w) __wake(l, 1, 1);
59 else __syscall(SYS_futex, l, FUTEX_REQUEUE|128, 0, 1, r) != -ENOSYS
60 || __syscall(SYS_futex, l, FUTEX_REQUEUE, 0, 1, r);
69 static void unwait(void *arg)
71 struct waiter *node = arg;
74 pthread_cond_t *c = node->cond;
75 pthread_mutex_t *m = node->mutex;
76 if (a_fetch_add(&c->_c_waiters, -1) == -0x7fffffff)
77 __wake(&c->_c_waiters, 1, 0);
78 node->mutex_ret = pthread_mutex_lock(m);
82 int oldstate = a_cas(&node->state, WAITING, LEAVING);
84 if (oldstate == WAITING) {
85 /* Access to cv object is valid because this waiter was not
86 * yet signaled and a new signal/broadcast cannot return
87 * after seeing a LEAVING waiter without getting notified
88 * via the futex notify below. */
90 pthread_cond_t *c = node->cond;
93 if (c->_c_head == node) c->_c_head = node->next;
94 else if (node->prev) node->prev->next = node->next;
95 if (c->_c_tail == node) c->_c_tail = node->prev;
96 else if (node->next) node->next->prev = node->prev;
101 if (a_fetch_add(node->notify, -1)==1)
102 __wake(node->notify, 1, 1);
105 /* Lock barrier first to control wake order. */
106 lock(&node->barrier);
109 node->mutex_ret = pthread_mutex_lock(node->mutex);
111 if (oldstate == WAITING) return;
113 if (!node->next) a_inc(&node->mutex->_m_waiters);
115 /* Unlock the barrier that's holding back the next waiter, and
116 * either wake it or requeue it to the mutex. */
118 unlock_requeue(&node->prev->barrier,
119 &node->mutex->_m_lock,
120 node->mutex->_m_type & 128);
122 a_dec(&node->mutex->_m_waiters);
126 int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
128 struct waiter node = { .cond = c, .mutex = m };
129 int e, seq, *fut, clock = c->_c_clock;
131 if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid)
134 if (ts && ts->tv_nsec >= 1000000000UL)
137 __pthread_testcancel();
143 a_inc(&c->_c_waiters);
147 seq = node.barrier = 2;
149 node.state = WAITING;
150 node.next = c->_c_head;
152 if (!c->_c_tail) c->_c_tail = &node;
153 else node.next->prev = &node;
158 __pthread_mutex_unlock(m);
160 do e = __timedwait(fut, seq, clock, ts, unwait, &node, !node.shared);
161 while (*fut==seq && (!e || e==EINTR));
162 if (e == EINTR) e = 0;
166 return node.mutex_ret ? node.mutex_ret : e;
169 int __private_cond_signal(pthread_cond_t *c, int n)
171 struct waiter *p, *first=0;
175 for (p=c->_c_tail; n && p; p=p->prev) {
176 if (a_cas(&p->state, WAITING, SIGNALED) != WAITING) {
184 /* Split the list, leaving any remainder on the cv. */
186 if (p->next) p->next->prev = 0;
194 /* Wait for any waiters in the LEAVING state to remove
195 * themselves from the list before returning or allowing
196 * signaled threads to proceed. */
197 while ((cur = ref)) __wait(&ref, 0, cur, 1);
199 /* Allow first signaled waiter, if any, to proceed. */
200 if (first) unlock(&first->barrier);
205 weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);