2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * Copyright (c) 2014 Andrey Semashev
9 * \file atomic/detail/ops_gcc_atomic.hpp
11 * This header contains implementation of the \c operations template.
14 #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_
18 #include <boost/memory_order.hpp>
19 #include <boost/atomic/detail/config.hpp>
20 #include <boost/atomic/detail/storage_type.hpp>
21 #include <boost/atomic/detail/operations_fwd.hpp>
22 #include <boost/atomic/capabilities.hpp>
23 #if (defined(__clang__) || (defined(BOOST_GCC) && (BOOST_GCC+0) >= 70000)) && (defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) || defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B))
24 #include <boost/atomic/detail/ops_gcc_x86_dcas.hpp>
25 #include <boost/atomic/detail/ops_cas_based.hpp>
28 #if __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE || __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE ||\
29 __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE || __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE ||\
30 __GCC_ATOMIC_CHAR_LOCK_FREE != BOOST_ATOMIC_CHAR_LOCK_FREE || __GCC_ATOMIC_BOOL_LOCK_FREE != BOOST_ATOMIC_BOOL_LOCK_FREE ||\
31 __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE
32 // There are platforms where we need to use larger storage types
33 #include <boost/atomic/detail/int_sizes.hpp>
34 #include <boost/atomic/detail/ops_extending_cas_based.hpp>
37 #ifdef BOOST_HAS_PRAGMA_ONCE
41 #if defined(__INTEL_COMPILER)
42 // This is used to suppress warning #32013 described below for Intel Compiler.
43 // In debug builds the compiler does not inline any functions, so basically
44 // every atomic function call results in this warning. I don't know any other
45 // way to selectively disable just this one warning.
54 * The function converts \c boost::memory_order values to the compiler-specific constants.
56 * NOTE: The intention is that the function is optimized away by the compiler, and the
57 * compiler-specific constants are passed to the intrinsics. I know constexpr doesn't
58 * work in this case because the standard atomics interface require memory ordering
59 * constants to be passed as function arguments, at which point they stop being constexpr.
60 * However it is crucial that the compiler sees constants and not runtime values,
61 * because otherwise it just ignores the ordering value and always uses seq_cst.
62 * This is the case with Intel C++ Compiler 14.0.3 (Composer XE 2013 SP1, update 3) and
63 * gcc 4.8.2. Intel Compiler issues a warning in this case:
65 * warning #32013: Invalid memory order specified. Defaulting to seq_cst memory order.
67 * while gcc acts silently.
69 * To mitigate the problem ALL functions, including the atomic<> members must be
70 * declared with BOOST_FORCEINLINE. In this case the compilers are able to see that
71 * all functions are called with constant orderings and call intrinstcts properly.
73 * Unfortunately, this still doesn't work in debug mode as the compiler doesn't
74 * inline functions even when marked with BOOST_FORCEINLINE. In this case all atomic
75 * operaions will be executed with seq_cst semantics.
77 BOOST_FORCEINLINE BOOST_CONSTEXPR int convert_memory_order_to_gcc(memory_order order) BOOST_NOEXCEPT
79 return (order == memory_order_relaxed ? __ATOMIC_RELAXED : (order == memory_order_consume ? __ATOMIC_CONSUME :
80 (order == memory_order_acquire ? __ATOMIC_ACQUIRE : (order == memory_order_release ? __ATOMIC_RELEASE :
81 (order == memory_order_acq_rel ? __ATOMIC_ACQ_REL : __ATOMIC_SEQ_CST)))));
84 template< typename T >
85 struct gcc_atomic_operations
87 typedef T storage_type;
89 // Note: In the current implementation, gcc_atomic_operations are used onlu when the particularly sized __atomic
90 // intrinsics are always lock-free (i.e. the corresponding LOCK_FREE macro is 2). Therefore it is safe to
91 // always set is_always_lock_free to true here.
92 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
94 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
96 __atomic_store_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
99 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
101 return __atomic_load_n(&storage, atomics::detail::convert_memory_order_to_gcc(order));
104 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
106 return __atomic_fetch_add(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
109 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
111 return __atomic_fetch_sub(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
114 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
116 return __atomic_exchange_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
119 static BOOST_FORCEINLINE bool compare_exchange_strong(
120 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
122 return __atomic_compare_exchange_n
124 &storage, &expected, desired, false,
125 atomics::detail::convert_memory_order_to_gcc(success_order),
126 atomics::detail::convert_memory_order_to_gcc(failure_order)
130 static BOOST_FORCEINLINE bool compare_exchange_weak(
131 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
133 return __atomic_compare_exchange_n
135 &storage, &expected, desired, true,
136 atomics::detail::convert_memory_order_to_gcc(success_order),
137 atomics::detail::convert_memory_order_to_gcc(failure_order)
141 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
143 return __atomic_fetch_and(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
146 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
148 return __atomic_fetch_or(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
151 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
153 return __atomic_fetch_xor(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
156 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
158 return __atomic_test_and_set(&storage, atomics::detail::convert_memory_order_to_gcc(order));
161 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
163 __atomic_clear(const_cast< storage_type* >(&storage), atomics::detail::convert_memory_order_to_gcc(order));
167 #if BOOST_ATOMIC_INT128_LOCK_FREE > 0
168 #if (defined(__clang__) || (defined(BOOST_GCC) && (BOOST_GCC+0) >= 70000)) && defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B)
170 // Workaround for clang bug: http://llvm.org/bugs/show_bug.cgi?id=19149
171 // Clang 3.4 does not implement 128-bit __atomic* intrinsics even though it defines __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16
172 // A similar problem exists with gcc 7 as well, as it requires to link with libatomic to use 16-byte intrinsics:
173 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80878
174 template< bool Signed >
175 struct operations< 16u, Signed > :
176 public cas_based_operations< gcc_dcas_x86_64< Signed > >
178 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 16u;
179 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
184 template< bool Signed >
185 struct operations< 16u, Signed > :
186 public gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >
188 typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
190 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 16u;
191 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
198 #if BOOST_ATOMIC_INT64_LOCK_FREE > 0
199 #if defined(__clang__) && defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B)
201 // Workaround for clang bug http://llvm.org/bugs/show_bug.cgi?id=19355
202 template< bool Signed >
203 struct operations< 8u, Signed > :
204 public cas_based_operations< gcc_dcas_x86< Signed > >
206 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 8u;
207 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
210 #elif (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 8 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\
211 (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 8 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\
212 (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 8 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\
213 (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 8 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\
214 (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 8 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE)
216 #define BOOST_ATOMIC_DETAIL_INT64_EXTENDED
218 template< bool Signed >
219 struct operations< 8u, Signed > :
220 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 8u, Signed >
222 typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
224 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 8u;
225 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
230 template< bool Signed >
231 struct operations< 8u, Signed > :
232 public gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >
234 typedef typename make_storage_type< 8u, Signed >::aligned aligned_storage_type;
236 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 8u;
237 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
243 #if BOOST_ATOMIC_INT32_LOCK_FREE > 0
244 #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 4 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\
245 (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 4 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\
246 (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 4 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\
247 (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 4 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\
248 (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 4 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE)
250 #define BOOST_ATOMIC_DETAIL_INT32_EXTENDED
252 #if !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
254 template< bool Signed >
255 struct operations< 4u, Signed > :
256 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 4u, Signed >
258 typedef typename make_storage_type< 8u, Signed >::aligned aligned_storage_type;
260 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 8u;
261 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
264 #else // !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
266 template< bool Signed >
267 struct operations< 4u, Signed > :
268 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 4u, Signed >
270 typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
272 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 16u;
273 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
276 #endif // !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
280 template< bool Signed >
281 struct operations< 4u, Signed > :
282 public gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type >
284 typedef typename make_storage_type< 4u, Signed >::aligned aligned_storage_type;
286 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 4u;
287 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
293 #if BOOST_ATOMIC_INT16_LOCK_FREE > 0
294 #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 2 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\
295 (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 2 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\
296 (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 2 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\
297 (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 2 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\
298 (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 2 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE)
300 #define BOOST_ATOMIC_DETAIL_INT16_EXTENDED
302 #if !defined(BOOST_ATOMIC_DETAIL_INT32_EXTENDED)
304 template< bool Signed >
305 struct operations< 2u, Signed > :
306 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type >, 2u, Signed >
308 typedef typename make_storage_type< 4u, Signed >::aligned aligned_storage_type;
310 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 4u;
311 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
314 #elif !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
316 template< bool Signed >
317 struct operations< 2u, Signed > :
318 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 2u, Signed >
320 typedef typename make_storage_type< 8u, Signed >::aligned aligned_storage_type;
322 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 8u;
323 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
328 template< bool Signed >
329 struct operations< 2u, Signed > :
330 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 2u, Signed >
332 typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
334 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 16u;
335 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
342 template< bool Signed >
343 struct operations< 2u, Signed > :
344 public gcc_atomic_operations< typename make_storage_type< 2u, Signed >::type >
346 typedef typename make_storage_type< 2u, Signed >::aligned aligned_storage_type;
348 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 2u;
349 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
355 #if BOOST_ATOMIC_INT8_LOCK_FREE > 0
356 #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 1 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\
357 (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 1 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\
358 (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 1 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\
359 (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 1 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\
360 (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 1 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE) ||\
361 (__GCC_ATOMIC_CHAR_LOCK_FREE != BOOST_ATOMIC_CHAR_LOCK_FREE) ||\
362 (__GCC_ATOMIC_BOOL_LOCK_FREE != BOOST_ATOMIC_BOOL_LOCK_FREE)
364 #if !defined(BOOST_ATOMIC_DETAIL_INT16_EXTENDED)
366 template< bool Signed >
367 struct operations< 1u, Signed > :
368 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 2u, Signed >::type >, 1u, Signed >
370 typedef typename make_storage_type< 2u, Signed >::aligned aligned_storage_type;
372 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 2u;
373 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
376 #elif !defined(BOOST_ATOMIC_DETAIL_INT32_EXTENDED)
378 template< bool Signed >
379 struct operations< 1u, Signed > :
380 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type >, 1u, Signed >
382 typedef typename make_storage_type< 4u, Signed >::aligned aligned_storage_type;
384 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 4u;
385 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
388 #elif !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
390 template< bool Signed >
391 struct operations< 1u, Signed > :
392 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 1u, Signed >
394 typedef typename make_storage_type< 8u, Signed >::aligned aligned_storage_type;
396 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 8u;
397 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
402 template< bool Signed >
403 struct operations< 1u, Signed > :
404 public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 1u, Signed >
406 typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
408 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 16u;
409 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
416 template< bool Signed >
417 struct operations< 1u, Signed > :
418 public gcc_atomic_operations< typename make_storage_type< 1u, Signed >::type >
420 typedef typename make_storage_type< 1u, Signed >::aligned aligned_storage_type;
422 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 1u;
423 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
429 #undef BOOST_ATOMIC_DETAIL_INT16_EXTENDED
430 #undef BOOST_ATOMIC_DETAIL_INT32_EXTENDED
431 #undef BOOST_ATOMIC_DETAIL_INT64_EXTENDED
433 BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
435 __atomic_thread_fence(atomics::detail::convert_memory_order_to_gcc(order));
438 BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
440 __atomic_signal_fence(atomics::detail::convert_memory_order_to_gcc(order));
443 } // namespace detail
444 } // namespace atomics
447 #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_