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