9b1edbf94ad19a46a93fc9d68e497db662148ff4
[musl] / src / thread / pthread_barrier_wait.c
1 #include "pthread_impl.h"
2
3 static int vmlock[2];
4
5 void __vm_lock(int inc)
6 {
7         for (;;) {
8                 int v = vmlock[0];
9                 if (inc*v < 0) __wait(vmlock, vmlock+1, v, 1);
10                 else if (a_cas(vmlock, v, v+inc)==v) break;
11         }
12 }
13
14 void __vm_unlock(void)
15 {
16         if (vmlock[0]>0) a_dec(vmlock);
17         else a_inc(vmlock);
18         if (vmlock[1]) __wake(vmlock, 1, 1);
19 }
20
21 static int pshared_barrier_wait(pthread_barrier_t *b)
22 {
23         int limit = (b->_b_limit & INT_MAX) + 1;
24         int seq;
25         int ret = 0;
26         int v, w;
27
28         if (limit==1) return PTHREAD_BARRIER_SERIAL_THREAD;
29
30         while ((v=a_cas(&b->_b_lock, 0, limit)))
31                 __wait(&b->_b_lock, &b->_b_waiters, v, 0);
32
33         seq = b->_b_seq;
34
35         if (++b->_b_count == limit) {
36                 ret = PTHREAD_BARRIER_SERIAL_THREAD;
37                 b->_b_seq++;
38                 __wake(&b->_b_seq, -1, 0);
39         } else {
40                 a_store(&b->_b_lock, 0);
41                 if (b->_b_waiters) __wake(&b->_b_lock, 1, 0);
42                 __wait(&b->_b_seq, 0, seq, 0);
43         }
44
45         __vm_lock(+1);
46
47         if (a_fetch_add(&b->_b_count, -1)==1) {
48                 b->_b_seq++;
49                 __wake(&b->_b_seq, -1, 0);
50         } else {
51                 __wait(&b->_b_seq, 0, seq+1, 0);
52         }
53         
54         /* Perform a recursive unlock suitable for self-sync'd destruction */
55         do {
56                 v = b->_b_lock;
57                 w = b->_b_waiters;
58         } while (a_cas(&b->_b_lock, v, v-1 & INT_MAX) != v);
59
60         if (v==INT_MIN+1 || (v==1 && w))
61                 __wake(&b->_b_lock, 1, 0);
62
63         __vm_unlock();
64
65         return ret;
66 }
67
68 struct instance
69 {
70         int count;
71         int last;
72         int waiters;
73         int finished;
74 };
75
76 int pthread_barrier_wait(pthread_barrier_t *b)
77 {
78         int limit = b->_b_limit;
79         struct instance *inst;
80
81         /* Trivial case: count was set at 1 */
82         if (!limit) return PTHREAD_BARRIER_SERIAL_THREAD;
83
84         /* Process-shared barriers require a separate, inefficient wait */
85         if (limit < 0) return pshared_barrier_wait(b);
86
87         /* Otherwise we need a lock on the barrier object */
88         while (a_swap(&b->_b_lock, 1))
89                 __wait(&b->_b_lock, &b->_b_waiters, 1, 1);
90         inst = b->_b_inst;
91
92         /* First thread to enter the barrier becomes the "instance owner" */
93         if (!inst) {
94                 struct instance new_inst = { 0 };
95                 int spins = 10000;
96                 b->_b_inst = inst = &new_inst;
97                 a_store(&b->_b_lock, 0);
98                 if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
99                 while (spins-- && !inst->finished)
100                         a_spin();
101                 a_inc(&inst->finished);
102                 while (inst->finished == 1)
103                         __syscall(SYS_futex, &inst->finished, FUTEX_WAIT,1,0);
104                 return PTHREAD_BARRIER_SERIAL_THREAD;
105         }
106
107         /* Last thread to enter the barrier wakes all non-instance-owners */
108         if (++inst->count == limit) {
109                 b->_b_inst = 0;
110                 a_store(&b->_b_lock, 0);
111                 if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
112                 a_store(&inst->last, 1);
113                 if (inst->waiters)
114                         __wake(&inst->last, -1, 1);
115         } else {
116                 a_store(&b->_b_lock, 0);
117                 if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
118                 __wait(&inst->last, &inst->waiters, 0, 1);
119         }
120
121         /* Last thread to exit the barrier wakes the instance owner */
122         if (a_fetch_add(&inst->count,-1)==1 && a_fetch_add(&inst->finished,1))
123                 __wake(&inst->finished, 1, 1);
124
125         return 0;
126 }