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#ifdef _GLIBCXX_HAVE_LINUX_FUTEX
79#define _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
80#else
81// define _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT and implement __platform_wait_until
82// if there is a more efficient primitive supported by the platform
83// (e.g. __ulock_wait) which is better than pthread_cond_clockwait.
84#endif // ! HAVE_LINUX_FUTEX
85
86 __wait_result_type
87 __wait_until_impl(const void* __addr, __wait_args_base& __args,
88 const __wait_clock_t::duration& __atime);
89
90 template<typename _Clock, typename _Dur>
91 __wait_result_type
92 __wait_until(const void* __addr, __wait_args_base& __args,
93 const chrono::time_point<_Clock, _Dur>& __atime) noexcept
94 {
95 auto __at = __detail::__to_wait_clock(__atime);
96 auto __res = __detail::__wait_until_impl(__addr, __args,
97 __at.time_since_epoch());
98
99 if constexpr (!is_same_v<__wait_clock_t, _Clock>)
100 if (__res._M_timeout)
101 {
102 // We got a timeout when measured against __clock_t but
103 // we need to check against the caller-supplied clock
104 // to tell whether we should return a timeout.
105 if (_Clock::now() < __atime)
106 __res._M_timeout = false;
107 }
108 return __res;
109 }
110
111 template<typename _Rep, typename _Period>
112 __wait_result_type
113 __wait_for(const void* __addr, __wait_args_base& __args,
114 const chrono::duration<_Rep, _Period>& __rtime) noexcept
115 {
116 if (!__rtime.count())
117 {
118 // no rtime supplied, just spin a bit
119 __args._M_flags |= __wait_flags::__do_spin | __wait_flags::__spin_only;
120 return __detail::__wait_impl(__addr, __args);
121 }
122
123 auto const __reltime = chrono::ceil<__wait_clock_t::duration>(__rtime);
124 auto const __atime = chrono::steady_clock::now() + __reltime;
125 return __detail::__wait_until(__addr, __args, __atime);
126 }
127 } // namespace __detail
128
129 // returns true if wait ended before timeout
130 template<typename _Tp,
131 typename _Pred, typename _ValFn,
132 typename _Clock, typename _Dur>
133 bool
134 __atomic_wait_address_until(const _Tp* __addr, _Pred&& __pred,
135 _ValFn&& __vfn,
137 bool __bare_wait = false) noexcept
138 {
139 __detail::__wait_args __args{ __addr, __bare_wait };
140 _Tp __val = __args._M_setup_wait(__addr, __vfn);
141 while (!__pred(__val))
142 {
143 auto __res = __detail::__wait_until(__addr, __args, __atime);
144 if (__res._M_timeout)
145 return false; // C++26 will also return last observed __val
146 __val = __args._M_setup_wait(__addr, __vfn, __res);
147 }
148 return true; // C++26 will also return last observed __val
149 }
150
151 template<typename _Clock, typename _Dur>
152 bool
153 __atomic_wait_address_until_v(const __detail::__platform_wait_t* __addr,
154 __detail::__platform_wait_t __old,
155 int __order,
157 bool __bare_wait = false) noexcept
158 {
159#ifndef _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
160 __glibcxx_assert(false); // This function can't be used for proxy wait.
161#endif
162 __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
163 auto __res = __detail::__wait_until(__addr, __args, __atime);
164 return !__res._M_timeout; // C++26 will also return last observed __val
165 }
166
167 template<typename _Tp, typename _ValFn,
168 typename _Clock, typename _Dur>
169 bool
170 __atomic_wait_address_until_v(const _Tp* __addr, _Tp&& __old,
171 _ValFn&& __vfn,
173 bool __bare_wait = false) noexcept
174 {
175 auto __pfn = [&](const _Tp& __val) {
176 return !__detail::__atomic_eq(__old, __val);
177 };
178 return std::__atomic_wait_address_until(__addr, __pfn, __vfn, __atime,
179 __bare_wait);
180 }
181
182 template<typename _Tp,
183 typename _Pred, typename _ValFn,
184 typename _Rep, typename _Period>
185 bool
186 __atomic_wait_address_for(const _Tp* __addr, _Pred&& __pred,
187 _ValFn&& __vfn,
188 const chrono::duration<_Rep, _Period>& __rtime,
189 bool __bare_wait = false) noexcept
190 {
191 __detail::__wait_args __args{ __addr, __bare_wait };
192 _Tp __val = __args._M_setup_wait(__addr, __vfn);
193 while (!__pred(__val))
194 {
195 auto __res = __detail::__wait_for(__addr, __args, __rtime);
196 if (__res._M_timeout)
197 return false; // C++26 will also return last observed __val
198 __val = __args._M_setup_wait(__addr, __vfn);
199 }
200 return true; // C++26 will also return last observed __val
201 }
202
203 template<typename _Rep, typename _Period>
204 bool
205 __atomic_wait_address_for_v(const __detail::__platform_wait_t* __addr,
206 __detail::__platform_wait_t __old,
207 int __order,
208 const chrono::duration<_Rep, _Period>& __rtime,
209 bool __bare_wait = false) noexcept
210 {
211#ifndef _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
212 __glibcxx_assert(false); // This function can't be used for proxy wait.
213#endif
214 __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
215 auto __res = __detail::__wait_for(__addr, __args, __rtime);
216 return !__res._M_timeout; // C++26 will also return last observed __val
217 }
218
219 template<typename _Tp, typename _ValFn,
220 typename _Rep, typename _Period>
221 bool
222 __atomic_wait_address_for_v(const _Tp* __addr, _Tp&& __old, _ValFn&& __vfn,
223 const chrono::duration<_Rep, _Period>& __rtime,
224 bool __bare_wait = false) noexcept
225 {
226 auto __pfn = [&](const _Tp& __val) {
227 return !__detail::__atomic_eq(__old, __val);
228 };
229 return __atomic_wait_address_for(__addr, __pfn, forward<_ValFn>(__vfn),
230 __rtime, __bare_wait);
231 }
232_GLIBCXX_END_NAMESPACE_VERSION
233} // namespace std
234#endif // __cpp_lib_atomic_wait
235#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