1 #include "pthread_impl.h"
5 void __vm_lock(int inc)
9 if (inc*v < 0) __wait(vmlock, vmlock+1, v, 1);
10 else if (a_cas(vmlock, v, v+inc)==v) break;
14 void __vm_unlock(void)
16 if (vmlock[0]>0) a_dec(vmlock);
18 if (vmlock[1]) __wake(vmlock, 1, 1);
21 static int pshared_barrier_wait(pthread_barrier_t *b)
23 int limit = (b->_b_limit & INT_MAX) + 1;
27 if (!limit) return PTHREAD_BARRIER_SERIAL_THREAD;
29 while (a_swap(&b->_b_lock, 1))
30 __wait(&b->_b_lock, &b->_b_waiters, 1, 0);
34 if (++b->_b_count == limit) {
35 ret = PTHREAD_BARRIER_SERIAL_THREAD;
37 __wake(&b->_b_seq, -1, 0);
39 a_store(&b->_b_lock, 0);
40 if (b->_b_waiters) __wake(&b->_b_lock, 1, 0);
41 __wait(&b->_b_seq, 0, seq, 0);
46 if (a_fetch_add(&b->_b_count, -1)==1) {
48 __wake(&b->_b_seq, -1, 0);
49 a_store(&b->_b_lock, 0);
50 if (b->_b_waiters) __wake(&b->_b_lock, 1, 0);
52 __wait(&b->_b_seq, 0, seq+1, 0);
68 int pthread_barrier_wait(pthread_barrier_t *b)
70 int limit = b->_b_limit;
71 struct instance *inst;
73 /* Trivial case: count was set at 1 */
74 if (!limit) return PTHREAD_BARRIER_SERIAL_THREAD;
76 /* Process-shared barriers require a separate, inefficient wait */
77 if (limit < 0) return pshared_barrier_wait(b);
79 /* Otherwise we need a lock on the barrier object */
80 while (a_swap(&b->_b_lock, 1))
81 __wait(&b->_b_lock, &b->_b_waiters, 1, 1);
84 /* First thread to enter the barrier becomes the "instance owner" */
86 struct instance new_inst = { 0 };
88 b->_b_inst = inst = &new_inst;
89 a_store(&b->_b_lock, 0);
90 if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
91 while (spins-- && !inst->finished)
93 a_inc(&inst->finished);
94 while (inst->finished == 1)
95 __syscall(SYS_futex, &inst->finished, FUTEX_WAIT,1,0);
96 return PTHREAD_BARRIER_SERIAL_THREAD;
99 /* Last thread to enter the barrier wakes all non-instance-owners */
100 if (++inst->count == limit) {
102 a_store(&b->_b_lock, 0);
103 if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
104 a_store(&inst->last, 1);
106 __wake(&inst->last, -1, 1);
108 a_store(&b->_b_lock, 0);
109 if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
110 __wait(&inst->last, &inst->waiters, 0, 1);
113 /* Last thread to exit the barrier wakes the instance owner */
114 if (a_fetch_add(&inst->count,-1)==1 && a_fetch_add(&inst->finished,1))
115 __wake(&inst->finished, 1, 1);