]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/atomic/detail/core_ops_windows.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / atomic / detail / core_ops_windows.hpp
1 /*
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)
5 *
6 * Copyright (c) 2009 Helge Bahmann
7 * Copyright (c) 2012 Tim Blechmann
8 * Copyright (c) 2014 Andrey Semashev
9 */
10 /*!
11 * \file atomic/detail/core_ops_windows.hpp
12 *
13 * This header contains implementation of the \c core_operations template.
14 *
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.
18 *
19 * Notably, this implementation is not as efficient as other
20 * versions based on compiler intrinsics.
21 */
22
23 #ifndef BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_
24 #define BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_
25
26 #include <cstddef>
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>
36
37 #ifdef BOOST_HAS_PRAGMA_ONCE
38 #pragma once
39 #endif
40
41 namespace boost {
42 namespace atomics {
43 namespace detail {
44
45 struct core_operations_windows_base
46 {
47 static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
48 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
49
50 static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT
51 {
52 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
53 }
54
55 static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
56 {
57 BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
58 }
59 };
60
61 template< std::size_t Size, bool Signed, bool Interprocess, typename Derived >
62 struct core_operations_windows :
63 public core_operations_windows_base
64 {
65 typedef typename storage_traits< Size >::type storage_type;
66
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;
71
72 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
73 {
74 Derived::exchange(storage, v, order);
75 }
76
77 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
78 {
79 return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
80 }
81
82 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
83 {
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);
86 }
87
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
90 {
91 return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
92 }
93
94 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
95 {
96 return !!Derived::exchange(storage, (storage_type)1, order);
97 }
98
99 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
100 {
101 store(storage, (storage_type)0, order);
102 }
103 };
104
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 > >
108 {
109 typedef core_operations_windows< 4u, Signed, Interprocess, core_operations< 4u, Signed, Interprocess > > base_type;
110 typedef typename base_type::storage_type storage_type;
111
112 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
113 {
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);
117 return v;
118 }
119
120 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
121 {
122 base_type::fence_before(order);
123 v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
124 base_type::fence_after(order);
125 return v;
126 }
127
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
130 {
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));
134 expected = old_val;
135 // The success and failure fences are the same anyway
136 base_type::fence_after(success_order);
137 return (previous == old_val);
138 }
139
140 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
141 {
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);
146 return v;
147 #else
148 storage_type res = storage;
149 while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
150 return res;
151 #endif
152 }
153
154 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
155 {
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);
160 return v;
161 #else
162 storage_type res = storage;
163 while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
164 return res;
165 #endif
166 }
167
168 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
169 {
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);
174 return v;
175 #else
176 storage_type res = storage;
177 while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
178 return res;
179 #endif
180 }
181 };
182
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 >
186 {
187 };
188
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 >
192 {
193 };
194
195 } // namespace detail
196 } // namespace atomics
197 } // namespace boost
198
199 #include <boost/atomic/detail/footer.hpp>
200
201 #endif // BOOST_ATOMIC_DETAIL_CORE_OPS_WINDOWS_HPP_INCLUDED_