libstdc++
atomic_futex.h
Go to the documentation of this file.
1// -*- C++ -*- header.
2
3// Copyright (C) 2015-2024 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_futex.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly.
28 */
29
30#ifndef _GLIBCXX_ATOMIC_FUTEX_H
31#define _GLIBCXX_ATOMIC_FUTEX_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#include <atomic>
38#if ! (defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1)
39#include <mutex>
40#include <condition_variable>
41#endif
42#include <bits/chrono.h>
43
44#ifndef _GLIBCXX_ALWAYS_INLINE
45#define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
46#endif
47
48namespace std _GLIBCXX_VISIBILITY(default)
49{
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
51
52#ifdef _GLIBCXX_HAS_GTHREADS
53#if defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
54 struct __atomic_futex_unsigned_base
55 {
56 // __s and __ns are measured against CLOCK_REALTIME. Returns false
57 // iff a timeout occurred.
58 bool
59 _M_futex_wait_until(unsigned *__addr, unsigned __val, bool __has_timeout,
61
62 // __s and __ns are measured against CLOCK_MONOTONIC. Returns
63 // false iff a timeout occurred.
64 bool
65 _M_futex_wait_until_steady(unsigned *__addr, unsigned __val,
66 bool __has_timeout, chrono::seconds __s, chrono::nanoseconds __ns);
67
68 // This can be executed after the object has been destroyed.
69 static void _M_futex_notify_all(unsigned* __addr);
70 };
71
72 template <unsigned _Waiter_bit = 0x80000000>
73 class __atomic_futex_unsigned : __atomic_futex_unsigned_base
74 {
75 typedef chrono::steady_clock __clock_t;
76
77 // This must be lock-free and at offset 0.
78 atomic<unsigned> _M_data;
79
80 public:
81 explicit
82 __atomic_futex_unsigned(unsigned __data) : _M_data(__data)
83 { }
84
85 _GLIBCXX_ALWAYS_INLINE unsigned
86 _M_load(memory_order __mo)
87 {
88 return _M_data.load(__mo) & ~_Waiter_bit;
89 }
90
91 private:
92 // If a timeout occurs, returns a current value after the timeout;
93 // otherwise, returns the operand's value if equal is true or a different
94 // value if equal is false.
95 // The assumed value is the caller's assumption about the current value
96 // when making the call.
97 // __s and __ns are measured against CLOCK_REALTIME.
98 unsigned
99 _M_load_and_test_until(unsigned __assumed, unsigned __operand,
100 bool __equal, memory_order __mo, bool __has_timeout,
102 {
103 for (;;)
104 {
105 // Don't bother checking the value again because we expect the caller
106 // to have done it recently.
107 // memory_order_relaxed is sufficient because we can rely on just the
108 // modification order (store_notify uses an atomic RMW operation too),
109 // and the futex syscalls synchronize between themselves.
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);
114 // Fetch the current value after waiting (clears _Waiter_bit).
115 __assumed = _M_load(__mo);
116 if (!__ret || ((__operand == __assumed) == __equal))
117 return __assumed;
118 // TODO adapt wait time
119 }
120 }
121
122 // If a timeout occurs, returns a current value after the timeout;
123 // otherwise, returns the operand's value if equal is true or a different
124 // value if equal is false.
125 // The assumed value is the caller's assumption about the current value
126 // when making the call.
127 // __s and __ns are measured against CLOCK_MONOTONIC.
128 unsigned
129 _M_load_and_test_until_steady(unsigned __assumed, unsigned __operand,
130 bool __equal, memory_order __mo, bool __has_timeout,
132 {
133 for (;;)
134 {
135 // Don't bother checking the value again because we expect the caller
136 // to have done it recently.
137 // memory_order_relaxed is sufficient because we can rely on just the
138 // modification order (store_notify uses an atomic RMW operation too),
139 // and the futex syscalls synchronize between themselves.
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);
144 // Fetch the current value after waiting (clears _Waiter_bit).
145 __assumed = _M_load(__mo);
146 if (!__ret || ((__operand == __assumed) == __equal))
147 return __assumed;
148 // TODO adapt wait time
149 }
150 }
151
152 // Returns the operand's value if equal is true or a different value if
153 // equal is false.
154 // The assumed value is the caller's assumption about the current value
155 // when making the call.
156 unsigned
157 _M_load_and_test(unsigned __assumed, unsigned __operand,
158 bool __equal, memory_order __mo)
159 {
160 return _M_load_and_test_until(__assumed, __operand, __equal, __mo,
161 false, {}, {});
162 }
163
164 // If a timeout occurs, returns a current value after the timeout;
165 // otherwise, returns the operand's value if equal is true or a different
166 // value if equal is false.
167 // The assumed value is the caller's assumption about the current value
168 // when making the call.
169 template<typename _Dur>
170 unsigned
171 _M_load_and_test_until_impl(unsigned __assumed, unsigned __operand,
172 bool __equal, memory_order __mo,
173 const chrono::time_point<std::chrono::system_clock, _Dur>& __atime)
174 {
176 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
177 // XXX correct?
178 return _M_load_and_test_until(__assumed, __operand, __equal, __mo,
179 true, __s.time_since_epoch(), __ns);
180 }
181
182 template<typename _Dur>
183 unsigned
184 _M_load_and_test_until_impl(unsigned __assumed, unsigned __operand,
185 bool __equal, memory_order __mo,
186 const chrono::time_point<std::chrono::steady_clock, _Dur>& __atime)
187 {
189 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
190 // XXX correct?
191 return _M_load_and_test_until_steady(__assumed, __operand, __equal, __mo,
192 true, __s.time_since_epoch(), __ns);
193 }
194
195 public:
196
197 _GLIBCXX_ALWAYS_INLINE unsigned
198 _M_load_when_not_equal(unsigned __val, memory_order __mo)
199 {
200 unsigned __i = _M_load(__mo);
201 if ((__i & ~_Waiter_bit) != __val)
202 return (__i & ~_Waiter_bit);
203 // TODO Spin-wait first.
204 return _M_load_and_test(__i, __val, false, __mo);
205 }
206
207 _GLIBCXX_ALWAYS_INLINE void
208 _M_load_when_equal(unsigned __val, memory_order __mo)
209 {
210 unsigned __i = _M_load(__mo);
211 if ((__i & ~_Waiter_bit) == __val)
212 return;
213 // TODO Spin-wait first.
214 _M_load_and_test(__i, __val, true, __mo);
215 }
216
217 // Returns false iff a timeout occurred.
218 template<typename _Rep, typename _Period>
219 _GLIBCXX_ALWAYS_INLINE bool
220 _M_load_when_equal_for(unsigned __val, memory_order __mo,
221 const chrono::duration<_Rep, _Period>& __rtime)
222 {
223 using __dur = typename __clock_t::duration;
224 return _M_load_when_equal_until(__val, __mo,
225 __clock_t::now() + chrono::__detail::ceil<__dur>(__rtime));
226 }
227
228 // Returns false iff a timeout occurred.
229 template<typename _Clock, typename _Duration>
230 _GLIBCXX_ALWAYS_INLINE bool
231 _M_load_when_equal_until(unsigned __val, memory_order __mo,
232 const chrono::time_point<_Clock, _Duration>& __atime)
233 {
234 typename _Clock::time_point __c_entry = _Clock::now();
235 do {
236 const __clock_t::time_point __s_entry = __clock_t::now();
237 const auto __delta = __atime - __c_entry;
238 const auto __s_atime = __s_entry +
239 chrono::__detail::ceil<__clock_t::duration>(__delta);
240 if (_M_load_when_equal_until(__val, __mo, __s_atime))
241 return true;
242 __c_entry = _Clock::now();
243 } while (__c_entry < __atime);
244 return false;
245 }
246
247 // Returns false iff a timeout occurred.
248 template<typename _Duration>
249 _GLIBCXX_ALWAYS_INLINE bool
250 _M_load_when_equal_until(unsigned __val, memory_order __mo,
251 const chrono::time_point<std::chrono::system_clock, _Duration>& __atime)
252 {
253 unsigned __i = _M_load(__mo);
254 if ((__i & ~_Waiter_bit) == __val)
255 return true;
256 // TODO Spin-wait first. Ignore effect on timeout.
257 __i = _M_load_and_test_until_impl(__i, __val, true, __mo, __atime);
258 return (__i & ~_Waiter_bit) == __val;
259 }
260
261 // Returns false iff a timeout occurred.
262 template<typename _Duration>
263 _GLIBCXX_ALWAYS_INLINE bool
264 _M_load_when_equal_until(unsigned __val, memory_order __mo,
265 const chrono::time_point<std::chrono::steady_clock, _Duration>& __atime)
266 {
267 unsigned __i = _M_load(__mo);
268 if ((__i & ~_Waiter_bit) == __val)
269 return true;
270 // TODO Spin-wait first. Ignore effect on timeout.
271 __i = _M_load_and_test_until_impl(__i, __val, true, __mo, __atime);
272 return (__i & ~_Waiter_bit) == __val;
273 }
274
275 _GLIBCXX_ALWAYS_INLINE void
276 _M_store_notify_all(unsigned __val, memory_order __mo)
277 {
278 unsigned* __futex = (unsigned *)(void *)&_M_data;
279 if (_M_data.exchange(__val, __mo) & _Waiter_bit)
280 _M_futex_notify_all(__futex);
281 }
282 };
283
284#else // ! (_GLIBCXX_HAVE_LINUX_FUTEX && ATOMIC_INT_LOCK_FREE > 1)
285
286 // If futexes are not available, use a mutex and a condvar to wait.
287 // Because we access the data only within critical sections, all accesses
288 // are sequentially consistent; thus, we satisfy any provided memory_order.
289 template <unsigned _Waiter_bit = 0x80000000>
290 class __atomic_futex_unsigned
291 {
292 typedef chrono::system_clock __clock_t;
293
294 unsigned _M_data;
295 mutex _M_mutex;
296 condition_variable _M_condvar;
297
298 public:
299 explicit
300 __atomic_futex_unsigned(unsigned __data) : _M_data(__data)
301 { }
302
303 _GLIBCXX_ALWAYS_INLINE unsigned
304 _M_load(memory_order __mo)
305 {
306 unique_lock<mutex> __lock(_M_mutex);
307 return _M_data;
308 }
309
310 _GLIBCXX_ALWAYS_INLINE unsigned
311 _M_load_when_not_equal(unsigned __val, memory_order __mo)
312 {
313 unique_lock<mutex> __lock(_M_mutex);
314 while (_M_data == __val)
315 _M_condvar.wait(__lock);
316 return _M_data;
317 }
318
319 _GLIBCXX_ALWAYS_INLINE void
320 _M_load_when_equal(unsigned __val, memory_order __mo)
321 {
322 unique_lock<mutex> __lock(_M_mutex);
323 while (_M_data != __val)
324 _M_condvar.wait(__lock);
325 }
326
327 template<typename _Rep, typename _Period>
328 _GLIBCXX_ALWAYS_INLINE bool
329 _M_load_when_equal_for(unsigned __val, memory_order __mo,
330 const chrono::duration<_Rep, _Period>& __rtime)
331 {
332 unique_lock<mutex> __lock(_M_mutex);
333 return _M_condvar.wait_for(__lock, __rtime,
334 [&] { return _M_data == __val;});
335 }
336
337 template<typename _Clock, typename _Duration>
338 _GLIBCXX_ALWAYS_INLINE bool
339 _M_load_when_equal_until(unsigned __val, memory_order __mo,
340 const chrono::time_point<_Clock, _Duration>& __atime)
341 {
342 unique_lock<mutex> __lock(_M_mutex);
343 return _M_condvar.wait_until(__lock, __atime,
344 [&] { return _M_data == __val;});
345 }
346
347 _GLIBCXX_ALWAYS_INLINE void
348 _M_store_notify_all(unsigned __val, memory_order __mo)
349 {
350 unique_lock<mutex> __lock(_M_mutex);
351 _M_data = __val;
352 _M_condvar.notify_all();
353 }
354 };
355
356#endif // _GLIBCXX_HAVE_LINUX_FUTEX && ATOMIC_INT_LOCK_FREE > 1
357#endif // _GLIBCXX_HAS_GTHREADS
358
359_GLIBCXX_END_NAMESPACE_VERSION
360} // namespace std
361
362#endif
duration< int64_t, nano > nanoseconds
nanoseconds
Definition chrono.h:892
constexpr __enable_if_t< __is_duration< _ToDur >::value, time_point< _Clock, _ToDur > > time_point_cast(const time_point< _Clock, _Dur > &__t)
Definition chrono.h:1023
duration< int64_t > seconds
seconds
Definition chrono.h:901
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
Definition chrono.h:279
memory_order
Enumeration for memory_order.
Definition atomic_base.h:66
ISO C++ entities toplevel namespace is std.