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