2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * Copyright (c) 2009 Helge Bahmann
7 * Copyright (c) 2012 Tim Blechmann
8 * Copyright (c) 2014 Andrey Semashev
11 * \file atomic/detail/core_ops_windows.hpp
13 * This header contains implementation of the \c core_operations template.
15 * This implementation is the most basic version for Windows. It should
16 * work for any non-MSVC-like compilers as long as there are Interlocked WinAPI
17 * functions available. This version is also used for WinCE.
19 * Notably, this implementation is not as efficient as other
20 * versions based on compiler intrinsics.
23 #ifndef BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_
24 #define BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_
27 #include <boost/memory_order.hpp>
28 #include <boost/atomic/detail/config.hpp>
29 #include <boost/atomic/detail/interlocked.hpp>
30 #include <boost/atomic/detail/storage_traits.hpp>
31 #include <boost/atomic/detail/core_operations_fwd.hpp>
32 #include <boost/atomic/detail/type_traits/make_signed.hpp>
33 #include <boost/atomic/detail/ops_msvc_common.hpp>
34 #include <boost/atomic/detail/extending_cas_based_arithmetic.hpp>
35 #include <boost/atomic/detail/header.hpp>
37 #ifdef BOOST_HAS_PRAGMA_ONCE
45 struct core_operations_windows_base
47 static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
48 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
50 static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT
52 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
55 static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
57 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
61 template< std::size_t Size, bool Signed, bool Interprocess, typename Derived >
62 struct core_operations_windows :
63 public core_operations_windows_base
65 typedef typename storage_traits< Size >::type storage_type;
67 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
68 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = storage_traits< Size >::alignment;
69 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
70 static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
72 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
74 Derived::exchange(storage, v, order);
77 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
79 return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
82 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
84 typedef typename boost::atomics::detail::make_signed< storage_type >::type signed_storage_type;
85 return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(v)), order);
88 static BOOST_FORCEINLINE bool compare_exchange_weak(
89 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
91 return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
94 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
96 return !!Derived::exchange(storage, (storage_type)1, order);
99 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
101 store(storage, (storage_type)0, order);
105 template< bool Signed, bool Interprocess >
106 struct core_operations< 4u, Signed, bool Interprocess > :
107 public core_operations_windows< 4u, Signed, Interprocess, core_operations< 4u, Signed, Interprocess > >
109 typedef core_operations_windows< 4u, Signed, Interprocess, core_operations< 4u, Signed, Interprocess > > base_type;
110 typedef typename base_type::storage_type storage_type;
112 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
114 base_type::fence_before(order);
115 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v));
116 base_type::fence_after(order);
120 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
122 base_type::fence_before(order);
123 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
124 base_type::fence_after(order);
128 static BOOST_FORCEINLINE bool compare_exchange_strong(
129 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
131 storage_type previous = expected;
132 base_type::fence_before(success_order);
133 storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous));
135 // The success and failure fences are the same anyway
136 base_type::fence_after(success_order);
137 return (previous == old_val);
140 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
142 #if defined(BOOST_ATOMIC_INTERLOCKED_AND)
143 base_type::fence_before(order);
144 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v));
145 base_type::fence_after(order);
148 storage_type res = storage;
149 while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
154 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
156 #if defined(BOOST_ATOMIC_INTERLOCKED_OR)
157 base_type::fence_before(order);
158 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v));
159 base_type::fence_after(order);
162 storage_type res = storage;
163 while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
168 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
170 #if defined(BOOST_ATOMIC_INTERLOCKED_XOR)
171 base_type::fence_before(order);
172 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v));
173 base_type::fence_after(order);
176 storage_type res = storage;
177 while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
183 template< bool Signed, bool Interprocess >
184 struct core_operations< 1u, Signed, Interprocess > :
185 public extending_cas_based_arithmetic< core_operations< 4u, Signed, Interprocess >, 1u, Signed >
189 template< bool Signed, bool Interprocess >
190 struct core_operations< 2u, Signed, Interprocess > :
191 public extending_cas_based_arithmetic< core_operations< 4u, Signed, Interprocess >, 2u, Signed >
195 } // namespace detail
196 } // namespace atomics
199 #include <boost/atomic/detail/footer.hpp>
201 #endif // BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_