Rich Felker | 6fc5fdb | 2011-03-04 00:45:59 -0500 | [diff] [blame] | 1 | #include <semaphore.h> |
2 | #include "pthread_impl.h" | ||||
3 | |||||
4 | int sem_trywait(sem_t *sem) | ||||
5 | { | ||||
Rich Felker | 07e6295 | 2012-07-24 00:51:36 -0400 | [diff] [blame] | 6 | int val; |
7 | while ((val=sem->__val[0]) > 0) { | ||||
Rich Felker | 88c4e72 | 2011-08-02 19:19:09 -0400 | [diff] [blame] | 8 | int new = val-1-(val==1 && sem->__val[1]); |
9 | if (a_cas(sem->__val, val, new)==val) return 0; | ||||
10 | } | ||||
Rich Felker | 6fc5fdb | 2011-03-04 00:45:59 -0500 | [diff] [blame] | 11 | errno = EAGAIN; |
12 | return -1; | ||||
13 | } |