1 //===----------------------------------------------------------------------===////
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===////
10 #ifndef ATOMIC_SUPPORT_H
11 #define ATOMIC_SUPPORT_H
14 #include "memory" // for __libcpp_relaxed_load
16 #if defined(__clang__) && __has_builtin(__atomic_load_n) \
17 && __has_builtin(__atomic_store_n) \
18 && __has_builtin(__atomic_add_fetch) \
19 && __has_builtin(__atomic_exchange_n) \
20 && __has_builtin(__atomic_compare_exchange_n) \
21 && defined(__ATOMIC_RELAXED) \
22 && defined(__ATOMIC_CONSUME) \
23 && defined(__ATOMIC_ACQUIRE) \
24 && defined(__ATOMIC_RELEASE) \
25 && defined(__ATOMIC_ACQ_REL) \
26 && defined(__ATOMIC_SEQ_CST)
27 # define _LIBCPP_HAS_ATOMIC_BUILTINS
28 #elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
29 # define _LIBCPP_HAS_ATOMIC_BUILTINS
32 #if !defined(_LIBCPP_HAS_ATOMIC_BUILTINS) && !defined(_LIBCPP_HAS_NO_THREADS)
33 # if defined(_LIBCPP_WARNING)
34 _LIBCPP_WARNING("Building libc++ without __atomic builtins is unsupported")
36 # warning Building libc++ without __atomic builtins is unsupported
40 _LIBCPP_BEGIN_NAMESPACE_STD
44 #if defined(_LIBCPP_HAS_ATOMIC_BUILTINS) && !defined(_LIBCPP_HAS_NO_THREADS)
46 enum __libcpp_atomic_order {
47 _AO_Relaxed = __ATOMIC_RELAXED,
48 _AO_Consume = __ATOMIC_CONSUME,
49 _AO_Acquire = __ATOMIC_ACQUIRE,
50 _AO_Release = __ATOMIC_RELEASE,
51 _AO_Acq_Rel = __ATOMIC_ACQ_REL,
52 _AO_Seq = __ATOMIC_SEQ_CST
55 template <class _ValueType, class _FromType>
56 inline _LIBCPP_INLINE_VISIBILITY
57 void __libcpp_atomic_store(_ValueType* __dest, _FromType __val,
58 int __order = _AO_Seq)
60 __atomic_store_n(__dest, __val, __order);
63 template <class _ValueType, class _FromType>
64 inline _LIBCPP_INLINE_VISIBILITY
65 void __libcpp_relaxed_store(_ValueType* __dest, _FromType __val)
67 __atomic_store_n(__dest, __val, _AO_Relaxed);
70 template <class _ValueType>
71 inline _LIBCPP_INLINE_VISIBILITY
72 _ValueType __libcpp_atomic_load(_ValueType const* __val,
73 int __order = _AO_Seq)
75 return __atomic_load_n(__val, __order);
78 template <class _ValueType, class _AddType>
79 inline _LIBCPP_INLINE_VISIBILITY
80 _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a,
81 int __order = _AO_Seq)
83 return __atomic_add_fetch(__val, __a, __order);
86 template <class _ValueType>
87 inline _LIBCPP_INLINE_VISIBILITY
88 _ValueType __libcpp_atomic_exchange(_ValueType* __target,
89 _ValueType __value, int __order = _AO_Seq)
91 return __atomic_exchange_n(__target, __value, __order);
94 template <class _ValueType>
95 inline _LIBCPP_INLINE_VISIBILITY
96 bool __libcpp_atomic_compare_exchange(_ValueType* __val,
97 _ValueType* __expected, _ValueType __after,
98 int __success_order = _AO_Seq,
99 int __fail_order = _AO_Seq)
101 return __atomic_compare_exchange_n(__val, __expected, __after, true,
102 __success_order, __fail_order);
105 #else // _LIBCPP_HAS_NO_THREADS
107 enum __libcpp_atomic_order {
116 template <class _ValueType, class _FromType>
117 inline _LIBCPP_INLINE_VISIBILITY
118 void __libcpp_atomic_store(_ValueType* __dest, _FromType __val,
124 template <class _ValueType, class _FromType>
125 inline _LIBCPP_INLINE_VISIBILITY
126 void __libcpp_relaxed_store(_ValueType* __dest, _FromType __val)
131 template <class _ValueType>
132 inline _LIBCPP_INLINE_VISIBILITY
133 _ValueType __libcpp_atomic_load(_ValueType const* __val,
139 template <class _ValueType, class _AddType>
140 inline _LIBCPP_INLINE_VISIBILITY
141 _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a,
144 return *__val += __a;
147 template <class _ValueType>
148 inline _LIBCPP_INLINE_VISIBILITY
149 _ValueType __libcpp_atomic_exchange(_ValueType* __target,
150 _ValueType __value, int __order = _AO_Seq)
152 _ValueType old = *__target;
157 template <class _ValueType>
158 inline _LIBCPP_INLINE_VISIBILITY
159 bool __libcpp_atomic_compare_exchange(_ValueType* __val,
160 _ValueType* __expected, _ValueType __after,
163 if (*__val == *__expected) {
167 *__expected = *__val;
171 #endif // _LIBCPP_HAS_NO_THREADS
175 _LIBCPP_END_NAMESPACE_STD
177 #endif // ATOMIC_SUPPORT_H