xref: /freebsd-src/contrib/llvm-project/libcxx/include/__atomic/atomic_ref.h (revision 52418fc2be8efa5172b90a3a9e617017173612c4)
10fca6ea1SDimitry Andric // -*- C++ -*-
20fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
30fca6ea1SDimitry Andric //
40fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
60fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70fca6ea1SDimitry Andric //
80fca6ea1SDimitry Andric //                        Kokkos v. 4.0
90fca6ea1SDimitry Andric //       Copyright (2022) National Technology & Engineering
100fca6ea1SDimitry Andric //               Solutions of Sandia, LLC (NTESS).
110fca6ea1SDimitry Andric //
120fca6ea1SDimitry Andric // Under the terms of Contract DE-NA0003525 with NTESS,
130fca6ea1SDimitry Andric // the U.S. Government retains certain rights in this software.
140fca6ea1SDimitry Andric //
150fca6ea1SDimitry Andric //===---------------------------------------------------------------------===//
160fca6ea1SDimitry Andric 
170fca6ea1SDimitry Andric #ifndef _LIBCPP___ATOMIC_ATOMIC_REF_H
180fca6ea1SDimitry Andric #define _LIBCPP___ATOMIC_ATOMIC_REF_H
190fca6ea1SDimitry Andric 
200fca6ea1SDimitry Andric #include <__assert>
210fca6ea1SDimitry Andric #include <__atomic/atomic_sync.h>
220fca6ea1SDimitry Andric #include <__atomic/check_memory_order.h>
230fca6ea1SDimitry Andric #include <__atomic/to_gcc_order.h>
240fca6ea1SDimitry Andric #include <__concepts/arithmetic.h>
250fca6ea1SDimitry Andric #include <__concepts/same_as.h>
260fca6ea1SDimitry Andric #include <__config>
270fca6ea1SDimitry Andric #include <__memory/addressof.h>
280fca6ea1SDimitry Andric #include <__type_traits/has_unique_object_representation.h>
290fca6ea1SDimitry Andric #include <__type_traits/is_trivially_copyable.h>
300fca6ea1SDimitry Andric #include <cstddef>
310fca6ea1SDimitry Andric #include <cstdint>
320fca6ea1SDimitry Andric #include <cstring>
330fca6ea1SDimitry Andric 
340fca6ea1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
350fca6ea1SDimitry Andric #  pragma GCC system_header
360fca6ea1SDimitry Andric #endif
370fca6ea1SDimitry Andric 
380fca6ea1SDimitry Andric _LIBCPP_PUSH_MACROS
390fca6ea1SDimitry Andric #include <__undef_macros>
400fca6ea1SDimitry Andric 
410fca6ea1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
420fca6ea1SDimitry Andric 
430fca6ea1SDimitry Andric #if _LIBCPP_STD_VER >= 20
440fca6ea1SDimitry Andric 
45*52418fc2SDimitry Andric // These types are required to make __atomic_is_always_lock_free work across GCC and Clang.
46*52418fc2SDimitry Andric // The purpose of this trick is to make sure that we provide an object with the correct alignment
47*52418fc2SDimitry Andric // to __atomic_is_always_lock_free, since that answer depends on the alignment.
48*52418fc2SDimitry Andric template <size_t _Alignment>
49*52418fc2SDimitry Andric struct __alignment_checker_type {
50*52418fc2SDimitry Andric   alignas(_Alignment) char __data;
51*52418fc2SDimitry Andric };
52*52418fc2SDimitry Andric 
53*52418fc2SDimitry Andric template <size_t _Alignment>
54*52418fc2SDimitry Andric struct __get_aligner_instance {
55*52418fc2SDimitry Andric   static constexpr __alignment_checker_type<_Alignment> __instance{};
56*52418fc2SDimitry Andric };
57*52418fc2SDimitry Andric 
580fca6ea1SDimitry Andric template <class _Tp>
590fca6ea1SDimitry Andric struct __atomic_ref_base {
600fca6ea1SDimitry Andric private:
610fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static _Tp* __clear_padding(_Tp& __val) noexcept {
620fca6ea1SDimitry Andric     _Tp* __ptr = std::addressof(__val);
630fca6ea1SDimitry Andric #  if __has_builtin(__builtin_clear_padding)
640fca6ea1SDimitry Andric     __builtin_clear_padding(__ptr);
650fca6ea1SDimitry Andric #  endif
660fca6ea1SDimitry Andric     return __ptr;
670fca6ea1SDimitry Andric   }
680fca6ea1SDimitry Andric 
690fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static bool __compare_exchange(
700fca6ea1SDimitry Andric       _Tp* __ptr, _Tp* __expected, _Tp* __desired, bool __is_weak, int __success, int __failure) noexcept {
710fca6ea1SDimitry Andric     if constexpr (
720fca6ea1SDimitry Andric #  if __has_builtin(__builtin_clear_padding)
730fca6ea1SDimitry Andric         has_unique_object_representations_v<_Tp> || floating_point<_Tp>
740fca6ea1SDimitry Andric #  else
750fca6ea1SDimitry Andric         true // NOLINT(readability-simplify-boolean-expr)
760fca6ea1SDimitry Andric #  endif
770fca6ea1SDimitry Andric     ) {
780fca6ea1SDimitry Andric       return __atomic_compare_exchange(__ptr, __expected, __desired, __is_weak, __success, __failure);
790fca6ea1SDimitry Andric     } else { // _Tp has padding bits and __builtin_clear_padding is available
800fca6ea1SDimitry Andric       __clear_padding(*__desired);
810fca6ea1SDimitry Andric       _Tp __copy = *__expected;
820fca6ea1SDimitry Andric       __clear_padding(__copy);
830fca6ea1SDimitry Andric       // The algorithm we use here is basically to perform `__atomic_compare_exchange` on the
840fca6ea1SDimitry Andric       // values until it has either succeeded, or failed because the value representation of the
850fca6ea1SDimitry Andric       // objects involved was different. This is why we loop around __atomic_compare_exchange:
860fca6ea1SDimitry Andric       // we basically loop until its failure is caused by the value representation of the objects
870fca6ea1SDimitry Andric       // being different, not only their object representation.
880fca6ea1SDimitry Andric       while (true) {
890fca6ea1SDimitry Andric         _Tp __prev = __copy;
900fca6ea1SDimitry Andric         if (__atomic_compare_exchange(__ptr, std::addressof(__copy), __desired, __is_weak, __success, __failure)) {
910fca6ea1SDimitry Andric           return true;
920fca6ea1SDimitry Andric         }
930fca6ea1SDimitry Andric         _Tp __curr = __copy;
940fca6ea1SDimitry Andric         if (std::memcmp(__clear_padding(__prev), __clear_padding(__curr), sizeof(_Tp)) != 0) {
950fca6ea1SDimitry Andric           // Value representation without padding bits do not compare equal ->
960fca6ea1SDimitry Andric           // write the current content of *ptr into *expected
970fca6ea1SDimitry Andric           std::memcpy(__expected, std::addressof(__copy), sizeof(_Tp));
980fca6ea1SDimitry Andric           return false;
990fca6ea1SDimitry Andric         }
1000fca6ea1SDimitry Andric       }
1010fca6ea1SDimitry Andric     }
1020fca6ea1SDimitry Andric   }
1030fca6ea1SDimitry Andric 
1040fca6ea1SDimitry Andric   friend struct __atomic_waitable_traits<__atomic_ref_base<_Tp>>;
1050fca6ea1SDimitry Andric 
106*52418fc2SDimitry Andric   // require types that are 1, 2, 4, 8, or 16 bytes in length to be aligned to at least their size to be potentially
107*52418fc2SDimitry Andric   // used lock-free
108*52418fc2SDimitry Andric   static constexpr size_t __min_alignment = (sizeof(_Tp) & (sizeof(_Tp) - 1)) || (sizeof(_Tp) > 16) ? 0 : sizeof(_Tp);
109*52418fc2SDimitry Andric 
1100fca6ea1SDimitry Andric public:
1110fca6ea1SDimitry Andric   using value_type = _Tp;
1120fca6ea1SDimitry Andric 
113*52418fc2SDimitry Andric   static constexpr size_t required_alignment = alignof(_Tp) > __min_alignment ? alignof(_Tp) : __min_alignment;
1140fca6ea1SDimitry Andric 
1150fca6ea1SDimitry Andric   // The __atomic_always_lock_free builtin takes into account the alignment of the pointer if provided,
1160fca6ea1SDimitry Andric   // so we create a fake pointer with a suitable alignment when querying it. Note that we are guaranteed
1170fca6ea1SDimitry Andric   // that the pointer is going to be aligned properly at runtime because that is a (checked) precondition
1180fca6ea1SDimitry Andric   // of atomic_ref's constructor.
1190fca6ea1SDimitry Andric   static constexpr bool is_always_lock_free =
120*52418fc2SDimitry Andric       __atomic_always_lock_free(sizeof(_Tp), &__get_aligner_instance<required_alignment>::__instance);
1210fca6ea1SDimitry Andric 
1220fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool is_lock_free() const noexcept { return __atomic_is_lock_free(sizeof(_Tp), __ptr_); }
1230fca6ea1SDimitry Andric 
1240fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI void store(_Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept
1250fca6ea1SDimitry Andric       _LIBCPP_CHECK_STORE_MEMORY_ORDER(__order) {
1260fca6ea1SDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
1270fca6ea1SDimitry Andric         __order == memory_order::relaxed || __order == memory_order::release || __order == memory_order::seq_cst,
1280fca6ea1SDimitry Andric         "atomic_ref: memory order argument to atomic store operation is invalid");
1290fca6ea1SDimitry Andric     __atomic_store(__ptr_, __clear_padding(__desired), std::__to_gcc_order(__order));
1300fca6ea1SDimitry Andric   }
1310fca6ea1SDimitry Andric 
1320fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept {
1330fca6ea1SDimitry Andric     store(__desired);
1340fca6ea1SDimitry Andric     return __desired;
1350fca6ea1SDimitry Andric   }
1360fca6ea1SDimitry Andric 
1370fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp load(memory_order __order = memory_order::seq_cst) const noexcept
1380fca6ea1SDimitry Andric       _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__order) {
1390fca6ea1SDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
1400fca6ea1SDimitry Andric         __order == memory_order::relaxed || __order == memory_order::consume || __order == memory_order::acquire ||
1410fca6ea1SDimitry Andric             __order == memory_order::seq_cst,
1420fca6ea1SDimitry Andric         "atomic_ref: memory order argument to atomic load operation is invalid");
1430fca6ea1SDimitry Andric     alignas(_Tp) byte __mem[sizeof(_Tp)];
1440fca6ea1SDimitry Andric     auto* __ret = reinterpret_cast<_Tp*>(__mem);
1450fca6ea1SDimitry Andric     __atomic_load(__ptr_, __ret, std::__to_gcc_order(__order));
1460fca6ea1SDimitry Andric     return *__ret;
1470fca6ea1SDimitry Andric   }
1480fca6ea1SDimitry Andric 
1490fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI operator _Tp() const noexcept { return load(); }
1500fca6ea1SDimitry Andric 
1510fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp exchange(_Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept {
1520fca6ea1SDimitry Andric     alignas(_Tp) byte __mem[sizeof(_Tp)];
1530fca6ea1SDimitry Andric     auto* __ret = reinterpret_cast<_Tp*>(__mem);
1540fca6ea1SDimitry Andric     __atomic_exchange(__ptr_, __clear_padding(__desired), __ret, std::__to_gcc_order(__order));
1550fca6ea1SDimitry Andric     return *__ret;
1560fca6ea1SDimitry Andric   }
1570fca6ea1SDimitry Andric 
1580fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool
1590fca6ea1SDimitry Andric   compare_exchange_weak(_Tp& __expected, _Tp __desired, memory_order __success, memory_order __failure) const noexcept
1600fca6ea1SDimitry Andric       _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__success, __failure) {
1610fca6ea1SDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
1620fca6ea1SDimitry Andric         __failure == memory_order::relaxed || __failure == memory_order::consume ||
1630fca6ea1SDimitry Andric             __failure == memory_order::acquire || __failure == memory_order::seq_cst,
1640fca6ea1SDimitry Andric         "atomic_ref: failure memory order argument to weak atomic compare-and-exchange operation is invalid");
1650fca6ea1SDimitry Andric     return __compare_exchange(
1660fca6ea1SDimitry Andric         __ptr_,
1670fca6ea1SDimitry Andric         std::addressof(__expected),
1680fca6ea1SDimitry Andric         std::addressof(__desired),
1690fca6ea1SDimitry Andric         true,
1700fca6ea1SDimitry Andric         std::__to_gcc_order(__success),
1710fca6ea1SDimitry Andric         std::__to_gcc_order(__failure));
1720fca6ea1SDimitry Andric   }
1730fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool
1740fca6ea1SDimitry Andric   compare_exchange_strong(_Tp& __expected, _Tp __desired, memory_order __success, memory_order __failure) const noexcept
1750fca6ea1SDimitry Andric       _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__success, __failure) {
1760fca6ea1SDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
1770fca6ea1SDimitry Andric         __failure == memory_order::relaxed || __failure == memory_order::consume ||
1780fca6ea1SDimitry Andric             __failure == memory_order::acquire || __failure == memory_order::seq_cst,
1790fca6ea1SDimitry Andric         "atomic_ref: failure memory order argument to strong atomic compare-and-exchange operation is invalid");
1800fca6ea1SDimitry Andric     return __compare_exchange(
1810fca6ea1SDimitry Andric         __ptr_,
1820fca6ea1SDimitry Andric         std::addressof(__expected),
1830fca6ea1SDimitry Andric         std::addressof(__desired),
1840fca6ea1SDimitry Andric         false,
1850fca6ea1SDimitry Andric         std::__to_gcc_order(__success),
1860fca6ea1SDimitry Andric         std::__to_gcc_order(__failure));
1870fca6ea1SDimitry Andric   }
1880fca6ea1SDimitry Andric 
1890fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool
1900fca6ea1SDimitry Andric   compare_exchange_weak(_Tp& __expected, _Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept {
1910fca6ea1SDimitry Andric     return __compare_exchange(
1920fca6ea1SDimitry Andric         __ptr_,
1930fca6ea1SDimitry Andric         std::addressof(__expected),
1940fca6ea1SDimitry Andric         std::addressof(__desired),
1950fca6ea1SDimitry Andric         true,
1960fca6ea1SDimitry Andric         std::__to_gcc_order(__order),
1970fca6ea1SDimitry Andric         std::__to_gcc_failure_order(__order));
1980fca6ea1SDimitry Andric   }
1990fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool
2000fca6ea1SDimitry Andric   compare_exchange_strong(_Tp& __expected, _Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept {
2010fca6ea1SDimitry Andric     return __compare_exchange(
2020fca6ea1SDimitry Andric         __ptr_,
2030fca6ea1SDimitry Andric         std::addressof(__expected),
2040fca6ea1SDimitry Andric         std::addressof(__desired),
2050fca6ea1SDimitry Andric         false,
2060fca6ea1SDimitry Andric         std::__to_gcc_order(__order),
2070fca6ea1SDimitry Andric         std::__to_gcc_failure_order(__order));
2080fca6ea1SDimitry Andric   }
2090fca6ea1SDimitry Andric 
2100fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI void wait(_Tp __old, memory_order __order = memory_order::seq_cst) const noexcept
2110fca6ea1SDimitry Andric       _LIBCPP_CHECK_WAIT_MEMORY_ORDER(__order) {
2120fca6ea1SDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
2130fca6ea1SDimitry Andric         __order == memory_order::relaxed || __order == memory_order::consume || __order == memory_order::acquire ||
2140fca6ea1SDimitry Andric             __order == memory_order::seq_cst,
2150fca6ea1SDimitry Andric         "atomic_ref: memory order argument to atomic wait operation is invalid");
2160fca6ea1SDimitry Andric     std::__atomic_wait(*this, __old, __order);
2170fca6ea1SDimitry Andric   }
2180fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI void notify_one() const noexcept { std::__atomic_notify_one(*this); }
2190fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI void notify_all() const noexcept { std::__atomic_notify_all(*this); }
220*52418fc2SDimitry Andric 
221*52418fc2SDimitry Andric protected:
222*52418fc2SDimitry Andric   typedef _Tp _Aligned_Tp __attribute__((aligned(required_alignment)));
223*52418fc2SDimitry Andric   _Aligned_Tp* __ptr_;
224*52418fc2SDimitry Andric 
225*52418fc2SDimitry Andric   _LIBCPP_HIDE_FROM_ABI __atomic_ref_base(_Tp& __obj) : __ptr_(std::addressof(__obj)) {}
2260fca6ea1SDimitry Andric };
2270fca6ea1SDimitry Andric 
2280fca6ea1SDimitry Andric template <class _Tp>
2290fca6ea1SDimitry Andric struct __atomic_waitable_traits<__atomic_ref_base<_Tp>> {
2300fca6ea1SDimitry Andric   static _LIBCPP_HIDE_FROM_ABI _Tp __atomic_load(const __atomic_ref_base<_Tp>& __a, memory_order __order) {
2310fca6ea1SDimitry Andric     return __a.load(__order);
2320fca6ea1SDimitry Andric   }
2330fca6ea1SDimitry Andric   static _LIBCPP_HIDE_FROM_ABI const _Tp* __atomic_contention_address(const __atomic_ref_base<_Tp>& __a) {
2340fca6ea1SDimitry Andric     return __a.__ptr_;
2350fca6ea1SDimitry Andric   }
2360fca6ea1SDimitry Andric };
2370fca6ea1SDimitry Andric 
2380fca6ea1SDimitry Andric template <class _Tp>
2390fca6ea1SDimitry Andric struct atomic_ref : public __atomic_ref_base<_Tp> {
2400fca6ea1SDimitry Andric   static_assert(is_trivially_copyable_v<_Tp>, "std::atomic_ref<T> requires that 'T' be a trivially copyable type");
2410fca6ea1SDimitry Andric 
2420fca6ea1SDimitry Andric   using __base = __atomic_ref_base<_Tp>;
2430fca6ea1SDimitry Andric 
2440fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit atomic_ref(_Tp& __obj) : __base(__obj) {
2450fca6ea1SDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
2460fca6ea1SDimitry Andric         reinterpret_cast<uintptr_t>(std::addressof(__obj)) % __base::required_alignment == 0,
2470fca6ea1SDimitry Andric         "atomic_ref ctor: referenced object must be aligned to required_alignment");
2480fca6ea1SDimitry Andric   }
2490fca6ea1SDimitry Andric 
2500fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI atomic_ref(const atomic_ref&) noexcept = default;
2510fca6ea1SDimitry Andric 
2520fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept { return __base::operator=(__desired); }
2530fca6ea1SDimitry Andric 
2540fca6ea1SDimitry Andric   atomic_ref& operator=(const atomic_ref&) = delete;
2550fca6ea1SDimitry Andric };
2560fca6ea1SDimitry Andric 
2570fca6ea1SDimitry Andric template <class _Tp>
2580fca6ea1SDimitry Andric   requires(std::integral<_Tp> && !std::same_as<bool, _Tp>)
2590fca6ea1SDimitry Andric struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
2600fca6ea1SDimitry Andric   using __base = __atomic_ref_base<_Tp>;
2610fca6ea1SDimitry Andric 
26281e300dfSDimitry Andric   using difference_type = typename __base::value_type;
2630fca6ea1SDimitry Andric 
2640fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit atomic_ref(_Tp& __obj) : __base(__obj) {
2650fca6ea1SDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
2660fca6ea1SDimitry Andric         reinterpret_cast<uintptr_t>(std::addressof(__obj)) % __base::required_alignment == 0,
2670fca6ea1SDimitry Andric         "atomic_ref ctor: referenced object must be aligned to required_alignment");
2680fca6ea1SDimitry Andric   }
2690fca6ea1SDimitry Andric 
2700fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI atomic_ref(const atomic_ref&) noexcept = default;
2710fca6ea1SDimitry Andric 
2720fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept { return __base::operator=(__desired); }
2730fca6ea1SDimitry Andric 
2740fca6ea1SDimitry Andric   atomic_ref& operator=(const atomic_ref&) = delete;
2750fca6ea1SDimitry Andric 
2760fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
2770fca6ea1SDimitry Andric     return __atomic_fetch_add(this->__ptr_, __arg, std::__to_gcc_order(__order));
2780fca6ea1SDimitry Andric   }
2790fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
2800fca6ea1SDimitry Andric     return __atomic_fetch_sub(this->__ptr_, __arg, std::__to_gcc_order(__order));
2810fca6ea1SDimitry Andric   }
2820fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp fetch_and(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
2830fca6ea1SDimitry Andric     return __atomic_fetch_and(this->__ptr_, __arg, std::__to_gcc_order(__order));
2840fca6ea1SDimitry Andric   }
2850fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp fetch_or(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
2860fca6ea1SDimitry Andric     return __atomic_fetch_or(this->__ptr_, __arg, std::__to_gcc_order(__order));
2870fca6ea1SDimitry Andric   }
2880fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
2890fca6ea1SDimitry Andric     return __atomic_fetch_xor(this->__ptr_, __arg, std::__to_gcc_order(__order));
2900fca6ea1SDimitry Andric   }
2910fca6ea1SDimitry Andric 
2920fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator++(int) const noexcept { return fetch_add(_Tp(1)); }
2930fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator--(int) const noexcept { return fetch_sub(_Tp(1)); }
2940fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator++() const noexcept { return fetch_add(_Tp(1)) + _Tp(1); }
2950fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator--() const noexcept { return fetch_sub(_Tp(1)) - _Tp(1); }
2960fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __arg) const noexcept { return fetch_add(__arg) + __arg; }
2970fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __arg) const noexcept { return fetch_sub(__arg) - __arg; }
2980fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator&=(_Tp __arg) const noexcept { return fetch_and(__arg) & __arg; }
2990fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator|=(_Tp __arg) const noexcept { return fetch_or(__arg) | __arg; }
3000fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator^=(_Tp __arg) const noexcept { return fetch_xor(__arg) ^ __arg; }
3010fca6ea1SDimitry Andric };
3020fca6ea1SDimitry Andric 
3030fca6ea1SDimitry Andric template <class _Tp>
3040fca6ea1SDimitry Andric   requires std::floating_point<_Tp>
3050fca6ea1SDimitry Andric struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
3060fca6ea1SDimitry Andric   using __base = __atomic_ref_base<_Tp>;
3070fca6ea1SDimitry Andric 
30881e300dfSDimitry Andric   using difference_type = typename __base::value_type;
3090fca6ea1SDimitry Andric 
3100fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit atomic_ref(_Tp& __obj) : __base(__obj) {
3110fca6ea1SDimitry Andric     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
3120fca6ea1SDimitry Andric         reinterpret_cast<uintptr_t>(std::addressof(__obj)) % __base::required_alignment == 0,
3130fca6ea1SDimitry Andric         "atomic_ref ctor: referenced object must be aligned to required_alignment");
3140fca6ea1SDimitry Andric   }
3150fca6ea1SDimitry Andric 
3160fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI atomic_ref(const atomic_ref&) noexcept = default;
3170fca6ea1SDimitry Andric 
3180fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept { return __base::operator=(__desired); }
3190fca6ea1SDimitry Andric 
3200fca6ea1SDimitry Andric   atomic_ref& operator=(const atomic_ref&) = delete;
3210fca6ea1SDimitry Andric 
3220fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
3230fca6ea1SDimitry Andric     _Tp __old = this->load(memory_order_relaxed);
3240fca6ea1SDimitry Andric     _Tp __new = __old + __arg;
3250fca6ea1SDimitry Andric     while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) {
3260fca6ea1SDimitry Andric       __new = __old + __arg;
3270fca6ea1SDimitry Andric     }
3280fca6ea1SDimitry Andric     return __old;
3290fca6ea1SDimitry Andric   }
3300fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
3310fca6ea1SDimitry Andric     _Tp __old = this->load(memory_order_relaxed);
3320fca6ea1SDimitry Andric     _Tp __new = __old - __arg;
3330fca6ea1SDimitry Andric     while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) {
3340fca6ea1SDimitry Andric       __new = __old - __arg;
3350fca6ea1SDimitry Andric     }
3360fca6ea1SDimitry Andric     return __old;
3370fca6ea1SDimitry Andric   }
3380fca6ea1SDimitry Andric 
3390fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __arg) const noexcept { return fetch_add(__arg) + __arg; }
3400fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __arg) const noexcept { return fetch_sub(__arg) - __arg; }
3410fca6ea1SDimitry Andric };
3420fca6ea1SDimitry Andric 
3430fca6ea1SDimitry Andric template <class _Tp>
3440fca6ea1SDimitry Andric struct atomic_ref<_Tp*> : public __atomic_ref_base<_Tp*> {
3450fca6ea1SDimitry Andric   using __base = __atomic_ref_base<_Tp*>;
3460fca6ea1SDimitry Andric 
3470fca6ea1SDimitry Andric   using difference_type = ptrdiff_t;
3480fca6ea1SDimitry Andric 
3490fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit atomic_ref(_Tp*& __ptr) : __base(__ptr) {}
3500fca6ea1SDimitry Andric 
3510fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* operator=(_Tp* __desired) const noexcept { return __base::operator=(__desired); }
3520fca6ea1SDimitry Andric 
3530fca6ea1SDimitry Andric   atomic_ref& operator=(const atomic_ref&) = delete;
3540fca6ea1SDimitry Andric 
3550fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* fetch_add(ptrdiff_t __arg, memory_order __order = memory_order_seq_cst) const noexcept {
3560fca6ea1SDimitry Andric     return __atomic_fetch_add(this->__ptr_, __arg * sizeof(_Tp), std::__to_gcc_order(__order));
3570fca6ea1SDimitry Andric   }
3580fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* fetch_sub(ptrdiff_t __arg, memory_order __order = memory_order_seq_cst) const noexcept {
3590fca6ea1SDimitry Andric     return __atomic_fetch_sub(this->__ptr_, __arg * sizeof(_Tp), std::__to_gcc_order(__order));
3600fca6ea1SDimitry Andric   }
3610fca6ea1SDimitry Andric 
3620fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* operator++(int) const noexcept { return fetch_add(1); }
3630fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* operator--(int) const noexcept { return fetch_sub(1); }
3640fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* operator++() const noexcept { return fetch_add(1) + 1; }
3650fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* operator--() const noexcept { return fetch_sub(1) - 1; }
3660fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* operator+=(ptrdiff_t __arg) const noexcept { return fetch_add(__arg) + __arg; }
3670fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _Tp* operator-=(ptrdiff_t __arg) const noexcept { return fetch_sub(__arg) - __arg; }
3680fca6ea1SDimitry Andric };
3690fca6ea1SDimitry Andric 
3700fca6ea1SDimitry Andric _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(atomic_ref);
3710fca6ea1SDimitry Andric 
3720fca6ea1SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
3730fca6ea1SDimitry Andric 
3740fca6ea1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
3750fca6ea1SDimitry Andric 
3760fca6ea1SDimitry Andric _LIBCPP_POP_MACROS
3770fca6ea1SDimitry Andric 
3780fca6ea1SDimitry Andric #endif // _LIBCPP__ATOMIC_ATOMIC_REF_H
379