xref: /aosp_15_r20/external/musl/src/thread/sem_trywait.c (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1 #include <semaphore.h>
2 #include <limits.h>
3 #include "pthread_impl.h"
4 
sem_trywait(sem_t * sem)5 int sem_trywait(sem_t *sem)
6 {
7 	int val;
8 	while ((val=sem->__val[0]) & SEM_VALUE_MAX) {
9 		if (a_cas(sem->__val, val, val-1)==val) return 0;
10 	}
11 	errno = EAGAIN;
12 	return -1;
13 }
14