1 #include "pthread_impl.h"
3 int pthread_barrier_wait(pthread_barrier_t *b)
7 /* Trivial case: count was set at 1 */
8 if (!b->_b_limit) return PTHREAD_BARRIER_SERIAL_THREAD;
10 /* Wait for anyone still suspended at previous use of barrier */
11 while ((cur=b->_b_left))
12 __wait(&b->_b_left, &b->_b_waiters, cur, 0);
14 /* If we are the last to reach barrier, reset it and wake others */
15 if (a_fetch_add(&b->_b_count, 1) == b->_b_limit) {
16 b->_b_left = b->_b_limit;
18 __wake(&b->_b_count, -1, 0);
19 return PTHREAD_BARRIER_SERIAL_THREAD;
22 /* Wait for our peers to reach the barrier */
23 while ((cur=b->_b_count))
24 __wait(&b->_b_count, 0, cur, 0);
26 /* If we're the last to wake up and barrier is awaiting reuse */
27 if (a_fetch_add(&b->_b_left, -1) == 1 && b->_b_waiters)
28 __wake(&b->_b_left, -1, 0);