blob: 04edf46b524de72a575b1db7687f56d93fc1e291 [file] [log] [blame]
Rich Felker6fc5fdb2011-03-04 00:45:59 -05001#include <semaphore.h>
2#include "pthread_impl.h"
3
4int sem_trywait(sem_t *sem)
5{
Rich Felker07e62952012-07-24 00:51:36 -04006 int val;
7 while ((val=sem->__val[0]) > 0) {
Rich Felker88c4e722011-08-02 19:19:09 -04008 int new = val-1-(val==1 && sem->__val[1]);
9 if (a_cas(sem->__val, val, new)==val) return 0;
10 }
Rich Felker6fc5fdb2011-03-04 00:45:59 -050011 errno = EAGAIN;
12 return -1;
13}