libstdc++: atomic_wait.h Source File

libstdc++

atomic_wait.h
Go to the documentation of this file.
1// -*- C++ -*- header.
2
3// Copyright (C) 2020-2026 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_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_WAIT_H
31#define _GLIBCXX_ATOMIC_WAIT_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#include <bits/version.h>
38
39#if __glibcxx_atomic_wait
40#include <bits/gthr.h>
41#include <ext/numeric_traits.h>
42
43#include <bits/stl_pair.h>
44
45namespace std _GLIBCXX_VISIBILITY(default)
46{
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
48
49 namespace __detail
50 {
51 // TODO: this needs to be false for types with padding, e.g. __int20.
52 // TODO: should this be true only for integral, enum, and pointer types?
53 template<typename _Tp>
54 concept __waitable
55 = is_scalar_v<_Tp> && __builtin_popcountg(sizeof(_Tp)) == 1
56 && (sizeof(_Tp) <= sizeof(__UINT64_TYPE__));
57 }
58
59#if defined _GLIBCXX_HAVE_LINUX_FUTEX
60 namespace __detail
61 {
62 // Use futex syscall on int objects.
63 using __platform_wait_t = int;
64 inline constexpr size_t __platform_wait_alignment = 4;
65 }
66 // Defined to true for a subset of __waitable types which are statically
67 // known to definitely be able to use futex, not a proxy wait.
68 template<typename _Tp>
69 inline constexpr bool __platform_wait_uses_type
70 = __detail::__waitable<_Tp>
71 && sizeof(_Tp) == sizeof(int) && alignof(_Tp) >= 4;
72#elif defined __FreeBSD__ && __SIZEOF_LONG__ == 8
73 namespace __detail
74 {
75 using __platform_wait_t = __UINT64_TYPE__;
76 inline constexpr size_t __platform_wait_alignment = 8;
77 }
78 template<typename _Tp>
79 inline constexpr bool __platform_wait_uses_type
80 = __detail::__waitable<_Tp>
81 && ((sizeof(_Tp) == 4 && alignof(_Tp) >= 4)
82 || (sizeof(_Tp) == 8 && alignof(_Tp) >= 8));
83#else
84// define _GLIBCX_HAVE_PLATFORM_WAIT and implement __platform_wait()
85// and __platform_notify() if there is a more efficient primitive supported
86// by the platform (e.g. __ulock_wait()/__ulock_wake()) which is better than
87// a mutex/condvar based wait.
88 namespace __detail
89 {
90# if ATOMIC_LONG_LOCK_FREE == 2
91 using __platform_wait_t = unsigned long;
92# else
93 using __platform_wait_t = unsigned int;
94# endif
95 inline constexpr size_t __platform_wait_alignment
96 = sizeof(__platform_wait_t) < __alignof__(__platform_wait_t)
97 ? __alignof__(__platform_wait_t) : sizeof(__platform_wait_t);
98 } // namespace __detail
99
100 // This must be false for the general case where we don't know of any
101 // futex-like syscall.
102 template<typename>
103 inline constexpr bool __platform_wait_uses_type = false;
104#endif
105
106 namespace __detail
107 {
108 inline void
109 __thread_yield() noexcept
110 {
111#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
112 __gthread_yield();
113#endif
114 }
115
116 inline void
117 __thread_relax() noexcept
118 {
119#if defined __i386__ || defined __x86_64__
120 __builtin_ia32_pause();
121#else
122 __thread_yield();
123#endif
124 }
125
126 // return true if equal
127 template<typename _Tp>
128 inline bool
129 __atomic_eq(const _Tp& __a, const _Tp& __b)
130 {
131 // TODO make this do the correct padding bit ignoring comparison
132 return __builtin_memcmp(std::addressof(__a), std::addressof(__b),
133 sizeof(_Tp)) == 0;
134 }
135
136 // Storage for up to 64 bits of value, should be considered opaque bits.
137 using __wait_value_type = __UINT64_TYPE__;
138
139 // lightweight std::optional<__wait_value_type>
140 struct __wait_result_type
141 {
142 __wait_value_type _M_val;
143 unsigned char _M_has_val : 1; // _M_val value was loaded before return.
144 unsigned char _M_timeout : 1; // Waiting function ended with timeout.
145 unsigned char _M_unused : 6; // padding
146 };
147
148 enum class __wait_flags : __UINT_LEAST32_TYPE__
149 {
150 __abi_version = 0x00000000,
151 // currently unused = 1,
152 __track_contention = 2,
153 __do_spin = 4,
154 __spin_only = 8, // Ignored unless __do_spin is also set.
155 // __abi_version_mask = 0xff000000,
156 };
157
158 [[__gnu__::__always_inline__]]
159 constexpr __wait_flags
160 operator|(__wait_flags __l, __wait_flags __r) noexcept
161 {
162 using _Ut = underlying_type_t<__wait_flags>;
163 return static_cast<__wait_flags>(static_cast<_Ut>(__l)
164 | static_cast<_Ut>(__r));
165 }
166
167 [[__gnu__::__always_inline__]]
168 constexpr __wait_flags&
169 operator|=(__wait_flags& __l, __wait_flags __r) noexcept
170 { return __l = __l | __r; }
171
172 // Simple aggregate containing arguments used by implementation details.
173 struct __wait_args_base
174 {
175 __wait_flags _M_flags;
176 int _M_order = __ATOMIC_ACQUIRE; // Memory order for loads from _M_obj.
177 __wait_value_type _M_old = 0; // Previous value of *_M_obj.
178 void* _M_wait_state = nullptr; // For proxy wait and tracking contention.
179 const void* _M_obj = nullptr; // The address of the object to wait on.
180 unsigned char _M_obj_size = 0; // The size of that object.
181
182 // Test whether _M_flags & __flags is non-zero.
183 bool
184 operator&(__wait_flags __flags) const noexcept
185 {
186 using _Ut = underlying_type_t<__wait_flags>;
187 return static_cast<_Ut>(_M_flags) & static_cast<_Ut>(__flags);
188 }
189 };
190
191 // Utility for populating a __wait_args_base structure.
192 struct __wait_args : __wait_args_base
193 {
194 template<typename _Tp> requires (!is_same_v<_Tp, __wait_args>)
195 explicit
196 __wait_args(const _Tp* __addr, bool __bare_wait = false) noexcept
197 : __wait_args_base{ _S_flags_for(__addr, __bare_wait) }
198 {
199 _M_obj = __addr; // Might be replaced by _M_setup_wait
200 if constexpr (__waitable<_Tp>)
201 // __wait_impl might be able to wait directly on __addr
202 // instead of using a proxy, depending on its size.
203 _M_obj_size = sizeof(_Tp);
204 }
205
206 __wait_args(const __platform_wait_t* __addr, __platform_wait_t __old,
207 int __order, bool __bare_wait = false) noexcept
208 : __wait_args(__addr, __bare_wait)
209 {
210 _M_order = __order;
211 _M_old = __old;
212 }
213
214 __wait_args(const __wait_args&) noexcept = default;
215 __wait_args& operator=(const __wait_args&) noexcept = default;
216
217 template<typename _Tp, typename _ValFn>
218 _Tp
219 _M_setup_wait(const _Tp* __addr, _ValFn __vfn)
220 {
221 static_assert(is_same_v<_Tp, decay_t<decltype(__vfn())>>);
222
223 if constexpr (!__platform_wait_uses_type<_Tp>)
224 if (_M_setup_proxy_wait(__addr))
225 {
226 // We will use a proxy wait for this object.
227 // The library has set _M_wait_state, _M_obj, _M_obj_size,
228 // and _M_old.
229 // Call __vfn to load the current value from *__addr
230 // (which must happen after the call to _M_setup_proxy_wait).
231 return __vfn();
232 }
233
234 // We will use a futex-like operation to wait on this object,
235 // so just load the value, store it into _M_old, and return it.
236 return _M_store(__vfn());
237 }
238
239 // Called after a wait returns, to prepare to wait again.
240 template<typename _Tp, typename _ValFn>
241 _Tp
242 _M_on_wake(const _Tp* __addr, _ValFn __vfn, __wait_result_type __res)
243 {
244 if constexpr (!__platform_wait_uses_type<_Tp>) // maybe a proxy wait
245 if (_M_obj != __addr) // definitely a proxy wait
246 {
247 if (__res._M_has_val)
248 // Previous wait loaded a recent value from the proxy.
249 _M_old = __res._M_val;
250 else // Load a new value from the proxy and store in _M_old.
251 (void) _M_setup_proxy_wait(nullptr);
252 // Read the current value of *__addr
253 return __vfn();
254 }
255
256 if (__res._M_has_val) // The previous wait loaded a recent value.
257 {
258 _M_old = __res._M_val;
259
260 // Not a proxy wait, so the value in __res._M_val was loaded
261 // from *__addr and we don't need to call __vfn().
262 if constexpr (sizeof(_Tp) == sizeof(__UINT64_TYPE__))
263 return __builtin_bit_cast(_Tp, (__UINT64_TYPE__)_M_old);
264 else if constexpr (sizeof(_Tp) == sizeof(__UINT32_TYPE__))
265 return __builtin_bit_cast(_Tp, (__UINT32_TYPE__)_M_old);
266 else if constexpr (sizeof(_Tp) == sizeof(__UINT16_TYPE__))
267 return __builtin_bit_cast(_Tp, (__UINT16_TYPE__)_M_old);
268 else if constexpr (sizeof(_Tp) == sizeof(__UINT8_TYPE__))
269 return __builtin_bit_cast(_Tp, (__UINT8_TYPE__)_M_old);
270 else // Should be a proxy wait for this size!
271 __glibcxx_assert(false);
272 }
273 else
274 return _M_store(__vfn());
275 }
276
277 private:
278 // Store __val in _M_old.
279 // pre: This must be a non-proxy wait.
280 template<typename _Tp>
281 [[__gnu__::__always_inline__]]
282 _Tp
283 _M_store(_Tp __val)
284 {
285 // We have to consider various sizes, because a future libstdc++.so
286 // might enable non-proxy waits for additional sizes.
287 if constexpr (sizeof(_Tp) == sizeof(__UINT64_TYPE__))
288 _M_old = __builtin_bit_cast(__UINT64_TYPE__, __val);
289 else if constexpr (sizeof(_Tp) == sizeof(__UINT32_TYPE__))
290 _M_old = __builtin_bit_cast(__UINT32_TYPE__, __val);
291 else if constexpr (sizeof(_Tp) == sizeof(__UINT16_TYPE__))
292 _M_old = __builtin_bit_cast(__UINT16_TYPE__, __val);
293 else if constexpr (sizeof(_Tp) == sizeof(__UINT8_TYPE__))
294 _M_old = __builtin_bit_cast(__UINT8_TYPE__, __val);
295 else // Should be a proxy wait for this size!
296 __glibcxx_assert(false);
297 return __val;
298 }
299
300 // Prepare `*this` for a call to `__wait_impl` or `__wait_until_impl`.
301 // See comments in src/c++20/atomic.cc for more details.
302 bool
303 _M_setup_proxy_wait(const void* __addr);
304
305 template<typename _Tp>
306 static constexpr __wait_flags
307 _S_flags_for(const _Tp*, bool __bare_wait) noexcept
308 {
309 using enum __wait_flags;
310 __wait_flags __res = __abi_version | __do_spin;
311 if (!__bare_wait)
312 __res |= __track_contention;
313 return __res;
314 }
315 };
316
317 __wait_result_type
318 __wait_impl(const void* __addr, __wait_args_base&);
319
320 void
321 __notify_impl(const void* __addr, bool __all, const __wait_args_base&);
322 } // namespace __detail
323
324 // Wait on __addr while __pred(__vfn()) is false.
325 // If __bare_wait is false, increment a counter while waiting.
326 // For callers that keep their own count of waiters, use __bare_wait=true.
327 // The effect of __vfn() must be an atomic load from __addr and nothing else.
328 template<typename _Tp, typename _Pred, typename _ValFn>
329 void
330 __atomic_wait_address(const _Tp* __addr, _Pred&& __pred, _ValFn&& __vfn,
331 bool __bare_wait = false) noexcept
332 {
333 __detail::__wait_args __args{ __addr, __bare_wait };
334 _Tp __val = __args._M_setup_wait(__addr, __vfn);
335 while (!__pred(__val))
336 {
337 auto __res = __detail::__wait_impl(__addr, __args);
338 __val = __args._M_on_wake(__addr, __vfn, __res);
339 }
340 // C++26 will return __val
341 }
342
343 // Wait on __addr while *__addr == __old is true.
344 inline void
345 __atomic_wait_address_v(const __detail::__platform_wait_t* __addr,
346 __detail::__platform_wait_t __old,
347 int __order, bool __bare_wait = false)
348 {
349 // This function must not be used if __wait_impl might use a proxy wait:
350 __glibcxx_assert(__platform_wait_uses_type<__detail::__platform_wait_t>);
351
352 __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
353 // C++26 will not ignore the return value here
354 __detail::__wait_impl(__addr, __args);
355 }
356
357 // Wait on __addr while __vfn() == __old is true.
358 template<typename _Tp, typename _ValFn>
359 void
360 __atomic_wait_address_v(const _Tp* __addr, _Tp __old,
361 _ValFn __vfn) noexcept
362 {
363 auto __pfn = [&](const _Tp& __val)
364 { return !__detail::__atomic_eq(__old, __val); };
365 std::__atomic_wait_address(__addr, __pfn, forward<_ValFn>(__vfn));
366 }
367
368 template<typename _Tp>
369 void
370 __atomic_notify_address(const _Tp* __addr, bool __all,
371 bool __bare_wait = false) noexcept
372 {
373 __detail::__wait_args __args{ __addr, __bare_wait };
374 __detail::__notify_impl(__addr, __all, __args);
375 }
376
377_GLIBCXX_END_NAMESPACE_VERSION
378} // namespace std
379#endif // __glibcxx_atomic_wait
380#endif // _GLIBCXX_ATOMIC_WAIT_H
stl_pair.h
version.h
std::underlying_type_t
typename underlying_type< _Tp >::type underlying_type_t
Alias template for underlying_type.
Definition type_traits:2952
std::decay_t
typename decay< _Tp >::type decay_t
Alias template for decay.
Definition type_traits:2936
std::addressof
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
Definition move.h:176
std::forward
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
std
ISO C++ entities toplevel namespace is std.
std::operator|
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1628
std::operator&
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1618
std::__detail
Implementation details not part of the namespace std interface.
numeric_traits.h