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) 2011 Helge Bahmann
|
Chris@102
|
7 * Copyright (c) 2013 Tim Blechmann
|
Chris@102
|
8 * Copyright (c) 2014 Andrey Semashev
|
Chris@102
|
9 */
|
Chris@102
|
10 /*!
|
Chris@102
|
11 * \file atomic/detail/ops_gcc_sync.hpp
|
Chris@102
|
12 *
|
Chris@102
|
13 * This header contains implementation of the \c operations template.
|
Chris@102
|
14 */
|
Chris@102
|
15
|
Chris@102
|
16 #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_
|
Chris@102
|
17 #define BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_
|
Chris@102
|
18
|
Chris@102
|
19 #include <boost/memory_order.hpp>
|
Chris@102
|
20 #include <boost/atomic/detail/config.hpp>
|
Chris@102
|
21 #include <boost/atomic/detail/storage_type.hpp>
|
Chris@102
|
22 #include <boost/atomic/detail/operations_fwd.hpp>
|
Chris@102
|
23 #include <boost/atomic/detail/ops_extending_cas_based.hpp>
|
Chris@102
|
24 #include <boost/atomic/capabilities.hpp>
|
Chris@102
|
25
|
Chris@102
|
26 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@102
|
27 #pragma once
|
Chris@102
|
28 #endif
|
Chris@102
|
29
|
Chris@102
|
30 namespace boost {
|
Chris@102
|
31 namespace atomics {
|
Chris@102
|
32 namespace detail {
|
Chris@102
|
33
|
Chris@102
|
34 struct gcc_sync_operations_base
|
Chris@102
|
35 {
|
Chris@102
|
36 static BOOST_FORCEINLINE void fence_before_store(memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
37 {
|
Chris@102
|
38 if ((order & memory_order_release) != 0)
|
Chris@102
|
39 __sync_synchronize();
|
Chris@102
|
40 }
|
Chris@102
|
41
|
Chris@102
|
42 static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
43 {
|
Chris@102
|
44 if (order == memory_order_seq_cst)
|
Chris@102
|
45 __sync_synchronize();
|
Chris@102
|
46 }
|
Chris@102
|
47
|
Chris@102
|
48 static BOOST_FORCEINLINE void fence_after_load(memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
49 {
|
Chris@102
|
50 if ((order & (memory_order_acquire | memory_order_consume)) != 0)
|
Chris@102
|
51 __sync_synchronize();
|
Chris@102
|
52 }
|
Chris@102
|
53 };
|
Chris@102
|
54
|
Chris@102
|
55 template< typename T >
|
Chris@102
|
56 struct gcc_sync_operations :
|
Chris@102
|
57 public gcc_sync_operations_base
|
Chris@102
|
58 {
|
Chris@102
|
59 typedef T storage_type;
|
Chris@102
|
60
|
Chris@102
|
61 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
62 {
|
Chris@102
|
63 fence_before_store(order);
|
Chris@102
|
64 storage = v;
|
Chris@102
|
65 fence_after_store(order);
|
Chris@102
|
66 }
|
Chris@102
|
67
|
Chris@102
|
68 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
69 {
|
Chris@102
|
70 storage_type v = storage;
|
Chris@102
|
71 fence_after_load(order);
|
Chris@102
|
72 return v;
|
Chris@102
|
73 }
|
Chris@102
|
74
|
Chris@102
|
75 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
76 {
|
Chris@102
|
77 return __sync_fetch_and_add(&storage, v);
|
Chris@102
|
78 }
|
Chris@102
|
79
|
Chris@102
|
80 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
81 {
|
Chris@102
|
82 return __sync_fetch_and_sub(&storage, v);
|
Chris@102
|
83 }
|
Chris@102
|
84
|
Chris@102
|
85 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
86 {
|
Chris@102
|
87 // GCC docs mention that not all architectures may support full exchange semantics for this intrinsic. However, GCC's implementation of
|
Chris@102
|
88 // std::atomic<> uses this intrinsic unconditionally. We do so as well. In case if some architectures actually don't support this, we can always
|
Chris@102
|
89 // add a check here and fall back to a CAS loop.
|
Chris@102
|
90 if ((order & memory_order_release) != 0)
|
Chris@102
|
91 __sync_synchronize();
|
Chris@102
|
92 return __sync_lock_test_and_set(&storage, v);
|
Chris@102
|
93 }
|
Chris@102
|
94
|
Chris@102
|
95 static BOOST_FORCEINLINE bool compare_exchange_strong(
|
Chris@102
|
96 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
97 {
|
Chris@102
|
98 storage_type expected2 = expected;
|
Chris@102
|
99 storage_type old_val = __sync_val_compare_and_swap(&storage, expected2, desired);
|
Chris@102
|
100
|
Chris@102
|
101 if (old_val == expected2)
|
Chris@102
|
102 {
|
Chris@102
|
103 return true;
|
Chris@102
|
104 }
|
Chris@102
|
105 else
|
Chris@102
|
106 {
|
Chris@102
|
107 expected = old_val;
|
Chris@102
|
108 return false;
|
Chris@102
|
109 }
|
Chris@102
|
110 }
|
Chris@102
|
111
|
Chris@102
|
112 static BOOST_FORCEINLINE bool compare_exchange_weak(
|
Chris@102
|
113 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
|
Chris@102
|
114 {
|
Chris@102
|
115 return compare_exchange_strong(storage, expected, desired, success_order, failure_order);
|
Chris@102
|
116 }
|
Chris@102
|
117
|
Chris@102
|
118 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
119 {
|
Chris@102
|
120 return __sync_fetch_and_and(&storage, v);
|
Chris@102
|
121 }
|
Chris@102
|
122
|
Chris@102
|
123 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
124 {
|
Chris@102
|
125 return __sync_fetch_and_or(&storage, v);
|
Chris@102
|
126 }
|
Chris@102
|
127
|
Chris@102
|
128 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
|
Chris@102
|
129 {
|
Chris@102
|
130 return __sync_fetch_and_xor(&storage, v);
|
Chris@102
|
131 }
|
Chris@102
|
132
|
Chris@102
|
133 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
134 {
|
Chris@102
|
135 if ((order & memory_order_release) != 0)
|
Chris@102
|
136 __sync_synchronize();
|
Chris@102
|
137 return !!__sync_lock_test_and_set(&storage, 1);
|
Chris@102
|
138 }
|
Chris@102
|
139
|
Chris@102
|
140 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
141 {
|
Chris@102
|
142 __sync_lock_release(&storage);
|
Chris@102
|
143 if (order == memory_order_seq_cst)
|
Chris@102
|
144 __sync_synchronize();
|
Chris@102
|
145 }
|
Chris@102
|
146
|
Chris@102
|
147 static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
|
Chris@102
|
148 {
|
Chris@102
|
149 return true;
|
Chris@102
|
150 }
|
Chris@102
|
151 };
|
Chris@102
|
152
|
Chris@102
|
153 #if BOOST_ATOMIC_INT8_LOCK_FREE > 0
|
Chris@102
|
154 template< bool Signed >
|
Chris@102
|
155 struct operations< 1u, Signed > :
|
Chris@102
|
156 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1)
|
Chris@102
|
157 public gcc_sync_operations< typename make_storage_type< 1u, Signed >::type >
|
Chris@102
|
158 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
|
Chris@102
|
159 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 2u, Signed >::type >, 1u, Signed >
|
Chris@102
|
160 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
|
Chris@102
|
161 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 4u, Signed >::type >, 1u, Signed >
|
Chris@102
|
162 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
|
Chris@102
|
163 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 1u, Signed >
|
Chris@102
|
164 #else
|
Chris@102
|
165 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 1u, Signed >
|
Chris@102
|
166 #endif
|
Chris@102
|
167 {
|
Chris@102
|
168 };
|
Chris@102
|
169 #endif
|
Chris@102
|
170
|
Chris@102
|
171 #if BOOST_ATOMIC_INT16_LOCK_FREE > 0
|
Chris@102
|
172 template< bool Signed >
|
Chris@102
|
173 struct operations< 2u, Signed > :
|
Chris@102
|
174 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
|
Chris@102
|
175 public gcc_sync_operations< typename make_storage_type< 2u, Signed >::type >
|
Chris@102
|
176 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
|
Chris@102
|
177 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 4u, Signed >::type >, 2u, Signed >
|
Chris@102
|
178 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
|
Chris@102
|
179 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 2u, Signed >
|
Chris@102
|
180 #else
|
Chris@102
|
181 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 2u, Signed >
|
Chris@102
|
182 #endif
|
Chris@102
|
183 {
|
Chris@102
|
184 };
|
Chris@102
|
185 #endif
|
Chris@102
|
186
|
Chris@102
|
187 #if BOOST_ATOMIC_INT32_LOCK_FREE > 0
|
Chris@102
|
188 template< bool Signed >
|
Chris@102
|
189 struct operations< 4u, Signed > :
|
Chris@102
|
190 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
|
Chris@102
|
191 public gcc_sync_operations< typename make_storage_type< 4u, Signed >::type >
|
Chris@102
|
192 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
|
Chris@102
|
193 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 4u, Signed >
|
Chris@102
|
194 #else
|
Chris@102
|
195 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 4u, Signed >
|
Chris@102
|
196 #endif
|
Chris@102
|
197 {
|
Chris@102
|
198 };
|
Chris@102
|
199 #endif
|
Chris@102
|
200
|
Chris@102
|
201 #if BOOST_ATOMIC_INT64_LOCK_FREE > 0
|
Chris@102
|
202 template< bool Signed >
|
Chris@102
|
203 struct operations< 8u, Signed > :
|
Chris@102
|
204 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
|
Chris@102
|
205 public gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >
|
Chris@102
|
206 #else
|
Chris@102
|
207 public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 8u, Signed >
|
Chris@102
|
208 #endif
|
Chris@102
|
209 {
|
Chris@102
|
210 };
|
Chris@102
|
211 #endif
|
Chris@102
|
212
|
Chris@102
|
213 #if BOOST_ATOMIC_INT128_LOCK_FREE > 0
|
Chris@102
|
214 template< bool Signed >
|
Chris@102
|
215 struct operations< 16u, Signed > :
|
Chris@102
|
216 public gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >
|
Chris@102
|
217 {
|
Chris@102
|
218 };
|
Chris@102
|
219 #endif
|
Chris@102
|
220
|
Chris@102
|
221 BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
222 {
|
Chris@102
|
223 if (order != memory_order_relaxed)
|
Chris@102
|
224 __sync_synchronize();
|
Chris@102
|
225 }
|
Chris@102
|
226
|
Chris@102
|
227 BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
|
Chris@102
|
228 {
|
Chris@102
|
229 if (order != memory_order_relaxed)
|
Chris@102
|
230 __asm__ __volatile__ ("" ::: "memory");
|
Chris@102
|
231 }
|
Chris@102
|
232
|
Chris@102
|
233 } // namespace detail
|
Chris@102
|
234 } // namespace atomics
|
Chris@102
|
235 } // namespace boost
|
Chris@102
|
236
|
Chris@102
|
237 #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_
|