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) 2014 Andrey Semashev Chris@102: */ Chris@102: /*! Chris@102: * \file atomic/detail/ops_emulated.hpp Chris@102: * Chris@102: * This header contains lockpool-based implementation of the \c operations template. Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_ATOMIC_DETAIL_OPS_EMULATED_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_EMULATED_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: template< typename T > Chris@102: struct emulated_operations 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) BOOST_NOEXCEPT Chris@102: { Chris@102: lockpool::scoped_lock lock(&storage); Chris@102: const_cast< storage_type& >(storage) = v; 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: lockpool::scoped_lock lock(&storage); Chris@102: return const_cast< storage_type const& >(storage); 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: storage_type& s = const_cast< storage_type& >(storage); Chris@102: lockpool::scoped_lock lock(&storage); Chris@102: storage_type old_val = s; Chris@102: s += v; Chris@102: return old_val; 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: storage_type& s = const_cast< storage_type& >(storage); Chris@102: lockpool::scoped_lock lock(&storage); Chris@102: storage_type old_val = s; Chris@102: s -= v; Chris@102: return old_val; 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: storage_type& s = const_cast< storage_type& >(storage); Chris@102: lockpool::scoped_lock lock(&storage); Chris@102: storage_type old_val = s; Chris@102: s = v; Chris@102: return old_val; 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& s = const_cast< storage_type& >(storage); Chris@102: lockpool::scoped_lock lock(&storage); Chris@102: storage_type old_val = s; Chris@102: const bool res = old_val == expected; Chris@102: if (res) Chris@102: s = desired; Chris@102: expected = old_val; Chris@102: Chris@102: return res; 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: storage_type& s = const_cast< storage_type& >(storage); Chris@102: lockpool::scoped_lock lock(&storage); Chris@102: storage_type old_val = s; Chris@102: s &= v; Chris@102: return old_val; 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& s = const_cast< storage_type& >(storage); Chris@102: lockpool::scoped_lock lock(&storage); Chris@102: storage_type old_val = s; Chris@102: s |= v; Chris@102: return old_val; 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& s = const_cast< storage_type& >(storage); Chris@102: lockpool::scoped_lock lock(&storage); Chris@102: storage_type old_val = s; Chris@102: s ^= v; Chris@102: return old_val; 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 !!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 false; Chris@102: } Chris@102: }; Chris@102: Chris@102: template< unsigned int Size, bool Signed > Chris@102: struct operations : Chris@102: public emulated_operations< typename make_storage_type< Size, Signed >::type > Chris@102: { 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_EMULATED_HPP_INCLUDED_