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_
26 #include <boost/memory_order.hpp>
27 #include <boost/type_traits/make_signed.hpp>
28 #include <boost/atomic/detail/config.hpp>
29 #include <boost/atomic/detail/interlocked.hpp>
30 #include <boost/atomic/detail/storage_type.hpp>
31 #include <boost/atomic/detail/operations_fwd.hpp>
32 #include <boost/atomic/capabilities.hpp>
33 #include <boost/atomic/detail/ops_msvc_common.hpp>
34 #include <boost/atomic/detail/ops_extending_cas_based.hpp>
36 #ifdef BOOST_HAS_PRAGMA_ONCE
44 struct windows_operations_base
46 static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT
49 BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&tmp, 0);
52 static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT
54 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
57 static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
59 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
63 template< typename T, typename Derived >
64 struct windows_operations :
65 public windows_operations_base
67 typedef T storage_type;
69 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
71 Derived::exchange(storage, v, order);
74 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
76 return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
79 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
81 typedef typename make_signed< storage_type >::type signed_storage_type;
82 return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(v)), order);
85 static BOOST_FORCEINLINE bool compare_exchange_weak(
86 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
88 return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
91 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
93 return !!Derived::exchange(storage, (storage_type)1, order);
96 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
98 store(storage, (storage_type)0, order);
101 static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
107 template< bool Signed >
108 struct operations< 4u, Signed > :
109 public windows_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > >
111 typedef windows_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > > base_type;
112 typedef typename base_type::storage_type storage_type;
114 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
116 base_type::fence_before(order);
117 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v));
118 base_type::fence_after(order);
122 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
124 base_type::fence_before(order);
125 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
126 base_type::fence_after(order);
130 static BOOST_FORCEINLINE bool compare_exchange_strong(
131 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
133 storage_type previous = expected;
134 base_type::fence_before(success_order);
135 storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous));
137 // The success and failure fences are the same anyway
138 base_type::fence_after(success_order);
139 return (previous == old_val);
142 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
144 #if defined(BOOST_ATOMIC_INTERLOCKED_AND)
145 base_type::fence_before(order);
146 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v));
147 base_type::fence_after(order);
150 storage_type res = storage;
151 while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
156 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
158 #if defined(BOOST_ATOMIC_INTERLOCKED_OR)
159 base_type::fence_before(order);
160 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v));
161 base_type::fence_after(order);
164 storage_type res = storage;
165 while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
170 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
172 #if defined(BOOST_ATOMIC_INTERLOCKED_XOR)
173 base_type::fence_before(order);
174 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v));
175 base_type::fence_after(order);
178 storage_type res = storage;
179 while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
185 template< bool Signed >
186 struct operations< 1u, Signed > :
187 public extending_cas_based_operations< operations< 4u, Signed >, 1u, Signed >
191 template< bool Signed >
192 struct operations< 2u, Signed > :
193 public extending_cas_based_operations< operations< 4u, Signed >, 2u, Signed >
197 BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
199 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
200 if (order == memory_order_seq_cst)
201 windows_operations_base::hardware_full_fence();
202 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
205 BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
207 if (order != memory_order_relaxed)
208 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
211 } // namespace detail
212 } // namespace atomics
215 #endif // BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_