summaryrefslogtreecommitdiff
path: root/src/thread/sem_trywait.c
blob: 55d90075e274a5e0a82359ffc96e6b45a279e286 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <semaphore.h>
#include "pthread_impl.h"

int sem_trywait(sem_t *sem)
{
	int val = sem->__val[0];
	if (val>0) {
		int new = val-1-(val==1 && sem->__val[1]);
		if (a_cas(sem->__val, val, new)==val) return 0;
	}
	errno = EAGAIN;
	return -1;
}