annotate libcxxabi/src/include/atomic_support.h @ 173:0572611fdcc8 llvm10 llvm12

reorgnization done
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 11:55:54 +0900
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===----------------------------------------------------------------------===////
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===////
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 // FIXME: This file is copied from libcxx/src/include/atomic_support.h. Instead
anatofuz
parents:
diff changeset
10 // of duplicating the file in libc++abi we should require that the libc++
anatofuz
parents:
diff changeset
11 // sources are available when building libc++abi.
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #ifndef ATOMIC_SUPPORT_H
anatofuz
parents:
diff changeset
14 #define ATOMIC_SUPPORT_H
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 #include "__config"
anatofuz
parents:
diff changeset
17 #include "memory" // for __libcpp_relaxed_load
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 #if defined(__clang__) && __has_builtin(__atomic_load_n) \
anatofuz
parents:
diff changeset
20 && __has_builtin(__atomic_store_n) \
anatofuz
parents:
diff changeset
21 && __has_builtin(__atomic_add_fetch) \
anatofuz
parents:
diff changeset
22 && __has_builtin(__atomic_exchange_n) \
anatofuz
parents:
diff changeset
23 && __has_builtin(__atomic_compare_exchange_n) \
anatofuz
parents:
diff changeset
24 && defined(__ATOMIC_RELAXED) \
anatofuz
parents:
diff changeset
25 && defined(__ATOMIC_CONSUME) \
anatofuz
parents:
diff changeset
26 && defined(__ATOMIC_ACQUIRE) \
anatofuz
parents:
diff changeset
27 && defined(__ATOMIC_RELEASE) \
anatofuz
parents:
diff changeset
28 && defined(__ATOMIC_ACQ_REL) \
anatofuz
parents:
diff changeset
29 && defined(__ATOMIC_SEQ_CST)
anatofuz
parents:
diff changeset
30 # define _LIBCXXABI_HAS_ATOMIC_BUILTINS
anatofuz
parents:
diff changeset
31 #elif !defined(__clang__) && defined(_GNUC_VER) && _GNUC_VER >= 407
anatofuz
parents:
diff changeset
32 # define _LIBCXXABI_HAS_ATOMIC_BUILTINS
anatofuz
parents:
diff changeset
33 #endif
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 #if !defined(_LIBCXXABI_HAS_ATOMIC_BUILTINS) && !defined(_LIBCXXABI_HAS_NO_THREADS)
anatofuz
parents:
diff changeset
36 # if defined(_LIBCPP_WARNING)
anatofuz
parents:
diff changeset
37 _LIBCPP_WARNING("Building libc++ without __atomic builtins is unsupported")
anatofuz
parents:
diff changeset
38 # else
anatofuz
parents:
diff changeset
39 # warning Building libc++ without __atomic builtins is unsupported
anatofuz
parents:
diff changeset
40 # endif
anatofuz
parents:
diff changeset
41 #endif
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 _LIBCPP_BEGIN_NAMESPACE_STD
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 namespace {
anatofuz
parents:
diff changeset
46
anatofuz
parents:
diff changeset
47 #if defined(_LIBCXXABI_HAS_ATOMIC_BUILTINS) && !defined(_LIBCXXABI_HAS_NO_THREADS)
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 enum __libcpp_atomic_order {
anatofuz
parents:
diff changeset
50 _AO_Relaxed = __ATOMIC_RELAXED,
anatofuz
parents:
diff changeset
51 _AO_Consume = __ATOMIC_CONSUME,
anatofuz
parents:
diff changeset
52 _AO_Acquire = __ATOMIC_ACQUIRE,
anatofuz
parents:
diff changeset
53 _AO_Release = __ATOMIC_RELEASE,
anatofuz
parents:
diff changeset
54 _AO_Acq_Rel = __ATOMIC_ACQ_REL,
anatofuz
parents:
diff changeset
55 _AO_Seq = __ATOMIC_SEQ_CST
anatofuz
parents:
diff changeset
56 };
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 template <class _ValueType, class _FromType>
anatofuz
parents:
diff changeset
59 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
60 void __libcpp_atomic_store(_ValueType* __dest, _FromType __val,
anatofuz
parents:
diff changeset
61 int __order = _AO_Seq)
anatofuz
parents:
diff changeset
62 {
anatofuz
parents:
diff changeset
63 __atomic_store_n(__dest, __val, __order);
anatofuz
parents:
diff changeset
64 }
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 template <class _ValueType, class _FromType>
anatofuz
parents:
diff changeset
67 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
68 void __libcpp_relaxed_store(_ValueType* __dest, _FromType __val)
anatofuz
parents:
diff changeset
69 {
anatofuz
parents:
diff changeset
70 __atomic_store_n(__dest, __val, _AO_Relaxed);
anatofuz
parents:
diff changeset
71 }
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 template <class _ValueType>
anatofuz
parents:
diff changeset
74 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
75 _ValueType __libcpp_atomic_load(_ValueType const* __val,
anatofuz
parents:
diff changeset
76 int __order = _AO_Seq)
anatofuz
parents:
diff changeset
77 {
anatofuz
parents:
diff changeset
78 return __atomic_load_n(__val, __order);
anatofuz
parents:
diff changeset
79 }
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 template <class _ValueType, class _AddType>
anatofuz
parents:
diff changeset
82 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
83 _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a,
anatofuz
parents:
diff changeset
84 int __order = _AO_Seq)
anatofuz
parents:
diff changeset
85 {
anatofuz
parents:
diff changeset
86 return __atomic_add_fetch(__val, __a, __order);
anatofuz
parents:
diff changeset
87 }
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 template <class _ValueType>
anatofuz
parents:
diff changeset
90 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
91 _ValueType __libcpp_atomic_exchange(_ValueType* __target,
anatofuz
parents:
diff changeset
92 _ValueType __value, int __order = _AO_Seq)
anatofuz
parents:
diff changeset
93 {
anatofuz
parents:
diff changeset
94 return __atomic_exchange_n(__target, __value, __order);
anatofuz
parents:
diff changeset
95 }
anatofuz
parents:
diff changeset
96
anatofuz
parents:
diff changeset
97 template <class _ValueType>
anatofuz
parents:
diff changeset
98 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
99 bool __libcpp_atomic_compare_exchange(_ValueType* __val,
anatofuz
parents:
diff changeset
100 _ValueType* __expected, _ValueType __after,
anatofuz
parents:
diff changeset
101 int __success_order = _AO_Seq,
anatofuz
parents:
diff changeset
102 int __fail_order = _AO_Seq)
anatofuz
parents:
diff changeset
103 {
anatofuz
parents:
diff changeset
104 return __atomic_compare_exchange_n(__val, __expected, __after, true,
anatofuz
parents:
diff changeset
105 __success_order, __fail_order);
anatofuz
parents:
diff changeset
106 }
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 #else // _LIBCPP_HAS_NO_THREADS
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 enum __libcpp_atomic_order {
anatofuz
parents:
diff changeset
111 _AO_Relaxed,
anatofuz
parents:
diff changeset
112 _AO_Consume,
anatofuz
parents:
diff changeset
113 _AO_Acquire,
anatofuz
parents:
diff changeset
114 _AO_Release,
anatofuz
parents:
diff changeset
115 _AO_Acq_Rel,
anatofuz
parents:
diff changeset
116 _AO_Seq
anatofuz
parents:
diff changeset
117 };
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 template <class _ValueType, class _FromType>
anatofuz
parents:
diff changeset
120 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
121 void __libcpp_atomic_store(_ValueType* __dest, _FromType __val,
anatofuz
parents:
diff changeset
122 int = 0)
anatofuz
parents:
diff changeset
123 {
anatofuz
parents:
diff changeset
124 *__dest = __val;
anatofuz
parents:
diff changeset
125 }
anatofuz
parents:
diff changeset
126
anatofuz
parents:
diff changeset
127 template <class _ValueType, class _FromType>
anatofuz
parents:
diff changeset
128 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
129 void __libcpp_relaxed_store(_ValueType* __dest, _FromType __val)
anatofuz
parents:
diff changeset
130 {
anatofuz
parents:
diff changeset
131 *__dest = __val;
anatofuz
parents:
diff changeset
132 }
anatofuz
parents:
diff changeset
133
anatofuz
parents:
diff changeset
134 template <class _ValueType>
anatofuz
parents:
diff changeset
135 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
136 _ValueType __libcpp_atomic_load(_ValueType const* __val,
anatofuz
parents:
diff changeset
137 int = 0)
anatofuz
parents:
diff changeset
138 {
anatofuz
parents:
diff changeset
139 return *__val;
anatofuz
parents:
diff changeset
140 }
anatofuz
parents:
diff changeset
141
anatofuz
parents:
diff changeset
142 template <class _ValueType, class _AddType>
anatofuz
parents:
diff changeset
143 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
144 _ValueType __libcpp_atomic_add(_ValueType* __val, _AddType __a,
anatofuz
parents:
diff changeset
145 int = 0)
anatofuz
parents:
diff changeset
146 {
anatofuz
parents:
diff changeset
147 return *__val += __a;
anatofuz
parents:
diff changeset
148 }
anatofuz
parents:
diff changeset
149
anatofuz
parents:
diff changeset
150 template <class _ValueType>
anatofuz
parents:
diff changeset
151 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
152 _ValueType __libcpp_atomic_exchange(_ValueType* __target,
anatofuz
parents:
diff changeset
153 _ValueType __value, int = _AO_Seq)
anatofuz
parents:
diff changeset
154 {
anatofuz
parents:
diff changeset
155 _ValueType old = *__target;
anatofuz
parents:
diff changeset
156 *__target = __value;
anatofuz
parents:
diff changeset
157 return old;
anatofuz
parents:
diff changeset
158 }
anatofuz
parents:
diff changeset
159
anatofuz
parents:
diff changeset
160 template <class _ValueType>
anatofuz
parents:
diff changeset
161 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
162 bool __libcpp_atomic_compare_exchange(_ValueType* __val,
anatofuz
parents:
diff changeset
163 _ValueType* __expected, _ValueType __after,
anatofuz
parents:
diff changeset
164 int = 0, int = 0)
anatofuz
parents:
diff changeset
165 {
anatofuz
parents:
diff changeset
166 if (*__val == *__expected) {
anatofuz
parents:
diff changeset
167 *__val = __after;
anatofuz
parents:
diff changeset
168 return true;
anatofuz
parents:
diff changeset
169 }
anatofuz
parents:
diff changeset
170 *__expected = *__val;
anatofuz
parents:
diff changeset
171 return false;
anatofuz
parents:
diff changeset
172 }
anatofuz
parents:
diff changeset
173
anatofuz
parents:
diff changeset
174 #endif // _LIBCPP_HAS_NO_THREADS
anatofuz
parents:
diff changeset
175
anatofuz
parents:
diff changeset
176 } // end namespace
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 _LIBCPP_END_NAMESPACE_STD
anatofuz
parents:
diff changeset
179
anatofuz
parents:
diff changeset
180 #endif // ATOMIC_SUPPORT_H