2011-02-12 00:22:29 -05:00
|
|
|
#include "pthread_impl.h"
|
|
|
|
|
|
|
|
int pthread_barrier_wait(pthread_barrier_t *b)
|
|
|
|
{
|
|
|
|
int cur;
|
|
|
|
|
|
|
|
/* Trivial case: count was set at 1 */
|
2011-02-17 17:16:20 -05:00
|
|
|
if (!b->_b_limit) return PTHREAD_BARRIER_SERIAL_THREAD;
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
/* Wait for anyone still suspended at previous use of barrier */
|
2011-02-17 17:16:20 -05:00
|
|
|
while ((cur=b->_b_left))
|
|
|
|
__wait(&b->_b_left, &b->_b_waiters, cur, 0);
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
/* If we are the last to reach barrier, reset it and wake others */
|
2011-02-17 17:16:20 -05:00
|
|
|
if (a_fetch_add(&b->_b_count, 1) == b->_b_limit) {
|
|
|
|
b->_b_left = b->_b_limit;
|
|
|
|
b->_b_count = 0;
|
|
|
|
__wake(&b->_b_count, -1, 0);
|
2011-02-12 00:22:29 -05:00
|
|
|
return PTHREAD_BARRIER_SERIAL_THREAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for our peers to reach the barrier */
|
2011-02-17 17:16:20 -05:00
|
|
|
while ((cur=b->_b_count))
|
|
|
|
__wait(&b->_b_count, 0, cur, 0);
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
/* If we're the last to wake up and barrier is awaiting reuse */
|
2011-02-17 17:16:20 -05:00
|
|
|
if (a_fetch_add(&b->_b_left, -1) == 1 && b->_b_waiters)
|
|
|
|
__wake(&b->_b_left, -1, 0);
|
2011-02-12 00:22:29 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|