libstdc++
atomic
Go to the documentation of this file.
1// -*- C++ -*- header.
2
3// Copyright (C) 2008-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 include/atomic
26 * This is a Standard C++ Library header.
27 */
28
29// Based on "C++ Atomic Types and Operations" by Hans Boehm and Lawrence Crowl.
30// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2427.html
31
32#ifndef _GLIBCXX_ATOMIC
33#define _GLIBCXX_ATOMIC 1
34
35#ifdef _GLIBCXX_SYSHDR
36#pragma GCC system_header
37#endif
38
39#if __cplusplus < 201103L
40# include <bits/c++0x_warning.h>
41#else
42
43#define __glibcxx_want_atomic_is_always_lock_free
44#define __glibcxx_want_atomic_flag_test
45#define __glibcxx_want_atomic_float
46#define __glibcxx_want_atomic_min_max
47#define __glibcxx_want_atomic_ref
48#define __glibcxx_want_atomic_lock_free_type_aliases
49#define __glibcxx_want_atomic_value_initialization
50#define __glibcxx_want_atomic_wait
51#include <bits/version.h>
52
53#include <bits/atomic_base.h>
54#include <cstdint>
55#include <type_traits>
56
57namespace std _GLIBCXX_VISIBILITY(default)
58{
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
60
61 /**
62 * @addtogroup atomics
63 * @{
64 */
65
66 template<typename _Tp>
67 struct atomic;
68
69 /// atomic<bool>
70 // NB: No operators or fetch-operations for this type.
71 template<>
72 struct atomic<bool>
73 {
74 using value_type = bool;
75
76 private:
77 __atomic_base<bool> _M_base;
78
79 public:
80 atomic() noexcept = default;
81 ~atomic() noexcept = default;
82 atomic(const atomic&) = delete;
83 atomic& operator=(const atomic&) = delete;
84 atomic& operator=(const atomic&) volatile = delete;
85
86 constexpr atomic(bool __i) noexcept : _M_base(__i) { }
87
88 bool
89 operator=(bool __i) noexcept
90 { return _M_base.operator=(__i); }
91
92 bool
93 operator=(bool __i) volatile noexcept
94 { return _M_base.operator=(__i); }
95
96 operator bool() const noexcept
97 { return _M_base.load(); }
98
99 operator bool() const volatile noexcept
100 { return _M_base.load(); }
101
102 bool
103 is_lock_free() const noexcept { return _M_base.is_lock_free(); }
104
105 bool
106 is_lock_free() const volatile noexcept { return _M_base.is_lock_free(); }
107
108#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
109 static constexpr bool is_always_lock_free = ATOMIC_BOOL_LOCK_FREE == 2;
110#endif
111
112 void
113 store(bool __i, memory_order __m = memory_order_seq_cst) noexcept
114 { _M_base.store(__i, __m); }
115
116 void
117 store(bool __i, memory_order __m = memory_order_seq_cst) volatile noexcept
118 { _M_base.store(__i, __m); }
119
120 bool
121 load(memory_order __m = memory_order_seq_cst) const noexcept
122 { return _M_base.load(__m); }
123
124 bool
125 load(memory_order __m = memory_order_seq_cst) const volatile noexcept
126 { return _M_base.load(__m); }
127
128 bool
129 exchange(bool __i, memory_order __m = memory_order_seq_cst) noexcept
130 { return _M_base.exchange(__i, __m); }
131
132 bool
133 exchange(bool __i,
134 memory_order __m = memory_order_seq_cst) volatile noexcept
135 { return _M_base.exchange(__i, __m); }
136
137 bool
138 compare_exchange_weak(bool& __i1, bool __i2, memory_order __m1,
139 memory_order __m2) noexcept
140 { return _M_base.compare_exchange_weak(__i1, __i2, __m1, __m2); }
141
142 bool
143 compare_exchange_weak(bool& __i1, bool __i2, memory_order __m1,
144 memory_order __m2) volatile noexcept
145 { return _M_base.compare_exchange_weak(__i1, __i2, __m1, __m2); }
146
147 bool
148 compare_exchange_weak(bool& __i1, bool __i2,
149 memory_order __m = memory_order_seq_cst) noexcept
150 { return _M_base.compare_exchange_weak(__i1, __i2, __m); }
151
152 bool
153 compare_exchange_weak(bool& __i1, bool __i2,
154 memory_order __m = memory_order_seq_cst) volatile noexcept
155 { return _M_base.compare_exchange_weak(__i1, __i2, __m); }
156
157 bool
158 compare_exchange_strong(bool& __i1, bool __i2, memory_order __m1,
159 memory_order __m2) noexcept
160 { return _M_base.compare_exchange_strong(__i1, __i2, __m1, __m2); }
161
162 bool
163 compare_exchange_strong(bool& __i1, bool __i2, memory_order __m1,
164 memory_order __m2) volatile noexcept
165 { return _M_base.compare_exchange_strong(__i1, __i2, __m1, __m2); }
166
167 bool
168 compare_exchange_strong(bool& __i1, bool __i2,
169 memory_order __m = memory_order_seq_cst) noexcept
170 { return _M_base.compare_exchange_strong(__i1, __i2, __m); }
171
172 bool
173 compare_exchange_strong(bool& __i1, bool __i2,
174 memory_order __m = memory_order_seq_cst) volatile noexcept
175 { return _M_base.compare_exchange_strong(__i1, __i2, __m); }
176
177#if __cpp_lib_atomic_wait
178 void
179 wait(bool __old, memory_order __m = memory_order_seq_cst) const noexcept
180 { _M_base.wait(__old, __m); }
181
182 // TODO add const volatile overload
183
184 void
185 notify_one() noexcept
186 { _M_base.notify_one(); }
187
188 void
189 notify_all() noexcept
190 { _M_base.notify_all(); }
191#endif // __cpp_lib_atomic_wait
192 };
193
194 /**
195 * @brief Generic atomic type, primary class template.
196 *
197 * @tparam _Tp Type to be made atomic, must be trivially copyable.
198 */
199 template<typename _Tp>
200 struct atomic
201 {
202 using value_type = _Tp;
203
204 private:
205 // Align 1/2/4/8/16-byte types to at least their size.
206 static constexpr int _S_min_alignment
207 = (sizeof(_Tp) & (sizeof(_Tp) - 1)) || sizeof(_Tp) > 16
208 ? 0 : sizeof(_Tp);
209
210 static constexpr int _S_alignment
211 = _S_min_alignment > alignof(_Tp) ? _S_min_alignment : alignof(_Tp);
212
213 alignas(_S_alignment) _Tp _M_i;
214
215 static_assert(__is_trivially_copyable(_Tp),
216 "std::atomic requires a trivially copyable type");
217
218 static_assert(sizeof(_Tp) > 0,
219 "Incomplete or zero-sized types are not supported");
220
221 // _GLIBCXX_RESOLVE_LIB_DEFECTS
222 // 4069. std::atomic<volatile T> should be ill-formed
223 static_assert(is_same<_Tp, typename remove_cv<_Tp>::type>::value,
224 "template argument for std::atomic must not be const or volatile");
225
226#if __cplusplus > 201703L
227 static_assert(is_copy_constructible_v<_Tp>);
228 static_assert(is_move_constructible_v<_Tp>);
229 static_assert(is_copy_assignable_v<_Tp>);
230 static_assert(is_move_assignable_v<_Tp>);
231#endif
232
233 public:
234#if __cpp_lib_atomic_value_initialization
235 // _GLIBCXX_RESOLVE_LIB_DEFECTS
236 // 4169. std::atomic<T>'s default constructor should be constrained
237 constexpr atomic() noexcept(is_nothrow_default_constructible_v<_Tp>)
238 requires is_default_constructible_v<_Tp>
239 : _M_i()
240 {}
241#else
242 atomic() = default;
243#endif
244
245 ~atomic() noexcept = default;
246 atomic(const atomic&) = delete;
247 atomic& operator=(const atomic&) = delete;
248 atomic& operator=(const atomic&) volatile = delete;
249
250 constexpr atomic(_Tp __i) noexcept : _M_i(__i)
251 {
252#if __cplusplus >= 201402L && __has_builtin(__builtin_clear_padding)
253 if _GLIBCXX17_CONSTEXPR (__atomic_impl::__maybe_has_padding<_Tp>())
254 __builtin_clear_padding(std::__addressof(_M_i));
255#endif
256 }
257
258 operator _Tp() const noexcept
259 { return load(); }
260
261 operator _Tp() const volatile noexcept
262 { return load(); }
263
264 _Tp
265 operator=(_Tp __i) noexcept
266 { store(__i); return __i; }
267
268 _Tp
269 operator=(_Tp __i) volatile noexcept
270 { store(__i); return __i; }
271
272 bool
273 is_lock_free() const noexcept
274 {
275 // Produce a fake, minimally aligned pointer.
276 return __atomic_is_lock_free(sizeof(_M_i),
277 reinterpret_cast<void *>(-_S_alignment));
278 }
279
280 bool
281 is_lock_free() const volatile noexcept
282 {
283 // Produce a fake, minimally aligned pointer.
284 return __atomic_is_lock_free(sizeof(_M_i),
285 reinterpret_cast<void *>(-_S_alignment));
286 }
287
288#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
289 static constexpr bool is_always_lock_free
290 = __atomic_always_lock_free(sizeof(_M_i), 0);
291#endif
292
293 void
294 store(_Tp __i, memory_order __m = memory_order_seq_cst) noexcept
295 {
296 __atomic_store(std::__addressof(_M_i),
297 __atomic_impl::__clear_padding(__i),
298 int(__m));
299 }
300
301 void
302 store(_Tp __i, memory_order __m = memory_order_seq_cst) volatile noexcept
303 {
304 __atomic_store(std::__addressof(_M_i),
305 __atomic_impl::__clear_padding(__i),
306 int(__m));
307 }
308
309 _Tp
310 load(memory_order __m = memory_order_seq_cst) const noexcept
311 {
312 alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
313 _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
314 __atomic_load(std::__addressof(_M_i), __ptr, int(__m));
315 return *__ptr;
316 }
317
318 _Tp
319 load(memory_order __m = memory_order_seq_cst) const volatile noexcept
320 {
321 alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
322 _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
323 __atomic_load(std::__addressof(_M_i), __ptr, int(__m));
324 return *__ptr;
325 }
326
327 _Tp
328 exchange(_Tp __i, memory_order __m = memory_order_seq_cst) noexcept
329 {
330 alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
331 _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
332 __atomic_exchange(std::__addressof(_M_i),
333 __atomic_impl::__clear_padding(__i),
334 __ptr, int(__m));
335 return *__ptr;
336 }
337
338 _Tp
339 exchange(_Tp __i,
340 memory_order __m = memory_order_seq_cst) volatile noexcept
341 {
342 alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
343 _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
344 __atomic_exchange(std::__addressof(_M_i),
345 __atomic_impl::__clear_padding(__i),
346 __ptr, int(__m));
347 return *__ptr;
348 }
349
350 bool
351 compare_exchange_weak(_Tp& __e, _Tp __i, memory_order __s,
352 memory_order __f) noexcept
353 {
354 return __atomic_impl::__compare_exchange(_M_i, __e, __i, true,
355 __s, __f);
356 }
357
358 bool
359 compare_exchange_weak(_Tp& __e, _Tp __i, memory_order __s,
360 memory_order __f) volatile noexcept
361 {
362 return __atomic_impl::__compare_exchange(_M_i, __e, __i, true,
363 __s, __f);
364 }
365
366 bool
367 compare_exchange_weak(_Tp& __e, _Tp __i,
368 memory_order __m = memory_order_seq_cst) noexcept
369 { return compare_exchange_weak(__e, __i, __m,
370 __cmpexch_failure_order(__m)); }
371
372 bool
373 compare_exchange_weak(_Tp& __e, _Tp __i,
374 memory_order __m = memory_order_seq_cst) volatile noexcept
375 { return compare_exchange_weak(__e, __i, __m,
376 __cmpexch_failure_order(__m)); }
377
378 bool
379 compare_exchange_strong(_Tp& __e, _Tp __i, memory_order __s,
380 memory_order __f) noexcept
381 {
382 return __atomic_impl::__compare_exchange(_M_i, __e, __i, false,
383 __s, __f);
384 }
385
386 bool
387 compare_exchange_strong(_Tp& __e, _Tp __i, memory_order __s,
388 memory_order __f) volatile noexcept
389 {
390 return __atomic_impl::__compare_exchange(_M_i, __e, __i, false,
391 __s, __f);
392 }
393
394 bool
395 compare_exchange_strong(_Tp& __e, _Tp __i,
396 memory_order __m = memory_order_seq_cst) noexcept
397 { return compare_exchange_strong(__e, __i, __m,
398 __cmpexch_failure_order(__m)); }
399
400 bool
401 compare_exchange_strong(_Tp& __e, _Tp __i,
402 memory_order __m = memory_order_seq_cst) volatile noexcept
403 { return compare_exchange_strong(__e, __i, __m,
404 __cmpexch_failure_order(__m)); }
405
406#if __cpp_lib_atomic_wait // C++ >= 20
407 void
408 wait(_Tp __old, memory_order __m = memory_order_seq_cst) const noexcept
409 {
410 std::__atomic_wait_address_v(std::addressof(_M_i), __old,
411 [__m, this] { return this->load(__m); });
412 }
413
414 // TODO add const volatile overload
415
416 void
417 notify_one() noexcept
418 { std::__atomic_notify_address(std::addressof(_M_i), false); }
419
420 void
421 notify_all() noexcept
422 { std::__atomic_notify_address(std::addressof(_M_i), true); }
423#endif // __cpp_lib_atomic_wait
424 };
425
426 /// Partial specialization for pointer types.
427 template<typename _Tp>
428 struct atomic<_Tp*>
429 {
430 using value_type = _Tp*;
431 using difference_type = ptrdiff_t;
432
433 typedef _Tp* __pointer_type;
434 typedef __atomic_base<_Tp*> __base_type;
435 __base_type _M_b;
436
437 atomic() noexcept = default;
438 ~atomic() noexcept = default;
439 atomic(const atomic&) = delete;
440 atomic& operator=(const atomic&) = delete;
441 atomic& operator=(const atomic&) volatile = delete;
442
443 constexpr atomic(__pointer_type __p) noexcept : _M_b(__p) { }
444
445 operator __pointer_type() const noexcept
446 { return __pointer_type(_M_b); }
447
448 operator __pointer_type() const volatile noexcept
449 { return __pointer_type(_M_b); }
450
451 __pointer_type
452 operator=(__pointer_type __p) noexcept
453 { return _M_b.operator=(__p); }
454
455 __pointer_type
456 operator=(__pointer_type __p) volatile noexcept
457 { return _M_b.operator=(__p); }
458
459 __pointer_type
460 operator++(int) noexcept
461 {
462#if __cplusplus >= 201703L
463 static_assert( is_object_v<_Tp>, "pointer to object type" );
464#endif
465 return _M_b++;
466 }
467
468 __pointer_type
469 operator++(int) volatile noexcept
470 {
471#if __cplusplus >= 201703L
472 static_assert( is_object_v<_Tp>, "pointer to object type" );
473#endif
474 return _M_b++;
475 }
476
477 __pointer_type
478 operator--(int) noexcept
479 {
480#if __cplusplus >= 201703L
481 static_assert( is_object_v<_Tp>, "pointer to object type" );
482#endif
483 return _M_b--;
484 }
485
486 __pointer_type
487 operator--(int) volatile noexcept
488 {
489#if __cplusplus >= 201703L
490 static_assert( is_object_v<_Tp>, "pointer to object type" );
491#endif
492 return _M_b--;
493 }
494
495 __pointer_type
496 operator++() noexcept
497 {
498#if __cplusplus >= 201703L
499 static_assert( is_object_v<_Tp>, "pointer to object type" );
500#endif
501 return ++_M_b;
502 }
503
504 __pointer_type
505 operator++() volatile noexcept
506 {
507#if __cplusplus >= 201703L
508 static_assert( is_object_v<_Tp>, "pointer to object type" );
509#endif
510 return ++_M_b;
511 }
512
513 __pointer_type
514 operator--() noexcept
515 {
516#if __cplusplus >= 201703L
517 static_assert( is_object_v<_Tp>, "pointer to object type" );
518#endif
519 return --_M_b;
520 }
521
522 __pointer_type
523 operator--() volatile noexcept
524 {
525#if __cplusplus >= 201703L
526 static_assert( is_object_v<_Tp>, "pointer to object type" );
527#endif
528 return --_M_b;
529 }
530
531 __pointer_type
532 operator+=(ptrdiff_t __d) noexcept
533 {
534#if __cplusplus >= 201703L
535 static_assert( is_object_v<_Tp>, "pointer to object type" );
536#endif
537 return _M_b.operator+=(__d);
538 }
539
540 __pointer_type
541 operator+=(ptrdiff_t __d) volatile noexcept
542 {
543#if __cplusplus >= 201703L
544 static_assert( is_object_v<_Tp>, "pointer to object type" );
545#endif
546 return _M_b.operator+=(__d);
547 }
548
549 __pointer_type
550 operator-=(ptrdiff_t __d) noexcept
551 {
552#if __cplusplus >= 201703L
553 static_assert( is_object_v<_Tp>, "pointer to object type" );
554#endif
555 return _M_b.operator-=(__d);
556 }
557
558 __pointer_type
559 operator-=(ptrdiff_t __d) volatile noexcept
560 {
561#if __cplusplus >= 201703L
562 static_assert( is_object_v<_Tp>, "pointer to object type" );
563#endif
564 return _M_b.operator-=(__d);
565 }
566
567 bool
568 is_lock_free() const noexcept
569 { return _M_b.is_lock_free(); }
570
571 bool
572 is_lock_free() const volatile noexcept
573 { return _M_b.is_lock_free(); }
574
575#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
576 static constexpr bool is_always_lock_free
577 = ATOMIC_POINTER_LOCK_FREE == 2;
578#endif
579
580 void
581 store(__pointer_type __p,
582 memory_order __m = memory_order_seq_cst) noexcept
583 { return _M_b.store(__p, __m); }
584
585 void
586 store(__pointer_type __p,
587 memory_order __m = memory_order_seq_cst) volatile noexcept
588 { return _M_b.store(__p, __m); }
589
590 __pointer_type
591 load(memory_order __m = memory_order_seq_cst) const noexcept
592 { return _M_b.load(__m); }
593
594 __pointer_type
595 load(memory_order __m = memory_order_seq_cst) const volatile noexcept
596 { return _M_b.load(__m); }
597
598 __pointer_type
599 exchange(__pointer_type __p,
600 memory_order __m = memory_order_seq_cst) noexcept
601 { return _M_b.exchange(__p, __m); }
602
603 __pointer_type
604 exchange(__pointer_type __p,
605 memory_order __m = memory_order_seq_cst) volatile noexcept
606 { return _M_b.exchange(__p, __m); }
607
608 bool
609 compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
610 memory_order __m1, memory_order __m2) noexcept
611 { return _M_b.compare_exchange_weak(__p1, __p2, __m1, __m2); }
612
613 bool
614 compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
615 memory_order __m1,
616 memory_order __m2) volatile noexcept
617 { return _M_b.compare_exchange_weak(__p1, __p2, __m1, __m2); }
618
619 bool
620 compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
621 memory_order __m = memory_order_seq_cst) noexcept
622 {
623 return compare_exchange_weak(__p1, __p2, __m,
624 __cmpexch_failure_order(__m));
625 }
626
627 bool
628 compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
629 memory_order __m = memory_order_seq_cst) volatile noexcept
630 {
631 return compare_exchange_weak(__p1, __p2, __m,
632 __cmpexch_failure_order(__m));
633 }
634
635 bool
636 compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
637 memory_order __m1, memory_order __m2) noexcept
638 { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
639
640 bool
641 compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
642 memory_order __m1,
643 memory_order __m2) volatile noexcept
644 { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
645
646 bool
647 compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
648 memory_order __m = memory_order_seq_cst) noexcept
649 {
650 return _M_b.compare_exchange_strong(__p1, __p2, __m,
651 __cmpexch_failure_order(__m));
652 }
653
654 bool
655 compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
656 memory_order __m = memory_order_seq_cst) volatile noexcept
657 {
658 return _M_b.compare_exchange_strong(__p1, __p2, __m,
659 __cmpexch_failure_order(__m));
660 }
661
662#if __cpp_lib_atomic_wait
663 void
664 wait(__pointer_type __old, memory_order __m = memory_order_seq_cst) const noexcept
665 { _M_b.wait(__old, __m); }
666
667 // TODO add const volatile overload
668
669 void
670 notify_one() noexcept
671 { _M_b.notify_one(); }
672
673 void
674 notify_all() noexcept
675 { _M_b.notify_all(); }
676#endif // __cpp_lib_atomic_wait
677
678 __pointer_type
679 fetch_add(ptrdiff_t __d,
680 memory_order __m = memory_order_seq_cst) noexcept
681 {
682#if __cplusplus >= 201703L
683 static_assert( is_object_v<_Tp>, "pointer to object type" );
684#endif
685 return _M_b.fetch_add(__d, __m);
686 }
687
688 __pointer_type
689 fetch_add(ptrdiff_t __d,
690 memory_order __m = memory_order_seq_cst) volatile noexcept
691 {
692#if __cplusplus >= 201703L
693 static_assert( is_object_v<_Tp>, "pointer to object type" );
694#endif
695 return _M_b.fetch_add(__d, __m);
696 }
697
698 __pointer_type
699 fetch_sub(ptrdiff_t __d,
700 memory_order __m = memory_order_seq_cst) noexcept
701 {
702#if __cplusplus >= 201703L
703 static_assert( is_object_v<_Tp>, "pointer to object type" );
704#endif
705 return _M_b.fetch_sub(__d, __m);
706 }
707
708 __pointer_type
709 fetch_sub(ptrdiff_t __d,
710 memory_order __m = memory_order_seq_cst) volatile noexcept
711 {
712#if __cplusplus >= 201703L
713 static_assert( is_object_v<_Tp>, "pointer to object type" );
714#endif
715 return _M_b.fetch_sub(__d, __m);
716 }
717 };
718
719
720 /// Explicit specialization for char.
721 template<>
722 struct atomic<char> : __atomic_base<char>
723 {
724 typedef char __integral_type;
725 typedef __atomic_base<char> __base_type;
726
727 atomic() noexcept = default;
728 ~atomic() noexcept = default;
729 atomic(const atomic&) = delete;
730 atomic& operator=(const atomic&) = delete;
731 atomic& operator=(const atomic&) volatile = delete;
732
733 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
734
735 using __base_type::operator __integral_type;
736 using __base_type::operator=;
737
738#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
739 static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE == 2;
740#endif
741 };
742
743 /// Explicit specialization for signed char.
744 template<>
745 struct atomic<signed char> : __atomic_base<signed char>
746 {
747 typedef signed char __integral_type;
748 typedef __atomic_base<signed char> __base_type;
749
750 atomic() noexcept= default;
751 ~atomic() noexcept = default;
752 atomic(const atomic&) = delete;
753 atomic& operator=(const atomic&) = delete;
754 atomic& operator=(const atomic&) volatile = delete;
755
756 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
757
758 using __base_type::operator __integral_type;
759 using __base_type::operator=;
760
761#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
762 static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE == 2;
763#endif
764 };
765
766 /// Explicit specialization for unsigned char.
767 template<>
768 struct atomic<unsigned char> : __atomic_base<unsigned char>
769 {
770 typedef unsigned char __integral_type;
771 typedef __atomic_base<unsigned char> __base_type;
772
773 atomic() noexcept= default;
774 ~atomic() noexcept = default;
775 atomic(const atomic&) = delete;
776 atomic& operator=(const atomic&) = delete;
777 atomic& operator=(const atomic&) volatile = delete;
778
779 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
780
781 using __base_type::operator __integral_type;
782 using __base_type::operator=;
783
784#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
785 static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE == 2;
786#endif
787 };
788
789 /// Explicit specialization for short.
790 template<>
791 struct atomic<short> : __atomic_base<short>
792 {
793 typedef short __integral_type;
794 typedef __atomic_base<short> __base_type;
795
796 atomic() noexcept = default;
797 ~atomic() noexcept = default;
798 atomic(const atomic&) = delete;
799 atomic& operator=(const atomic&) = delete;
800 atomic& operator=(const atomic&) volatile = delete;
801
802 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
803
804 using __base_type::operator __integral_type;
805 using __base_type::operator=;
806
807#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
808 static constexpr bool is_always_lock_free = ATOMIC_SHORT_LOCK_FREE == 2;
809#endif
810 };
811
812 /// Explicit specialization for unsigned short.
813 template<>
814 struct atomic<unsigned short> : __atomic_base<unsigned short>
815 {
816 typedef unsigned short __integral_type;
817 typedef __atomic_base<unsigned short> __base_type;
818
819 atomic() noexcept = default;
820 ~atomic() noexcept = default;
821 atomic(const atomic&) = delete;
822 atomic& operator=(const atomic&) = delete;
823 atomic& operator=(const atomic&) volatile = delete;
824
825 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
826
827 using __base_type::operator __integral_type;
828 using __base_type::operator=;
829
830#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
831 static constexpr bool is_always_lock_free = ATOMIC_SHORT_LOCK_FREE == 2;
832#endif
833 };
834
835 /// Explicit specialization for int.
836 template<>
837 struct atomic<int> : __atomic_base<int>
838 {
839 typedef int __integral_type;
840 typedef __atomic_base<int> __base_type;
841
842 atomic() noexcept = default;
843 ~atomic() noexcept = default;
844 atomic(const atomic&) = delete;
845 atomic& operator=(const atomic&) = delete;
846 atomic& operator=(const atomic&) volatile = delete;
847
848 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
849
850 using __base_type::operator __integral_type;
851 using __base_type::operator=;
852
853#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
854 static constexpr bool is_always_lock_free = ATOMIC_INT_LOCK_FREE == 2;
855#endif
856 };
857
858 /// Explicit specialization for unsigned int.
859 template<>
860 struct atomic<unsigned int> : __atomic_base<unsigned int>
861 {
862 typedef unsigned int __integral_type;
863 typedef __atomic_base<unsigned int> __base_type;
864
865 atomic() noexcept = default;
866 ~atomic() noexcept = default;
867 atomic(const atomic&) = delete;
868 atomic& operator=(const atomic&) = delete;
869 atomic& operator=(const atomic&) volatile = delete;
870
871 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
872
873 using __base_type::operator __integral_type;
874 using __base_type::operator=;
875
876#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
877 static constexpr bool is_always_lock_free = ATOMIC_INT_LOCK_FREE == 2;
878#endif
879 };
880
881 /// Explicit specialization for long.
882 template<>
883 struct atomic<long> : __atomic_base<long>
884 {
885 typedef long __integral_type;
886 typedef __atomic_base<long> __base_type;
887
888 atomic() noexcept = default;
889 ~atomic() noexcept = default;
890 atomic(const atomic&) = delete;
891 atomic& operator=(const atomic&) = delete;
892 atomic& operator=(const atomic&) volatile = delete;
893
894 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
895
896 using __base_type::operator __integral_type;
897 using __base_type::operator=;
898
899#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
900 static constexpr bool is_always_lock_free = ATOMIC_LONG_LOCK_FREE == 2;
901#endif
902 };
903
904 /// Explicit specialization for unsigned long.
905 template<>
906 struct atomic<unsigned long> : __atomic_base<unsigned long>
907 {
908 typedef unsigned long __integral_type;
909 typedef __atomic_base<unsigned long> __base_type;
910
911 atomic() noexcept = default;
912 ~atomic() noexcept = default;
913 atomic(const atomic&) = delete;
914 atomic& operator=(const atomic&) = delete;
915 atomic& operator=(const atomic&) volatile = delete;
916
917 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
918
919 using __base_type::operator __integral_type;
920 using __base_type::operator=;
921
922#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
923 static constexpr bool is_always_lock_free = ATOMIC_LONG_LOCK_FREE == 2;
924#endif
925 };
926
927 /// Explicit specialization for long long.
928 template<>
929 struct atomic<long long> : __atomic_base<long long>
930 {
931 typedef long long __integral_type;
932 typedef __atomic_base<long long> __base_type;
933
934 atomic() noexcept = default;
935 ~atomic() noexcept = default;
936 atomic(const atomic&) = delete;
937 atomic& operator=(const atomic&) = delete;
938 atomic& operator=(const atomic&) volatile = delete;
939
940 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
941
942 using __base_type::operator __integral_type;
943 using __base_type::operator=;
944
945#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
946 static constexpr bool is_always_lock_free = ATOMIC_LLONG_LOCK_FREE == 2;
947#endif
948 };
949
950 /// Explicit specialization for unsigned long long.
951 template<>
952 struct atomic<unsigned long long> : __atomic_base<unsigned long long>
953 {
954 typedef unsigned long long __integral_type;
955 typedef __atomic_base<unsigned long long> __base_type;
956
957 atomic() noexcept = default;
958 ~atomic() noexcept = default;
959 atomic(const atomic&) = delete;
960 atomic& operator=(const atomic&) = delete;
961 atomic& operator=(const atomic&) volatile = delete;
962
963 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
964
965 using __base_type::operator __integral_type;
966 using __base_type::operator=;
967
968#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
969 static constexpr bool is_always_lock_free = ATOMIC_LLONG_LOCK_FREE == 2;
970#endif
971 };
972
973 /// Explicit specialization for wchar_t.
974 template<>
975 struct atomic<wchar_t> : __atomic_base<wchar_t>
976 {
977 typedef wchar_t __integral_type;
978 typedef __atomic_base<wchar_t> __base_type;
979
980 atomic() noexcept = default;
981 ~atomic() noexcept = default;
982 atomic(const atomic&) = delete;
983 atomic& operator=(const atomic&) = delete;
984 atomic& operator=(const atomic&) volatile = delete;
985
986 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
987
988 using __base_type::operator __integral_type;
989 using __base_type::operator=;
990
991#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
992 static constexpr bool is_always_lock_free = ATOMIC_WCHAR_T_LOCK_FREE == 2;
993#endif
994 };
995
996#ifdef _GLIBCXX_USE_CHAR8_T
997 /// Explicit specialization for char8_t.
998 template<>
999 struct atomic<char8_t> : __atomic_base<char8_t>
1000 {
1001 typedef char8_t __integral_type;
1002 typedef __atomic_base<char8_t> __base_type;
1003
1004 atomic() noexcept = default;
1005 ~atomic() noexcept = default;
1006 atomic(const atomic&) = delete;
1007 atomic& operator=(const atomic&) = delete;
1008 atomic& operator=(const atomic&) volatile = delete;
1009
1010 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
1011
1012 using __base_type::operator __integral_type;
1013 using __base_type::operator=;
1014
1015#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
1016 static constexpr bool is_always_lock_free
1017 = ATOMIC_CHAR8_T_LOCK_FREE == 2;
1018#endif
1019 };
1020#endif
1021
1022 /// Explicit specialization for char16_t.
1023 template<>
1024 struct atomic<char16_t> : __atomic_base<char16_t>
1025 {
1026 typedef char16_t __integral_type;
1027 typedef __atomic_base<char16_t> __base_type;
1028
1029 atomic() noexcept = default;
1030 ~atomic() noexcept = default;
1031 atomic(const atomic&) = delete;
1032 atomic& operator=(const atomic&) = delete;
1033 atomic& operator=(const atomic&) volatile = delete;
1034
1035 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
1036
1037 using __base_type::operator __integral_type;
1038 using __base_type::operator=;
1039
1040#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
1041 static constexpr bool is_always_lock_free
1042 = ATOMIC_CHAR16_T_LOCK_FREE == 2;
1043#endif
1044 };
1045
1046 /// Explicit specialization for char32_t.
1047 template<>
1048 struct atomic<char32_t> : __atomic_base<char32_t>
1049 {
1050 typedef char32_t __integral_type;
1051 typedef __atomic_base<char32_t> __base_type;
1052
1053 atomic() noexcept = default;
1054 ~atomic() noexcept = default;
1055 atomic(const atomic&) = delete;
1056 atomic& operator=(const atomic&) = delete;
1057 atomic& operator=(const atomic&) volatile = delete;
1058
1059 constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
1060
1061 using __base_type::operator __integral_type;
1062 using __base_type::operator=;
1063
1064#ifdef __cpp_lib_atomic_is_always_lock_free // C++ >= 17
1065 static constexpr bool is_always_lock_free
1066 = ATOMIC_CHAR32_T_LOCK_FREE == 2;
1067#endif
1068 };
1069
1070
1071 /// atomic_bool
1073
1074 /// atomic_char
1076
1077 /// atomic_schar
1079
1080 /// atomic_uchar
1082
1083 /// atomic_short
1085
1086 /// atomic_ushort
1088
1089 /// atomic_int
1091
1092 /// atomic_uint
1094
1095 /// atomic_long
1097
1098 /// atomic_ulong
1100
1101 /// atomic_llong
1103
1104 /// atomic_ullong
1106
1107 /// atomic_wchar_t
1109
1110#ifdef _GLIBCXX_USE_CHAR8_T
1111 /// atomic_char8_t
1112 typedef atomic<char8_t> atomic_char8_t;
1113#endif
1114
1115 /// atomic_char16_t
1117
1118 /// atomic_char32_t
1120
1121#ifdef _GLIBCXX_USE_C99_STDINT
1122 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1123 // 2441. Exact-width atomic typedefs should be provided
1124
1125 /// atomic_int8_t
1127
1128 /// atomic_uint8_t
1130
1131 /// atomic_int16_t
1133
1134 /// atomic_uint16_t
1136
1137 /// atomic_int32_t
1139
1140 /// atomic_uint32_t
1142
1143 /// atomic_int64_t
1145
1146 /// atomic_uint64_t
1148#endif
1149
1150 /// atomic_int_least8_t
1152
1153 /// atomic_uint_least8_t
1155
1156 /// atomic_int_least16_t
1158
1159 /// atomic_uint_least16_t
1161
1162 /// atomic_int_least32_t
1164
1165 /// atomic_uint_least32_t
1167
1168 /// atomic_int_least64_t
1170
1171 /// atomic_uint_least64_t
1173
1174
1175 /// atomic_int_fast8_t
1177
1178 /// atomic_uint_fast8_t
1180
1181 /// atomic_int_fast16_t
1183
1184 /// atomic_uint_fast16_t
1186
1187 /// atomic_int_fast32_t
1189
1190 /// atomic_uint_fast32_t
1192
1193 /// atomic_int_fast64_t
1195
1196 /// atomic_uint_fast64_t
1198
1199
1200 /// atomic_intptr_t
1202
1203 /// atomic_uintptr_t
1205
1206 /// atomic_size_t
1208
1209 /// atomic_ptrdiff_t
1211
1212 /// atomic_intmax_t
1214
1215 /// atomic_uintmax_t
1217
1218 // Function definitions, atomic_flag operations.
1219 inline bool
1220 atomic_flag_test_and_set_explicit(atomic_flag* __a,
1221 memory_order __m) noexcept
1222 { return __a->test_and_set(__m); }
1223
1224 inline bool
1225 atomic_flag_test_and_set_explicit(volatile atomic_flag* __a,
1226 memory_order __m) noexcept
1227 { return __a->test_and_set(__m); }
1228
1229#if __cpp_lib_atomic_flag_test
1230 inline bool
1231 atomic_flag_test(const atomic_flag* __a) noexcept
1232 { return __a->test(); }
1233
1234 inline bool
1235 atomic_flag_test(const volatile atomic_flag* __a) noexcept
1236 { return __a->test(); }
1237
1238 inline bool
1239 atomic_flag_test_explicit(const atomic_flag* __a,
1240 memory_order __m) noexcept
1241 { return __a->test(__m); }
1242
1243 inline bool
1244 atomic_flag_test_explicit(const volatile atomic_flag* __a,
1245 memory_order __m) noexcept
1246 { return __a->test(__m); }
1247#endif
1248
1249 inline void
1250 atomic_flag_clear_explicit(atomic_flag* __a, memory_order __m) noexcept
1251 { __a->clear(__m); }
1252
1253 inline void
1254 atomic_flag_clear_explicit(volatile atomic_flag* __a,
1255 memory_order __m) noexcept
1256 { __a->clear(__m); }
1257
1258 inline bool
1259 atomic_flag_test_and_set(atomic_flag* __a) noexcept
1260 { return atomic_flag_test_and_set_explicit(__a, memory_order_seq_cst); }
1261
1262 inline bool
1263 atomic_flag_test_and_set(volatile atomic_flag* __a) noexcept
1264 { return atomic_flag_test_and_set_explicit(__a, memory_order_seq_cst); }
1265
1266 inline void
1267 atomic_flag_clear(atomic_flag* __a) noexcept
1268 { atomic_flag_clear_explicit(__a, memory_order_seq_cst); }
1269
1270 inline void
1271 atomic_flag_clear(volatile atomic_flag* __a) noexcept
1272 { atomic_flag_clear_explicit(__a, memory_order_seq_cst); }
1273
1274#if __cpp_lib_atomic_wait
1275 inline void
1276 atomic_flag_wait(atomic_flag* __a, bool __old) noexcept
1277 { __a->wait(__old); }
1278
1279 inline void
1280 atomic_flag_wait_explicit(atomic_flag* __a, bool __old,
1281 memory_order __m) noexcept
1282 { __a->wait(__old, __m); }
1283
1284 inline void
1285 atomic_flag_notify_one(atomic_flag* __a) noexcept
1286 { __a->notify_one(); }
1287
1288 inline void
1289 atomic_flag_notify_all(atomic_flag* __a) noexcept
1290 { __a->notify_all(); }
1291#endif // __cpp_lib_atomic_wait
1292
1293 /// @cond undocumented
1294 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1295 // 3220. P0558 broke conforming C++14 uses of atomic shared_ptr
1296 template<typename _Tp>
1297 using __atomic_val_t = __type_identity_t<_Tp>;
1298 template<typename _Tp>
1299 using __atomic_diff_t = typename atomic<_Tp>::difference_type;
1300 /// @endcond
1301
1302 // [atomics.nonmembers] Non-member functions.
1303 // Function templates generally applicable to atomic types.
1304 template<typename _ITp>
1305 inline bool
1306 atomic_is_lock_free(const atomic<_ITp>* __a) noexcept
1307 { return __a->is_lock_free(); }
1308
1309 template<typename _ITp>
1310 inline bool
1311 atomic_is_lock_free(const volatile atomic<_ITp>* __a) noexcept
1312 { return __a->is_lock_free(); }
1313
1314 template<typename _ITp>
1315 inline void
1316 atomic_init(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
1317 { __a->store(__i, memory_order_relaxed); }
1318
1319 template<typename _ITp>
1320 inline void
1321 atomic_init(volatile atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
1322 { __a->store(__i, memory_order_relaxed); }
1323
1324 template<typename _ITp>
1325 inline void
1326 atomic_store_explicit(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i,
1327 memory_order __m) noexcept
1328 { __a->store(__i, __m); }
1329
1330 template<typename _ITp>
1331 inline void
1332 atomic_store_explicit(volatile atomic<_ITp>* __a, __atomic_val_t<_ITp> __i,
1333 memory_order __m) noexcept
1334 { __a->store(__i, __m); }
1335
1336 template<typename _ITp>
1337 inline _ITp
1338 atomic_load_explicit(const atomic<_ITp>* __a, memory_order __m) noexcept
1339 { return __a->load(__m); }
1340
1341 template<typename _ITp>
1342 inline _ITp
1343 atomic_load_explicit(const volatile atomic<_ITp>* __a,
1344 memory_order __m) noexcept
1345 { return __a->load(__m); }
1346
1347 template<typename _ITp>
1348 inline _ITp
1349 atomic_exchange_explicit(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i,
1350 memory_order __m) noexcept
1351 { return __a->exchange(__i, __m); }
1352
1353 template<typename _ITp>
1354 inline _ITp
1355 atomic_exchange_explicit(volatile atomic<_ITp>* __a,
1356 __atomic_val_t<_ITp> __i,
1357 memory_order __m) noexcept
1358 { return __a->exchange(__i, __m); }
1359
1360 template<typename _ITp>
1361 inline bool
1362 atomic_compare_exchange_weak_explicit(atomic<_ITp>* __a,
1363 __atomic_val_t<_ITp>* __i1,
1364 __atomic_val_t<_ITp> __i2,
1365 memory_order __m1,
1366 memory_order __m2) noexcept
1367 { return __a->compare_exchange_weak(*__i1, __i2, __m1, __m2); }
1368
1369 template<typename _ITp>
1370 inline bool
1371 atomic_compare_exchange_weak_explicit(volatile atomic<_ITp>* __a,
1372 __atomic_val_t<_ITp>* __i1,
1373 __atomic_val_t<_ITp> __i2,
1374 memory_order __m1,
1375 memory_order __m2) noexcept
1376 { return __a->compare_exchange_weak(*__i1, __i2, __m1, __m2); }
1377
1378 template<typename _ITp>
1379 inline bool
1380 atomic_compare_exchange_strong_explicit(atomic<_ITp>* __a,
1381 __atomic_val_t<_ITp>* __i1,
1382 __atomic_val_t<_ITp> __i2,
1383 memory_order __m1,
1384 memory_order __m2) noexcept
1385 { return __a->compare_exchange_strong(*__i1, __i2, __m1, __m2); }
1386
1387 template<typename _ITp>
1388 inline bool
1389 atomic_compare_exchange_strong_explicit(volatile atomic<_ITp>* __a,
1390 __atomic_val_t<_ITp>* __i1,
1391 __atomic_val_t<_ITp> __i2,
1392 memory_order __m1,
1393 memory_order __m2) noexcept
1394 { return __a->compare_exchange_strong(*__i1, __i2, __m1, __m2); }
1395
1396
1397 template<typename _ITp>
1398 inline void
1399 atomic_store(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
1400 { atomic_store_explicit(__a, __i, memory_order_seq_cst); }
1401
1402 template<typename _ITp>
1403 inline void
1404 atomic_store(volatile atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
1405 { atomic_store_explicit(__a, __i, memory_order_seq_cst); }
1406
1407 template<typename _ITp>
1408 inline _ITp
1409 atomic_load(const atomic<_ITp>* __a) noexcept
1410 { return atomic_load_explicit(__a, memory_order_seq_cst); }
1411
1412 template<typename _ITp>
1413 inline _ITp
1414 atomic_load(const volatile atomic<_ITp>* __a) noexcept
1415 { return atomic_load_explicit(__a, memory_order_seq_cst); }
1416
1417 template<typename _ITp>
1418 inline _ITp
1419 atomic_exchange(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
1420 { return atomic_exchange_explicit(__a, __i, memory_order_seq_cst); }
1421
1422 template<typename _ITp>
1423 inline _ITp
1424 atomic_exchange(volatile atomic<_ITp>* __a,
1425 __atomic_val_t<_ITp> __i) noexcept
1426 { return atomic_exchange_explicit(__a, __i, memory_order_seq_cst); }
1427
1428 template<typename _ITp>
1429 inline bool
1430 atomic_compare_exchange_weak(atomic<_ITp>* __a,
1431 __atomic_val_t<_ITp>* __i1,
1432 __atomic_val_t<_ITp> __i2) noexcept
1433 {
1434 return atomic_compare_exchange_weak_explicit(__a, __i1, __i2,
1435 memory_order_seq_cst,
1436 memory_order_seq_cst);
1437 }
1438
1439 template<typename _ITp>
1440 inline bool
1441 atomic_compare_exchange_weak(volatile atomic<_ITp>* __a,
1442 __atomic_val_t<_ITp>* __i1,
1443 __atomic_val_t<_ITp> __i2) noexcept
1444 {
1445 return atomic_compare_exchange_weak_explicit(__a, __i1, __i2,
1446 memory_order_seq_cst,
1447 memory_order_seq_cst);
1448 }
1449
1450 template<typename _ITp>
1451 inline bool
1452 atomic_compare_exchange_strong(atomic<_ITp>* __a,
1453 __atomic_val_t<_ITp>* __i1,
1454 __atomic_val_t<_ITp> __i2) noexcept
1455 {
1456 return atomic_compare_exchange_strong_explicit(__a, __i1, __i2,
1457 memory_order_seq_cst,
1458 memory_order_seq_cst);
1459 }
1460
1461 template<typename _ITp>
1462 inline bool
1463 atomic_compare_exchange_strong(volatile atomic<_ITp>* __a,
1464 __atomic_val_t<_ITp>* __i1,
1465 __atomic_val_t<_ITp> __i2) noexcept
1466 {
1467 return atomic_compare_exchange_strong_explicit(__a, __i1, __i2,
1468 memory_order_seq_cst,
1469 memory_order_seq_cst);
1470 }
1471
1472
1473#if __cpp_lib_atomic_wait
1474 template<typename _Tp>
1475 inline void
1476 atomic_wait(const atomic<_Tp>* __a,
1477 typename std::atomic<_Tp>::value_type __old) noexcept
1478 { __a->wait(__old); }
1479
1480 template<typename _Tp>
1481 inline void
1482 atomic_wait_explicit(const atomic<_Tp>* __a,
1483 typename std::atomic<_Tp>::value_type __old,
1484 std::memory_order __m) noexcept
1485 { __a->wait(__old, __m); }
1486
1487 template<typename _Tp>
1488 inline void
1489 atomic_notify_one(atomic<_Tp>* __a) noexcept
1490 { __a->notify_one(); }
1491
1492 template<typename _Tp>
1493 inline void
1494 atomic_notify_all(atomic<_Tp>* __a) noexcept
1495 { __a->notify_all(); }
1496#endif // __cpp_lib_atomic_wait
1497
1498 // Function templates for atomic_integral and atomic_pointer operations only.
1499 // Some operations (and, or, xor) are only available for atomic integrals,
1500 // which is implemented by taking a parameter of type __atomic_base<_ITp>*.
1501
1502 template<typename _ITp>
1503 inline _ITp
1504 atomic_fetch_add_explicit(atomic<_ITp>* __a,
1505 __atomic_diff_t<_ITp> __i,
1506 memory_order __m) noexcept
1507 { return __a->fetch_add(__i, __m); }
1508
1509 template<typename _ITp>
1510 inline _ITp
1511 atomic_fetch_add_explicit(volatile atomic<_ITp>* __a,
1512 __atomic_diff_t<_ITp> __i,
1513 memory_order __m) noexcept
1514 { return __a->fetch_add(__i, __m); }
1515
1516 template<typename _ITp>
1517 inline _ITp
1518 atomic_fetch_sub_explicit(atomic<_ITp>* __a,
1519 __atomic_diff_t<_ITp> __i,
1520 memory_order __m) noexcept
1521 { return __a->fetch_sub(__i, __m); }
1522
1523 template<typename _ITp>
1524 inline _ITp
1525 atomic_fetch_sub_explicit(volatile atomic<_ITp>* __a,
1526 __atomic_diff_t<_ITp> __i,
1527 memory_order __m) noexcept
1528 { return __a->fetch_sub(__i, __m); }
1529
1530 template<typename _ITp>
1531 inline _ITp
1532 atomic_fetch_and_explicit(__atomic_base<_ITp>* __a,
1533 __atomic_val_t<_ITp> __i,
1534 memory_order __m) noexcept
1535 { return __a->fetch_and(__i, __m); }
1536
1537 template<typename _ITp>
1538 inline _ITp
1539 atomic_fetch_and_explicit(volatile __atomic_base<_ITp>* __a,
1540 __atomic_val_t<_ITp> __i,
1541 memory_order __m) noexcept
1542 { return __a->fetch_and(__i, __m); }
1543
1544 template<typename _ITp>
1545 inline _ITp
1546 atomic_fetch_or_explicit(__atomic_base<_ITp>* __a,
1547 __atomic_val_t<_ITp> __i,
1548 memory_order __m) noexcept
1549 { return __a->fetch_or(__i, __m); }
1550
1551 template<typename _ITp>
1552 inline _ITp
1553 atomic_fetch_or_explicit(volatile __atomic_base<_ITp>* __a,
1554 __atomic_val_t<_ITp> __i,
1555 memory_order __m) noexcept
1556 { return __a->fetch_or(__i, __m); }
1557
1558 template<typename _ITp>
1559 inline _ITp
1560 atomic_fetch_xor_explicit(__atomic_base<_ITp>* __a,
1561 __atomic_val_t<_ITp> __i,
1562 memory_order __m) noexcept
1563 { return __a->fetch_xor(__i, __m); }
1564
1565 template<typename _ITp>
1566 inline _ITp
1567 atomic_fetch_xor_explicit(volatile __atomic_base<_ITp>* __a,
1568 __atomic_val_t<_ITp> __i,
1569 memory_order __m) noexcept
1570 { return __a->fetch_xor(__i, __m); }
1571
1572#ifdef __cpp_lib_atomic_min_max
1573 template<typename _ITp>
1574 inline _ITp
1575 atomic_fetch_min_explicit(__atomic_base<_ITp>* __a,
1576 __atomic_val_t<_ITp> __i,
1577 memory_order __m) noexcept
1578 { return __a->fetch_min(__i, __m); }
1579
1580 template<typename _ITp>
1581 inline _ITp
1582 atomic_fetch_min_explicit(volatile __atomic_base<_ITp>* __a,
1583 __atomic_val_t<_ITp> __i,
1584 memory_order __m) noexcept
1585 { return __a->fetch_min(__i, __m); }
1586
1587 template<typename _ITp>
1588 inline _ITp
1589 atomic_fetch_max_explicit(__atomic_base<_ITp>* __a,
1590 __atomic_val_t<_ITp> __i,
1591 memory_order __m) noexcept
1592 { return __a->fetch_max(__i, __m); }
1593
1594 template<typename _ITp>
1595 inline _ITp
1596 atomic_fetch_max_explicit(volatile __atomic_base<_ITp>* __a,
1597 __atomic_val_t<_ITp> __i,
1598 memory_order __m) noexcept
1599 { return __a->fetch_max(__i, __m); }
1600#endif
1601
1602 template<typename _ITp>
1603 inline _ITp
1604 atomic_fetch_add(atomic<_ITp>* __a,
1605 __atomic_diff_t<_ITp> __i) noexcept
1606 { return atomic_fetch_add_explicit(__a, __i, memory_order_seq_cst); }
1607
1608 template<typename _ITp>
1609 inline _ITp
1610 atomic_fetch_add(volatile atomic<_ITp>* __a,
1611 __atomic_diff_t<_ITp> __i) noexcept
1612 { return atomic_fetch_add_explicit(__a, __i, memory_order_seq_cst); }
1613
1614 template<typename _ITp>
1615 inline _ITp
1616 atomic_fetch_sub(atomic<_ITp>* __a,
1617 __atomic_diff_t<_ITp> __i) noexcept
1618 { return atomic_fetch_sub_explicit(__a, __i, memory_order_seq_cst); }
1619
1620 template<typename _ITp>
1621 inline _ITp
1622 atomic_fetch_sub(volatile atomic<_ITp>* __a,
1623 __atomic_diff_t<_ITp> __i) noexcept
1624 { return atomic_fetch_sub_explicit(__a, __i, memory_order_seq_cst); }
1625
1626 template<typename _ITp>
1627 inline _ITp
1628 atomic_fetch_and(__atomic_base<_ITp>* __a,
1629 __atomic_val_t<_ITp> __i) noexcept
1630 { return atomic_fetch_and_explicit(__a, __i, memory_order_seq_cst); }
1631
1632 template<typename _ITp>
1633 inline _ITp
1634 atomic_fetch_and(volatile __atomic_base<_ITp>* __a,
1635 __atomic_val_t<_ITp> __i) noexcept
1636 { return atomic_fetch_and_explicit(__a, __i, memory_order_seq_cst); }
1637
1638 template<typename _ITp>
1639 inline _ITp
1640 atomic_fetch_or(__atomic_base<_ITp>* __a,
1641 __atomic_val_t<_ITp> __i) noexcept
1642 { return atomic_fetch_or_explicit(__a, __i, memory_order_seq_cst); }
1643
1644 template<typename _ITp>
1645 inline _ITp
1646 atomic_fetch_or(volatile __atomic_base<_ITp>* __a,
1647 __atomic_val_t<_ITp> __i) noexcept
1648 { return atomic_fetch_or_explicit(__a, __i, memory_order_seq_cst); }
1649
1650 template<typename _ITp>
1651 inline _ITp
1652 atomic_fetch_xor(__atomic_base<_ITp>* __a,
1653 __atomic_val_t<_ITp> __i) noexcept
1654 { return atomic_fetch_xor_explicit(__a, __i, memory_order_seq_cst); }
1655
1656 template<typename _ITp>
1657 inline _ITp
1658 atomic_fetch_xor(volatile __atomic_base<_ITp>* __a,
1659 __atomic_val_t<_ITp> __i) noexcept
1660 { return atomic_fetch_xor_explicit(__a, __i, memory_order_seq_cst); }
1661
1662#ifdef __cpp_lib_atomic_min_max
1663 template<typename _ITp>
1664 inline _ITp
1665 atomic_fetch_min(__atomic_base<_ITp>* __a,
1666 __atomic_val_t<_ITp> __i) noexcept
1667 { return atomic_fetch_min_explicit(__a, __i, memory_order_seq_cst); }
1668
1669 template<typename _ITp>
1670 inline _ITp
1671 atomic_fetch_min(volatile __atomic_base<_ITp>* __a,
1672 __atomic_val_t<_ITp> __i) noexcept
1673 { return atomic_fetch_min_explicit(__a, __i, memory_order_seq_cst); }
1674
1675 template<typename _ITp>
1676 inline _ITp
1677 atomic_fetch_max(__atomic_base<_ITp>* __a,
1678 __atomic_val_t<_ITp> __i) noexcept
1679 { return atomic_fetch_max_explicit(__a, __i, memory_order_seq_cst); }
1680
1681 template<typename _ITp>
1682 inline _ITp
1683 atomic_fetch_max(volatile __atomic_base<_ITp>* __a,
1684 __atomic_val_t<_ITp> __i) noexcept
1685 { return atomic_fetch_max_explicit(__a, __i, memory_order_seq_cst); }
1686#endif
1687
1688#ifdef __cpp_lib_atomic_float
1689 template<>
1690 struct atomic<float> : __atomic_float<float>
1691 {
1692 atomic() noexcept = default;
1693
1694 constexpr
1695 atomic(float __fp) noexcept : __atomic_float<float>(__fp)
1696 { }
1697
1698 atomic& operator=(const atomic&) volatile = delete;
1699 atomic& operator=(const atomic&) = delete;
1700
1701 using __atomic_float<float>::operator=;
1702 };
1703
1704 template<>
1705 struct atomic<double> : __atomic_float<double>
1706 {
1707 atomic() noexcept = default;
1708
1709 constexpr
1710 atomic(double __fp) noexcept : __atomic_float<double>(__fp)
1711 { }
1712
1713 atomic& operator=(const atomic&) volatile = delete;
1714 atomic& operator=(const atomic&) = delete;
1715
1716 using __atomic_float<double>::operator=;
1717 };
1718
1719 template<>
1720 struct atomic<long double> : __atomic_float<long double>
1721 {
1722 atomic() noexcept = default;
1723
1724 constexpr
1725 atomic(long double __fp) noexcept : __atomic_float<long double>(__fp)
1726 { }
1727
1728 atomic& operator=(const atomic&) volatile = delete;
1729 atomic& operator=(const atomic&) = delete;
1730
1731 using __atomic_float<long double>::operator=;
1732 };
1733
1734#ifdef __STDCPP_FLOAT16_T__
1735 template<>
1736 struct atomic<_Float16> : __atomic_float<_Float16>
1737 {
1738 atomic() noexcept = default;
1739
1740 constexpr
1741 atomic(_Float16 __fp) noexcept : __atomic_float<_Float16>(__fp)
1742 { }
1743
1744 atomic& operator=(const atomic&) volatile = delete;
1745 atomic& operator=(const atomic&) = delete;
1746
1747 using __atomic_float<_Float16>::operator=;
1748 };
1749#endif
1750
1751#ifdef __STDCPP_FLOAT32_T__
1752 template<>
1753 struct atomic<_Float32> : __atomic_float<_Float32>
1754 {
1755 atomic() noexcept = default;
1756
1757 constexpr
1758 atomic(_Float32 __fp) noexcept : __atomic_float<_Float32>(__fp)
1759 { }
1760
1761 atomic& operator=(const atomic&) volatile = delete;
1762 atomic& operator=(const atomic&) = delete;
1763
1764 using __atomic_float<_Float32>::operator=;
1765 };
1766#endif
1767
1768#ifdef __STDCPP_FLOAT64_T__
1769 template<>
1770 struct atomic<_Float64> : __atomic_float<_Float64>
1771 {
1772 atomic() noexcept = default;
1773
1774 constexpr
1775 atomic(_Float64 __fp) noexcept : __atomic_float<_Float64>(__fp)
1776 { }
1777
1778 atomic& operator=(const atomic&) volatile = delete;
1779 atomic& operator=(const atomic&) = delete;
1780
1781 using __atomic_float<_Float64>::operator=;
1782 };
1783#endif
1784
1785#ifdef __STDCPP_FLOAT128_T__
1786 template<>
1787 struct atomic<_Float128> : __atomic_float<_Float128>
1788 {
1789 atomic() noexcept = default;
1790
1791 constexpr
1792 atomic(_Float128 __fp) noexcept : __atomic_float<_Float128>(__fp)
1793 { }
1794
1795 atomic& operator=(const atomic&) volatile = delete;
1796 atomic& operator=(const atomic&) = delete;
1797
1798 using __atomic_float<_Float128>::operator=;
1799 };
1800#endif
1801
1802#ifdef __STDCPP_BFLOAT16_T__
1803 template<>
1804 struct atomic<__gnu_cxx::__bfloat16_t> : __atomic_float<__gnu_cxx::__bfloat16_t>
1805 {
1806 atomic() noexcept = default;
1807
1808 constexpr
1809 atomic(__gnu_cxx::__bfloat16_t __fp) noexcept : __atomic_float<__gnu_cxx::__bfloat16_t>(__fp)
1810 { }
1811
1812 atomic& operator=(const atomic&) volatile = delete;
1813 atomic& operator=(const atomic&) = delete;
1814
1815 using __atomic_float<__gnu_cxx::__bfloat16_t>::operator=;
1816 };
1817#endif
1818#endif // __cpp_lib_atomic_float
1819
1820#ifdef __cpp_lib_atomic_ref
1821 /// Class template to provide atomic operations on a non-atomic variable.
1822 template<typename _Tp>
1823 struct atomic_ref : __atomic_ref<_Tp>
1824 {
1825 explicit
1826 atomic_ref(_Tp& __t) noexcept
1827 : __atomic_ref<_Tp>(std::addressof(__t))
1828 {
1829 __glibcxx_assert(((__UINTPTR_TYPE__)this->_M_ptr % this->required_alignment) == 0);
1830 }
1831
1832 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1833 // 4472. std::atomic_ref<const T> can be constructed from temporaries
1834 explicit
1835 atomic_ref(_Tp&&) = delete;
1836
1837 template<typename _Up>
1838 requires is_convertible_v<_Up(*)[1], _Tp(*)[1]>
1839 atomic_ref(atomic_ref<_Up> __other) noexcept
1840 : __atomic_ref<_Tp>(__other._M_ptr)
1841 { }
1842
1843 atomic_ref& operator=(const atomic_ref&) = delete;
1844
1845 atomic_ref(const atomic_ref&) = default;
1846
1847 using __atomic_ref<_Tp>::operator=;
1848
1849 template<typename>
1850 friend struct atomic_ref;
1851 };
1852#endif // __cpp_lib_atomic_ref
1853
1854#ifdef __cpp_lib_atomic_lock_free_type_aliases
1855# ifdef _GLIBCXX_HAVE_PLATFORM_WAIT
1856 using atomic_signed_lock_free
1858 using atomic_unsigned_lock_free
1860# elif ATOMIC_INT_LOCK_FREE == 2
1861 using atomic_signed_lock_free = atomic<signed int>;
1862 using atomic_unsigned_lock_free = atomic<unsigned int>;
1863# elif ATOMIC_LONG_LOCK_FREE == 2
1864 using atomic_signed_lock_free = atomic<signed long>;
1865 using atomic_unsigned_lock_free = atomic<unsigned long>;
1866# elif ATOMIC_CHAR_LOCK_FREE == 2
1867 using atomic_signed_lock_free = atomic<signed char>;
1868 using atomic_unsigned_lock_free = atomic<unsigned char>;
1869# else
1870# error "libstdc++ bug: no lock-free atomics but they were emitted in <version>"
1871# endif
1872#endif
1873
1874 /// @} group atomics
1875
1876_GLIBCXX_END_NAMESPACE_VERSION
1877} // namespace
1878
1879#endif // C++11
1880
1881#endif // _GLIBCXX_ATOMIC
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
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
atomic< unsigned long > atomic_ulong
atomic_ulong
Definition atomic:1099
atomic< intmax_t > atomic_intmax_t
atomic_intmax_t
Definition atomic:1213
atomic< uintptr_t > atomic_uintptr_t
atomic_uintptr_t
Definition atomic:1204
atomic< signed char > atomic_schar
atomic_schar
Definition atomic:1078
atomic< int_least8_t > atomic_int_least8_t
atomic_int_least8_t
Definition atomic:1151
atomic< unsigned long long > atomic_ullong
atomic_ullong
Definition atomic:1105
atomic< uint_fast8_t > atomic_uint_fast8_t
atomic_uint_fast8_t
Definition atomic:1179
atomic< intptr_t > atomic_intptr_t
atomic_intptr_t
Definition atomic:1201
atomic< int16_t > atomic_int16_t
atomic_int16_t
Definition atomic:1132
atomic< size_t > atomic_size_t
atomic_size_t
Definition atomic:1207
atomic< long > atomic_long
atomic_long
Definition atomic:1096
atomic< uint_least8_t > atomic_uint_least8_t
atomic_uint_least8_t
Definition atomic:1154
atomic< short > atomic_short
atomic_short
Definition atomic:1084
atomic< uint_least16_t > atomic_uint_least16_t
atomic_uint_least16_t
Definition atomic:1160
atomic< uint16_t > atomic_uint16_t
atomic_uint16_t
Definition atomic:1135
atomic< uint64_t > atomic_uint64_t
atomic_uint64_t
Definition atomic:1147
atomic< int_least32_t > atomic_int_least32_t
atomic_int_least32_t
Definition atomic:1163
atomic< uint8_t > atomic_uint8_t
atomic_uint8_t
Definition atomic:1129
#define ATOMIC_BOOL_LOCK_FREE
atomic< wchar_t > atomic_wchar_t
atomic_wchar_t
Definition atomic:1108
atomic< unsigned int > atomic_uint
atomic_uint
Definition atomic:1093
atomic< uint_least32_t > atomic_uint_least32_t
atomic_uint_least32_t
Definition atomic:1166
atomic< uint_fast64_t > atomic_uint_fast64_t
atomic_uint_fast64_t
Definition atomic:1197
atomic< int_fast32_t > atomic_int_fast32_t
atomic_int_fast32_t
Definition atomic:1188
atomic< char > atomic_char
atomic_char
Definition atomic:1075
atomic< int > atomic_int
atomic_int
Definition atomic:1090
atomic< uint_least64_t > atomic_uint_least64_t
atomic_uint_least64_t
Definition atomic:1172
atomic< int64_t > atomic_int64_t
atomic_int64_t
Definition atomic:1144
atomic< uintmax_t > atomic_uintmax_t
atomic_uintmax_t
Definition atomic:1216
atomic< int_fast16_t > atomic_int_fast16_t
atomic_int_fast16_t
Definition atomic:1182
atomic< int32_t > atomic_int32_t
atomic_int32_t
Definition atomic:1138
atomic< uint_fast16_t > atomic_uint_fast16_t
atomic_uint_fast16_t
Definition atomic:1185
atomic< int8_t > atomic_int8_t
atomic_int8_t
Definition atomic:1126
atomic< long long > atomic_llong
atomic_llong
Definition atomic:1102
atomic< char16_t > atomic_char16_t
atomic_char16_t
Definition atomic:1116
atomic< int_fast64_t > atomic_int_fast64_t
atomic_int_fast64_t
Definition atomic:1194
atomic< ptrdiff_t > atomic_ptrdiff_t
atomic_ptrdiff_t
Definition atomic:1210
atomic< char32_t > atomic_char32_t
atomic_char32_t
Definition atomic:1119
atomic< int_least16_t > atomic_int_least16_t
atomic_int_least16_t
Definition atomic:1157
atomic< unsigned char > atomic_uchar
atomic_uchar
Definition atomic:1081
atomic< int_fast8_t > atomic_int_fast8_t
atomic_int_fast8_t
Definition atomic:1176
memory_order
Enumeration for memory_order.
Definition atomic_base.h:66
atomic< unsigned short > atomic_ushort
atomic_ushort
Definition atomic:1087
atomic< int_least64_t > atomic_int_least64_t
atomic_int_least64_t
Definition atomic:1169
atomic< bool > atomic_bool
atomic_bool
Definition atomic:1072
atomic< uint_fast32_t > atomic_uint_fast32_t
atomic_uint_fast32_t
Definition atomic:1191
atomic< uint32_t > atomic_uint32_t
atomic_uint32_t
Definition atomic:1141
ISO C++ entities toplevel namespace is std.
Generic atomic type, primary class template.
Definition atomic:201
atomic_flag