4e12389cefc4cae94d6e96e7a3b4d9caba6c7468
[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                 if (r) {
17                         errno = r;
18                         return -1;
19                 }
20         }
21         return 0;
22 }