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 450fca6ea1SDimitry Andric template <class _Tp> 460fca6ea1SDimitry Andric struct __atomic_ref_base { 470fca6ea1SDimitry Andric protected: 480fca6ea1SDimitry Andric _Tp* __ptr_; 490fca6ea1SDimitry Andric 500fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI __atomic_ref_base(_Tp& __obj) : __ptr_(std::addressof(__obj)) {} 510fca6ea1SDimitry Andric 520fca6ea1SDimitry Andric private: 530fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI static _Tp* __clear_padding(_Tp& __val) noexcept { 540fca6ea1SDimitry Andric _Tp* __ptr = std::addressof(__val); 550fca6ea1SDimitry Andric # if __has_builtin(__builtin_clear_padding) 560fca6ea1SDimitry Andric __builtin_clear_padding(__ptr); 570fca6ea1SDimitry Andric # endif 580fca6ea1SDimitry Andric return __ptr; 590fca6ea1SDimitry Andric } 600fca6ea1SDimitry Andric 610fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI static bool __compare_exchange( 620fca6ea1SDimitry Andric _Tp* __ptr, _Tp* __expected, _Tp* __desired, bool __is_weak, int __success, int __failure) noexcept { 630fca6ea1SDimitry Andric if constexpr ( 640fca6ea1SDimitry Andric # if __has_builtin(__builtin_clear_padding) 650fca6ea1SDimitry Andric has_unique_object_representations_v<_Tp> || floating_point<_Tp> 660fca6ea1SDimitry Andric # else 670fca6ea1SDimitry Andric true // NOLINT(readability-simplify-boolean-expr) 680fca6ea1SDimitry Andric # endif 690fca6ea1SDimitry Andric ) { 700fca6ea1SDimitry Andric return __atomic_compare_exchange(__ptr, __expected, __desired, __is_weak, __success, __failure); 710fca6ea1SDimitry Andric } else { // _Tp has padding bits and __builtin_clear_padding is available 720fca6ea1SDimitry Andric __clear_padding(*__desired); 730fca6ea1SDimitry Andric _Tp __copy = *__expected; 740fca6ea1SDimitry Andric __clear_padding(__copy); 750fca6ea1SDimitry Andric // The algorithm we use here is basically to perform `__atomic_compare_exchange` on the 760fca6ea1SDimitry Andric // values until it has either succeeded, or failed because the value representation of the 770fca6ea1SDimitry Andric // objects involved was different. This is why we loop around __atomic_compare_exchange: 780fca6ea1SDimitry Andric // we basically loop until its failure is caused by the value representation of the objects 790fca6ea1SDimitry Andric // being different, not only their object representation. 800fca6ea1SDimitry Andric while (true) { 810fca6ea1SDimitry Andric _Tp __prev = __copy; 820fca6ea1SDimitry Andric if (__atomic_compare_exchange(__ptr, std::addressof(__copy), __desired, __is_weak, __success, __failure)) { 830fca6ea1SDimitry Andric return true; 840fca6ea1SDimitry Andric } 850fca6ea1SDimitry Andric _Tp __curr = __copy; 860fca6ea1SDimitry Andric if (std::memcmp(__clear_padding(__prev), __clear_padding(__curr), sizeof(_Tp)) != 0) { 870fca6ea1SDimitry Andric // Value representation without padding bits do not compare equal -> 880fca6ea1SDimitry Andric // write the current content of *ptr into *expected 890fca6ea1SDimitry Andric std::memcpy(__expected, std::addressof(__copy), sizeof(_Tp)); 900fca6ea1SDimitry Andric return false; 910fca6ea1SDimitry Andric } 920fca6ea1SDimitry Andric } 930fca6ea1SDimitry Andric } 940fca6ea1SDimitry Andric } 950fca6ea1SDimitry Andric 960fca6ea1SDimitry Andric friend struct __atomic_waitable_traits<__atomic_ref_base<_Tp>>; 970fca6ea1SDimitry Andric 980fca6ea1SDimitry Andric public: 990fca6ea1SDimitry Andric using value_type = _Tp; 1000fca6ea1SDimitry Andric 1010fca6ea1SDimitry Andric static constexpr size_t required_alignment = alignof(_Tp); 1020fca6ea1SDimitry Andric 1030fca6ea1SDimitry Andric // The __atomic_always_lock_free builtin takes into account the alignment of the pointer if provided, 1040fca6ea1SDimitry Andric // so we create a fake pointer with a suitable alignment when querying it. Note that we are guaranteed 1050fca6ea1SDimitry Andric // that the pointer is going to be aligned properly at runtime because that is a (checked) precondition 1060fca6ea1SDimitry Andric // of atomic_ref's constructor. 1070fca6ea1SDimitry Andric static constexpr bool is_always_lock_free = 1080fca6ea1SDimitry Andric __atomic_always_lock_free(sizeof(_Tp), reinterpret_cast<void*>(-required_alignment)); 1090fca6ea1SDimitry Andric 1100fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI bool is_lock_free() const noexcept { return __atomic_is_lock_free(sizeof(_Tp), __ptr_); } 1110fca6ea1SDimitry Andric 1120fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI void store(_Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept 1130fca6ea1SDimitry Andric _LIBCPP_CHECK_STORE_MEMORY_ORDER(__order) { 1140fca6ea1SDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 1150fca6ea1SDimitry Andric __order == memory_order::relaxed || __order == memory_order::release || __order == memory_order::seq_cst, 1160fca6ea1SDimitry Andric "atomic_ref: memory order argument to atomic store operation is invalid"); 1170fca6ea1SDimitry Andric __atomic_store(__ptr_, __clear_padding(__desired), std::__to_gcc_order(__order)); 1180fca6ea1SDimitry Andric } 1190fca6ea1SDimitry Andric 1200fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept { 1210fca6ea1SDimitry Andric store(__desired); 1220fca6ea1SDimitry Andric return __desired; 1230fca6ea1SDimitry Andric } 1240fca6ea1SDimitry Andric 1250fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp load(memory_order __order = memory_order::seq_cst) const noexcept 1260fca6ea1SDimitry Andric _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__order) { 1270fca6ea1SDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 1280fca6ea1SDimitry Andric __order == memory_order::relaxed || __order == memory_order::consume || __order == memory_order::acquire || 1290fca6ea1SDimitry Andric __order == memory_order::seq_cst, 1300fca6ea1SDimitry Andric "atomic_ref: memory order argument to atomic load operation is invalid"); 1310fca6ea1SDimitry Andric alignas(_Tp) byte __mem[sizeof(_Tp)]; 1320fca6ea1SDimitry Andric auto* __ret = reinterpret_cast<_Tp*>(__mem); 1330fca6ea1SDimitry Andric __atomic_load(__ptr_, __ret, std::__to_gcc_order(__order)); 1340fca6ea1SDimitry Andric return *__ret; 1350fca6ea1SDimitry Andric } 1360fca6ea1SDimitry Andric 1370fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI operator _Tp() const noexcept { return load(); } 1380fca6ea1SDimitry Andric 1390fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp exchange(_Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept { 1400fca6ea1SDimitry Andric alignas(_Tp) byte __mem[sizeof(_Tp)]; 1410fca6ea1SDimitry Andric auto* __ret = reinterpret_cast<_Tp*>(__mem); 1420fca6ea1SDimitry Andric __atomic_exchange(__ptr_, __clear_padding(__desired), __ret, std::__to_gcc_order(__order)); 1430fca6ea1SDimitry Andric return *__ret; 1440fca6ea1SDimitry Andric } 1450fca6ea1SDimitry Andric 1460fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI bool 1470fca6ea1SDimitry Andric compare_exchange_weak(_Tp& __expected, _Tp __desired, memory_order __success, memory_order __failure) const noexcept 1480fca6ea1SDimitry Andric _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__success, __failure) { 1490fca6ea1SDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 1500fca6ea1SDimitry Andric __failure == memory_order::relaxed || __failure == memory_order::consume || 1510fca6ea1SDimitry Andric __failure == memory_order::acquire || __failure == memory_order::seq_cst, 1520fca6ea1SDimitry Andric "atomic_ref: failure memory order argument to weak atomic compare-and-exchange operation is invalid"); 1530fca6ea1SDimitry Andric return __compare_exchange( 1540fca6ea1SDimitry Andric __ptr_, 1550fca6ea1SDimitry Andric std::addressof(__expected), 1560fca6ea1SDimitry Andric std::addressof(__desired), 1570fca6ea1SDimitry Andric true, 1580fca6ea1SDimitry Andric std::__to_gcc_order(__success), 1590fca6ea1SDimitry Andric std::__to_gcc_order(__failure)); 1600fca6ea1SDimitry Andric } 1610fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI bool 1620fca6ea1SDimitry Andric compare_exchange_strong(_Tp& __expected, _Tp __desired, memory_order __success, memory_order __failure) const noexcept 1630fca6ea1SDimitry Andric _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__success, __failure) { 1640fca6ea1SDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 1650fca6ea1SDimitry Andric __failure == memory_order::relaxed || __failure == memory_order::consume || 1660fca6ea1SDimitry Andric __failure == memory_order::acquire || __failure == memory_order::seq_cst, 1670fca6ea1SDimitry Andric "atomic_ref: failure memory order argument to strong atomic compare-and-exchange operation is invalid"); 1680fca6ea1SDimitry Andric return __compare_exchange( 1690fca6ea1SDimitry Andric __ptr_, 1700fca6ea1SDimitry Andric std::addressof(__expected), 1710fca6ea1SDimitry Andric std::addressof(__desired), 1720fca6ea1SDimitry Andric false, 1730fca6ea1SDimitry Andric std::__to_gcc_order(__success), 1740fca6ea1SDimitry Andric std::__to_gcc_order(__failure)); 1750fca6ea1SDimitry Andric } 1760fca6ea1SDimitry Andric 1770fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI bool 1780fca6ea1SDimitry Andric compare_exchange_weak(_Tp& __expected, _Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept { 1790fca6ea1SDimitry Andric return __compare_exchange( 1800fca6ea1SDimitry Andric __ptr_, 1810fca6ea1SDimitry Andric std::addressof(__expected), 1820fca6ea1SDimitry Andric std::addressof(__desired), 1830fca6ea1SDimitry Andric true, 1840fca6ea1SDimitry Andric std::__to_gcc_order(__order), 1850fca6ea1SDimitry Andric std::__to_gcc_failure_order(__order)); 1860fca6ea1SDimitry Andric } 1870fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI bool 1880fca6ea1SDimitry Andric compare_exchange_strong(_Tp& __expected, _Tp __desired, memory_order __order = memory_order::seq_cst) const noexcept { 1890fca6ea1SDimitry Andric return __compare_exchange( 1900fca6ea1SDimitry Andric __ptr_, 1910fca6ea1SDimitry Andric std::addressof(__expected), 1920fca6ea1SDimitry Andric std::addressof(__desired), 1930fca6ea1SDimitry Andric false, 1940fca6ea1SDimitry Andric std::__to_gcc_order(__order), 1950fca6ea1SDimitry Andric std::__to_gcc_failure_order(__order)); 1960fca6ea1SDimitry Andric } 1970fca6ea1SDimitry Andric 1980fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI void wait(_Tp __old, memory_order __order = memory_order::seq_cst) const noexcept 1990fca6ea1SDimitry Andric _LIBCPP_CHECK_WAIT_MEMORY_ORDER(__order) { 2000fca6ea1SDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 2010fca6ea1SDimitry Andric __order == memory_order::relaxed || __order == memory_order::consume || __order == memory_order::acquire || 2020fca6ea1SDimitry Andric __order == memory_order::seq_cst, 2030fca6ea1SDimitry Andric "atomic_ref: memory order argument to atomic wait operation is invalid"); 2040fca6ea1SDimitry Andric std::__atomic_wait(*this, __old, __order); 2050fca6ea1SDimitry Andric } 2060fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI void notify_one() const noexcept { std::__atomic_notify_one(*this); } 2070fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI void notify_all() const noexcept { std::__atomic_notify_all(*this); } 2080fca6ea1SDimitry Andric }; 2090fca6ea1SDimitry Andric 2100fca6ea1SDimitry Andric template <class _Tp> 2110fca6ea1SDimitry Andric struct __atomic_waitable_traits<__atomic_ref_base<_Tp>> { 2120fca6ea1SDimitry Andric static _LIBCPP_HIDE_FROM_ABI _Tp __atomic_load(const __atomic_ref_base<_Tp>& __a, memory_order __order) { 2130fca6ea1SDimitry Andric return __a.load(__order); 2140fca6ea1SDimitry Andric } 2150fca6ea1SDimitry Andric static _LIBCPP_HIDE_FROM_ABI const _Tp* __atomic_contention_address(const __atomic_ref_base<_Tp>& __a) { 2160fca6ea1SDimitry Andric return __a.__ptr_; 2170fca6ea1SDimitry Andric } 2180fca6ea1SDimitry Andric }; 2190fca6ea1SDimitry Andric 2200fca6ea1SDimitry Andric template <class _Tp> 2210fca6ea1SDimitry Andric struct atomic_ref : public __atomic_ref_base<_Tp> { 2220fca6ea1SDimitry Andric static_assert(is_trivially_copyable_v<_Tp>, "std::atomic_ref<T> requires that 'T' be a trivially copyable type"); 2230fca6ea1SDimitry Andric 2240fca6ea1SDimitry Andric using __base = __atomic_ref_base<_Tp>; 2250fca6ea1SDimitry Andric 2260fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit atomic_ref(_Tp& __obj) : __base(__obj) { 2270fca6ea1SDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 2280fca6ea1SDimitry Andric reinterpret_cast<uintptr_t>(std::addressof(__obj)) % __base::required_alignment == 0, 2290fca6ea1SDimitry Andric "atomic_ref ctor: referenced object must be aligned to required_alignment"); 2300fca6ea1SDimitry Andric } 2310fca6ea1SDimitry Andric 2320fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI atomic_ref(const atomic_ref&) noexcept = default; 2330fca6ea1SDimitry Andric 2340fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept { return __base::operator=(__desired); } 2350fca6ea1SDimitry Andric 2360fca6ea1SDimitry Andric atomic_ref& operator=(const atomic_ref&) = delete; 2370fca6ea1SDimitry Andric }; 2380fca6ea1SDimitry Andric 2390fca6ea1SDimitry Andric template <class _Tp> 2400fca6ea1SDimitry Andric requires(std::integral<_Tp> && !std::same_as<bool, _Tp>) 2410fca6ea1SDimitry Andric struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> { 2420fca6ea1SDimitry Andric using __base = __atomic_ref_base<_Tp>; 2430fca6ea1SDimitry Andric 244*81e300dfSDimitry Andric using difference_type = typename __base::value_type; 2450fca6ea1SDimitry Andric 2460fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit atomic_ref(_Tp& __obj) : __base(__obj) { 2470fca6ea1SDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 2480fca6ea1SDimitry Andric reinterpret_cast<uintptr_t>(std::addressof(__obj)) % __base::required_alignment == 0, 2490fca6ea1SDimitry Andric "atomic_ref ctor: referenced object must be aligned to required_alignment"); 2500fca6ea1SDimitry Andric } 2510fca6ea1SDimitry Andric 2520fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI atomic_ref(const atomic_ref&) noexcept = default; 2530fca6ea1SDimitry Andric 2540fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept { return __base::operator=(__desired); } 2550fca6ea1SDimitry Andric 2560fca6ea1SDimitry Andric atomic_ref& operator=(const atomic_ref&) = delete; 2570fca6ea1SDimitry Andric 2580fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept { 2590fca6ea1SDimitry Andric return __atomic_fetch_add(this->__ptr_, __arg, std::__to_gcc_order(__order)); 2600fca6ea1SDimitry Andric } 2610fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept { 2620fca6ea1SDimitry Andric return __atomic_fetch_sub(this->__ptr_, __arg, std::__to_gcc_order(__order)); 2630fca6ea1SDimitry Andric } 2640fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp fetch_and(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept { 2650fca6ea1SDimitry Andric return __atomic_fetch_and(this->__ptr_, __arg, std::__to_gcc_order(__order)); 2660fca6ea1SDimitry Andric } 2670fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp fetch_or(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept { 2680fca6ea1SDimitry Andric return __atomic_fetch_or(this->__ptr_, __arg, std::__to_gcc_order(__order)); 2690fca6ea1SDimitry Andric } 2700fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept { 2710fca6ea1SDimitry Andric return __atomic_fetch_xor(this->__ptr_, __arg, std::__to_gcc_order(__order)); 2720fca6ea1SDimitry Andric } 2730fca6ea1SDimitry Andric 2740fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator++(int) const noexcept { return fetch_add(_Tp(1)); } 2750fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator--(int) const noexcept { return fetch_sub(_Tp(1)); } 2760fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator++() const noexcept { return fetch_add(_Tp(1)) + _Tp(1); } 2770fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator--() const noexcept { return fetch_sub(_Tp(1)) - _Tp(1); } 2780fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __arg) const noexcept { return fetch_add(__arg) + __arg; } 2790fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __arg) const noexcept { return fetch_sub(__arg) - __arg; } 2800fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator&=(_Tp __arg) const noexcept { return fetch_and(__arg) & __arg; } 2810fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator|=(_Tp __arg) const noexcept { return fetch_or(__arg) | __arg; } 2820fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator^=(_Tp __arg) const noexcept { return fetch_xor(__arg) ^ __arg; } 2830fca6ea1SDimitry Andric }; 2840fca6ea1SDimitry Andric 2850fca6ea1SDimitry Andric template <class _Tp> 2860fca6ea1SDimitry Andric requires std::floating_point<_Tp> 2870fca6ea1SDimitry Andric struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> { 2880fca6ea1SDimitry Andric using __base = __atomic_ref_base<_Tp>; 2890fca6ea1SDimitry Andric 290*81e300dfSDimitry Andric using difference_type = typename __base::value_type; 2910fca6ea1SDimitry Andric 2920fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit atomic_ref(_Tp& __obj) : __base(__obj) { 2930fca6ea1SDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN( 2940fca6ea1SDimitry Andric reinterpret_cast<uintptr_t>(std::addressof(__obj)) % __base::required_alignment == 0, 2950fca6ea1SDimitry Andric "atomic_ref ctor: referenced object must be aligned to required_alignment"); 2960fca6ea1SDimitry Andric } 2970fca6ea1SDimitry Andric 2980fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI atomic_ref(const atomic_ref&) noexcept = default; 2990fca6ea1SDimitry Andric 3000fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator=(_Tp __desired) const noexcept { return __base::operator=(__desired); } 3010fca6ea1SDimitry Andric 3020fca6ea1SDimitry Andric atomic_ref& operator=(const atomic_ref&) = delete; 3030fca6ea1SDimitry Andric 3040fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept { 3050fca6ea1SDimitry Andric _Tp __old = this->load(memory_order_relaxed); 3060fca6ea1SDimitry Andric _Tp __new = __old + __arg; 3070fca6ea1SDimitry Andric while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) { 3080fca6ea1SDimitry Andric __new = __old + __arg; 3090fca6ea1SDimitry Andric } 3100fca6ea1SDimitry Andric return __old; 3110fca6ea1SDimitry Andric } 3120fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept { 3130fca6ea1SDimitry Andric _Tp __old = this->load(memory_order_relaxed); 3140fca6ea1SDimitry Andric _Tp __new = __old - __arg; 3150fca6ea1SDimitry Andric while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) { 3160fca6ea1SDimitry Andric __new = __old - __arg; 3170fca6ea1SDimitry Andric } 3180fca6ea1SDimitry Andric return __old; 3190fca6ea1SDimitry Andric } 3200fca6ea1SDimitry Andric 3210fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __arg) const noexcept { return fetch_add(__arg) + __arg; } 3220fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __arg) const noexcept { return fetch_sub(__arg) - __arg; } 3230fca6ea1SDimitry Andric }; 3240fca6ea1SDimitry Andric 3250fca6ea1SDimitry Andric template <class _Tp> 3260fca6ea1SDimitry Andric struct atomic_ref<_Tp*> : public __atomic_ref_base<_Tp*> { 3270fca6ea1SDimitry Andric using __base = __atomic_ref_base<_Tp*>; 3280fca6ea1SDimitry Andric 3290fca6ea1SDimitry Andric using difference_type = ptrdiff_t; 3300fca6ea1SDimitry Andric 3310fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit atomic_ref(_Tp*& __ptr) : __base(__ptr) {} 3320fca6ea1SDimitry Andric 3330fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* operator=(_Tp* __desired) const noexcept { return __base::operator=(__desired); } 3340fca6ea1SDimitry Andric 3350fca6ea1SDimitry Andric atomic_ref& operator=(const atomic_ref&) = delete; 3360fca6ea1SDimitry Andric 3370fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* fetch_add(ptrdiff_t __arg, memory_order __order = memory_order_seq_cst) const noexcept { 3380fca6ea1SDimitry Andric return __atomic_fetch_add(this->__ptr_, __arg * sizeof(_Tp), std::__to_gcc_order(__order)); 3390fca6ea1SDimitry Andric } 3400fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* fetch_sub(ptrdiff_t __arg, memory_order __order = memory_order_seq_cst) const noexcept { 3410fca6ea1SDimitry Andric return __atomic_fetch_sub(this->__ptr_, __arg * sizeof(_Tp), std::__to_gcc_order(__order)); 3420fca6ea1SDimitry Andric } 3430fca6ea1SDimitry Andric 3440fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* operator++(int) const noexcept { return fetch_add(1); } 3450fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* operator--(int) const noexcept { return fetch_sub(1); } 3460fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* operator++() const noexcept { return fetch_add(1) + 1; } 3470fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* operator--() const noexcept { return fetch_sub(1) - 1; } 3480fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* operator+=(ptrdiff_t __arg) const noexcept { return fetch_add(__arg) + __arg; } 3490fca6ea1SDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp* operator-=(ptrdiff_t __arg) const noexcept { return fetch_sub(__arg) - __arg; } 3500fca6ea1SDimitry Andric }; 3510fca6ea1SDimitry Andric 3520fca6ea1SDimitry Andric _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(atomic_ref); 3530fca6ea1SDimitry Andric 3540fca6ea1SDimitry Andric #endif // _LIBCPP_STD_VER >= 20 3550fca6ea1SDimitry Andric 3560fca6ea1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 3570fca6ea1SDimitry Andric 3580fca6ea1SDimitry Andric _LIBCPP_POP_MACROS 3590fca6ea1SDimitry Andric 3600fca6ea1SDimitry Andric #endif // _LIBCPP__ATOMIC_ATOMIC_REF_H 361