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