make all objects used with atomic operations volatile
[musl] / src / thread / pthread_cond_timedwait.c
1 #include "pthread_impl.h"
2
3 void __pthread_testcancel(void);
4 int __pthread_mutex_lock(pthread_mutex_t *);
5 int __pthread_mutex_unlock(pthread_mutex_t *);
6 int __pthread_setcancelstate(int, int *);
7
8 /*
9  * struct waiter
10  *
11  * Waiter objects have automatic storage on the waiting thread, and
12  * are used in building a linked list representing waiters currently
13  * waiting on the condition variable or a group of waiters woken
14  * together by a broadcast or signal; in the case of signal, this is a
15  * degenerate list of one member.
16  *
17  * Waiter lists attached to the condition variable itself are
18  * protected by the lock on the cv. Detached waiter lists are never
19  * modified again, but can only be traversed in reverse order, and are
20  * protected by the "barrier" locks in each node, which are unlocked
21  * in turn to control wake order.
22  *
23  * Since process-shared cond var semantics do not necessarily allow
24  * one thread to see another's automatic storage (they may be in
25  * different processes), the waiter list is not used for the
26  * process-shared case, but the structure is still used to store data
27  * needed by the cancellation cleanup handler.
28  */
29
30 struct waiter {
31         struct waiter *prev, *next;
32         volatile int state, barrier;
33         volatile int *notify;
34 };
35
36 /* Self-synchronized-destruction-safe lock functions */
37
38 static inline void lock(volatile int *l)
39 {
40         if (a_cas(l, 0, 1)) {
41                 a_cas(l, 1, 2);
42                 do __wait(l, 0, 2, 1);
43                 while (a_cas(l, 0, 2));
44         }
45 }
46
47 static inline void unlock(volatile int *l)
48 {
49         if (a_swap(l, 0)==2)
50                 __wake(l, 1, 1);
51 }
52
53 static inline void unlock_requeue(volatile int *l, volatile int *r, int w)
54 {
55         a_store(l, 0);
56         if (w) __wake(l, 1, 1);
57         else __syscall(SYS_futex, l, FUTEX_REQUEUE|128, 0, 1, r) != -ENOSYS
58                 || __syscall(SYS_futex, l, FUTEX_REQUEUE, 0, 1, r);
59 }
60
61 enum {
62         WAITING,
63         SIGNALED,
64         LEAVING,
65 };
66
67 int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
68 {
69         struct waiter node = { 0 };
70         int e, seq, clock = c->_c_clock, cs, shared=0, oldstate, tmp;
71         volatile int *fut;
72
73         if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid)
74                 return EPERM;
75
76         if (ts && ts->tv_nsec >= 1000000000UL)
77                 return EINVAL;
78
79         __pthread_testcancel();
80
81         if (c->_c_shared) {
82                 shared = 1;
83                 fut = &c->_c_seq;
84                 seq = c->_c_seq;
85                 a_inc(&c->_c_waiters);
86         } else {
87                 lock(&c->_c_lock);
88
89                 seq = node.barrier = 2;
90                 fut = &node.barrier;
91                 node.state = WAITING;
92                 node.next = c->_c_head;
93                 c->_c_head = &node;
94                 if (!c->_c_tail) c->_c_tail = &node;
95                 else node.next->prev = &node;
96
97                 unlock(&c->_c_lock);
98         }
99
100         __pthread_mutex_unlock(m);
101
102         __pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs);
103
104         do e = __timedwait_cp(fut, seq, clock, ts, !shared);
105         while (*fut==seq && (!e || e==EINTR));
106         if (e == EINTR) e = 0;
107
108         if (shared) {
109                 /* Suppress cancellation if a signal was potentially
110                  * consumed; this is a legitimate form of spurious
111                  * wake even if not. */
112                 if (e == ECANCELED && c->_c_seq != seq) e = 0;
113                 if (a_fetch_add(&c->_c_waiters, -1) == -0x7fffffff)
114                         __wake(&c->_c_waiters, 1, 0);
115                 oldstate = WAITING;
116                 goto relock;
117         }
118
119         oldstate = a_cas(&node.state, WAITING, LEAVING);
120
121         if (oldstate == WAITING) {
122                 /* Access to cv object is valid because this waiter was not
123                  * yet signaled and a new signal/broadcast cannot return
124                  * after seeing a LEAVING waiter without getting notified
125                  * via the futex notify below. */
126
127                 lock(&c->_c_lock);
128                 
129                 if (c->_c_head == &node) c->_c_head = node.next;
130                 else if (node.prev) node.prev->next = node.next;
131                 if (c->_c_tail == &node) c->_c_tail = node.prev;
132                 else if (node.next) node.next->prev = node.prev;
133                 
134                 unlock(&c->_c_lock);
135
136                 if (node.notify) {
137                         if (a_fetch_add(node.notify, -1)==1)
138                                 __wake(node.notify, 1, 1);
139                 }
140         } else {
141                 /* Lock barrier first to control wake order. */
142                 lock(&node.barrier);
143         }
144
145 relock:
146         /* Errors locking the mutex override any existing error or
147          * cancellation, since the caller must see them to know the
148          * state of the mutex. */
149         if ((tmp = pthread_mutex_lock(m))) e = tmp;
150
151         if (oldstate == WAITING) goto done;
152
153         if (!node.next) a_inc(&m->_m_waiters);
154
155         /* Unlock the barrier that's holding back the next waiter, and
156          * either wake it or requeue it to the mutex. */
157         if (node.prev)
158                 unlock_requeue(&node.prev->barrier, &m->_m_lock, m->_m_type & 128);
159         else
160                 a_dec(&m->_m_waiters);
161
162         /* Since a signal was consumed, cancellation is not permitted. */
163         if (e == ECANCELED) e = 0;
164
165 done:
166         __pthread_setcancelstate(cs, 0);
167
168         if (e == ECANCELED) {
169                 __pthread_testcancel();
170                 __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
171         }
172
173         return e;
174 }
175
176 int __private_cond_signal(pthread_cond_t *c, int n)
177 {
178         struct waiter *p, *first=0;
179         volatile int ref = 0;
180         int cur;
181
182         lock(&c->_c_lock);
183         for (p=c->_c_tail; n && p; p=p->prev) {
184                 if (a_cas(&p->state, WAITING, SIGNALED) != WAITING) {
185                         ref++;
186                         p->notify = &ref;
187                 } else {
188                         n--;
189                         if (!first) first=p;
190                 }
191         }
192         /* Split the list, leaving any remainder on the cv. */
193         if (p) {
194                 if (p->next) p->next->prev = 0;
195                 p->next = 0;
196         } else {
197                 c->_c_head = 0;
198         }
199         c->_c_tail = p;
200         unlock(&c->_c_lock);
201
202         /* Wait for any waiters in the LEAVING state to remove
203          * themselves from the list before returning or allowing
204          * signaled threads to proceed. */
205         while ((cur = ref)) __wait(&ref, 0, cur, 1);
206
207         /* Allow first signaled waiter, if any, to proceed. */
208         if (first) unlock(&first->barrier);
209
210         return 0;
211 }
212
213 weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);