1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ATOMIC_SUPPORT_C11_H
10 #define _LIBCPP___ATOMIC_SUPPORT_C11_H
12 #include <__atomic/memory_order.h>
14 #include <__cstddef/ptrdiff_t.h>
15 #include <__memory/addressof.h>
16 #include <__type_traits/remove_const.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
23 // This file implements support for C11-style atomics
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 template <typename _Tp
>
29 struct __cxx_atomic_base_impl
{
31 #ifndef _LIBCPP_CXX03_LANG
32 __cxx_atomic_base_impl() _NOEXCEPT
= default;
34 __cxx_atomic_base_impl() _NOEXCEPT
: __a_value() {
36 #endif // _LIBCPP_CXX03_LANG
37 _LIBCPP_CONSTEXPR
explicit __cxx_atomic_base_impl(_Tp __value
) _NOEXCEPT
: __a_value(__value
) {}
38 _LIBCPP_DISABLE_EXTENSION_WARNING
_Atomic(_Tp
) __a_value
;
41 #define __cxx_atomic_is_lock_free(__s) __c11_atomic_is_lock_free(__s)
43 _LIBCPP_HIDE_FROM_ABI
inline void __cxx_atomic_thread_fence(memory_order __order
) _NOEXCEPT
{
44 __c11_atomic_thread_fence(static_cast<__memory_order_underlying_t
>(__order
));
47 _LIBCPP_HIDE_FROM_ABI
inline void __cxx_atomic_signal_fence(memory_order __order
) _NOEXCEPT
{
48 __c11_atomic_signal_fence(static_cast<__memory_order_underlying_t
>(__order
));
52 _LIBCPP_HIDE_FROM_ABI
void __cxx_atomic_init(__cxx_atomic_base_impl
<_Tp
> volatile* __a
, _Tp __val
) _NOEXCEPT
{
53 __c11_atomic_init(std::addressof(__a
->__a_value
), __val
);
56 _LIBCPP_HIDE_FROM_ABI
void __cxx_atomic_init(__cxx_atomic_base_impl
<_Tp
>* __a
, _Tp __val
) _NOEXCEPT
{
57 __c11_atomic_init(std::addressof(__a
->__a_value
), __val
);
61 _LIBCPP_HIDE_FROM_ABI
void
62 __cxx_atomic_store(__cxx_atomic_base_impl
<_Tp
> volatile* __a
, _Tp __val
, memory_order __order
) _NOEXCEPT
{
63 __c11_atomic_store(std::addressof(__a
->__a_value
), __val
, static_cast<__memory_order_underlying_t
>(__order
));
66 _LIBCPP_HIDE_FROM_ABI
void
67 __cxx_atomic_store(__cxx_atomic_base_impl
<_Tp
>* __a
, _Tp __val
, memory_order __order
) _NOEXCEPT
{
68 __c11_atomic_store(std::addressof(__a
->__a_value
), __val
, static_cast<__memory_order_underlying_t
>(__order
));
72 _LIBCPP_HIDE_FROM_ABI _Tp
73 __cxx_atomic_load(__cxx_atomic_base_impl
<_Tp
> const volatile* __a
, memory_order __order
) _NOEXCEPT
{
74 using __ptr_type
= __remove_const_t
<decltype(__a
->__a_value
)>*;
75 return __c11_atomic_load(
76 const_cast<__ptr_type
>(std::addressof(__a
->__a_value
)), static_cast<__memory_order_underlying_t
>(__order
));
79 _LIBCPP_HIDE_FROM_ABI _Tp
__cxx_atomic_load(__cxx_atomic_base_impl
<_Tp
> const* __a
, memory_order __order
) _NOEXCEPT
{
80 using __ptr_type
= __remove_const_t
<decltype(__a
->__a_value
)>*;
81 return __c11_atomic_load(
82 const_cast<__ptr_type
>(std::addressof(__a
->__a_value
)), static_cast<__memory_order_underlying_t
>(__order
));
86 _LIBCPP_HIDE_FROM_ABI
void
87 __cxx_atomic_load_inplace(__cxx_atomic_base_impl
<_Tp
> const volatile* __a
, _Tp
* __dst
, memory_order __order
) _NOEXCEPT
{
88 using __ptr_type
= __remove_const_t
<decltype(__a
->__a_value
)>*;
89 *__dst
= __c11_atomic_load(
90 const_cast<__ptr_type
>(std::addressof(__a
->__a_value
)), static_cast<__memory_order_underlying_t
>(__order
));
93 _LIBCPP_HIDE_FROM_ABI
void
94 __cxx_atomic_load_inplace(__cxx_atomic_base_impl
<_Tp
> const* __a
, _Tp
* __dst
, memory_order __order
) _NOEXCEPT
{
95 using __ptr_type
= __remove_const_t
<decltype(__a
->__a_value
)>*;
96 *__dst
= __c11_atomic_load(
97 const_cast<__ptr_type
>(std::addressof(__a
->__a_value
)), static_cast<__memory_order_underlying_t
>(__order
));
101 _LIBCPP_HIDE_FROM_ABI _Tp
102 __cxx_atomic_exchange(__cxx_atomic_base_impl
<_Tp
> volatile* __a
, _Tp __value
, memory_order __order
) _NOEXCEPT
{
103 return __c11_atomic_exchange(
104 std::addressof(__a
->__a_value
), __value
, static_cast<__memory_order_underlying_t
>(__order
));
107 _LIBCPP_HIDE_FROM_ABI _Tp
108 __cxx_atomic_exchange(__cxx_atomic_base_impl
<_Tp
>* __a
, _Tp __value
, memory_order __order
) _NOEXCEPT
{
109 return __c11_atomic_exchange(
110 std::addressof(__a
->__a_value
), __value
, static_cast<__memory_order_underlying_t
>(__order
));
113 _LIBCPP_HIDE_FROM_ABI
inline _LIBCPP_CONSTEXPR memory_order
__to_failure_order(memory_order __order
) {
114 // Avoid switch statement to make this a constexpr.
115 return __order
== memory_order_release
116 ? memory_order_relaxed
117 : (__order
== memory_order_acq_rel
? memory_order_acquire
: __order
);
121 _LIBCPP_HIDE_FROM_ABI
bool __cxx_atomic_compare_exchange_strong(
122 __cxx_atomic_base_impl
<_Tp
> volatile* __a
,
125 memory_order __success
,
126 memory_order __failure
) _NOEXCEPT
{
127 return __c11_atomic_compare_exchange_strong(
128 std::addressof(__a
->__a_value
),
131 static_cast<__memory_order_underlying_t
>(__success
),
132 static_cast<__memory_order_underlying_t
>(__to_failure_order(__failure
)));
135 _LIBCPP_HIDE_FROM_ABI
bool __cxx_atomic_compare_exchange_strong(
136 __cxx_atomic_base_impl
<_Tp
>* __a
, _Tp
* __expected
, _Tp __value
, memory_order __success
, memory_order __failure
)
138 return __c11_atomic_compare_exchange_strong(
139 std::addressof(__a
->__a_value
),
142 static_cast<__memory_order_underlying_t
>(__success
),
143 static_cast<__memory_order_underlying_t
>(__to_failure_order(__failure
)));
147 _LIBCPP_HIDE_FROM_ABI
bool __cxx_atomic_compare_exchange_weak(
148 __cxx_atomic_base_impl
<_Tp
> volatile* __a
,
151 memory_order __success
,
152 memory_order __failure
) _NOEXCEPT
{
153 return __c11_atomic_compare_exchange_weak(
154 std::addressof(__a
->__a_value
),
157 static_cast<__memory_order_underlying_t
>(__success
),
158 static_cast<__memory_order_underlying_t
>(__to_failure_order(__failure
)));
161 _LIBCPP_HIDE_FROM_ABI
bool __cxx_atomic_compare_exchange_weak(
162 __cxx_atomic_base_impl
<_Tp
>* __a
, _Tp
* __expected
, _Tp __value
, memory_order __success
, memory_order __failure
)
164 return __c11_atomic_compare_exchange_weak(
165 std::addressof(__a
->__a_value
),
168 static_cast<__memory_order_underlying_t
>(__success
),
169 static_cast<__memory_order_underlying_t
>(__to_failure_order(__failure
)));
173 _LIBCPP_HIDE_FROM_ABI _Tp
174 __cxx_atomic_fetch_add(__cxx_atomic_base_impl
<_Tp
> volatile* __a
, _Tp __delta
, memory_order __order
) _NOEXCEPT
{
175 return __c11_atomic_fetch_add(
176 std::addressof(__a
->__a_value
), __delta
, static_cast<__memory_order_underlying_t
>(__order
));
179 _LIBCPP_HIDE_FROM_ABI _Tp
180 __cxx_atomic_fetch_add(__cxx_atomic_base_impl
<_Tp
>* __a
, _Tp __delta
, memory_order __order
) _NOEXCEPT
{
181 return __c11_atomic_fetch_add(
182 std::addressof(__a
->__a_value
), __delta
, static_cast<__memory_order_underlying_t
>(__order
));
186 _LIBCPP_HIDE_FROM_ABI _Tp
*
187 __cxx_atomic_fetch_add(__cxx_atomic_base_impl
<_Tp
*> volatile* __a
, ptrdiff_t __delta
, memory_order __order
) _NOEXCEPT
{
188 return __c11_atomic_fetch_add(
189 std::addressof(__a
->__a_value
), __delta
, static_cast<__memory_order_underlying_t
>(__order
));
192 _LIBCPP_HIDE_FROM_ABI _Tp
*
193 __cxx_atomic_fetch_add(__cxx_atomic_base_impl
<_Tp
*>* __a
, ptrdiff_t __delta
, memory_order __order
) _NOEXCEPT
{
194 return __c11_atomic_fetch_add(
195 std::addressof(__a
->__a_value
), __delta
, static_cast<__memory_order_underlying_t
>(__order
));
199 _LIBCPP_HIDE_FROM_ABI _Tp
200 __cxx_atomic_fetch_sub(__cxx_atomic_base_impl
<_Tp
> volatile* __a
, _Tp __delta
, memory_order __order
) _NOEXCEPT
{
201 return __c11_atomic_fetch_sub(
202 std::addressof(__a
->__a_value
), __delta
, static_cast<__memory_order_underlying_t
>(__order
));
205 _LIBCPP_HIDE_FROM_ABI _Tp
206 __cxx_atomic_fetch_sub(__cxx_atomic_base_impl
<_Tp
>* __a
, _Tp __delta
, memory_order __order
) _NOEXCEPT
{
207 return __c11_atomic_fetch_sub(
208 std::addressof(__a
->__a_value
), __delta
, static_cast<__memory_order_underlying_t
>(__order
));
211 _LIBCPP_HIDE_FROM_ABI _Tp
*
212 __cxx_atomic_fetch_sub(__cxx_atomic_base_impl
<_Tp
*> volatile* __a
, ptrdiff_t __delta
, memory_order __order
) _NOEXCEPT
{
213 return __c11_atomic_fetch_sub(
214 std::addressof(__a
->__a_value
), __delta
, static_cast<__memory_order_underlying_t
>(__order
));
217 _LIBCPP_HIDE_FROM_ABI _Tp
*
218 __cxx_atomic_fetch_sub(__cxx_atomic_base_impl
<_Tp
*>* __a
, ptrdiff_t __delta
, memory_order __order
) _NOEXCEPT
{
219 return __c11_atomic_fetch_sub(
220 std::addressof(__a
->__a_value
), __delta
, static_cast<__memory_order_underlying_t
>(__order
));
224 _LIBCPP_HIDE_FROM_ABI _Tp
225 __cxx_atomic_fetch_and(__cxx_atomic_base_impl
<_Tp
> volatile* __a
, _Tp __pattern
, memory_order __order
) _NOEXCEPT
{
226 return __c11_atomic_fetch_and(
227 std::addressof(__a
->__a_value
), __pattern
, static_cast<__memory_order_underlying_t
>(__order
));
230 _LIBCPP_HIDE_FROM_ABI _Tp
231 __cxx_atomic_fetch_and(__cxx_atomic_base_impl
<_Tp
>* __a
, _Tp __pattern
, memory_order __order
) _NOEXCEPT
{
232 return __c11_atomic_fetch_and(
233 std::addressof(__a
->__a_value
), __pattern
, static_cast<__memory_order_underlying_t
>(__order
));
237 _LIBCPP_HIDE_FROM_ABI _Tp
238 __cxx_atomic_fetch_or(__cxx_atomic_base_impl
<_Tp
> volatile* __a
, _Tp __pattern
, memory_order __order
) _NOEXCEPT
{
239 return __c11_atomic_fetch_or(
240 std::addressof(__a
->__a_value
), __pattern
, static_cast<__memory_order_underlying_t
>(__order
));
243 _LIBCPP_HIDE_FROM_ABI _Tp
244 __cxx_atomic_fetch_or(__cxx_atomic_base_impl
<_Tp
>* __a
, _Tp __pattern
, memory_order __order
) _NOEXCEPT
{
245 return __c11_atomic_fetch_or(
246 std::addressof(__a
->__a_value
), __pattern
, static_cast<__memory_order_underlying_t
>(__order
));
250 _LIBCPP_HIDE_FROM_ABI _Tp
251 __cxx_atomic_fetch_xor(__cxx_atomic_base_impl
<_Tp
> volatile* __a
, _Tp __pattern
, memory_order __order
) _NOEXCEPT
{
252 return __c11_atomic_fetch_xor(
253 std::addressof(__a
->__a_value
), __pattern
, static_cast<__memory_order_underlying_t
>(__order
));
256 _LIBCPP_HIDE_FROM_ABI _Tp
257 __cxx_atomic_fetch_xor(__cxx_atomic_base_impl
<_Tp
>* __a
, _Tp __pattern
, memory_order __order
) _NOEXCEPT
{
258 return __c11_atomic_fetch_xor(
259 std::addressof(__a
->__a_value
), __pattern
, static_cast<__memory_order_underlying_t
>(__order
));
262 _LIBCPP_END_NAMESPACE_STD
264 #endif // _LIBCPP___ATOMIC_SUPPORT_C11_H