MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Sync.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
5#include <cerrno>
6#include <climits>
7#include <cstdint>
8#include <atomic>
9#include <unistd.h>
10#if defined(__linux__)
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>
16#endif
18
19namespace mxl::lib
20{
21 namespace
22 {
23 template<typename T, typename U>
24 constexpr inline auto const same_properties_as_v = (sizeof(T) == sizeof(U)) && (alignof(T) == alignof(U));
25
26 template<typename T>
27 constexpr inline auto const valid_specialization_v = same_properties_as_v<T, std::int32_t>;
28
29#if defined(__linux__)
30 int do_wait(void const* futex, std::uint32_t expected, Duration timeout)
31 {
32 auto const timeoutTs = asTimeSpec(timeout);
33 return ::syscall(SYS_futex, futex, FUTEX_WAIT, expected, &timeoutTs, nullptr, 0);
34 }
35
36 int do_wake_one(void const* futex)
37 {
38 return ::syscall(SYS_futex, futex, FUTEX_WAKE, 1, nullptr, nullptr, 0);
39 }
40
41 int do_wake_all(void const* futex)
42 {
43 return ::syscall(SYS_futex, futex, FUTEX_WAKE, INT_MAX, nullptr, nullptr, 0);
44 }
45
46#elif defined(__APPLE__)
47 int do_wait(void const* futex, std::uint32_t expected, Duration timeout)
48 {
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);
51 }
52
53 int do_wake_one(void const* futex)
54 {
55 return ::os_sync_wake_by_address_any(const_cast<void*>(futex), sizeof(std::uint32_t), OS_SYNC_WAKE_BY_ADDRESS_SHARED);
56 }
57
58 int do_wake_all(void const* futex)
59 {
60 return ::os_sync_wake_by_address_all(const_cast<void*>(futex), sizeof(std::uint32_t), OS_SYNC_WAKE_BY_ADDRESS_SHARED);
61 }
62#else
63# pragma GCC error "Unsupported platform"
64#endif
65 }
66
67 template<typename T>
68 bool waitUntilChanged(T const* in_addr, T in_expected, Timepoint in_deadline)
69 {
70 static_assert(valid_specialization_v<T>, "Only 32 bit types with natural alignment are supported.");
71
72#if _LIBCPP_VERSION
73 // libc++ limitation due to: https://github.com/llvm/llvm-project/issues/118378
74 auto syncObject = std::atomic_ref{*const_cast<T*>(in_addr)};
75#else
76 auto syncObject = std::atomic_ref{*in_addr};
77#endif
78 while (syncObject.load(std::memory_order_acquire) == in_expected)
79 {
80 auto const now = currentTime(Clock::Realtime);
81 if (now >= in_deadline)
82 {
83 MXL_DEBUG("Deadline already reached");
84 return false;
85 }
86
87 // ATTENTION: Do not check for != 0, as positive values are success indicators
88 // in the macOS implementation of this function.
89 // NOTE: Unfortunately syncObject.address() is only available from C++26 onwards
90 if (auto const ret = do_wait(in_addr, in_expected, in_deadline - now); ret == -1)
91 {
92 switch (errno)
93 {
94 case EAGAIN:
95 // On linux EAGAIN indicates that the kernel detected *in_addr to
96 // not be equal to in_expected.
97 continue;
98
99 case EINTR:
100 // Interrupted. try again.
101 continue;
102
103 case ETIMEDOUT: MXL_TRACE("ETIMEDOUT. returning false"); break;
104
105 default: break;
106 }
107 return false;
108 }
109 }
110 return true;
111 }
112
113 template<typename T>
114 bool waitUntilChanged(T const* in_addr, T in_expected, Duration in_timeout)
115 {
116 static_assert(valid_specialization_v<T>, "Only 32 bit types with natural alignment are supported.");
117
118 return waitUntilChanged(in_addr, in_expected, currentTime(Clock::Realtime) + in_timeout);
119 }
120
121 template<typename T>
122 void wakeOne(T const* in_addr)
123 {
124 static_assert(valid_specialization_v<T>, "Only 32 bit types with natural alignment are supported.");
125
126 MXL_TRACE("Wake one waiting on = {}, val : {}", static_cast<void const*>(in_addr), *in_addr);
127 do_wake_one(in_addr);
128 }
129
130 template<typename T>
131 void wakeAll(T const* in_addr)
132 {
133 static_assert(valid_specialization_v<T>, "Only 32 bit types with natural alignment are supported.");
134
135 MXL_TRACE("Wake all waiting on = {}, val : {}", static_cast<void const*>(in_addr), *in_addr);
136 do_wake_all(in_addr);
137 }
138
139 // Explicit template instantiations
140 template bool waitUntilChanged<std::uint32_t>(std::uint32_t const* in_addr, std::uint32_t in_expected, Timepoint in_deadline);
141 template bool waitUntilChanged<std::uint32_t>(std::uint32_t const* in_addr, std::uint32_t in_expected, Duration in_timeout);
142 template bool waitUntilChanged<std::int32_t>(std::int32_t const* in_addr, std::int32_t in_expected, Timepoint in_deadline);
143 template bool waitUntilChanged<std::int32_t>(std::int32_t const* in_addr, std::int32_t in_expected, Duration in_timeout);
144 template void wakeOne<std::uint32_t>(std::uint32_t const* in_addr);
145 template void wakeOne<std::int32_t>(std::int32_t const* in_addr);
146 template void wakeAll<std::uint32_t>(std::uint32_t const* in_addr);
147 template void wakeAll<std::int32_t>(std::int32_t const* in_addr);
148}
#define MXL_DEBUG(...)
Definition Logging.hpp:34
#define MXL_TRACE(...)
Definition Logging.hpp:23
void wakeAll(T const *in_addr)
Definition Sync.cpp:131
template void wakeOne< std::uint32_t >(std::uint32_t const *in_addr)
constexpr std::timespec asTimeSpec(Timepoint timepoint) noexcept
Definition Timing.hpp:241
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)
Definition Sync.cpp:122
bool waitUntilChanged(T const *in_addr, T in_expected, Timepoint in_deadline)
Definition Sync.cpp:68
template void wakeAll< std::int32_t >(std::int32_t const *in_addr)
auto const now
auto const currentTime
Definition test_time.cpp:58