11# include <sys/syscall.h>
12# include <bits/time.h>
13# include <linux/futex.h>
14#elif defined(__APPLE__)
15# include <os/os_sync_wait_on_address.h>
23 template<
typename T,
typename U>
24 constexpr inline auto const same_properties_as_v = (
sizeof(T) ==
sizeof(U)) && (
alignof(T) ==
alignof(U));
27 constexpr inline auto const valid_specialization_v = same_properties_as_v<T, std::int32_t>;
30 int do_wait(
void const* futex, std::uint32_t expected,
Duration timeout)
33 return ::syscall(SYS_futex, futex, FUTEX_WAIT, expected, &timeoutTs,
nullptr, 0);
36 int do_wake_one(
void const* futex)
38 return ::syscall(SYS_futex, futex, FUTEX_WAKE, 1,
nullptr,
nullptr, 0);
41 int do_wake_all(
void const* futex)
43 return ::syscall(SYS_futex, futex, FUTEX_WAKE, INT_MAX,
nullptr,
nullptr, 0);
46#elif defined(__APPLE__)
47 int do_wait(
void const* futex, std::uint32_t expected,
Duration timeout)
49 return ::os_sync_wait_on_address_with_timeout(
50 const_cast<void*
>(futex), expected,
sizeof(std::uint32_t), OS_SYNC_WAIT_ON_ADDRESS_SHARED, OS_CLOCK_MACH_ABSOLUTE_TIME, timeout.value);
53 int do_wake_one(
void const* futex)
55 return ::os_sync_wake_by_address_any(
const_cast<void*
>(futex),
sizeof(std::uint32_t), OS_SYNC_WAKE_BY_ADDRESS_SHARED);
58 int do_wake_all(
void const* futex)
60 return ::os_sync_wake_by_address_all(
const_cast<void*
>(futex),
sizeof(std::uint32_t), OS_SYNC_WAKE_BY_ADDRESS_SHARED);
63# pragma GCC error "Unsupported platform"
70 static_assert(valid_specialization_v<T>,
"Only 32 bit types with natural alignment are supported.");
74 auto syncObject = std::atomic_ref{*
const_cast<T*
>(in_addr)};
76 auto syncObject = std::atomic_ref{*in_addr};
78 while (syncObject.load(std::memory_order_acquire) == in_expected)
81 if (
now >= in_deadline)
90 if (
auto const ret = do_wait(in_addr, in_expected, in_deadline -
now); ret == -1)
103 case ETIMEDOUT:
MXL_TRACE(
"ETIMEDOUT. returning false");
break;
116 static_assert(valid_specialization_v<T>,
"Only 32 bit types with natural alignment are supported.");
124 static_assert(valid_specialization_v<T>,
"Only 32 bit types with natural alignment are supported.");
126 MXL_TRACE(
"Wake one waiting on = {}, val : {}",
static_cast<void const*
>(in_addr), *in_addr);
127 do_wake_one(in_addr);
133 static_assert(valid_specialization_v<T>,
"Only 32 bit types with natural alignment are supported.");
135 MXL_TRACE(
"Wake all waiting on = {}, val : {}",
static_cast<void const*
>(in_addr), *in_addr);
136 do_wake_all(in_addr);
void wakeAll(T const *in_addr)
template void wakeOne< std::uint32_t >(std::uint32_t const *in_addr)
constexpr std::timespec asTimeSpec(Timepoint timepoint) noexcept
template bool waitUntilChanged< std::uint32_t >(std::uint32_t const *in_addr, std::uint32_t in_expected, Timepoint in_deadline)
template void wakeOne< std::int32_t >(std::int32_t const *in_addr)
template bool waitUntilChanged< std::int32_t >(std::int32_t const *in_addr, std::int32_t in_expected, Timepoint in_deadline)
template void wakeAll< std::uint32_t >(std::uint32_t const *in_addr)
void wakeOne(T const *in_addr)
bool waitUntilChanged(T const *in_addr, T in_expected, Timepoint in_deadline)
template void wakeAll< std::int32_t >(std::int32_t const *in_addr)