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_cas_based.hpp Chris@102: * Chris@102: * This header contains CAS-based implementation of the \c operations template. Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_ATOMIC_DETAIL_OPS_CAS_BASED_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_CAS_BASED_HPP_INCLUDED_ Chris@102: 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 Base > Chris@102: struct cas_based_operations : Chris@102: public Base Chris@102: { Chris@102: typedef typename Base::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: storage_type old_val = Base::load(storage, memory_order_relaxed); Chris@102: while (!Base::compare_exchange_weak(storage, old_val, old_val + v, order, memory_order_relaxed)) {} 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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type old_val = Base::load(storage, memory_order_relaxed); Chris@102: while (!Base::compare_exchange_weak(storage, old_val, old_val - v, order, memory_order_relaxed)) {} 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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type old_val = Base::load(storage, memory_order_relaxed); Chris@102: while (!Base::compare_exchange_weak(storage, old_val, v, order, memory_order_relaxed)) {} Chris@102: return 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: storage_type old_val = Base::load(storage, memory_order_relaxed); Chris@102: while (!Base::compare_exchange_weak(storage, old_val, old_val & v, order, memory_order_relaxed)) {} 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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type old_val = Base::load(storage, memory_order_relaxed); Chris@102: while (!Base::compare_exchange_weak(storage, old_val, old_val | v, order, memory_order_relaxed)) {} 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 order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type old_val = Base::load(storage, memory_order_relaxed); Chris@102: while (!Base::compare_exchange_weak(storage, old_val, old_val ^ v, order, memory_order_relaxed)) {} 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: Base::store(storage, (storage_type)0, order); 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_CAS_BASED_HPP_INCLUDED_