64b4342ca2212ef0744440e705c6b6bf1e3692c5
[musl] / src / thread / sem_timedwait.c
1 #include <semaphore.h>
2 #include "pthread_impl.h"
3
4 static void cleanup(void *p)
5 {
6         a_dec(p);
7 }
8
9 int sem_timedwait(sem_t *sem, const struct timespec *at)
10 {
11         while (sem_trywait(sem)) {
12                 int r;
13                 a_inc(sem->__val+1);
14                 a_cas(sem->__val, 0, -1);
15                 r = __timedwait(sem->__val, -1, CLOCK_REALTIME, at, cleanup, sem->__val+1, 0);
16                 a_dec(sem->__val+1);
17                 if (r) {
18                         errno = r;
19                         return -1;
20                 }
21         }
22         return 0;
23 }