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) 2009 Helge Bahmann
0007  * Copyright (c) 2012 Tim Blechmann
0008  * Copyright (c) 2014 Andrey Semashev
0009  */
0010 /*!
0011  * \file   atomic/detail/core_ops_windows.hpp
0012  *
0013  * This header contains implementation of the \c core_operations template.
0014  *
0015  * This implementation is the most basic version for Windows. It should
0016  * work for any non-MSVC-like compilers as long as there are Interlocked WinAPI
0017  * functions available. This version is also used for WinCE.
0018  *
0019  * Notably, this implementation is not as efficient as other
0020  * versions based on compiler intrinsics.
0021  */
0022 
0023 #ifndef BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_
0024 #define BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_
0025 
0026 #include <cstddef>
0027 #include <boost/memory_order.hpp>
0028 #include <boost/atomic/detail/config.hpp>
0029 #include <boost/atomic/detail/interlocked.hpp>
0030 #include <boost/atomic/detail/storage_traits.hpp>
0031 #include <boost/atomic/detail/core_operations_fwd.hpp>
0032 #include <boost/atomic/detail/type_traits/make_signed.hpp>
0033 #include <boost/atomic/detail/ops_msvc_common.hpp>
0034 #include <boost/atomic/detail/extending_cas_based_arithmetic.hpp>
0035 #include <boost/atomic/detail/header.hpp>
0036 
0037 #ifdef BOOST_HAS_PRAGMA_ONCE
0038 #pragma once
0039 #endif
0040 
0041 namespace boost {
0042 namespace atomics {
0043 namespace detail {
0044 
0045 struct core_operations_windows_base
0046 {
0047     static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
0048     static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
0049 
0050     static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT
0051     {
0052         BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
0053     }
0054 
0055     static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
0056     {
0057         BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
0058     }
0059 };
0060 
0061 template< std::size_t Size, bool Signed, bool Interprocess, typename Derived >
0062 struct core_operations_windows :
0063     public core_operations_windows_base
0064 {
0065     typedef typename storage_traits< Size >::type storage_type;
0066 
0067     static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
0068     static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = storage_traits< Size >::alignment;
0069     static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
0070     static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
0071 
0072     static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0073     {
0074         Derived::exchange(storage, v, order);
0075     }
0076 
0077     static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
0078     {
0079         return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
0080     }
0081 
0082     static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0083     {
0084         typedef typename boost::atomics::detail::make_signed< storage_type >::type signed_storage_type;
0085         return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(v)), order);
0086     }
0087 
0088     static BOOST_FORCEINLINE bool compare_exchange_weak(
0089         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
0090     {
0091         return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
0092     }
0093 
0094     static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0095     {
0096         return !!Derived::exchange(storage, (storage_type)1, order);
0097     }
0098 
0099     static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0100     {
0101         store(storage, (storage_type)0, order);
0102     }
0103 };
0104 
0105 template< bool Signed, bool Interprocess >
0106 struct core_operations< 4u, Signed, bool Interprocess > :
0107     public core_operations_windows< 4u, Signed, Interprocess, core_operations< 4u, Signed, Interprocess > >
0108 {
0109     typedef core_operations_windows< 4u, Signed, Interprocess, core_operations< 4u, Signed, Interprocess > > base_type;
0110     typedef typename base_type::storage_type storage_type;
0111 
0112     static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0113     {
0114         base_type::fence_before(order);
0115         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v));
0116         base_type::fence_after(order);
0117         return v;
0118     }
0119 
0120     static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0121     {
0122         base_type::fence_before(order);
0123         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
0124         base_type::fence_after(order);
0125         return v;
0126     }
0127 
0128     static BOOST_FORCEINLINE bool compare_exchange_strong(
0129         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
0130     {
0131         storage_type previous = expected;
0132         base_type::fence_before(success_order);
0133         storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous));
0134         expected = old_val;
0135         // The success and failure fences are the same anyway
0136         base_type::fence_after(success_order);
0137         return (previous == old_val);
0138     }
0139 
0140     static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0141     {
0142 #if defined(BOOST_ATOMIC_INTERLOCKED_AND)
0143         base_type::fence_before(order);
0144         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v));
0145         base_type::fence_after(order);
0146         return v;
0147 #else
0148         storage_type res = storage;
0149         while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
0150         return res;
0151 #endif
0152     }
0153 
0154     static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0155     {
0156 #if defined(BOOST_ATOMIC_INTERLOCKED_OR)
0157         base_type::fence_before(order);
0158         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v));
0159         base_type::fence_after(order);
0160         return v;
0161 #else
0162         storage_type res = storage;
0163         while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
0164         return res;
0165 #endif
0166     }
0167 
0168     static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0169     {
0170 #if defined(BOOST_ATOMIC_INTERLOCKED_XOR)
0171         base_type::fence_before(order);
0172         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v));
0173         base_type::fence_after(order);
0174         return v;
0175 #else
0176         storage_type res = storage;
0177         while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
0178         return res;
0179 #endif
0180     }
0181 };
0182 
0183 template< bool Signed, bool Interprocess >
0184 struct core_operations< 1u, Signed, Interprocess > :
0185     public extending_cas_based_arithmetic< core_operations< 4u, Signed, Interprocess >, 1u, Signed >
0186 {
0187 };
0188 
0189 template< bool Signed, bool Interprocess >
0190 struct core_operations< 2u, Signed, Interprocess > :
0191     public extending_cas_based_arithmetic< core_operations< 4u, Signed, Interprocess >, 2u, Signed >
0192 {
0193 };
0194 
0195 } // namespace detail
0196 } // namespace atomics
0197 } // namespace boost
0198 
0199 #include <boost/atomic/detail/footer.hpp>
0200 
0201 #endif // BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_