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/ops_windows.hpp
13 * This header contains implementation of the \c 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_OPS_WINDOWS_HPP_INCLUDED_
24 #define BOOST_ATOMIC_DETAIL_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/operations_fwd.hpp>
32 #include <boost/atomic/detail/type_traits/make_signed.hpp>
33 #include <boost/atomic/capabilities.hpp>
34 #include <boost/atomic/detail/ops_msvc_common.hpp>
35 #include <boost/atomic/detail/ops_extending_cas_based.hpp>
37 #ifdef BOOST_HAS_PRAGMA_ONCE
45 struct windows_operations_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 hardware_full_fence() BOOST_NOEXCEPT
53 BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&tmp, 0);
56 static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT
58 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
61 static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
63 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
67 template< std::size_t Size, bool Signed, typename Derived >
68 struct windows_operations :
69 public windows_operations_base
71 typedef typename storage_traits< Size >::type storage_type;
73 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
74 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = storage_traits< Size >::alignment;
75 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
77 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
79 Derived::exchange(storage, v, order);
82 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
84 return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
87 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
89 typedef typename boost::atomics::detail::make_signed< storage_type >::type signed_storage_type;
90 return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(v)), order);
93 static BOOST_FORCEINLINE bool compare_exchange_weak(
94 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
96 return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
99 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
101 return !!Derived::exchange(storage, (storage_type)1, order);
104 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
106 store(storage, (storage_type)0, order);
110 template< bool Signed >
111 struct operations< 4u, Signed > :
112 public windows_operations< 4u, Signed, operations< 4u, Signed > >
114 typedef windows_operations< 4u, Signed, operations< 4u, Signed > > base_type;
115 typedef typename base_type::storage_type storage_type;
117 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
119 base_type::fence_before(order);
120 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v));
121 base_type::fence_after(order);
125 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
127 base_type::fence_before(order);
128 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
129 base_type::fence_after(order);
133 static BOOST_FORCEINLINE bool compare_exchange_strong(
134 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
136 storage_type previous = expected;
137 base_type::fence_before(success_order);
138 storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous));
140 // The success and failure fences are the same anyway
141 base_type::fence_after(success_order);
142 return (previous == old_val);
145 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
147 #if defined(BOOST_ATOMIC_INTERLOCKED_AND)
148 base_type::fence_before(order);
149 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v));
150 base_type::fence_after(order);
153 storage_type res = storage;
154 while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
159 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
161 #if defined(BOOST_ATOMIC_INTERLOCKED_OR)
162 base_type::fence_before(order);
163 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v));
164 base_type::fence_after(order);
167 storage_type res = storage;
168 while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
173 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
175 #if defined(BOOST_ATOMIC_INTERLOCKED_XOR)
176 base_type::fence_before(order);
177 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v));
178 base_type::fence_after(order);
181 storage_type res = storage;
182 while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
188 template< bool Signed >
189 struct operations< 1u, Signed > :
190 public extending_cas_based_operations< operations< 4u, Signed >, 1u, Signed >
194 template< bool Signed >
195 struct operations< 2u, Signed > :
196 public extending_cas_based_operations< operations< 4u, Signed >, 2u, Signed >
200 BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
202 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
203 if (order == memory_order_seq_cst)
204 windows_operations_base::hardware_full_fence();
205 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
208 BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
210 if (order != memory_order_relaxed)
211 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
214 } // namespace detail
215 } // namespace atomics
218 #endif // BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_