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_extending_cas_based.hpp Chris@102: * Chris@102: * This header contains a boilerplate of the \c operations template implementation that requires sign/zero extension in arithmetic operations. Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_ATOMIC_DETAIL_OPS_EXTENDING_CAS_BASED_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_EXTENDING_CAS_BASED_HPP_INCLUDED_ Chris@102: 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 Base, unsigned int Size, bool Signed > Chris@102: struct extending_cas_based_operations : Chris@102: public Base Chris@102: { Chris@102: typedef typename Base::storage_type storage_type; Chris@102: typedef typename make_storage_type< Size, Signed >::type emulated_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: emulated_storage_type new_val; Chris@102: do Chris@102: { Chris@102: new_val = static_cast< emulated_storage_type >(old_val) + static_cast< emulated_storage_type >(v); Chris@102: } Chris@102: while (!Base::compare_exchange_weak(storage, old_val, static_cast< storage_type >(new_val), 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: emulated_storage_type new_val; Chris@102: do Chris@102: { Chris@102: new_val = static_cast< emulated_storage_type >(old_val) - static_cast< emulated_storage_type >(v); Chris@102: } Chris@102: while (!Base::compare_exchange_weak(storage, old_val, static_cast< storage_type >(new_val), order, memory_order_relaxed)); Chris@102: return old_val; 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_EXTENDING_CAS_BASED_HPP_INCLUDED_