Chris@102
|
1 /*
|
Chris@102
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@102
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@102
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
5 *
|
Chris@102
|
6 * Copyright (c) 2009 Helge Bahmann
|
Chris@102
|
7 * Copyright (c) 2012 Tim Blechmann
|
Chris@102
|
8 * Copyright (c) 2014 Andrey Semashev
|
Chris@102
|
9 */
|
Chris@102
|
10 /*!
|
Chris@102
|
11 * \file atomic/detail/ops_windows.hpp
|
Chris@102
|
12 *
|
Chris@102
|
13 * This header contains implementation of the \c operations template.
|
Chris@102
|
14 *
|
Chris@102
|
15 * This implementation is the most basic version for Windows. It should
|
Chris@102
|
16 * work for any non-MSVC-like compilers as long as there are Interlocked WinAPI
|
Chris@102
|
17 * functions available. This version is also used for WinCE.
|
Chris@102
|
18 *
|
Chris@102
|
19 * Notably, this implementation is not as efficient as other
|
Chris@102
|
20 * versions based on compiler intrinsics.
|
Chris@102
|
21 */
|
Chris@102
|
22
|
Chris@102
|
23 #ifndef BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_
|
Chris@102
|
24 #define BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_
|
Chris@102
|
25
|
Chris@102
|
26 #include <boost/memory_order.hpp>
|
Chris@102
|
27 #include <boost/type_traits/make_signed.hpp>
|
Chris@102
|
28 #include <boost/atomic/detail/config.hpp>
|
Chris@102
|
29 #include <boost/atomic/detail/interlocked.hpp>
|
Chris@102
|
30 #include <boost/atomic/detail/storage_type.hpp>
|
Chris@102
|
31 #include <boost/atomic/detail/operations_fwd.hpp>
|
Chris@102
|
32 #include <boost/atomic/capabilities.hpp>
|
Chris@102
|
33 #include <boost/atomic/detail/ops_msvc_common.hpp>
|
Chris@102
|
34 #include <boost/atomic/detail/ops_extending_cas_based.hpp>
|
Chris@102
|
35
|
Chris@102
|
36 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@102
|
37 #pragma once
|
Chris@102
|
38 #endif
|
Chris@102
|
39
|
Chris@102
|
40 namespace boost {
|
Chris@102
|
41 namespace atomics {
|
Chris@102
|
42 namespace detail {
|
Chris@102
|
43
|
Chris@102
|
44 struct windows_operations_base
|
Chris@102
|
45 {
|
Chris@102
|
46 static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT
|
Chris@102
|
47 {
|
Chris@102
|
48 long tmp;
|
Chris@102
|
49 BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&tmp, 0);
|
Chris@102
|
50 }
|
Chris@102
|
51
|
Chris@102
|
52 static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT
|
Chris@102
|
53 {
|
Chris@102
|
54 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
|
Chris@102
|
55 }
|
Chris@102
|
56
|
Chris@102
|
57 static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
|
Chris@102
|
58 {
|
Chris@102
|
59 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
|
Chris@102
|
60 }
|
Chris@102
|
61 };
|
Chris@102
|
62
|
Chris@102
|
63 template< typename T, typename Derived >
|
Chris@102
|
64 struct windows_operations :
|
Chris@102
|
65 public windows_operations_base
|
Chris@102
|
66 {
|
Chris@102
|
67 typedef T storage_type;
|
Chris@102
|
68
|
Chris@102
|
69 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
70 {
|
Chris@102
|
71 Derived::exchange(storage, v, order);
|
Chris@102
|
72 }
|
Chris@102
|
73
|
Chris@102
|
74 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
75 {
|
Chris@102
|
76 return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
|
Chris@102
|
77 }
|
Chris@102
|
78
|
Chris@102
|
79 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
80 {
|
Chris@102
|
81 typedef typename make_signed< storage_type >::type signed_storage_type;
|
Chris@102
|
82 return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(v)), order);
|
Chris@102
|
83 }
|
Chris@102
|
84
|
Chris@102
|
85 static BOOST_FORCEINLINE bool compare_exchange_weak(
|
Chris@102
|
86 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
|
Chris@102
|
87 {
|
Chris@102
|
88 return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
|
Chris@102
|
89 }
|
Chris@102
|
90
|
Chris@102
|
91 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
92 {
|
Chris@102
|
93 return !!Derived::exchange(storage, (storage_type)1, order);
|
Chris@102
|
94 }
|
Chris@102
|
95
|
Chris@102
|
96 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
97 {
|
Chris@102
|
98 store(storage, (storage_type)0, order);
|
Chris@102
|
99 }
|
Chris@102
|
100
|
Chris@102
|
101 static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
|
Chris@102
|
102 {
|
Chris@102
|
103 return true;
|
Chris@102
|
104 }
|
Chris@102
|
105 };
|
Chris@102
|
106
|
Chris@102
|
107 template< bool Signed >
|
Chris@102
|
108 struct operations< 4u, Signed > :
|
Chris@102
|
109 public windows_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > >
|
Chris@102
|
110 {
|
Chris@102
|
111 typedef windows_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > > base_type;
|
Chris@102
|
112 typedef typename base_type::storage_type storage_type;
|
Chris@102
|
113
|
Chris@102
|
114 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
115 {
|
Chris@102
|
116 base_type::fence_before(order);
|
Chris@102
|
117 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v));
|
Chris@102
|
118 base_type::fence_after(order);
|
Chris@102
|
119 return v;
|
Chris@102
|
120 }
|
Chris@102
|
121
|
Chris@102
|
122 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
123 {
|
Chris@102
|
124 base_type::fence_before(order);
|
Chris@102
|
125 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
|
Chris@102
|
126 base_type::fence_after(order);
|
Chris@102
|
127 return v;
|
Chris@102
|
128 }
|
Chris@102
|
129
|
Chris@102
|
130 static BOOST_FORCEINLINE bool compare_exchange_strong(
|
Chris@102
|
131 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
|
Chris@102
|
132 {
|
Chris@102
|
133 storage_type previous = expected;
|
Chris@102
|
134 base_type::fence_before(success_order);
|
Chris@102
|
135 storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous));
|
Chris@102
|
136 expected = old_val;
|
Chris@102
|
137 // The success and failure fences are the same anyway
|
Chris@102
|
138 base_type::fence_after(success_order);
|
Chris@102
|
139 return (previous == old_val);
|
Chris@102
|
140 }
|
Chris@102
|
141
|
Chris@102
|
142 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
143 {
|
Chris@102
|
144 #if defined(BOOST_ATOMIC_INTERLOCKED_AND)
|
Chris@102
|
145 base_type::fence_before(order);
|
Chris@102
|
146 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v));
|
Chris@102
|
147 base_type::fence_after(order);
|
Chris@102
|
148 return v;
|
Chris@102
|
149 #else
|
Chris@102
|
150 storage_type res = storage;
|
Chris@102
|
151 while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
|
Chris@102
|
152 return res;
|
Chris@102
|
153 #endif
|
Chris@102
|
154 }
|
Chris@102
|
155
|
Chris@102
|
156 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
157 {
|
Chris@102
|
158 #if defined(BOOST_ATOMIC_INTERLOCKED_OR)
|
Chris@102
|
159 base_type::fence_before(order);
|
Chris@102
|
160 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v));
|
Chris@102
|
161 base_type::fence_after(order);
|
Chris@102
|
162 return v;
|
Chris@102
|
163 #else
|
Chris@102
|
164 storage_type res = storage;
|
Chris@102
|
165 while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
|
Chris@102
|
166 return res;
|
Chris@102
|
167 #endif
|
Chris@102
|
168 }
|
Chris@102
|
169
|
Chris@102
|
170 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
171 {
|
Chris@102
|
172 #if defined(BOOST_ATOMIC_INTERLOCKED_XOR)
|
Chris@102
|
173 base_type::fence_before(order);
|
Chris@102
|
174 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v));
|
Chris@102
|
175 base_type::fence_after(order);
|
Chris@102
|
176 return v;
|
Chris@102
|
177 #else
|
Chris@102
|
178 storage_type res = storage;
|
Chris@102
|
179 while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
|
Chris@102
|
180 return res;
|
Chris@102
|
181 #endif
|
Chris@102
|
182 }
|
Chris@102
|
183 };
|
Chris@102
|
184
|
Chris@102
|
185 template< bool Signed >
|
Chris@102
|
186 struct operations< 1u, Signed > :
|
Chris@102
|
187 public extending_cas_based_operations< operations< 4u, Signed >, 1u, Signed >
|
Chris@102
|
188 {
|
Chris@102
|
189 };
|
Chris@102
|
190
|
Chris@102
|
191 template< bool Signed >
|
Chris@102
|
192 struct operations< 2u, Signed > :
|
Chris@102
|
193 public extending_cas_based_operations< operations< 4u, Signed >, 2u, Signed >
|
Chris@102
|
194 {
|
Chris@102
|
195 };
|
Chris@102
|
196
|
Chris@102
|
197 BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
198 {
|
Chris@102
|
199 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
|
Chris@102
|
200 if (order == memory_order_seq_cst)
|
Chris@102
|
201 windows_operations_base::hardware_full_fence();
|
Chris@102
|
202 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
|
Chris@102
|
203 }
|
Chris@102
|
204
|
Chris@102
|
205 BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
206 {
|
Chris@102
|
207 if (order != memory_order_relaxed)
|
Chris@102
|
208 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
|
Chris@102
|
209 }
|
Chris@102
|
210
|
Chris@102
|
211 } // namespace detail
|
Chris@102
|
212 } // namespace atomics
|
Chris@102
|
213 } // namespace boost
|
Chris@102
|
214
|
Chris@102
|
215 #endif // BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_
|