Chris@102: /* Chris@102: * Distributed under the Boost Software License, Version 1.0. Chris@102: * (See accompanying file LICENSE_1_0.txt or copy at Chris@102: * http://www.boost.org/LICENSE_1_0.txt) Chris@102: * Chris@102: * Copyright (c) 2014 Andrey Semashev Chris@102: */ Chris@102: /*! Chris@102: * \file atomic/detail/ops_gcc_atomic.hpp Chris@102: * Chris@102: * This header contains implementation of the \c operations template. Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_ Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #if defined(__clang__) && (defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) || defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B)) Chris@102: #include Chris@102: #include Chris@102: #endif Chris@102: Chris@102: #if __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE || __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE ||\ Chris@102: __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE || __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE ||\ Chris@102: __GCC_ATOMIC_CHAR_LOCK_FREE != BOOST_ATOMIC_CHAR_LOCK_FREE || __GCC_ATOMIC_BOOL_LOCK_FREE != BOOST_ATOMIC_BOOL_LOCK_FREE ||\ Chris@102: __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE Chris@102: // There are platforms where we need to use larger storage types Chris@102: #include Chris@102: #include Chris@102: #endif Chris@102: Chris@102: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@102: #pragma once Chris@102: #endif Chris@102: Chris@102: #if defined(__INTEL_COMPILER) Chris@102: // This is used to suppress warning #32013 described below for Intel Compiler. Chris@102: // In debug builds the compiler does not inline any functions, so basically Chris@102: // every atomic function call results in this warning. I don't know any other Chris@102: // way to selectively disable just this one warning. Chris@102: #pragma system_header Chris@102: #endif Chris@102: Chris@102: namespace boost { Chris@102: namespace atomics { Chris@102: namespace detail { Chris@102: Chris@102: /*! Chris@102: * The function converts \c boost::memory_order values to the compiler-specific constants. Chris@102: * Chris@102: * NOTE: The intention is that the function is optimized away by the compiler, and the Chris@102: * compiler-specific constants are passed to the intrinsics. I know constexpr doesn't Chris@102: * work in this case because the standard atomics interface require memory ordering Chris@102: * constants to be passed as function arguments, at which point they stop being constexpr. Chris@102: * However it is crucial that the compiler sees constants and not runtime values, Chris@102: * because otherwise it just ignores the ordering value and always uses seq_cst. Chris@102: * This is the case with Intel C++ Compiler 14.0.3 (Composer XE 2013 SP1, update 3) and Chris@102: * gcc 4.8.2. Intel Compiler issues a warning in this case: Chris@102: * Chris@102: * warning #32013: Invalid memory order specified. Defaulting to seq_cst memory order. Chris@102: * Chris@102: * while gcc acts silently. Chris@102: * Chris@102: * To mitigate the problem ALL functions, including the atomic<> members must be Chris@102: * declared with BOOST_FORCEINLINE. In this case the compilers are able to see that Chris@102: * all functions are called with constant orderings and call intrinstcts properly. Chris@102: * Chris@102: * Unfortunately, this still doesn't work in debug mode as the compiler doesn't Chris@102: * inline functions even when marked with BOOST_FORCEINLINE. In this case all atomic Chris@102: * operaions will be executed with seq_cst semantics. Chris@102: */ Chris@102: BOOST_FORCEINLINE BOOST_CONSTEXPR int convert_memory_order_to_gcc(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return (order == memory_order_relaxed ? __ATOMIC_RELAXED : (order == memory_order_consume ? __ATOMIC_CONSUME : Chris@102: (order == memory_order_acquire ? __ATOMIC_ACQUIRE : (order == memory_order_release ? __ATOMIC_RELEASE : Chris@102: (order == memory_order_acq_rel ? __ATOMIC_ACQ_REL : __ATOMIC_SEQ_CST))))); Chris@102: } Chris@102: Chris@102: template< typename T > Chris@102: struct gcc_atomic_operations Chris@102: { Chris@102: typedef T storage_type; Chris@102: Chris@102: static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: __atomic_store_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_load_n(&storage, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_fetch_add(&storage, v, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_fetch_sub(&storage, v, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_exchange_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool compare_exchange_strong( Chris@102: storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_compare_exchange_n Chris@102: ( Chris@102: &storage, &expected, desired, false, Chris@102: atomics::detail::convert_memory_order_to_gcc(success_order), Chris@102: atomics::detail::convert_memory_order_to_gcc(failure_order) Chris@102: ); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool compare_exchange_weak( Chris@102: storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_compare_exchange_n Chris@102: ( Chris@102: &storage, &expected, desired, true, Chris@102: atomics::detail::convert_memory_order_to_gcc(success_order), Chris@102: atomics::detail::convert_memory_order_to_gcc(failure_order) Chris@102: ); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_fetch_and(&storage, v, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_fetch_or(&storage, v, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_fetch_xor(&storage, v, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_test_and_set(&storage, atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: __atomic_clear(const_cast< storage_type* >(&storage), atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile& storage) BOOST_NOEXCEPT Chris@102: { Chris@102: return __atomic_is_lock_free(sizeof(storage_type), &storage); Chris@102: } Chris@102: }; Chris@102: Chris@102: #if BOOST_ATOMIC_INT128_LOCK_FREE > 0 Chris@102: #if defined(__clang__) && defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B) Chris@102: Chris@102: // Workaround for clang bug: http://llvm.org/bugs/show_bug.cgi?id=19149 Chris@102: // Clang 3.4 does not implement 128-bit __atomic* intrinsics even though it defines __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 Chris@102: template< bool Signed > Chris@102: struct operations< 16u, Signed > : Chris@102: public cas_based_operations< gcc_dcas_x86_64< Signed > > Chris@102: { Chris@102: }; Chris@102: Chris@102: #else Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 16u, Signed > : Chris@102: public gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: #endif Chris@102: Chris@102: Chris@102: #if BOOST_ATOMIC_INT64_LOCK_FREE > 0 Chris@102: #if defined(__clang__) && defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) Chris@102: Chris@102: // Workaround for clang bug http://llvm.org/bugs/show_bug.cgi?id=19355 Chris@102: template< bool Signed > Chris@102: struct operations< 8u, Signed > : Chris@102: public cas_based_operations< gcc_dcas_x86< Signed > > Chris@102: { Chris@102: }; Chris@102: Chris@102: #elif (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 8 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 8 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 8 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 8 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 8 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE) Chris@102: Chris@102: #define BOOST_ATOMIC_DETAIL_INT64_EXTENDED Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 8u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 8u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #else Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 8u, Signed > : Chris@102: public gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: #endif Chris@102: Chris@102: #if BOOST_ATOMIC_INT32_LOCK_FREE > 0 Chris@102: #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 4 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 4 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 4 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 4 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 4 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE) Chris@102: Chris@102: #define BOOST_ATOMIC_DETAIL_INT32_EXTENDED Chris@102: Chris@102: #if !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 4u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 4u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #else // !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 4u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 4u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif // !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED) Chris@102: Chris@102: #else Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 4u, Signed > : Chris@102: public gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: #endif Chris@102: Chris@102: #if BOOST_ATOMIC_INT16_LOCK_FREE > 0 Chris@102: #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 2 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 2 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 2 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 2 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 2 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE) Chris@102: Chris@102: #define BOOST_ATOMIC_DETAIL_INT16_EXTENDED Chris@102: Chris@102: #if !defined(BOOST_ATOMIC_DETAIL_INT32_EXTENDED) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type >, 2u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #elif !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 2u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #else Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 2u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: Chris@102: #else Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: public gcc_atomic_operations< typename make_storage_type< 2u, Signed >::type > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: #endif Chris@102: Chris@102: #if BOOST_ATOMIC_INT8_LOCK_FREE > 0 Chris@102: #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 1 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 1 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 1 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 1 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\ Chris@102: (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 1 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE) ||\ Chris@102: (__GCC_ATOMIC_CHAR_LOCK_FREE != BOOST_ATOMIC_CHAR_LOCK_FREE) ||\ Chris@102: (__GCC_ATOMIC_BOOL_LOCK_FREE != BOOST_ATOMIC_BOOL_LOCK_FREE) Chris@102: Chris@102: #if !defined(BOOST_ATOMIC_DETAIL_INT16_EXTENDED) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 2u, Signed >::type >, 1u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #elif !defined(BOOST_ATOMIC_DETAIL_INT32_EXTENDED) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type >, 1u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #elif !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 1u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #else Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 1u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: Chris@102: #else Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: public gcc_atomic_operations< typename make_storage_type< 1u, Signed >::type > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: #endif Chris@102: Chris@102: #undef BOOST_ATOMIC_DETAIL_INT16_EXTENDED Chris@102: #undef BOOST_ATOMIC_DETAIL_INT32_EXTENDED Chris@102: #undef BOOST_ATOMIC_DETAIL_INT64_EXTENDED Chris@102: Chris@102: BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: __atomic_thread_fence(atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: __atomic_signal_fence(atomics::detail::convert_memory_order_to_gcc(order)); Chris@102: } Chris@102: Chris@102: } // namespace detail Chris@102: } // namespace atomics Chris@102: } // namespace boost Chris@102: Chris@102: #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_