libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
futex.c
1#include <clod/sys/futex.h>
2#include <linux/time.h>
3#include <linux/errno.h>
4#include "syscall.h"
5
6enum clod_futex_error clod_futex_wait(const int *ptr, const int expected, int64_t timeout_us) {
7 struct timespec ts;
8 ts.tv_sec = timeout_us / 1000000;
9 ts.tv_nsec = (timeout_us - ts.tv_sec * 1000000) * 1000;
10 const long res = syscall(__NR_futex, (long)ptr, __NR_futex_wait, expected, (long)&ts);
11 if (res < 0) {
12 if (-res == ETIMEDOUT) return CLOD_FUTEX_OK;
13 if (-res == EAGAIN) return CLOD_FUTEX_OK;
14 if (-res == EINTR) return CLOD_FUTEX_INTERRUPT;
15 return CLOD_FUTEX_INVALID;
16 }
17 return CLOD_FUTEX_OK;
18}
19
20enum clod_futex_error clod_futex_wake_one(const int *ptr) {
21 const long res = syscall(__NR_futex, (long)ptr, __NR_futex_wake, 1);
22 if (res < 0) {
23 return CLOD_FUTEX_INVALID;
24 }
25 return CLOD_FUTEX_OK;
26}
27
28enum clod_futex_error clod_futex_wake_all(const int *ptr) {
29 const long res = syscall(__NR_futex, (long)ptr, __NR_futex_wake, (long)INT_MAX);
30 if (res < 0) {
31 return CLOD_FUTEX_INVALID;
32 }
33 return CLOD_FUTEX_OK;
34}
clod_futex_error
Definition futex.h:13
@ CLOD_FUTEX_OK
No worries.
Definition futex.h:15
@ CLOD_FUTEX_INVALID
Invalid usage i.e. argument, pointer.
Definition futex.h:17
@ CLOD_FUTEX_INTERRUPT
The operation was interrupted by a signal.
Definition futex.h:19