blob: af2910ee125c662cb7679f9e07a24bf3e7064ce2 [file] [log] [blame]
#include <semaphore.h>
#include "atomic.h"
#include "threads_impl.h"
int sem_trywait(sem_t* sem) {
int val;
while ((val = atomic_load(&sem->_s_value)) > 0) {
int new = val - 1 - (val == 1 && atomic_load(&sem->_s_waiters));
if (a_cas_shim(&sem->_s_value, val, new) == val)
return 0;
}
errno = EAGAIN;
return -1;
}