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