30#ifndef _GLIBCXX_ATOMIC_TIMED_WAIT_H
31#define _GLIBCXX_ATOMIC_TIMED_WAIT_H 1
34#pragma GCC system_header
39#if __glibcxx_atomic_wait
43#ifdef _GLIBCXX_HAVE_LINUX_FUTEX
47namespace std _GLIBCXX_VISIBILITY(default)
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
53 using __wait_clock_t = chrono::steady_clock;
55 template<
typename _Clock,
typename _Dur>
56 __wait_clock_t::time_point
57 __to_wait_clock(
const chrono::time_point<_Clock, _Dur>& __atime)
noexcept
59 const typename _Clock::time_point __c_entry = _Clock::now();
60 const __wait_clock_t::time_point __w_entry = __wait_clock_t::now();
61 const auto __delta = __atime - __c_entry;
62 using __w_dur =
typename __wait_clock_t::duration;
66 template<
typename _Dur>
67 __wait_clock_t::time_point
68 __to_wait_clock(
const chrono::time_point<__wait_clock_t,
69 _Dur>& __atime)
noexcept
71 using __w_dur =
typename __wait_clock_t::duration;
72 if constexpr (is_same_v<__w_dur, _Dur>)
78#ifdef _GLIBCXX_HAVE_LINUX_FUTEX
79#define _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
87 __wait_until_impl(
const void* __addr, __wait_args_base& __args,
88 const __wait_clock_t::duration& __atime);
90 template<
typename _Clock,
typename _Dur>
92 __wait_until(
const void* __addr, __wait_args_base& __args,
93 const chrono::time_point<_Clock, _Dur>& __atime)
noexcept
95 auto __at = __detail::__to_wait_clock(__atime);
96 auto __res = __detail::__wait_until_impl(__addr, __args,
97 __at.time_since_epoch());
99 if constexpr (!is_same_v<__wait_clock_t, _Clock>)
100 if (__res._M_timeout)
105 if (_Clock::now() < __atime)
106 __res._M_timeout =
false;
111 template<
typename _Rep,
typename _Period>
113 __wait_for(
const void* __addr, __wait_args_base& __args,
114 const chrono::duration<_Rep, _Period>& __rtime)
noexcept
116 if (!__rtime.count())
119 __args._M_flags |= __wait_flags::__do_spin | __wait_flags::__spin_only;
120 return __detail::__wait_impl(__addr, __args);
124 auto const __atime = chrono::steady_clock::now() + __reltime;
125 return __detail::__wait_until(__addr, __args, __atime);
130 template<
typename _Tp,
131 typename _Pred,
typename _ValFn,
132 typename _Clock,
typename _Dur>
134 __atomic_wait_address_until(
const _Tp* __addr, _Pred&& __pred,
137 bool __bare_wait =
false) noexcept
139 __detail::__wait_args __args{ __addr, __bare_wait };
140 _Tp __val = __args._M_setup_wait(__addr, __vfn);
141 while (!__pred(__val))
143 auto __res = __detail::__wait_until(__addr, __args, __atime);
144 if (__res._M_timeout)
146 __val = __args._M_setup_wait(__addr, __vfn, __res);
151 template<
typename _Clock,
typename _Dur>
153 __atomic_wait_address_until_v(
const __detail::__platform_wait_t* __addr,
154 __detail::__platform_wait_t __old,
157 bool __bare_wait =
false) noexcept
159#ifndef _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
160 __glibcxx_assert(
false);
162 __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
163 auto __res = __detail::__wait_until(__addr, __args, __atime);
164 return !__res._M_timeout;
167 template<
typename _Tp,
typename _ValFn,
168 typename _Clock,
typename _Dur>
170 __atomic_wait_address_until_v(
const _Tp* __addr, _Tp&& __old,
173 bool __bare_wait =
false) noexcept
175 auto __pfn = [&](
const _Tp& __val) {
176 return !__detail::__atomic_eq(__old, __val);
178 return std::__atomic_wait_address_until(__addr, __pfn, __vfn, __atime,
182 template<
typename _Tp,
183 typename _Pred,
typename _ValFn,
184 typename _Rep,
typename _Period>
186 __atomic_wait_address_for(
const _Tp* __addr, _Pred&& __pred,
189 bool __bare_wait =
false) noexcept
191 __detail::__wait_args __args{ __addr, __bare_wait };
192 _Tp __val = __args._M_setup_wait(__addr, __vfn);
193 while (!__pred(__val))
195 auto __res = __detail::__wait_for(__addr, __args, __rtime);
196 if (__res._M_timeout)
198 __val = __args._M_setup_wait(__addr, __vfn);
203 template<
typename _Rep,
typename _Period>
205 __atomic_wait_address_for_v(
const __detail::__platform_wait_t* __addr,
206 __detail::__platform_wait_t __old,
209 bool __bare_wait =
false) noexcept
211#ifndef _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
212 __glibcxx_assert(
false);
214 __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
215 auto __res = __detail::__wait_for(__addr, __args, __rtime);
216 return !__res._M_timeout;
219 template<
typename _Tp,
typename _ValFn,
220 typename _Rep,
typename _Period>
222 __atomic_wait_address_for_v(
const _Tp* __addr, _Tp&& __old, _ValFn&& __vfn,
224 bool __bare_wait =
false) noexcept
226 auto __pfn = [&](
const _Tp& __val) {
227 return !__detail::__atomic_eq(__old, __val);
229 return __atomic_wait_address_for(__addr, __pfn,
forward<_ValFn>(__vfn),
230 __rtime, __bare_wait);
232_GLIBCXX_END_NAMESPACE_VERSION
constexpr __enable_if_is_duration< _ToDur > ceil(const duration< _Rep, _Period > &__d)
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.
Implementation details not part of the namespace std interface.
chrono::duration represents a distance between two points in time
chrono::time_point represents a point in time as measured by a clock