- /* Wait for our peers to reach the barrier */
- while ((cur=b->_b_count))
- __wait(&b->_b_count, 0, cur, 0);
+ /* Last thread to enter the barrier wakes all non-instance-owners */
+ if (++inst->count == limit) {
+ b->_b_inst = 0;
+ a_store(&b->_b_lock, 0);
+ if (b->_b_waiters) __wake(&b->_b_lock, 1, 0);
+ a_store(&inst->last, 1);
+ if (inst->waiters)
+ __wake(&inst->last, -1, 0);
+ } else {
+ a_store(&b->_b_lock, 0);
+ if (b->_b_waiters) __wake(&b->_b_lock, 1, 0);
+ __wait(&inst->last, &inst->waiters, 0, 0);
+ }