Warning: This file is not a C or C++ file. It does not have highlighting.
| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___CXX03___ATOMIC_ATOMIC_BASE_H |
| 10 | #define _LIBCPP___CXX03___ATOMIC_ATOMIC_BASE_H |
| 11 | |
| 12 | #include <__cxx03/__atomic/atomic_sync.h> |
| 13 | #include <__cxx03/__atomic/check_memory_order.h> |
| 14 | #include <__cxx03/__atomic/cxx_atomic_impl.h> |
| 15 | #include <__cxx03/__atomic/is_always_lock_free.h> |
| 16 | #include <__cxx03/__atomic/memory_order.h> |
| 17 | #include <__cxx03/__config> |
| 18 | #include <__cxx03/__memory/addressof.h> |
| 19 | #include <__cxx03/__type_traits/is_integral.h> |
| 20 | #include <__cxx03/__type_traits/is_nothrow_constructible.h> |
| 21 | #include <__cxx03/__type_traits/is_same.h> |
| 22 | #include <__cxx03/version> |
| 23 | |
| 24 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 25 | # pragma GCC system_header |
| 26 | #endif |
| 27 | |
| 28 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 29 | |
| 30 | template <class _Tp, bool = is_integral<_Tp>::value && !is_same<_Tp, bool>::value> |
| 31 | struct __atomic_base // false |
| 32 | { |
| 33 | mutable __cxx_atomic_impl<_Tp> __a_; |
| 34 | |
| 35 | _LIBCPP_HIDE_FROM_ABI bool is_lock_free() const volatile _NOEXCEPT { |
| 36 | return __cxx_atomic_is_lock_free(sizeof(__cxx_atomic_impl<_Tp>)); |
| 37 | } |
| 38 | _LIBCPP_HIDE_FROM_ABI bool is_lock_free() const _NOEXCEPT { |
| 39 | return static_cast<__atomic_base const volatile*>(this)->is_lock_free(); |
| 40 | } |
| 41 | _LIBCPP_HIDE_FROM_ABI void store(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT |
| 42 | _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) { |
| 43 | std::__cxx_atomic_store(std::addressof(__a_), __d, __m); |
| 44 | } |
| 45 | _LIBCPP_HIDE_FROM_ABI void store(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT |
| 46 | _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) { |
| 47 | std::__cxx_atomic_store(std::addressof(__a_), __d, __m); |
| 48 | } |
| 49 | _LIBCPP_HIDE_FROM_ABI _Tp load(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT |
| 50 | _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) { |
| 51 | return std::__cxx_atomic_load(std::addressof(__a_), __m); |
| 52 | } |
| 53 | _LIBCPP_HIDE_FROM_ABI _Tp load(memory_order __m = memory_order_seq_cst) const _NOEXCEPT |
| 54 | _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) { |
| 55 | return std::__cxx_atomic_load(std::addressof(__a_), __m); |
| 56 | } |
| 57 | _LIBCPP_HIDE_FROM_ABI operator _Tp() const volatile _NOEXCEPT { return load(); } |
| 58 | _LIBCPP_HIDE_FROM_ABI operator _Tp() const _NOEXCEPT { return load(); } |
| 59 | _LIBCPP_HIDE_FROM_ABI _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT { |
| 60 | return std::__cxx_atomic_exchange(std::addressof(__a_), __d, __m); |
| 61 | } |
| 62 | _LIBCPP_HIDE_FROM_ABI _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT { |
| 63 | return std::__cxx_atomic_exchange(std::addressof(__a_), __d, __m); |
| 64 | } |
| 65 | _LIBCPP_HIDE_FROM_ABI bool |
| 66 | compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) volatile _NOEXCEPT |
| 67 | _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) { |
| 68 | return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __s, __f); |
| 69 | } |
| 70 | _LIBCPP_HIDE_FROM_ABI bool compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) _NOEXCEPT |
| 71 | _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) { |
| 72 | return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __s, __f); |
| 73 | } |
| 74 | _LIBCPP_HIDE_FROM_ABI bool |
| 75 | compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) volatile _NOEXCEPT |
| 76 | _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) { |
| 77 | return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __s, __f); |
| 78 | } |
| 79 | _LIBCPP_HIDE_FROM_ABI bool compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) _NOEXCEPT |
| 80 | _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) { |
| 81 | return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __s, __f); |
| 82 | } |
| 83 | _LIBCPP_HIDE_FROM_ABI bool |
| 84 | compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT { |
| 85 | return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __m, __m); |
| 86 | } |
| 87 | _LIBCPP_HIDE_FROM_ABI bool |
| 88 | compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT { |
| 89 | return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __m, __m); |
| 90 | } |
| 91 | _LIBCPP_HIDE_FROM_ABI bool |
| 92 | compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT { |
| 93 | return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __m, __m); |
| 94 | } |
| 95 | _LIBCPP_HIDE_FROM_ABI bool |
| 96 | compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT { |
| 97 | return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __m, __m); |
| 98 | } |
| 99 | |
| 100 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait(_Tp __v, memory_order __m = memory_order_seq_cst) const |
| 101 | volatile _NOEXCEPT { |
| 102 | std::__atomic_wait(*this, __v, __m); |
| 103 | } |
| 104 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void |
| 105 | wait(_Tp __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT { |
| 106 | std::__atomic_wait(*this, __v, __m); |
| 107 | } |
| 108 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() volatile _NOEXCEPT { |
| 109 | std::__atomic_notify_one(*this); |
| 110 | } |
| 111 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT { std::__atomic_notify_one(*this); } |
| 112 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() volatile _NOEXCEPT { |
| 113 | std::__atomic_notify_all(*this); |
| 114 | } |
| 115 | _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT { std::__atomic_notify_all(*this); } |
| 116 | |
| 117 | _LIBCPP_HIDE_FROM_ABI __atomic_base() _NOEXCEPT = default; |
| 118 | |
| 119 | _LIBCPP_HIDE_FROM_ABI __atomic_base(_Tp __d) _NOEXCEPT : __a_(__d) {} |
| 120 | |
| 121 | __atomic_base(const __atomic_base&) = delete; |
| 122 | }; |
| 123 | |
| 124 | // atomic<Integral> |
| 125 | |
| 126 | template <class _Tp> |
| 127 | struct __atomic_base<_Tp, true> : public __atomic_base<_Tp, false> { |
| 128 | using __base = __atomic_base<_Tp, false>; |
| 129 | |
| 130 | _LIBCPP_HIDE_FROM_ABI __atomic_base() _NOEXCEPT = default; |
| 131 | |
| 132 | _LIBCPP_HIDE_FROM_ABI __atomic_base(_Tp __d) _NOEXCEPT : __base(__d) {} |
| 133 | |
| 134 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT { |
| 135 | return std::__cxx_atomic_fetch_add(std::addressof(this->__a_), __op, __m); |
| 136 | } |
| 137 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT { |
| 138 | return std::__cxx_atomic_fetch_add(std::addressof(this->__a_), __op, __m); |
| 139 | } |
| 140 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT { |
| 141 | return std::__cxx_atomic_fetch_sub(std::addressof(this->__a_), __op, __m); |
| 142 | } |
| 143 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT { |
| 144 | return std::__cxx_atomic_fetch_sub(std::addressof(this->__a_), __op, __m); |
| 145 | } |
| 146 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT { |
| 147 | return std::__cxx_atomic_fetch_and(std::addressof(this->__a_), __op, __m); |
| 148 | } |
| 149 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT { |
| 150 | return std::__cxx_atomic_fetch_and(std::addressof(this->__a_), __op, __m); |
| 151 | } |
| 152 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT { |
| 153 | return std::__cxx_atomic_fetch_or(std::addressof(this->__a_), __op, __m); |
| 154 | } |
| 155 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT { |
| 156 | return std::__cxx_atomic_fetch_or(std::addressof(this->__a_), __op, __m); |
| 157 | } |
| 158 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT { |
| 159 | return std::__cxx_atomic_fetch_xor(std::addressof(this->__a_), __op, __m); |
| 160 | } |
| 161 | _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT { |
| 162 | return std::__cxx_atomic_fetch_xor(std::addressof(this->__a_), __op, __m); |
| 163 | } |
| 164 | |
| 165 | _LIBCPP_HIDE_FROM_ABI _Tp operator++(int) volatile _NOEXCEPT { return fetch_add(_Tp(1)); } |
| 166 | _LIBCPP_HIDE_FROM_ABI _Tp operator++(int) _NOEXCEPT { return fetch_add(_Tp(1)); } |
| 167 | _LIBCPP_HIDE_FROM_ABI _Tp operator--(int) volatile _NOEXCEPT { return fetch_sub(_Tp(1)); } |
| 168 | _LIBCPP_HIDE_FROM_ABI _Tp operator--(int) _NOEXCEPT { return fetch_sub(_Tp(1)); } |
| 169 | _LIBCPP_HIDE_FROM_ABI _Tp operator++() volatile _NOEXCEPT { return fetch_add(_Tp(1)) + _Tp(1); } |
| 170 | _LIBCPP_HIDE_FROM_ABI _Tp operator++() _NOEXCEPT { return fetch_add(_Tp(1)) + _Tp(1); } |
| 171 | _LIBCPP_HIDE_FROM_ABI _Tp operator--() volatile _NOEXCEPT { return fetch_sub(_Tp(1)) - _Tp(1); } |
| 172 | _LIBCPP_HIDE_FROM_ABI _Tp operator--() _NOEXCEPT { return fetch_sub(_Tp(1)) - _Tp(1); } |
| 173 | _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __op) volatile _NOEXCEPT { return fetch_add(__op) + __op; } |
| 174 | _LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __op) _NOEXCEPT { return fetch_add(__op) + __op; } |
| 175 | _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __op) volatile _NOEXCEPT { return fetch_sub(__op) - __op; } |
| 176 | _LIBCPP_HIDE_FROM_ABI _Tp operator-=(_Tp __op) _NOEXCEPT { return fetch_sub(__op) - __op; } |
| 177 | _LIBCPP_HIDE_FROM_ABI _Tp operator&=(_Tp __op) volatile _NOEXCEPT { return fetch_and(__op) & __op; } |
| 178 | _LIBCPP_HIDE_FROM_ABI _Tp operator&=(_Tp __op) _NOEXCEPT { return fetch_and(__op) & __op; } |
| 179 | _LIBCPP_HIDE_FROM_ABI _Tp operator|=(_Tp __op) volatile _NOEXCEPT { return fetch_or(__op) | __op; } |
| 180 | _LIBCPP_HIDE_FROM_ABI _Tp operator|=(_Tp __op) _NOEXCEPT { return fetch_or(__op) | __op; } |
| 181 | _LIBCPP_HIDE_FROM_ABI _Tp operator^=(_Tp __op) volatile _NOEXCEPT { return fetch_xor(__op) ^ __op; } |
| 182 | _LIBCPP_HIDE_FROM_ABI _Tp operator^=(_Tp __op) _NOEXCEPT { return fetch_xor(__op) ^ __op; } |
| 183 | }; |
| 184 | |
| 185 | // Here we need _IsIntegral because the default template argument is not enough |
| 186 | // e.g __atomic_base<int> is __atomic_base<int, true>, which inherits from |
| 187 | // __atomic_base<int, false> and the caller of the wait function is |
| 188 | // __atomic_base<int, false>. So specializing __atomic_base<_Tp> does not work |
| 189 | template <class _Tp, bool _IsIntegral> |
| 190 | struct __atomic_waitable_traits<__atomic_base<_Tp, _IsIntegral> > { |
| 191 | static _LIBCPP_HIDE_FROM_ABI _Tp __atomic_load(const __atomic_base<_Tp, _IsIntegral>& __a, memory_order __order) { |
| 192 | return __a.load(__order); |
| 193 | } |
| 194 | |
| 195 | static _LIBCPP_HIDE_FROM_ABI _Tp |
| 196 | __atomic_load(const volatile __atomic_base<_Tp, _IsIntegral>& __this, memory_order __order) { |
| 197 | return __this.load(__order); |
| 198 | } |
| 199 | |
| 200 | static _LIBCPP_HIDE_FROM_ABI const __cxx_atomic_impl<_Tp>* |
| 201 | __atomic_contention_address(const __atomic_base<_Tp, _IsIntegral>& __a) { |
| 202 | return std::addressof(__a.__a_); |
| 203 | } |
| 204 | |
| 205 | static _LIBCPP_HIDE_FROM_ABI const volatile __cxx_atomic_impl<_Tp>* |
| 206 | __atomic_contention_address(const volatile __atomic_base<_Tp, _IsIntegral>& __this) { |
| 207 | return std::addressof(__this.__a_); |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | _LIBCPP_END_NAMESPACE_STD |
| 212 | |
| 213 | #endif // _LIBCPP___CXX03___ATOMIC_ATOMIC_BASE_H |
| 214 |
Warning: This file is not a C or C++ file. It does not have highlighting.
