Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:33:53

0001 /*
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * Copyright (c) 2014 Andrey Semashev
0007  */
0008 /*!
0009  * \file   atomic/detail/extending_cas_based_arithmetic.hpp
0010  *
0011  * This header contains a boilerplate of core atomic operations that require sign/zero extension in arithmetic operations.
0012  */
0013 
0014 #ifndef BOOST_ATOMIC_DETAIL_EXTENDING_CAS_BASED_ARITHMETIC_HPP_INCLUDED_
0015 #define BOOST_ATOMIC_DETAIL_EXTENDING_CAS_BASED_ARITHMETIC_HPP_INCLUDED_
0016 
0017 #include <cstddef>
0018 #include <boost/memory_order.hpp>
0019 #include <boost/atomic/detail/config.hpp>
0020 #include <boost/atomic/detail/storage_traits.hpp>
0021 #include <boost/atomic/detail/integral_conversions.hpp>
0022 #include <boost/atomic/detail/header.hpp>
0023 
0024 #ifdef BOOST_HAS_PRAGMA_ONCE
0025 #pragma once
0026 #endif
0027 
0028 namespace boost {
0029 namespace atomics {
0030 namespace detail {
0031 
0032 template< typename Base, std::size_t Size, bool Signed >
0033 struct extending_cas_based_arithmetic :
0034     public Base
0035 {
0036     typedef typename Base::storage_type storage_type;
0037     typedef typename storage_traits< Size >::type emulated_storage_type;
0038 
0039     static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0040     {
0041         storage_type old_val;
0042         atomics::detail::non_atomic_load(storage, old_val);
0043         storage_type new_val;
0044         do
0045         {
0046             new_val = atomics::detail::integral_extend< Signed, storage_type >(static_cast< emulated_storage_type >(old_val + v));
0047         }
0048         while (!Base::compare_exchange_weak(storage, old_val, new_val, order, memory_order_relaxed));
0049         return old_val;
0050     }
0051 
0052     static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0053     {
0054         storage_type old_val;
0055         atomics::detail::non_atomic_load(storage, old_val);
0056         storage_type new_val;
0057         do
0058         {
0059             new_val = atomics::detail::integral_extend< Signed, storage_type >(static_cast< emulated_storage_type >(old_val - v));
0060         }
0061         while (!Base::compare_exchange_weak(storage, old_val, new_val, order, memory_order_relaxed));
0062         return old_val;
0063     }
0064 };
0065 
0066 } // namespace detail
0067 } // namespace atomics
0068 } // namespace boost
0069 
0070 #include <boost/atomic/detail/footer.hpp>
0071 
0072 #endif // BOOST_ATOMIC_DETAIL_EXTENDING_CAS_BASED_ARITHMETIC_HPP_INCLUDED_