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