libstdc++
atomic_timed_wait.h
Go to the documentation of this file.
1// -*- C++ -*- header.
2
3// Copyright (C) 2020-2025 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/atomic_timed_wait.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{atomic}
28 */
29
30#ifndef _GLIBCXX_ATOMIC_TIMED_WAIT_H
31#define _GLIBCXX_ATOMIC_TIMED_WAIT_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#include <bits/atomic_wait.h>
38
39#if __glibcxx_atomic_wait
41#include <bits/chrono.h>
42
43#ifdef _GLIBCXX_HAVE_LINUX_FUTEX
44#include <sys/time.h>
45#endif
46
47namespace std _GLIBCXX_VISIBILITY(default)
48{
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
50
51 namespace __detail
52 {
53 using __wait_clock_t = chrono::steady_clock;
54
55 template<typename _Clock, typename _Dur>
56 __wait_clock_t::time_point
57 __to_wait_clock(const chrono::time_point<_Clock, _Dur>& __atime) noexcept
58 {
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;
63 return __w_entry + chrono::ceil<__w_dur>(__delta);
64 }
65
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
70 {
71 using __w_dur = typename __wait_clock_t::duration;
72 if constexpr (is_same_v<__w_dur, _Dur>)
73 return __atime;
74 else
75 return chrono::ceil<__w_dur>(__atime);
76 }
77
78 __wait_result_type
79 __wait_until_impl(const void* __addr, __wait_args_base& __args,
80 const __wait_clock_t::duration& __atime);
81
82 template<typename _Clock, typename _Dur>
83 __wait_result_type
84 __wait_until(const void* __addr, __wait_args_base& __args,
85 const chrono::time_point<_Clock, _Dur>& __atime) noexcept
86 {
87 auto __at = __detail::__to_wait_clock(__atime);
88 auto __res = __detail::__wait_until_impl(__addr, __args,
89 __at.time_since_epoch());
90
91 if constexpr (!is_same_v<__wait_clock_t, _Clock>)
92 if (__res._M_timeout)
93 {
94 // We got a timeout when measured against __clock_t but
95 // we need to check against the caller-supplied clock
96 // to tell whether we should return a timeout.
97 if (_Clock::now() < __atime)
98 __res._M_timeout = false;
99 }
100 return __res;
101 }
102
103 template<typename _Rep, typename _Period>
104 __wait_result_type
105 __wait_for(const void* __addr, __wait_args_base& __args,
106 const chrono::duration<_Rep, _Period>& __rtime) noexcept
107 {
108 if (!__rtime.count())
109 {
110 // no rtime supplied, just spin a bit
111 __args._M_flags |= __wait_flags::__do_spin | __wait_flags::__spin_only;
112 return __detail::__wait_impl(__addr, __args);
113 }
114
115 auto const __reltime = chrono::ceil<__wait_clock_t::duration>(__rtime);
116 auto const __atime = chrono::steady_clock::now() + __reltime;
117 return __detail::__wait_until(__addr, __args, __atime);
118 }
119 } // namespace __detail
120
121 // returns true if wait ended before timeout
122 template<typename _Tp,
123 typename _Pred, typename _ValFn,
124 typename _Clock, typename _Dur>
125 bool
126 __atomic_wait_address_until(const _Tp* __addr, _Pred&& __pred,
127 _ValFn&& __vfn,
129 bool __bare_wait = false) noexcept
130 {
131 __detail::__wait_args __args{ __addr, __bare_wait };
132 _Tp __val = __args._M_setup_wait(__addr, __vfn);
133 while (!__pred(__val))
134 {
135 auto __res = __detail::__wait_until(__addr, __args, __atime);
136 if (__res._M_timeout)
137 return false; // C++26 will also return last observed __val
138 __val = __args._M_setup_wait(__addr, __vfn, __res);
139 }
140 return true; // C++26 will also return last observed __val
141 }
142
143 template<typename _Clock, typename _Dur>
144 bool
145 __atomic_wait_address_until_v(const __detail::__platform_wait_t* __addr,
146 __detail::__platform_wait_t __old,
147 int __order,
149 bool __bare_wait = false) noexcept
150 {
151 // This function must not be used if __wait_impl might use a proxy wait:
152 __glibcxx_assert(__platform_wait_uses_type<__detail::__platform_wait_t>);
153
154 __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
155 auto __res = __detail::__wait_until(__addr, __args, __atime);
156 return !__res._M_timeout; // C++26 will also return last observed __val
157 }
158
159 template<typename _Tp, typename _ValFn,
160 typename _Clock, typename _Dur>
161 bool
162 __atomic_wait_address_until_v(const _Tp* __addr, _Tp&& __old,
163 _ValFn&& __vfn,
165 bool __bare_wait = false) noexcept
166 {
167 auto __pfn = [&](const _Tp& __val) {
168 return !__detail::__atomic_eq(__old, __val);
169 };
170 return std::__atomic_wait_address_until(__addr, __pfn, __vfn, __atime,
171 __bare_wait);
172 }
173
174 template<typename _Tp,
175 typename _Pred, typename _ValFn,
176 typename _Rep, typename _Period>
177 bool
178 __atomic_wait_address_for(const _Tp* __addr, _Pred&& __pred,
179 _ValFn&& __vfn,
180 const chrono::duration<_Rep, _Period>& __rtime,
181 bool __bare_wait = false) noexcept
182 {
183 __detail::__wait_args __args{ __addr, __bare_wait };
184 _Tp __val = __args._M_setup_wait(__addr, __vfn);
185 while (!__pred(__val))
186 {
187 auto __res = __detail::__wait_for(__addr, __args, __rtime);
188 if (__res._M_timeout)
189 return false; // C++26 will also return last observed __val
190 __val = __args._M_setup_wait(__addr, __vfn);
191 }
192 return true; // C++26 will also return last observed __val
193 }
194
195 template<typename _Rep, typename _Period>
196 bool
197 __atomic_wait_address_for_v(const __detail::__platform_wait_t* __addr,
198 __detail::__platform_wait_t __old,
199 int __order,
200 const chrono::duration<_Rep, _Period>& __rtime,
201 bool __bare_wait = false) noexcept
202 {
203 // This function must not be used if __wait_impl might use a proxy wait:
204 __glibcxx_assert(__platform_wait_uses_type<__detail::__platform_wait_t>);
205
206 __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
207 auto __res = __detail::__wait_for(__addr, __args, __rtime);
208 return !__res._M_timeout; // C++26 will also return last observed __val
209 }
210
211 template<typename _Tp, typename _ValFn,
212 typename _Rep, typename _Period>
213 bool
214 __atomic_wait_address_for_v(const _Tp* __addr, _Tp&& __old, _ValFn&& __vfn,
215 const chrono::duration<_Rep, _Period>& __rtime,
216 bool __bare_wait = false) noexcept
217 {
218 auto __pfn = [&](const _Tp& __val) {
219 return !__detail::__atomic_eq(__old, __val);
220 };
221 return __atomic_wait_address_for(__addr, __pfn, forward<_ValFn>(__vfn),
222 __rtime, __bare_wait);
223 }
224_GLIBCXX_END_NAMESPACE_VERSION
225} // namespace std
226#endif // __cpp_lib_atomic_wait
227#endif // _GLIBCXX_ATOMIC_TIMED_WAIT_H
constexpr __enable_if_is_duration< _ToDur > ceil(const duration< _Rep, _Period > &__d)
Definition chrono.h:412
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
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
Definition chrono.h:516
chrono::time_point represents a point in time as measured by a clock
Definition chrono.h:927