summaryrefslogtreecommitdiffstats
path: root/libc-top-half/musl/src/thread/sem_trywait.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc-top-half/musl/src/thread/sem_trywait.c')
-rw-r--r--libc-top-half/musl/src/thread/sem_trywait.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/libc-top-half/musl/src/thread/sem_trywait.c b/libc-top-half/musl/src/thread/sem_trywait.c
new file mode 100644
index 0000000..04edf46
--- /dev/null
+++ b/libc-top-half/musl/src/thread/sem_trywait.c
@@ -0,0 +1,13 @@
+#include <semaphore.h>
+#include "pthread_impl.h"
+
+int sem_trywait(sem_t *sem)
+{
+ int val;
+ while ((val=sem->__val[0]) > 0) {
+ int new = val-1-(val==1 && sem->__val[1]);
+ if (a_cas(sem->__val, val, new)==val) return 0;
+ }
+ errno = EAGAIN;
+ return -1;
+}