1 #include <semaphore.h>
2 #include <limits.h>
3 #include "pthread_impl.h"
4
cleanup(void * p)5 static void cleanup(void *p)
6 {
7 a_dec(p);
8 }
9
__sem_timedwait(sem_t * restrict sem,const struct timespec * restrict at)10 int __sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at)
11 {
12 int spins = 100;
13 while (spins-- && !(sem->__val[0] & SEM_VALUE_MAX) && !sem->__val[1])
14 a_spin();
15
16 while (sem_trywait(sem)) {
17 int r, priv = sem->__val[2];
18 a_inc(sem->__val+1);
19 a_cas(sem->__val, 0, 0x80000000);
20 pthread_cleanup_push(cleanup, (void *)(sem->__val+1));
21 r = __timedwait_cp(sem->__val, 0x80000000, CLOCK_REALTIME, at, priv);
22 pthread_cleanup_pop(1);
23 if (r) {
24 errno = r;
25 return -1;
26 }
27 }
28 return 0;
29 }
30
sem_timedwait(sem_t * restrict sem,const struct timespec * restrict at)31 int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at)
32 {
33 pthread_testcancel();
34
35 if (!sem_trywait(sem)) return 0;
36
37 return __sem_timedwait(sem, at);
38 }
39