major semaphore improvements (performance and correctness)
[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         do e = __timedwait(&c->_c_block, 1, c->_c_clock, ts, 0);
20         while (e == EINTR);
21         CANCELPT_END;
22
23         pthread_cleanup_pop(0);
24         if ((r=pthread_mutex_lock(m))) return r;
25
26         CANCELPT_BEGIN;
27         CANCELPT_END;
28         return e;
29 }