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) 2011 Helge Bahmann Chris@102: * Copyright (c) 2013 Tim Blechmann Chris@102: * Copyright (c) 2014 Andrey Semashev Chris@102: */ Chris@102: /*! Chris@102: * \file atomic/detail/ops_gcc_sync.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_SYNC_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_ Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@102: #pragma once Chris@102: #endif Chris@102: Chris@102: namespace boost { Chris@102: namespace atomics { Chris@102: namespace detail { Chris@102: Chris@102: struct gcc_sync_operations_base Chris@102: { Chris@102: static BOOST_FORCEINLINE void fence_before_store(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if ((order & memory_order_release) != 0) Chris@102: __sync_synchronize(); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if (order == memory_order_seq_cst) Chris@102: __sync_synchronize(); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void fence_after_load(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if ((order & (memory_order_acquire | memory_order_consume)) != 0) Chris@102: __sync_synchronize(); Chris@102: } Chris@102: }; Chris@102: Chris@102: template< typename T > Chris@102: struct gcc_sync_operations : Chris@102: public gcc_sync_operations_base 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: fence_before_store(order); Chris@102: storage = v; Chris@102: fence_after_store(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: storage_type v = storage; Chris@102: fence_after_load(order); Chris@102: return v; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __sync_fetch_and_add(&storage, v); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __sync_fetch_and_sub(&storage, v); 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: // GCC docs mention that not all architectures may support full exchange semantics for this intrinsic. However, GCC's implementation of Chris@102: // std::atomic<> uses this intrinsic unconditionally. We do so as well. In case if some architectures actually don't support this, we can always Chris@102: // add a check here and fall back to a CAS loop. Chris@102: if ((order & memory_order_release) != 0) Chris@102: __sync_synchronize(); Chris@102: return __sync_lock_test_and_set(&storage, v); 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, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type expected2 = expected; Chris@102: storage_type old_val = __sync_val_compare_and_swap(&storage, expected2, desired); Chris@102: Chris@102: if (old_val == expected2) Chris@102: { Chris@102: return true; Chris@102: } Chris@102: else Chris@102: { Chris@102: expected = old_val; Chris@102: return false; 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 compare_exchange_strong(storage, expected, desired, success_order, failure_order); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __sync_fetch_and_and(&storage, v); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __sync_fetch_and_or(&storage, v); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: return __sync_fetch_and_xor(&storage, v); 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: if ((order & memory_order_release) != 0) Chris@102: __sync_synchronize(); Chris@102: return !!__sync_lock_test_and_set(&storage, 1); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: __sync_lock_release(&storage); Chris@102: if (order == memory_order_seq_cst) Chris@102: __sync_synchronize(); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT Chris@102: { Chris@102: return true; Chris@102: } Chris@102: }; Chris@102: Chris@102: #if BOOST_ATOMIC_INT8_LOCK_FREE > 0 Chris@102: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1) Chris@102: public gcc_sync_operations< typename make_storage_type< 1u, Signed >::type > Chris@102: #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 2u, Signed >::type >, 1u, Signed > Chris@102: #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 4u, Signed >::type >, 1u, Signed > Chris@102: #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 1u, Signed > Chris@102: #else Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 1u, Signed > Chris@102: #endif Chris@102: { Chris@102: }; Chris@102: #endif Chris@102: Chris@102: #if BOOST_ATOMIC_INT16_LOCK_FREE > 0 Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) Chris@102: public gcc_sync_operations< typename make_storage_type< 2u, Signed >::type > Chris@102: #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 4u, Signed >::type >, 2u, Signed > Chris@102: #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 2u, Signed > Chris@102: #else Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 2u, Signed > Chris@102: #endif Chris@102: { Chris@102: }; Chris@102: #endif Chris@102: Chris@102: #if BOOST_ATOMIC_INT32_LOCK_FREE > 0 Chris@102: template< bool Signed > Chris@102: struct operations< 4u, Signed > : Chris@102: #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) Chris@102: public gcc_sync_operations< typename make_storage_type< 4u, Signed >::type > Chris@102: #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 4u, Signed > Chris@102: #else Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 4u, Signed > Chris@102: #endif Chris@102: { Chris@102: }; Chris@102: #endif Chris@102: Chris@102: #if BOOST_ATOMIC_INT64_LOCK_FREE > 0 Chris@102: template< bool Signed > Chris@102: struct operations< 8u, Signed > : Chris@102: #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) Chris@102: public gcc_sync_operations< typename make_storage_type< 8u, Signed >::type > Chris@102: #else Chris@102: public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 8u, Signed > Chris@102: #endif Chris@102: { Chris@102: }; Chris@102: #endif Chris@102: Chris@102: #if BOOST_ATOMIC_INT128_LOCK_FREE > 0 Chris@102: template< bool Signed > Chris@102: struct operations< 16u, Signed > : Chris@102: public gcc_sync_operations< typename make_storage_type< 16u, Signed >::type > Chris@102: { Chris@102: }; Chris@102: #endif Chris@102: Chris@102: BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if (order != memory_order_relaxed) Chris@102: __sync_synchronize(); Chris@102: } Chris@102: Chris@102: BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if (order != memory_order_relaxed) Chris@102: __asm__ __volatile__ ("" ::: "memory"); 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_SYNC_HPP_INCLUDED_