3 // Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/atomic_base.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{atomic}
30 #ifndef _GLIBCXX_ATOMIC_BASE_H
31 #define _GLIBCXX_ATOMIC_BASE_H 1
33 #pragma GCC system_header
35 #include <bits/c++config.h>
39 namespace std _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * @defgroup atomics Atomics
46 * Components for performing atomic operations.
50 /// Enumeration for memory_order
51 typedef enum memory_order
62 __calculate_memory_order(memory_order __m)
64 const bool __cond1 = __m == memory_order_release;
65 const bool __cond2 = __m == memory_order_acq_rel;
66 memory_order __mo1(__cond1 ? memory_order_relaxed : __m);
67 memory_order __mo2(__cond2 ? memory_order_acquire : __mo1);
72 atomic_thread_fence(memory_order);
75 atomic_signal_fence(memory_order);
78 template<typename _Tp>
80 kill_dependency(_Tp __y)
87 * @brief Base type for atomic_flag.
89 * Base type is POD with data, allowing atomic_flag to derive from
90 * it and meet the standard layout type requirement. In addition to
91 * compatibilty with a C interface, this allows different
92 * implementations of atomic_flag to use the same atomic operation
93 * functions, via a standard conversion to the __atomic_flag_base
96 _GLIBCXX_BEGIN_EXTERN_C
98 struct __atomic_flag_base
103 _GLIBCXX_END_EXTERN_C
105 #define ATOMIC_FLAG_INIT { false }
108 // Base types for atomics.
110 // Three nested namespaces for atomic implementation details.
112 // The nested namespace inlined into std:: is determined by the value
113 // of the _GLIBCXX_ATOMIC_PROPERTY macro and the resulting
114 // ATOMIC_*_LOCK_FREE macros.
116 // 0 == __atomic0 == Never lock-free
117 // 1 == __atomic1 == Best available, sometimes lock-free
118 // 2 == __atomic2 == Always lock-free
124 template<typename _IntTp>
125 struct __atomic_base;
132 template<typename _IntTp>
133 struct __atomic_base;
138 using __atomic2::atomic_flag;
139 using __atomic0::__atomic_base;
142 /// Lock-free Property
143 #if defined(_GLIBCXX_ATOMIC_BUILTINS_1) && defined(_GLIBCXX_ATOMIC_BUILTINS_2) \
144 && defined(_GLIBCXX_ATOMIC_BUILTINS_4) && defined(_GLIBCXX_ATOMIC_BUILTINS_8)
145 # define _GLIBCXX_ATOMIC_PROPERTY 2
146 # define _GLIBCXX_ATOMIC_NAMESPACE __atomic2
147 #elif defined(_GLIBCXX_ATOMIC_BUILTINS_1)
148 # define _GLIBCXX_ATOMIC_PROPERTY 1
149 # define _GLIBCXX_ATOMIC_NAMESPACE __atomic1
151 # define _GLIBCXX_ATOMIC_PROPERTY 0
152 # define _GLIBCXX_ATOMIC_NAMESPACE __atomic0
155 #define ATOMIC_CHAR_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
156 #define ATOMIC_CHAR16_T_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
157 #define ATOMIC_CHAR32_T_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
158 #define ATOMIC_WCHAR_T_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
159 #define ATOMIC_SHORT_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
160 #define ATOMIC_INT_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
161 #define ATOMIC_LONG_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
162 #define ATOMIC_LLONG_LOCK_FREE _GLIBCXX_ATOMIC_PROPERTY
164 inline namespace _GLIBCXX_ATOMIC_NAMESPACE { }
168 typedef __atomic_base<char> atomic_char;
171 typedef __atomic_base<signed char> atomic_schar;
174 typedef __atomic_base<unsigned char> atomic_uchar;
177 typedef __atomic_base<short> atomic_short;
180 typedef __atomic_base<unsigned short> atomic_ushort;
183 typedef __atomic_base<int> atomic_int;
186 typedef __atomic_base<unsigned int> atomic_uint;
189 typedef __atomic_base<long> atomic_long;
192 typedef __atomic_base<unsigned long> atomic_ulong;
195 typedef __atomic_base<long long> atomic_llong;
198 typedef __atomic_base<unsigned long long> atomic_ullong;
201 typedef __atomic_base<wchar_t> atomic_wchar_t;
204 typedef __atomic_base<char16_t> atomic_char16_t;
207 typedef __atomic_base<char32_t> atomic_char32_t;
210 typedef __atomic_base<char32_t> atomic_char32_t;
213 /// atomic_int_least8_t
214 typedef __atomic_base<int_least8_t> atomic_int_least8_t;
216 /// atomic_uint_least8_t
217 typedef __atomic_base<uint_least8_t> atomic_uint_least8_t;
219 /// atomic_int_least16_t
220 typedef __atomic_base<int_least16_t> atomic_int_least16_t;
222 /// atomic_uint_least16_t
223 typedef __atomic_base<uint_least16_t> atomic_uint_least16_t;
225 /// atomic_int_least32_t
226 typedef __atomic_base<int_least32_t> atomic_int_least32_t;
228 /// atomic_uint_least32_t
229 typedef __atomic_base<uint_least32_t> atomic_uint_least32_t;
231 /// atomic_int_least64_t
232 typedef __atomic_base<int_least64_t> atomic_int_least64_t;
234 /// atomic_uint_least64_t
235 typedef __atomic_base<uint_least64_t> atomic_uint_least64_t;
238 /// atomic_int_fast8_t
239 typedef __atomic_base<int_fast8_t> atomic_int_fast8_t;
241 /// atomic_uint_fast8_t
242 typedef __atomic_base<uint_fast8_t> atomic_uint_fast8_t;
244 /// atomic_int_fast16_t
245 typedef __atomic_base<int_fast16_t> atomic_int_fast16_t;
247 /// atomic_uint_fast16_t
248 typedef __atomic_base<uint_fast16_t> atomic_uint_fast16_t;
250 /// atomic_int_fast32_t
251 typedef __atomic_base<int_fast32_t> atomic_int_fast32_t;
253 /// atomic_uint_fast32_t
254 typedef __atomic_base<uint_fast32_t> atomic_uint_fast32_t;
256 /// atomic_int_fast64_t
257 typedef __atomic_base<int_fast64_t> atomic_int_fast64_t;
259 /// atomic_uint_fast64_t
260 typedef __atomic_base<uint_fast64_t> atomic_uint_fast64_t;
264 typedef __atomic_base<intptr_t> atomic_intptr_t;
267 typedef __atomic_base<uintptr_t> atomic_uintptr_t;
270 typedef __atomic_base<size_t> atomic_size_t;
273 typedef __atomic_base<intmax_t> atomic_intmax_t;
276 typedef __atomic_base<uintmax_t> atomic_uintmax_t;
279 typedef __atomic_base<ptrdiff_t> atomic_ptrdiff_t;
282 #define ATOMIC_VAR_INIT(_VI) { _VI }
284 template<typename _Tp>
287 template<typename _Tp>
292 _GLIBCXX_END_NAMESPACE_VERSION