fix deadlock in condition wait whenever there are multiple waiters
[musl] / src / thread / pthread_cond_timedwait.c
1 #include "pthread_impl.h"
2
3 struct cm {
4         pthread_cond_t *c;
5         pthread_mutex_t *m;
6 };
7
8 static void cleanup(void *p)
9 {
10         struct cm *cm = p;
11         a_dec(&cm->c->_c_waiters);
12         pthread_mutex_lock(cm->m);
13 }
14
15 int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, const struct timespec *ts)
16 {
17         struct cm cm = { .c=c, .m=m };
18         int r, e=0;
19
20         if (ts && ts->tv_nsec >= 1000000000UL)
21                 return EINVAL;
22
23         pthread_testcancel();
24
25         c->_c_block = 1;
26         if ((r=pthread_mutex_unlock(m))) return r;
27
28         a_inc(&c->_c_waiters);
29         do e = __timedwait(&c->_c_block, 1, c->_c_clock, ts, cleanup, &cm, 0);
30         while (e == EINTR);
31         a_dec(&c->_c_waiters);
32
33         if ((r=pthread_mutex_lock(m))) return r;
34
35         pthread_testcancel();
36         return e;
37 }