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_windows.hpp Chris@102: * Chris@102: * This header contains implementation of the \c operations template. Chris@102: * Chris@102: * This implementation is the most basic version for Windows. It should Chris@102: * work for any non-MSVC-like compilers as long as there are Interlocked WinAPI Chris@102: * functions available. This version is also used for WinCE. Chris@102: * Chris@102: * Notably, this implementation is not as efficient as other Chris@102: * versions based on compiler intrinsics. Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_WINDOWS_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: #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 windows_operations_base Chris@102: { Chris@102: static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT Chris@102: { Chris@102: long tmp; Chris@102: BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&tmp, 0); 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: Chris@102: template< typename T, typename Derived > Chris@102: struct windows_operations : Chris@102: public windows_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: Derived::exchange(storage, v, 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 Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, 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: 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 windows_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > > Chris@102: { Chris@102: typedef windows_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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: base_type::fence_before(order); Chris@102: v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v)); 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: v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v)); 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 failure_order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type previous = expected; Chris@102: base_type::fence_before(success_order); 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: // The success and failure fences are the same anyway Chris@102: base_type::fence_after(success_order); 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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: #if defined(BOOST_ATOMIC_INTERLOCKED_AND) Chris@102: base_type::fence_before(order); Chris@102: v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v)); Chris@102: base_type::fence_after(order); Chris@102: return v; Chris@102: #else 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: #endif 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: #if defined(BOOST_ATOMIC_INTERLOCKED_OR) Chris@102: base_type::fence_before(order); Chris@102: v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v)); Chris@102: base_type::fence_after(order); Chris@102: return v; Chris@102: #else 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: #endif 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: #if defined(BOOST_ATOMIC_INTERLOCKED_XOR) Chris@102: base_type::fence_before(order); Chris@102: v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v)); Chris@102: base_type::fence_after(order); Chris@102: return v; Chris@102: #else 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: #endif Chris@102: } Chris@102: }; 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: 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: 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: windows_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: #endif // BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_