Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-16 09:05:56

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, 2020 Andrey Semashev
0007  */
0008 /*!
0009  * \file   atomic/detail/core_operations_emulated.hpp
0010  *
0011  * This header contains lock pool-based implementation of the core atomic operations.
0012  */
0013 
0014 #ifndef BOOST_ATOMIC_DETAIL_CORE_OPERATIONS_EMULATED_HPP_INCLUDED_
0015 #define BOOST_ATOMIC_DETAIL_CORE_OPERATIONS_EMULATED_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/core_operations_emulated_fwd.hpp>
0022 #include <boost/atomic/detail/lock_pool.hpp>
0023 #include <boost/atomic/detail/header.hpp>
0024 
0025 #ifdef BOOST_HAS_PRAGMA_ONCE
0026 #pragma once
0027 #endif
0028 
0029 namespace boost {
0030 namespace atomics {
0031 namespace detail {
0032 
0033 template< std::size_t Size, std::size_t Alignment, bool = Alignment >= storage_traits< Size >::native_alignment >
0034 struct core_operations_emulated_base
0035 {
0036     typedef typename storage_traits< Size >::type storage_type;
0037 };
0038 
0039 template< std::size_t Size, std::size_t Alignment >
0040 struct core_operations_emulated_base< Size, Alignment, false >
0041 {
0042     typedef buffer_storage< Size, Alignment > storage_type;
0043 };
0044 
0045 //! Emulated implementation of core atomic operations
0046 template< std::size_t Size, std::size_t Alignment, bool Signed, bool Interprocess >
0047 struct core_operations_emulated :
0048     public core_operations_emulated_base< Size, Alignment >
0049 {
0050     typedef core_operations_emulated_base< Size, Alignment > base_type;
0051 
0052     // Define storage_type to have alignment not greater than Alignment. This will allow operations to work with value_types
0053     // that possibly have weaker alignment requirements than storage_traits< Size >::type would. This is important for atomic_ref<>.
0054     // atomic<> will allow higher alignment requirement than its value_type.
0055     // Note that storage_type should be an integral type, if possible, so that arithmetic and bitwise operations are possible.
0056     typedef typename base_type::storage_type storage_type;
0057 
0058     static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
0059     static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = Alignment >= storage_traits< Size >::alignment ? storage_traits< Size >::alignment : Alignment;
0060 
0061     static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
0062     static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
0063     static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
0064 
0065     static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = false;
0066 
0067     typedef lock_pool::scoped_lock< storage_alignment > scoped_lock;
0068 
0069     static void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0070     {
0071         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0072         scoped_lock lock(&storage);
0073         const_cast< storage_type& >(storage) = v;
0074     }
0075 
0076     static storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT
0077     {
0078         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0079         scoped_lock lock(&storage);
0080         return const_cast< storage_type const& >(storage);
0081     }
0082 
0083     static storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0084     {
0085         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0086         storage_type& s = const_cast< storage_type& >(storage);
0087         scoped_lock lock(&storage);
0088         storage_type old_val = s;
0089         s += v;
0090         return old_val;
0091     }
0092 
0093     static storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0094     {
0095         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0096         storage_type& s = const_cast< storage_type& >(storage);
0097         scoped_lock lock(&storage);
0098         storage_type old_val = s;
0099         s -= v;
0100         return old_val;
0101     }
0102 
0103     static storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0104     {
0105         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0106         storage_type& s = const_cast< storage_type& >(storage);
0107         scoped_lock lock(&storage);
0108         storage_type old_val = s;
0109         s = v;
0110         return old_val;
0111     }
0112 
0113     static bool compare_exchange_strong(
0114         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
0115     {
0116         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0117         storage_type& s = const_cast< storage_type& >(storage);
0118         scoped_lock lock(&storage);
0119         storage_type old_val = s;
0120         const bool res = old_val == expected;
0121         if (res)
0122             s = desired;
0123         expected = old_val;
0124 
0125         return res;
0126     }
0127 
0128     static bool compare_exchange_weak(
0129         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
0130     {
0131         // Note: This function is the exact copy of compare_exchange_strong. The reason we're not just forwarding the call
0132         // is that MSVC-12 ICEs in this case.
0133         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0134         storage_type& s = const_cast< storage_type& >(storage);
0135         scoped_lock lock(&storage);
0136         storage_type old_val = s;
0137         const bool res = old_val == expected;
0138         if (res)
0139             s = desired;
0140         expected = old_val;
0141 
0142         return res;
0143     }
0144 
0145     static storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0146     {
0147         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0148         storage_type& s = const_cast< storage_type& >(storage);
0149         scoped_lock lock(&storage);
0150         storage_type old_val = s;
0151         s &= v;
0152         return old_val;
0153     }
0154 
0155     static storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0156     {
0157         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0158         storage_type& s = const_cast< storage_type& >(storage);
0159         scoped_lock lock(&storage);
0160         storage_type old_val = s;
0161         s |= v;
0162         return old_val;
0163     }
0164 
0165     static storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0166     {
0167         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0168         storage_type& s = const_cast< storage_type& >(storage);
0169         scoped_lock lock(&storage);
0170         storage_type old_val = s;
0171         s ^= v;
0172         return old_val;
0173     }
0174 
0175     static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0176     {
0177         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0178         return !!exchange(storage, (storage_type)1, order);
0179     }
0180 
0181     static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0182     {
0183         static_assert(!is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
0184         store(storage, (storage_type)0, order);
0185     }
0186 };
0187 
0188 } // namespace detail
0189 } // namespace atomics
0190 } // namespace boost
0191 
0192 #include <boost/atomic/detail/footer.hpp>
0193 
0194 #endif // BOOST_ATOMIC_DETAIL_CORE_OPERATIONS_EMULATED_HPP_INCLUDED_