xref: /freebsd/contrib/llvm-project/libcxx/include/__atomic/atomic_base.h (revision 02e9120893770924227138ba49df1edb3896112a)
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___ATOMIC_ATOMIC_BASE_H
10 #define _LIBCPP___ATOMIC_ATOMIC_BASE_H
11 
12 #include <__atomic/atomic_sync.h>
13 #include <__atomic/check_memory_order.h>
14 #include <__atomic/cxx_atomic_impl.h>
15 #include <__atomic/is_always_lock_free.h>
16 #include <__atomic/memory_order.h>
17 #include <__availability>
18 #include <__config>
19 #include <__memory/addressof.h>
20 #include <__type_traits/is_integral.h>
21 #include <__type_traits/is_nothrow_default_constructible.h>
22 #include <__type_traits/is_same.h>
23 #include <version>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _Tp, bool = is_integral<_Tp>::value && !is_same<_Tp, bool>::value>
32 struct __atomic_base  // false
33 {
34     mutable __cxx_atomic_impl<_Tp> __a_;
35 
36 #if _LIBCPP_STD_VER >= 17
37   static _LIBCPP_CONSTEXPR bool is_always_lock_free = __libcpp_is_always_lock_free<__cxx_atomic_impl<_Tp> >::__value;
38 #endif
39 
40     _LIBCPP_HIDE_FROM_ABI
41     bool is_lock_free() const volatile _NOEXCEPT
42         {return __cxx_atomic_is_lock_free(sizeof(_Tp));}
43     _LIBCPP_HIDE_FROM_ABI
44     bool is_lock_free() const _NOEXCEPT
45         {return static_cast<__atomic_base const volatile*>(this)->is_lock_free();}
46     _LIBCPP_HIDE_FROM_ABI void store(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT
47         _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) {
48       std::__cxx_atomic_store(std::addressof(__a_), __d, __m);
49     }
50     _LIBCPP_HIDE_FROM_ABI void store(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT
51         _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) {
52       std::__cxx_atomic_store(std::addressof(__a_), __d, __m);
53     }
54     _LIBCPP_HIDE_FROM_ABI _Tp load(memory_order __m = memory_order_seq_cst) const volatile _NOEXCEPT
55         _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) {
56       return std::__cxx_atomic_load(std::addressof(__a_), __m);
57     }
58     _LIBCPP_HIDE_FROM_ABI _Tp load(memory_order __m = memory_order_seq_cst) const _NOEXCEPT
59         _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) {
60       return std::__cxx_atomic_load(std::addressof(__a_), __m);
61     }
62     _LIBCPP_HIDE_FROM_ABI
63     operator _Tp() const volatile _NOEXCEPT {return load();}
64     _LIBCPP_HIDE_FROM_ABI
65     operator _Tp() const _NOEXCEPT          {return load();}
66     _LIBCPP_HIDE_FROM_ABI _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
67       return std::__cxx_atomic_exchange(std::addressof(__a_), __d, __m);
68     }
69     _LIBCPP_HIDE_FROM_ABI _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
70       return std::__cxx_atomic_exchange(std::addressof(__a_), __d, __m);
71     }
72     _LIBCPP_HIDE_FROM_ABI bool
73     compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) volatile _NOEXCEPT
74         _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) {
75       return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __s, __f);
76     }
77     _LIBCPP_HIDE_FROM_ABI bool compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) _NOEXCEPT
78         _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) {
79       return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __s, __f);
80     }
81     _LIBCPP_HIDE_FROM_ABI bool
82     compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) volatile _NOEXCEPT
83         _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) {
84       return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __s, __f);
85     }
86     _LIBCPP_HIDE_FROM_ABI bool compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __s, memory_order __f) _NOEXCEPT
87         _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) {
88       return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __s, __f);
89     }
90     _LIBCPP_HIDE_FROM_ABI bool
91     compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
92       return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
93     }
94     _LIBCPP_HIDE_FROM_ABI bool
95     compare_exchange_weak(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
96       return std::__cxx_atomic_compare_exchange_weak(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
97     }
98     _LIBCPP_HIDE_FROM_ABI bool
99     compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
100       return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
101     }
102     _LIBCPP_HIDE_FROM_ABI bool
103     compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
104       return std::__cxx_atomic_compare_exchange_strong(std::addressof(__a_), std::addressof(__e), __d, __m, __m);
105     }
106 
107     _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void wait(_Tp __v, memory_order __m = memory_order_seq_cst) const
108         volatile _NOEXCEPT {
109       std::__cxx_atomic_wait(std::addressof(__a_), __v, __m);
110     }
111     _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
112     wait(_Tp __v, memory_order __m = memory_order_seq_cst) const _NOEXCEPT {
113       std::__cxx_atomic_wait(std::addressof(__a_), __v, __m);
114     }
115     _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() volatile _NOEXCEPT {
116       std::__cxx_atomic_notify_one(std::addressof(__a_));
117     }
118     _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT {
119       std::__cxx_atomic_notify_one(std::addressof(__a_));
120     }
121     _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() volatile _NOEXCEPT {
122       std::__cxx_atomic_notify_all(std::addressof(__a_));
123     }
124     _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT {
125       std::__cxx_atomic_notify_all(std::addressof(__a_));
126     }
127 
128 #if _LIBCPP_STD_VER >= 20
129     _LIBCPP_HIDE_FROM_ABI constexpr
130     __atomic_base() noexcept(is_nothrow_default_constructible_v<_Tp>) : __a_(_Tp()) {}
131 #else
132     _LIBCPP_HIDE_FROM_ABI
133     __atomic_base() _NOEXCEPT = default;
134 #endif
135 
136     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
137     __atomic_base(_Tp __d) _NOEXCEPT : __a_(__d) {}
138 
139     __atomic_base(const __atomic_base&) = delete;
140 };
141 
142 #if _LIBCPP_STD_VER >= 17
143 template <class _Tp, bool __b>
144 _LIBCPP_CONSTEXPR bool __atomic_base<_Tp, __b>::is_always_lock_free;
145 #endif
146 
147 // atomic<Integral>
148 
149 template <class _Tp>
150 struct __atomic_base<_Tp, true>
151     : public __atomic_base<_Tp, false>
152 {
153     using __base = __atomic_base<_Tp, false>;
154 
155     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
156     __atomic_base() _NOEXCEPT = default;
157 
158     _LIBCPP_HIDE_FROM_ABI
159     _LIBCPP_CONSTEXPR __atomic_base(_Tp __d) _NOEXCEPT : __base(__d) {}
160 
161     _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
162       return std::__cxx_atomic_fetch_add(std::addressof(this->__a_), __op, __m);
163     }
164     _LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
165       return std::__cxx_atomic_fetch_add(std::addressof(this->__a_), __op, __m);
166     }
167     _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
168       return std::__cxx_atomic_fetch_sub(std::addressof(this->__a_), __op, __m);
169     }
170     _LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
171       return std::__cxx_atomic_fetch_sub(std::addressof(this->__a_), __op, __m);
172     }
173     _LIBCPP_HIDE_FROM_ABI _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
174       return std::__cxx_atomic_fetch_and(std::addressof(this->__a_), __op, __m);
175     }
176     _LIBCPP_HIDE_FROM_ABI _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
177       return std::__cxx_atomic_fetch_and(std::addressof(this->__a_), __op, __m);
178     }
179     _LIBCPP_HIDE_FROM_ABI _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
180       return std::__cxx_atomic_fetch_or(std::addressof(this->__a_), __op, __m);
181     }
182     _LIBCPP_HIDE_FROM_ABI _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
183       return std::__cxx_atomic_fetch_or(std::addressof(this->__a_), __op, __m);
184     }
185     _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) volatile _NOEXCEPT {
186       return std::__cxx_atomic_fetch_xor(std::addressof(this->__a_), __op, __m);
187     }
188     _LIBCPP_HIDE_FROM_ABI _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) _NOEXCEPT {
189       return std::__cxx_atomic_fetch_xor(std::addressof(this->__a_), __op, __m);
190     }
191 
192     _LIBCPP_HIDE_FROM_ABI
193     _Tp operator++(int) volatile _NOEXCEPT      {return fetch_add(_Tp(1));}
194     _LIBCPP_HIDE_FROM_ABI
195     _Tp operator++(int) _NOEXCEPT               {return fetch_add(_Tp(1));}
196     _LIBCPP_HIDE_FROM_ABI
197     _Tp operator--(int) volatile _NOEXCEPT      {return fetch_sub(_Tp(1));}
198     _LIBCPP_HIDE_FROM_ABI
199     _Tp operator--(int) _NOEXCEPT               {return fetch_sub(_Tp(1));}
200     _LIBCPP_HIDE_FROM_ABI
201     _Tp operator++() volatile _NOEXCEPT         {return fetch_add(_Tp(1)) + _Tp(1);}
202     _LIBCPP_HIDE_FROM_ABI
203     _Tp operator++() _NOEXCEPT                  {return fetch_add(_Tp(1)) + _Tp(1);}
204     _LIBCPP_HIDE_FROM_ABI
205     _Tp operator--() volatile _NOEXCEPT         {return fetch_sub(_Tp(1)) - _Tp(1);}
206     _LIBCPP_HIDE_FROM_ABI
207     _Tp operator--() _NOEXCEPT                  {return fetch_sub(_Tp(1)) - _Tp(1);}
208     _LIBCPP_HIDE_FROM_ABI
209     _Tp operator+=(_Tp __op) volatile _NOEXCEPT {return fetch_add(__op) + __op;}
210     _LIBCPP_HIDE_FROM_ABI
211     _Tp operator+=(_Tp __op) _NOEXCEPT          {return fetch_add(__op) + __op;}
212     _LIBCPP_HIDE_FROM_ABI
213     _Tp operator-=(_Tp __op) volatile _NOEXCEPT {return fetch_sub(__op) - __op;}
214     _LIBCPP_HIDE_FROM_ABI
215     _Tp operator-=(_Tp __op) _NOEXCEPT          {return fetch_sub(__op) - __op;}
216     _LIBCPP_HIDE_FROM_ABI
217     _Tp operator&=(_Tp __op) volatile _NOEXCEPT {return fetch_and(__op) & __op;}
218     _LIBCPP_HIDE_FROM_ABI
219     _Tp operator&=(_Tp __op) _NOEXCEPT          {return fetch_and(__op) & __op;}
220     _LIBCPP_HIDE_FROM_ABI
221     _Tp operator|=(_Tp __op) volatile _NOEXCEPT {return fetch_or(__op) | __op;}
222     _LIBCPP_HIDE_FROM_ABI
223     _Tp operator|=(_Tp __op) _NOEXCEPT          {return fetch_or(__op) | __op;}
224     _LIBCPP_HIDE_FROM_ABI
225     _Tp operator^=(_Tp __op) volatile _NOEXCEPT {return fetch_xor(__op) ^ __op;}
226     _LIBCPP_HIDE_FROM_ABI
227     _Tp operator^=(_Tp __op) _NOEXCEPT          {return fetch_xor(__op) ^ __op;}
228 };
229 
230 _LIBCPP_END_NAMESPACE_STD
231 
232 #endif // _LIBCPP___ATOMIC_ATOMIC_BASE_H
233