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) 2009, 2011 Helge Bahmann Chris@102: * Copyright (c) 2009 Phil Endecott Chris@102: * Copyright (c) 2013 Tim Blechmann Chris@102: * Linux-specific code by Phil Endecott Chris@102: * Copyright (c) 2014 Andrey Semashev Chris@102: */ Chris@102: /*! Chris@102: * \file atomic/detail/ops_linux_arm.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_LINUX_ARM_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_LINUX_ARM_HPP_INCLUDED_ Chris@102: Chris@102: #include 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: // Different ARM processors have different atomic instructions. In particular, Chris@102: // architecture versions before v6 (which are still in widespread use, e.g. the Chris@102: // Intel/Marvell XScale chips like the one in the NSLU2) have only atomic swap. Chris@102: // On Linux the kernel provides some support that lets us abstract away from Chris@102: // these differences: it provides emulated CAS and barrier functions at special Chris@102: // addresses that are guaranteed not to be interrupted by the kernel. Using Chris@102: // this facility is slightly slower than inline assembler would be, but much Chris@102: // faster than a system call. Chris@102: // Chris@102: // While this emulated CAS is "strong" in the sense that it does not fail Chris@102: // "spuriously" (i.e.: it never fails to perform the exchange when the value Chris@102: // found equals the value expected), it does not return the found value on Chris@102: // failure. To satisfy the atomic API, compare_exchange_{weak|strong} must Chris@102: // return the found value on failure, and we have to manually load this value Chris@102: // after the emulated CAS reports failure. This in turn introduces a race Chris@102: // between the CAS failing (due to the "wrong" value being found) and subsequently Chris@102: // loading (which might turn up the "right" value). From an application's Chris@102: // point of view this looks like "spurious failure", and therefore the Chris@102: // emulated CAS is only good enough to provide compare_exchange_weak Chris@102: // semantics. Chris@102: Chris@102: struct linux_arm_cas_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: hardware_full_fence(); 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: hardware_full_fence(); 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_consume | memory_order_acquire)) != 0) Chris@102: hardware_full_fence(); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT Chris@102: { Chris@102: typedef void (*kernel_dmb_t)(void); Chris@102: ((kernel_dmb_t)0xffff0fa0)(); Chris@102: } Chris@102: }; Chris@102: Chris@102: template< bool Signed > Chris@102: struct linux_arm_cas : Chris@102: public linux_arm_cas_base Chris@102: { Chris@102: typedef typename make_storage_type< 4u, Signed >::type 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 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: while (true) Chris@102: { Chris@102: storage_type tmp = expected; Chris@102: if (compare_exchange_weak(storage, tmp, desired, success_order, failure_order)) Chris@102: return true; Chris@102: if (tmp != expected) Chris@102: { Chris@102: expected = tmp; Chris@102: return false; Chris@102: } 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, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: typedef storage_type (*kernel_cmpxchg32_t)(storage_type oldval, storage_type newval, volatile storage_type* ptr); Chris@102: Chris@102: if (((kernel_cmpxchg32_t)0xffff0fc0)(expected, desired, &storage) == 0) Chris@102: { Chris@102: return true; Chris@102: } Chris@102: else Chris@102: { Chris@102: expected = storage; Chris@102: return false; Chris@102: } 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: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: public extending_cas_based_operations< cas_based_operations< linux_arm_cas< Signed > >, 1u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: public extending_cas_based_operations< cas_based_operations< linux_arm_cas< Signed > >, 2u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 4u, Signed > : Chris@102: public cas_based_operations< linux_arm_cas< Signed > > Chris@102: { Chris@102: }; 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: linux_arm_cas_base::hardware_full_fence(); 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_LINUX_ARM_HPP_INCLUDED_