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_msvc_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_MSVC_X86_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_MSVC_X86_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: #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: #include Chris@102: #if !defined(_M_IX86) && !(defined(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE8) && defined(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE16)) 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(BOOST_MSVC) Chris@102: #pragma warning(push) Chris@102: // frame pointer register 'ebx' modified by inline assembly code. See the note below. Chris@102: #pragma warning(disable: 4731) Chris@102: #endif Chris@102: Chris@102: #if defined(_MSC_VER) && (defined(_M_AMD64) || (defined(_M_IX86) && defined(_M_IX86_FP) && _M_IX86_FP >= 2)) Chris@102: extern "C" void _mm_mfence(void); Chris@102: #if defined(BOOST_MSVC) Chris@102: #pragma intrinsic(_mm_mfence) Chris@102: #endif Chris@102: #endif Chris@102: Chris@102: namespace boost { Chris@102: namespace atomics { Chris@102: namespace detail { Chris@102: Chris@102: /* Chris@102: * Implementation note for asm blocks. Chris@102: * Chris@102: * http://msdn.microsoft.com/en-us/data/k1a8ss06%28v=vs.105%29 Chris@102: * Chris@102: * Some SSE types require eight-byte stack alignment, forcing the compiler to emit dynamic stack-alignment code. Chris@102: * To be able to access both the local variables and the function parameters after the alignment, the compiler Chris@102: * maintains two frame pointers. If the compiler performs frame pointer omission (FPO), it will use EBP and ESP. Chris@102: * If the compiler does not perform FPO, it will use EBX and EBP. To ensure code runs correctly, do not modify EBX Chris@102: * in asm code if the function requires dynamic stack alignment as it could modify the frame pointer. Chris@102: * Either move the eight-byte aligned types out of the function, or avoid using EBX. Chris@102: * Chris@102: * Since we have no way of knowing that the compiler uses FPO, we have to always save and restore ebx Chris@102: * whenever we have to clobber it. Additionally, we disable warning C4731 above so that the compiler Chris@102: * doesn't spam about ebx use. Chris@102: */ Chris@102: Chris@102: struct msvc_x86_operations_base Chris@102: { Chris@102: static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT Chris@102: { Chris@102: #if defined(_MSC_VER) && (defined(_M_AMD64) || (defined(_M_IX86) && defined(_M_IX86_FP) && _M_IX86_FP >= 2)) Chris@102: // Use mfence only if SSE2 is available Chris@102: _mm_mfence(); Chris@102: #else Chris@102: long tmp; Chris@102: BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&tmp, 0); Chris@102: #endif Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: BOOST_ATOMIC_DETAIL_COMPILER_BARRIER(); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: BOOST_ATOMIC_DETAIL_COMPILER_BARRIER(); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void fence_after_load(memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: BOOST_ATOMIC_DETAIL_COMPILER_BARRIER(); Chris@102: Chris@102: // On x86 and x86_64 there is no need for a hardware barrier, Chris@102: // even if seq_cst memory order is requested, because all Chris@102: // seq_cst writes are implemented with lock-prefixed operations Chris@102: // or xchg which has implied lock prefix. Therefore normal loads Chris@102: // are already ordered with seq_cst stores on these architectures. Chris@102: } Chris@102: }; Chris@102: Chris@102: template< typename T, typename Derived > Chris@102: struct msvc_x86_operations : Chris@102: public msvc_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_load(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: typedef typename make_signed< storage_type >::type signed_storage_type; Chris@102: return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(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< 4u, Signed > : Chris@102: public msvc_x86_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > > Chris@102: { Chris@102: typedef msvc_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: return static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, 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: return static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&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 previous = expected; Chris@102: storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous)); Chris@102: expected = old_val; Chris@102: return (previous == old_val); Chris@102: } Chris@102: Chris@102: #if defined(BOOST_ATOMIC_INTERLOCKED_AND) 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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v)); Chris@102: } Chris@102: #else 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: storage_type res = storage; Chris@102: while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {} Chris@102: return res; Chris@102: } Chris@102: #endif Chris@102: Chris@102: #if defined(BOOST_ATOMIC_INTERLOCKED_OR) 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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v)); Chris@102: } Chris@102: #else 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: storage_type res = storage; Chris@102: while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {} Chris@102: return res; Chris@102: } Chris@102: #endif Chris@102: Chris@102: #if defined(BOOST_ATOMIC_INTERLOCKED_XOR) 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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v)); Chris@102: } Chris@102: #else 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: storage_type res = storage; Chris@102: while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {} Chris@102: return res; Chris@102: } Chris@102: #endif Chris@102: }; Chris@102: Chris@102: #if defined(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE8) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: public msvc_x86_operations< typename make_storage_type< 1u, Signed >::type, operations< 1u, Signed > > Chris@102: { Chris@102: typedef msvc_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: return static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD8(&storage, 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: return static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE8(&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 previous = expected; Chris@102: storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE8(&storage, desired, previous)); Chris@102: expected = old_val; Chris@102: return (previous == old_val); 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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND8(&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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR8(&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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR8(&storage, v)); Chris@102: } Chris@102: }; Chris@102: Chris@102: #elif defined(_M_IX86) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 1u, Signed > : Chris@102: public msvc_x86_operations< typename make_storage_type< 1u, Signed >::type, operations< 1u, Signed > > Chris@102: { Chris@102: typedef msvc_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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: base_type::fence_before(order); Chris@102: __asm Chris@102: { Chris@102: mov edx, storage Chris@102: movzx eax, v Chris@102: lock xadd byte ptr [edx], al Chris@102: mov v, al Chris@102: }; Chris@102: base_type::fence_after(order); 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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: base_type::fence_before(order); Chris@102: __asm Chris@102: { Chris@102: mov edx, storage Chris@102: movzx eax, v Chris@102: xchg byte ptr [edx], al Chris@102: mov v, al Chris@102: }; Chris@102: base_type::fence_after(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) BOOST_NOEXCEPT Chris@102: { Chris@102: base_type::fence_before(success_order); Chris@102: bool success; Chris@102: __asm Chris@102: { Chris@102: mov esi, expected Chris@102: mov edi, storage Chris@102: movzx eax, byte ptr [esi] Chris@102: movzx edx, desired Chris@102: lock cmpxchg byte ptr [edi], dl Chris@102: mov byte ptr [esi], al Chris@102: sete success Chris@102: }; Chris@102: // The success and failure fences are equivalent anyway Chris@102: base_type::fence_after(success_order); Chris@102: return success; 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: base_type::fence_before(order); Chris@102: int backup; Chris@102: __asm Chris@102: { Chris@102: mov backup, ebx Chris@102: xor edx, edx Chris@102: mov edi, storage Chris@102: movzx ebx, v Chris@102: movzx eax, byte ptr [edi] Chris@102: align 16 Chris@102: again: Chris@102: mov dl, al Chris@102: and dl, bl Chris@102: lock cmpxchg byte ptr [edi], dl Chris@102: jne again Chris@102: mov v, al Chris@102: mov ebx, backup Chris@102: }; Chris@102: base_type::fence_after(order); Chris@102: return v; 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: base_type::fence_before(order); Chris@102: int backup; Chris@102: __asm Chris@102: { Chris@102: mov backup, ebx Chris@102: xor edx, edx Chris@102: mov edi, storage Chris@102: movzx ebx, v Chris@102: movzx eax, byte ptr [edi] Chris@102: align 16 Chris@102: again: Chris@102: mov dl, al Chris@102: or dl, bl Chris@102: lock cmpxchg byte ptr [edi], dl Chris@102: jne again Chris@102: mov v, al Chris@102: mov ebx, backup Chris@102: }; Chris@102: base_type::fence_after(order); Chris@102: return v; 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: base_type::fence_before(order); Chris@102: int backup; Chris@102: __asm Chris@102: { Chris@102: mov backup, ebx Chris@102: xor edx, edx Chris@102: mov edi, storage Chris@102: movzx ebx, v Chris@102: movzx eax, byte ptr [edi] Chris@102: align 16 Chris@102: again: Chris@102: mov dl, al Chris@102: xor dl, bl Chris@102: lock cmpxchg byte ptr [edi], dl Chris@102: jne again Chris@102: mov v, al Chris@102: mov ebx, backup Chris@102: }; Chris@102: base_type::fence_after(order); Chris@102: return v; 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< operations< 4u, Signed >, 1u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: Chris@102: #if defined(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE16) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: public msvc_x86_operations< typename make_storage_type< 2u, Signed >::type, operations< 2u, Signed > > Chris@102: { Chris@102: typedef msvc_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: return static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD16(&storage, 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: return static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE16(&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 previous = expected; Chris@102: storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE16(&storage, desired, previous)); Chris@102: expected = old_val; Chris@102: return (previous == old_val); 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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND16(&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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR16(&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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR16(&storage, v)); Chris@102: } Chris@102: }; Chris@102: Chris@102: #elif defined(_M_IX86) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 2u, Signed > : Chris@102: public msvc_x86_operations< typename make_storage_type< 2u, Signed >::type, operations< 2u, Signed > > Chris@102: { Chris@102: typedef msvc_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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: base_type::fence_before(order); Chris@102: __asm Chris@102: { Chris@102: mov edx, storage Chris@102: movzx eax, v Chris@102: lock xadd word ptr [edx], ax Chris@102: mov v, ax Chris@102: }; Chris@102: base_type::fence_after(order); 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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: base_type::fence_before(order); Chris@102: __asm Chris@102: { Chris@102: mov edx, storage Chris@102: movzx eax, v Chris@102: xchg word ptr [edx], ax Chris@102: mov v, ax Chris@102: }; Chris@102: base_type::fence_after(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) BOOST_NOEXCEPT Chris@102: { Chris@102: base_type::fence_before(success_order); Chris@102: bool success; Chris@102: __asm Chris@102: { Chris@102: mov esi, expected Chris@102: mov edi, storage Chris@102: movzx eax, word ptr [esi] Chris@102: movzx edx, desired Chris@102: lock cmpxchg word ptr [edi], dx Chris@102: mov word ptr [esi], ax Chris@102: sete success Chris@102: }; Chris@102: // The success and failure fences are equivalent anyway Chris@102: base_type::fence_after(success_order); Chris@102: return success; 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: base_type::fence_before(order); Chris@102: int backup; Chris@102: __asm Chris@102: { Chris@102: mov backup, ebx Chris@102: xor edx, edx Chris@102: mov edi, storage Chris@102: movzx ebx, v Chris@102: movzx eax, word ptr [edi] Chris@102: align 16 Chris@102: again: Chris@102: mov dx, ax Chris@102: and dx, bx Chris@102: lock cmpxchg word ptr [edi], dx Chris@102: jne again Chris@102: mov v, ax Chris@102: mov ebx, backup Chris@102: }; Chris@102: base_type::fence_after(order); Chris@102: return v; 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: base_type::fence_before(order); Chris@102: int backup; Chris@102: __asm Chris@102: { Chris@102: mov backup, ebx Chris@102: xor edx, edx Chris@102: mov edi, storage Chris@102: movzx ebx, v Chris@102: movzx eax, word ptr [edi] Chris@102: align 16 Chris@102: again: Chris@102: mov dx, ax Chris@102: or dx, bx Chris@102: lock cmpxchg word ptr [edi], dx Chris@102: jne again Chris@102: mov v, ax Chris@102: mov ebx, backup Chris@102: }; Chris@102: base_type::fence_after(order); Chris@102: return v; 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: base_type::fence_before(order); Chris@102: int backup; Chris@102: __asm Chris@102: { Chris@102: mov backup, ebx Chris@102: xor edx, edx Chris@102: mov edi, storage Chris@102: movzx ebx, v Chris@102: movzx eax, word ptr [edi] Chris@102: align 16 Chris@102: again: Chris@102: mov dx, ax Chris@102: xor dx, bx Chris@102: lock cmpxchg word ptr [edi], dx Chris@102: jne again Chris@102: mov v, ax Chris@102: mov ebx, backup Chris@102: }; Chris@102: base_type::fence_after(order); Chris@102: return v; 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< operations< 4u, Signed >, 2u, Signed > Chris@102: { Chris@102: }; Chris@102: Chris@102: #endif Chris@102: Chris@102: Chris@102: #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) Chris@102: Chris@102: template< bool Signed > Chris@102: struct msvc_dcas_x86 Chris@102: { Chris@102: typedef typename make_storage_type< 8u, Signed >::type storage_type; Chris@102: Chris@102: // Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 3A, 8.1.1. Guaranteed Atomic Operations: Chris@102: // Chris@102: // The Pentium processor (and newer processors since) guarantees that the following additional memory operations will always be carried out atomically: Chris@102: // * Reading or writing a quadword aligned on a 64-bit boundary Chris@102: // Chris@102: // Luckily, the memory is almost always 8-byte aligned in our case because atomic<> uses 64 bit native types for storage and dynamic memory allocations Chris@102: // have at least 8 byte alignment. The only unfortunate case is when atomic is placeod on the stack and it is not 8-byte aligned (like on 32 bit Windows). Chris@102: Chris@102: static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type volatile* p = &storage; Chris@102: if (((uint32_t)p & 0x00000007) == 0) Chris@102: { Chris@102: #if defined(_M_IX86_FP) && _M_IX86_FP >= 2 Chris@102: #if defined(__AVX__) Chris@102: __asm Chris@102: { Chris@102: mov edx, p Chris@102: vmovq xmm4, v Chris@102: vmovq qword ptr [edx], xmm4 Chris@102: }; Chris@102: #else Chris@102: __asm Chris@102: { Chris@102: mov edx, p Chris@102: movq xmm4, v Chris@102: movq qword ptr [edx], xmm4 Chris@102: }; Chris@102: #endif Chris@102: #else Chris@102: __asm Chris@102: { Chris@102: mov edx, p Chris@102: fild v Chris@102: fistp qword ptr [edx] Chris@102: }; Chris@102: #endif Chris@102: } Chris@102: else Chris@102: { Chris@102: int backup; Chris@102: __asm Chris@102: { Chris@102: mov backup, ebx Chris@102: mov edi, p Chris@102: mov ebx, dword ptr [v] Chris@102: mov ecx, dword ptr [v + 4] Chris@102: mov eax, dword ptr [edi] Chris@102: mov edx, dword ptr [edi + 4] Chris@102: align 16 Chris@102: again: Chris@102: lock cmpxchg8b qword ptr [edi] Chris@102: jne again Chris@102: mov ebx, backup Chris@102: }; Chris@102: } Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type const volatile* p = &storage; Chris@102: storage_type value; Chris@102: Chris@102: if (((uint32_t)p & 0x00000007) == 0) Chris@102: { Chris@102: #if defined(_M_IX86_FP) && _M_IX86_FP >= 2 Chris@102: #if defined(__AVX__) Chris@102: __asm Chris@102: { Chris@102: mov edx, p Chris@102: vmovq xmm4, qword ptr [edx] Chris@102: vmovq value, xmm4 Chris@102: }; Chris@102: #else Chris@102: __asm Chris@102: { Chris@102: mov edx, p Chris@102: movq xmm4, qword ptr [edx] Chris@102: movq value, xmm4 Chris@102: }; Chris@102: #endif Chris@102: #else Chris@102: __asm Chris@102: { Chris@102: mov edx, p Chris@102: fild qword ptr [edx] Chris@102: fistp value Chris@102: }; Chris@102: #endif Chris@102: } Chris@102: else Chris@102: { Chris@102: // We don't care for comparison result here; the previous value will be stored into value anyway. Chris@102: // Also we don't care for ebx and ecx values, they just have to be equal to eax and edx before cmpxchg8b. Chris@102: __asm Chris@102: { Chris@102: mov edi, p Chris@102: mov eax, ebx Chris@102: mov edx, ecx Chris@102: lock cmpxchg8b qword ptr [edi] Chris@102: mov dword ptr [value], eax Chris@102: mov dword ptr [value + 4], edx Chris@102: }; Chris@102: } Chris@102: Chris@102: return value; 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 volatile* p = &storage; Chris@102: #if defined(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE64) Chris@102: const storage_type old_val = (storage_type)BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE64(p, desired, expected); Chris@102: const bool result = (old_val == expected); Chris@102: expected = old_val; Chris@102: return result; Chris@102: #else Chris@102: bool result; Chris@102: int backup; Chris@102: __asm Chris@102: { Chris@102: mov backup, ebx Chris@102: mov edi, p Chris@102: mov esi, expected Chris@102: mov ebx, dword ptr [desired] Chris@102: mov ecx, dword ptr [desired + 4] Chris@102: mov eax, dword ptr [esi] Chris@102: mov edx, dword ptr [esi + 4] Chris@102: lock cmpxchg8b qword ptr [edi] Chris@102: mov dword ptr [esi], eax Chris@102: mov dword ptr [esi + 4], edx Chris@102: mov ebx, backup Chris@102: sete result Chris@102: }; Chris@102: return result; Chris@102: #endif 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 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< 8u, Signed > : Chris@102: public cas_based_operations< msvc_dcas_x86< Signed > > Chris@102: { Chris@102: }; Chris@102: Chris@102: #elif defined(_M_AMD64) Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 8u, Signed > : Chris@102: public msvc_x86_operations< typename make_storage_type< 8u, Signed >::type, operations< 8u, Signed > > Chris@102: { Chris@102: typedef msvc_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: return static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD64(&storage, 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: return static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE64(&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 previous = expected; Chris@102: storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE64(&storage, desired, previous)); Chris@102: expected = old_val; Chris@102: return (previous == old_val); 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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND64(&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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR64(&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 static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR64(&storage, v)); Chris@102: } 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 msvc_dcas_x86_64 Chris@102: { Chris@102: typedef typename make_storage_type< 16u, Signed >::type storage_type; Chris@102: Chris@102: static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type value = const_cast< storage_type& >(storage); Chris@102: while (!BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE128(&storage, v, &value)) {} Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type value = storage_type(); Chris@102: BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE128(&storage, value, &value); Chris@102: return value; 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: return !!BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE128(&storage, desired, &expected); 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 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< 16u, Signed > : Chris@102: public cas_based_operations< msvc_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: BOOST_ATOMIC_DETAIL_COMPILER_BARRIER(); Chris@102: if (order == memory_order_seq_cst) Chris@102: msvc_x86_operations_base::hardware_full_fence(); Chris@102: BOOST_ATOMIC_DETAIL_COMPILER_BARRIER(); 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: BOOST_ATOMIC_DETAIL_COMPILER_BARRIER(); Chris@102: } Chris@102: Chris@102: } // namespace detail Chris@102: } // namespace atomics Chris@102: } // namespace boost Chris@102: Chris@102: #if defined(BOOST_MSVC) Chris@102: #pragma warning(pop) Chris@102: #endif Chris@102: Chris@102: #endif // BOOST_ATOMIC_DETAIL_OPS_MSVC_X86_HPP_INCLUDED_