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