30#ifndef _GLIBCXX_ATOMIC_FUTEX_H
31#define _GLIBCXX_ATOMIC_FUTEX_H 1
34#pragma GCC system_header
38#if ! (defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1)
44#ifndef _GLIBCXX_ALWAYS_INLINE
45#define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
52#ifdef _GLIBCXX_HAS_GTHREADS
53#if defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
54 struct __atomic_futex_unsigned_base
59 _M_futex_wait_until(
unsigned *__addr,
unsigned __val,
bool __has_timeout,
65 _M_futex_wait_until_steady(
unsigned *__addr,
unsigned __val,
69 static void _M_futex_notify_all(
unsigned* __addr);
72 template <
unsigned _Waiter_bit = 0x80000000>
73 class __atomic_futex_unsigned : __atomic_futex_unsigned_base
75 typedef chrono::steady_clock __clock_t;
78 atomic<unsigned> _M_data;
82 __atomic_futex_unsigned(
unsigned __data) : _M_data(__data)
85 _GLIBCXX_ALWAYS_INLINE
unsigned
88 return _M_data.load(__mo) & ~_Waiter_bit;
99 _M_load_and_test_until(
unsigned __assumed,
unsigned __operand,
110 _M_data.fetch_or(_Waiter_bit, memory_order_relaxed);
111 bool __ret = _M_futex_wait_until((
unsigned*)(
void*)&_M_data,
112 __assumed | _Waiter_bit,
113 __has_timeout, __s, __ns);
115 __assumed = _M_load(__mo);
116 if (!__ret || ((__operand == __assumed) == __equal))
129 _M_load_and_test_until_steady(
unsigned __assumed,
unsigned __operand,
140 _M_data.fetch_or(_Waiter_bit, memory_order_relaxed);
141 bool __ret = _M_futex_wait_until_steady((
unsigned*)(
void*)&_M_data,
142 __assumed | _Waiter_bit,
143 __has_timeout, __s, __ns);
145 __assumed = _M_load(__mo);
146 if (!__ret || ((__operand == __assumed) == __equal))
157 _M_load_and_test(
unsigned __assumed,
unsigned __operand,
160 return _M_load_and_test_until(__assumed, __operand, __equal, __mo,
169 template<
typename _Dur>
171 _M_load_and_test_until_impl(
unsigned __assumed,
unsigned __operand,
173 const chrono::time_point<std::chrono::system_clock, _Dur>& __atime)
175 auto __d = __atime.time_since_epoch();
176 if (__d < __d.zero()) [[__unlikely__]]
180 return _M_load_and_test_until(__assumed, __operand, __equal, __mo,
184 template<
typename _Dur>
186 _M_load_and_test_until_impl(
unsigned __assumed,
unsigned __operand,
188 const chrono::time_point<std::chrono::steady_clock, _Dur>& __atime)
190 auto __d = __atime.time_since_epoch();
191 if (__d < __d.zero()) [[__unlikely__]]
195 return _M_load_and_test_until_steady(__assumed, __operand, __equal, __mo,
201 _GLIBCXX_ALWAYS_INLINE
unsigned
202 _M_load_when_not_equal(
unsigned __val,
memory_order __mo)
204 unsigned __i = _M_load(__mo);
205 if ((__i & ~_Waiter_bit) != __val)
206 return (__i & ~_Waiter_bit);
208 return _M_load_and_test(__i, __val,
false, __mo);
211 _GLIBCXX_ALWAYS_INLINE
void
214 unsigned __i = _M_load(__mo);
215 if ((__i & ~_Waiter_bit) == __val)
218 _M_load_and_test(__i, __val,
true, __mo);
222 template<
typename _Rep,
typename _Period>
223 _GLIBCXX_ALWAYS_INLINE
bool
224 _M_load_when_equal_for(
unsigned __val,
memory_order __mo,
225 const chrono::duration<_Rep, _Period>& __rtime)
227 using __dur =
typename __clock_t::duration;
228 return _M_load_when_equal_until(__val, __mo,
229 __clock_t::now() + chrono::__detail::ceil<__dur>(__rtime));
233 template<
typename _Clock,
typename _Duration>
234 _GLIBCXX_ALWAYS_INLINE
bool
235 _M_load_when_equal_until(
unsigned __val,
memory_order __mo,
236 const chrono::time_point<_Clock, _Duration>& __atime)
238 typename _Clock::time_point __c_entry = _Clock::now();
240 const __clock_t::time_point __s_entry = __clock_t::now();
241 const auto __delta = __atime - __c_entry;
242 const auto __s_atime = __s_entry +
243 chrono::__detail::ceil<__clock_t::duration>(__delta);
244 if (_M_load_when_equal_until(__val, __mo, __s_atime))
246 __c_entry = _Clock::now();
247 }
while (__c_entry < __atime);
252 template<
typename _Duration>
253 _GLIBCXX_ALWAYS_INLINE
bool
254 _M_load_when_equal_until(
unsigned __val,
memory_order __mo,
255 const chrono::time_point<std::chrono::system_clock, _Duration>& __atime)
257 unsigned __i = _M_load(__mo);
258 if ((__i & ~_Waiter_bit) == __val)
261 __i = _M_load_and_test_until_impl(__i, __val,
true, __mo, __atime);
262 return (__i & ~_Waiter_bit) == __val;
266 template<
typename _Duration>
267 _GLIBCXX_ALWAYS_INLINE
bool
268 _M_load_when_equal_until(
unsigned __val,
memory_order __mo,
269 const chrono::time_point<std::chrono::steady_clock, _Duration>& __atime)
271 unsigned __i = _M_load(__mo);
272 if ((__i & ~_Waiter_bit) == __val)
275 __i = _M_load_and_test_until_impl(__i, __val,
true, __mo, __atime);
276 return (__i & ~_Waiter_bit) == __val;
279 _GLIBCXX_ALWAYS_INLINE
void
282 unsigned* __futex = (
unsigned *)(
void *)&_M_data;
283 if (_M_data.exchange(__val, __mo) & _Waiter_bit)
284 _M_futex_notify_all(__futex);
293 template <
unsigned _Waiter_bit = 0x80000000>
294 class __atomic_futex_unsigned
296 typedef chrono::system_clock __clock_t;
300 condition_variable _M_condvar;
304 __atomic_futex_unsigned(
unsigned __data) : _M_data(__data)
307 _GLIBCXX_ALWAYS_INLINE
unsigned
310 unique_lock<mutex> __lock(_M_mutex);
314 _GLIBCXX_ALWAYS_INLINE
unsigned
317 unique_lock<mutex> __lock(_M_mutex);
318 while (_M_data == __val)
319 _M_condvar.wait(__lock);
323 _GLIBCXX_ALWAYS_INLINE
void
326 unique_lock<mutex> __lock(_M_mutex);
327 while (_M_data != __val)
328 _M_condvar.wait(__lock);
331 template<
typename _Rep,
typename _Period>
332 _GLIBCXX_ALWAYS_INLINE
bool
334 const chrono::duration<_Rep, _Period>& __rtime)
336 unique_lock<mutex> __lock(_M_mutex);
337 return _M_condvar.wait_for(__lock, __rtime,
338 [&] {
return _M_data == __val;});
341 template<
typename _Clock,
typename _Duration>
342 _GLIBCXX_ALWAYS_INLINE
bool
344 const chrono::time_point<_Clock, _Duration>& __atime)
346 unique_lock<mutex> __lock(_M_mutex);
347 return _M_condvar.wait_until(__lock, __atime,
348 [&] {
return _M_data == __val;});
351 _GLIBCXX_ALWAYS_INLINE
void
354 unique_lock<mutex> __lock(_M_mutex);
356 _M_condvar.notify_all();
363_GLIBCXX_END_NAMESPACE_VERSION
duration< int64_t, nano > nanoseconds
nanoseconds
duration< int64_t > seconds
seconds
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
memory_order
Enumeration for memory_order.
ISO C++ entities toplevel namespace is std.