25#ifndef JOIN_CORE_FUTEX_HPP
26#define JOIN_CORE_FUTEX_HPP
32#include <sys/syscall.h>
33#include <linux/futex.h>
91 uint32_t expected = 0;
92 if (_futex.compare_exchange_strong (expected, 1, std::memory_order_acquire, std::memory_order_relaxed))
100 _futex.compare_exchange_strong (expected, 2, std::memory_order_relaxed, std::memory_order_relaxed))
102 ::syscall (SYS_futex,
reinterpret_cast<uint32_t*
> (&_futex), FUTEX_WAIT_PRIVATE, 2,
nullptr,
107 while (!_futex.compare_exchange_strong (expected, 2, std::memory_order_acquire, std::memory_order_relaxed));
116 uint32_t expected = 0;
117 return _futex.compare_exchange_strong (expected, 1, std::memory_order_acquire, std::memory_order_relaxed);
125 if (_futex.exchange (0, std::memory_order_release) == 2)
127 ::syscall (SYS_futex,
reinterpret_cast<uint32_t*
> (&_futex), FUTEX_WAKE_PRIVATE, 1,
nullptr,
nullptr,
143 alignas (4) std::atomic_uint32_t _futex;
196 uint32_t expected = 0;
197 if (_futex.compare_exchange_strong (expected, 1, std::memory_order_acquire, std::memory_order_relaxed))
205 _futex.compare_exchange_strong (expected, 2, std::memory_order_relaxed, std::memory_order_relaxed))
207 ::syscall (SYS_futex,
reinterpret_cast<uint32_t*
> (&_futex), FUTEX_WAIT, 2,
nullptr,
nullptr, 0);
211 while (!_futex.compare_exchange_strong (expected, 2, std::memory_order_acquire, std::memory_order_relaxed));
220 uint32_t expected = 0;
221 return _futex.compare_exchange_strong (expected, 1, std::memory_order_acquire, std::memory_order_relaxed);
229 if (_futex.exchange (0, std::memory_order_release) == 2)
231 ::syscall (SYS_futex,
reinterpret_cast<uint32_t*
> (&_futex), FUTEX_WAKE, 1,
nullptr,
nullptr, 0);
246 alignas (4) std::atomic_uint32_t _futex;
Futex-based mutex for intra-process locking.
Definition futex.hpp:45
~Futex()=default
destructor.
Futex() noexcept
default constructor.
Definition futex.hpp:50
Futex(const Futex &other)=delete
copy constructor.
Futex(Futex &&other)=delete
move constructor.
void lock() noexcept
lock the futex, blocking until it becomes available.
Definition futex.hpp:89
void unlock() noexcept
unlock the futex, waking one waiter if any.
Definition futex.hpp:123
Futex & operator=(const Futex &other)=delete
copy assignment.
bool tryLock() noexcept
try to lock the futex without blocking.
Definition futex.hpp:114
std::atomic_uint32_t * handle() noexcept
get a pointer to the underlying futex word.
Definition futex.hpp:136
Futex-based mutex suitable for inter-process locking via shared memory.
Definition futex.hpp:150
std::atomic_uint32_t * handle() noexcept
get a pointer to the underlying futex word.
Definition futex.hpp:239
SharedFutex(const SharedFutex &other)=delete
copy constructor.
SharedFutex & operator=(const SharedFutex &other)=delete
copy assignment.
SharedFutex(SharedFutex &&other)=delete
move constructor.
bool tryLock() noexcept
try to lock the futex without blocking.
Definition futex.hpp:218
void unlock() noexcept
unlock the futex, waking one waiter if any.
Definition futex.hpp:227
~SharedFutex()=default
destructor.
void lock() noexcept
lock the futex, blocking until it becomes available.
Definition futex.hpp:194
SharedFutex() noexcept
default constructor.
Definition futex.hpp:155
Definition acceptor.hpp:32