overhaul cancellation to fix resource leaks and dangerous behavior with signals
[musl] / src / thread / pthread_cond_timedwait.c
1 #include "pthread_impl.h"
2
3 static void relock(void *m)
4 {
5         pthread_mutex_lock(m);
6 }
7
8 int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, const struct timespec *ts)
9 {
10         int r, e=0;
11         CANCELPT_BEGIN;
12         CANCELPT_END;
13
14         pthread_cleanup_push(relock, m);
15         c->_c_block = 1;
16         if ((r=pthread_mutex_unlock(m))) return r;
17
18         CANCELPT_BEGIN;
19         e = __timedwait(&c->_c_block, 1, c->_c_clock, ts, 0);
20         CANCELPT_END;
21
22         pthread_cleanup_pop(0);
23         if ((r=pthread_mutex_lock(m))) return r;
24
25         CANCELPT_BEGIN;
26         CANCELPT_END;
27         return e;
28 }