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 Helge Bahmann Chris@102: * Copyright (c) 2012 Tim Blechmann Chris@102: * Copyright (c) 2014 Andrey Semashev Chris@102: */ Chris@102: /*! Chris@102: * \file atomic/detail/ops_gcc_x86.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_X86_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_GCC_X86_HPP_INCLUDED_ Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #if 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: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@102: #pragma once Chris@102: #endif Chris@102: Chris@102: #if defined(__x86_64__) Chris@102: #define BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER "rdx" Chris@102: #else Chris@102: #define BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER "edx" Chris@102: #endif Chris@102: Chris@102: namespace boost { Chris@102: namespace atomics { Chris@102: namespace detail { Chris@102: Chris@102: struct gcc_x86_operations_base Chris@102: { Chris@102: static BOOST_FORCEINLINE void fence_before(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if ((order & memory_order_release) != 0) Chris@102: __asm__ __volatile__ ("" ::: "memory"); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void fence_after(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if ((order & memory_order_acquire) != 0) Chris@102: __asm__ __volatile__ ("" ::: "memory"); Chris@102: } Chris@102: }; Chris@102: Chris@102: template< typename T, typename Derived > Chris@102: struct gcc_x86_operations : Chris@102: public gcc_x86_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: if (order != memory_order_seq_cst) Chris@102: { Chris@102: fence_before(order); Chris@102: storage = v; Chris@102: fence_after(order); Chris@102: } Chris@102: else Chris@102: { Chris@102: Derived::exchange(storage, v, order); Chris@102: } 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(order); Chris@102: return v; 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 Derived::fetch_add(storage, -v, order); 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 Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_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 !!Derived::exchange(storage, (storage_type)1, 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: store(storage, (storage_type)0, order); 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 gcc_x86_operations< typename make_storage_type< 1u, Signed >::type, operations< 1u, Signed > > Chris@102: { Chris@102: typedef gcc_x86_operations< typename make_storage_type< 1u, Signed >::type, operations< 1u, Signed > > base_type; Chris@102: typedef typename base_type::storage_type storage_type; 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: __asm__ __volatile__ Chris@102: ( Chris@102: "lock; xaddb %0, %1" Chris@102: : "+q" (v), "+m" (storage) Chris@102: : Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory" Chris@102: ); Chris@102: return v; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "xchgb %0, %1" Chris@102: : "+q" (v), "+m" (storage) Chris@102: : Chris@102: : "memory" Chris@102: ); 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, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type previous = expected; Chris@102: bool success; Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "lock; cmpxchgb %3, %1\n\t" Chris@102: "sete %2" Chris@102: : "+a" (previous), "+m" (storage), "=q" (success) Chris@102: : "q" (desired) Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory" Chris@102: ); Chris@102: expected = previous; Chris@102: return success; Chris@102: } Chris@102: Chris@102: #define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)\ Chris@102: __asm__ __volatile__\ Chris@102: (\ Chris@102: "xor %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER ", %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER "\n\t"\ Chris@102: ".align 16\n\t"\ Chris@102: "1: movb %[arg], %%dl\n\t"\ Chris@102: op " %%al, %%dl\n\t"\ Chris@102: "lock; cmpxchgb %%dl, %[storage]\n\t"\ Chris@102: "jne 1b"\ Chris@102: : [res] "+a" (result), [storage] "+m" (storage)\ Chris@102: : [arg] "q" (argument)\ Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER, "memory"\ 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("andb", v, res); Chris@102: return res; 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("orb", v, res); Chris@102: return res; 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("xorb", v, res); Chris@102: return res; Chris@102: } Chris@102: Chris@102: #undef BOOST_ATOMIC_DETAIL_CAS_LOOP Chris@102: }; Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: public gcc_x86_operations< typename make_storage_type< 2u, Signed >::type, operations< 2u, Signed > > Chris@102: { Chris@102: typedef gcc_x86_operations< typename make_storage_type< 2u, Signed >::type, operations< 2u, Signed > > base_type; Chris@102: typedef typename base_type::storage_type storage_type; 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: __asm__ __volatile__ Chris@102: ( Chris@102: "lock; xaddw %0, %1" Chris@102: : "+q" (v), "+m" (storage) Chris@102: : Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory" Chris@102: ); Chris@102: return v; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "xchgw %0, %1" Chris@102: : "+q" (v), "+m" (storage) Chris@102: : Chris@102: : "memory" Chris@102: ); 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, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type previous = expected; Chris@102: bool success; Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "lock; cmpxchgw %3, %1\n\t" Chris@102: "sete %2" Chris@102: : "+a" (previous), "+m" (storage), "=q" (success) Chris@102: : "q" (desired) Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory" Chris@102: ); Chris@102: expected = previous; Chris@102: return success; Chris@102: } Chris@102: Chris@102: #define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)\ Chris@102: __asm__ __volatile__\ Chris@102: (\ Chris@102: "xor %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER ", %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER "\n\t"\ Chris@102: ".align 16\n\t"\ Chris@102: "1: movw %[arg], %%dx\n\t"\ Chris@102: op " %%ax, %%dx\n\t"\ Chris@102: "lock; cmpxchgw %%dx, %[storage]\n\t"\ Chris@102: "jne 1b"\ Chris@102: : [res] "+a" (result), [storage] "+m" (storage)\ Chris@102: : [arg] "q" (argument)\ Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER, "memory"\ 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("andw", v, res); Chris@102: return res; 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("orw", v, res); Chris@102: return res; 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("xorw", v, res); Chris@102: return res; Chris@102: } Chris@102: Chris@102: #undef BOOST_ATOMIC_DETAIL_CAS_LOOP Chris@102: }; Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 4u, Signed > : Chris@102: public gcc_x86_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > > Chris@102: { Chris@102: typedef gcc_x86_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > > base_type; Chris@102: typedef typename base_type::storage_type storage_type; 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: __asm__ __volatile__ Chris@102: ( Chris@102: "lock; xaddl %0, %1" Chris@102: : "+r" (v), "+m" (storage) Chris@102: : Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory" Chris@102: ); Chris@102: return v; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "xchgl %0, %1" Chris@102: : "+r" (v), "+m" (storage) Chris@102: : Chris@102: : "memory" Chris@102: ); 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, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type previous = expected; Chris@102: bool success; Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "lock; cmpxchgl %3, %1\n\t" Chris@102: "sete %2" Chris@102: : "+a" (previous), "+m" (storage), "=q" (success) Chris@102: : "r" (desired) Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory" Chris@102: ); Chris@102: expected = previous; Chris@102: return success; Chris@102: } Chris@102: Chris@102: #define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)\ Chris@102: __asm__ __volatile__\ Chris@102: (\ Chris@102: "xor %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER ", %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER "\n\t"\ Chris@102: ".align 16\n\t"\ Chris@102: "1: movl %[arg], %%edx\n\t"\ Chris@102: op " %%eax, %%edx\n\t"\ Chris@102: "lock; cmpxchgl %%edx, %[storage]\n\t"\ Chris@102: "jne 1b"\ Chris@102: : [res] "+a" (result), [storage] "+m" (storage)\ Chris@102: : [arg] "r" (argument)\ Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER, "memory"\ 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("andl", v, res); Chris@102: return res; 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("orl", v, res); Chris@102: return res; 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("xorl", v, res); Chris@102: return res; Chris@102: } Chris@102: Chris@102: #undef BOOST_ATOMIC_DETAIL_CAS_LOOP Chris@102: }; Chris@102: Chris@102: #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) Chris@102: 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 defined(__x86_64__) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 8u, Signed > : Chris@102: public gcc_x86_operations< typename make_storage_type< 8u, Signed >::type, operations< 8u, Signed > > Chris@102: { Chris@102: typedef gcc_x86_operations< typename make_storage_type< 8u, Signed >::type, operations< 8u, Signed > > base_type; Chris@102: typedef typename base_type::storage_type storage_type; 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: __asm__ __volatile__ Chris@102: ( Chris@102: "lock; xaddq %0, %1" Chris@102: : "+r" (v), "+m" (storage) Chris@102: : Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory" Chris@102: ); Chris@102: return v; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "xchgq %0, %1" Chris@102: : "+r" (v), "+m" (storage) Chris@102: : Chris@102: : "memory" Chris@102: ); 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, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type previous = expected; Chris@102: bool success; Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "lock; cmpxchgq %3, %1\n\t" Chris@102: "sete %2" Chris@102: : "+a" (previous), "+m" (storage), "=q" (success) Chris@102: : "r" (desired) Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory" Chris@102: ); Chris@102: expected = previous; Chris@102: return success; Chris@102: } Chris@102: Chris@102: #define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)\ Chris@102: __asm__ __volatile__\ Chris@102: (\ Chris@102: "xor %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER ", %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER "\n\t"\ Chris@102: ".align 16\n\t"\ Chris@102: "1: movq %[arg], %%rdx\n\t"\ Chris@102: op " %%rax, %%rdx\n\t"\ Chris@102: "lock; cmpxchgq %%rdx, %[storage]\n\t"\ Chris@102: "jne 1b"\ Chris@102: : [res] "+a" (result), [storage] "+m" (storage)\ Chris@102: : [arg] "r" (argument)\ Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER, "memory"\ 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("andq", v, res); Chris@102: return res; 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("orq", v, res); Chris@102: return res; 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: storage_type res = storage; Chris@102: BOOST_ATOMIC_DETAIL_CAS_LOOP("xorq", v, res); Chris@102: return res; Chris@102: } Chris@102: Chris@102: #undef BOOST_ATOMIC_DETAIL_CAS_LOOP Chris@102: }; Chris@102: Chris@102: #endif Chris@102: Chris@102: #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B) Chris@102: 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: #endif // defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B) Chris@102: Chris@102: BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if (order == memory_order_seq_cst) Chris@102: { Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: #if defined(__x86_64__) || defined(__SSE2__) Chris@102: "mfence\n" Chris@102: #else Chris@102: "lock; addl $0, (%%esp)\n" Chris@102: #endif Chris@102: ::: "memory" Chris@102: ); Chris@102: } Chris@102: else if ((order & (memory_order_acquire | memory_order_release)) != 0) Chris@102: { Chris@102: __asm__ __volatile__ ("" ::: "memory"); Chris@102: } 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: #undef BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER Chris@102: Chris@102: #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_X86_HPP_INCLUDED_