33#include <linux/futex.h>
34#include <sys/syscall.h>
39 static int futex(
int *uaddr,
int futex_op,
int val,
40 const struct timespec *timeout,
int *uaddr2,
int val3) {
41 return syscall(SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
53 bool wait(
struct timespec *timeout =
nullptr) {
56 if (i.compare_exchange_strong(one, 0, std::memory_order_relaxed,
57 std::memory_order_relaxed)) {
60 auto rc = futex((
int *)&i, FUTEX_WAIT, 0, timeout,
nullptr, 0);
62 if (errno == ETIMEDOUT) {
65 if (errno != EAGAIN) {
74 if (i.compare_exchange_strong(zero, 1, std::memory_order_relaxed,
75 std::memory_order_relaxed)) {
76 if (futex((
int *)&i, FUTEX_WAKE, 1,
nullptr,
nullptr, 0) == -1) {
83 std::atomic<int> i = {0};
Binary semaphore class to be used for thread synchronisation.
Definition binarySemaphore.hpp:50
bool wait(struct timespec *timeout=nullptr)
Definition binarySemaphore.hpp:53
void post()
Definition binarySemaphore.hpp:72
Definition binarySemaphore.hpp:45