Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:43:47

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/core_ops_cas_based.hpp
0010  *
0011  * This header contains CAS-based implementation of core atomic operations.
0012  */
0013 
0014 #ifndef BOOST_ATOMIC_DETAIL_CORE_OPS_CAS_BASED_HPP_INCLUDED_
0015 #define BOOST_ATOMIC_DETAIL_CORE_OPS_CAS_BASED_HPP_INCLUDED_
0016 
0017 #include <boost/memory_order.hpp>
0018 #include <boost/atomic/detail/config.hpp>
0019 #include <boost/atomic/detail/header.hpp>
0020 
0021 #ifdef BOOST_HAS_PRAGMA_ONCE
0022 #pragma once
0023 #endif
0024 
0025 namespace boost {
0026 namespace atomics {
0027 namespace detail {
0028 
0029 template< typename Base >
0030 struct core_operations_cas_based :
0031     public Base
0032 {
0033     typedef typename Base::storage_type storage_type;
0034 
0035     static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = true;
0036 
0037     static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0038     {
0039         storage_type old_val;
0040         atomics::detail::non_atomic_load(storage, old_val);
0041         while (!Base::compare_exchange_weak(storage, old_val, old_val + v, order, memory_order_relaxed)) {}
0042         return old_val;
0043     }
0044 
0045     static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0046     {
0047         storage_type old_val;
0048         atomics::detail::non_atomic_load(storage, old_val);
0049         while (!Base::compare_exchange_weak(storage, old_val, old_val - v, order, memory_order_relaxed)) {}
0050         return old_val;
0051     }
0052 
0053     static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0054     {
0055         storage_type old_val;
0056         atomics::detail::non_atomic_load(storage, old_val);
0057         while (!Base::compare_exchange_weak(storage, old_val, old_val & v, order, memory_order_relaxed)) {}
0058         return old_val;
0059     }
0060 
0061     static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0062     {
0063         storage_type old_val;
0064         atomics::detail::non_atomic_load(storage, old_val);
0065         while (!Base::compare_exchange_weak(storage, old_val, old_val | v, order, memory_order_relaxed)) {}
0066         return old_val;
0067     }
0068 
0069     static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0070     {
0071         storage_type old_val;
0072         atomics::detail::non_atomic_load(storage, old_val);
0073         while (!Base::compare_exchange_weak(storage, old_val, old_val ^ v, order, memory_order_relaxed)) {}
0074         return old_val;
0075     }
0076 
0077     static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0078     {
0079         return !!Base::exchange(storage, (storage_type)1, order);
0080     }
0081 
0082     static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0083     {
0084         Base::store(storage, (storage_type)0, order);
0085     }
0086 };
0087 
0088 } // namespace detail
0089 } // namespace atomics
0090 } // namespace boost
0091 
0092 #include <boost/atomic/detail/footer.hpp>
0093 
0094 #endif // BOOST_ATOMIC_DETAIL_CORE_OPS_CAS_BASED_HPP_INCLUDED_