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) 2014 Andrey Semashev
7  */
8 /*!
9  * \file   atomic/detail/ops_gcc_atomic.hpp
10  *
11  * This header contains implementation of the \c operations template.
12  */
13 
14 #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_
16 
17 #include <boost/memory_order.hpp>
18 #include <boost/atomic/detail/config.hpp>
19 #include <boost/atomic/detail/storage_type.hpp>
20 #include <boost/atomic/detail/operations_fwd.hpp>
21 #include <boost/atomic/capabilities.hpp>
22 #if defined(__clang__) && (defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) || defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B))
23 #include <boost/atomic/detail/ops_gcc_x86_dcas.hpp>
24 #include <boost/atomic/detail/ops_cas_based.hpp>
25 #endif
26 
27 #if __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE || __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE ||\
28     __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE || __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE ||\
29     __GCC_ATOMIC_CHAR_LOCK_FREE != BOOST_ATOMIC_CHAR_LOCK_FREE || __GCC_ATOMIC_BOOL_LOCK_FREE != BOOST_ATOMIC_BOOL_LOCK_FREE ||\
30     __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE
31 // There are platforms where we need to use larger storage types
32 #include <boost/atomic/detail/int_sizes.hpp>
33 #include <boost/atomic/detail/ops_extending_cas_based.hpp>
34 #endif
35 
36 #ifdef BOOST_HAS_PRAGMA_ONCE
37 #pragma once
38 #endif
39 
40 #if defined(__INTEL_COMPILER)
41 // This is used to suppress warning #32013 described below for Intel Compiler.
42 // In debug builds the compiler does not inline any functions, so basically
43 // every atomic function call results in this warning. I don't know any other
44 // way to selectively disable just this one warning.
45 #pragma system_header
46 #endif
47 
48 namespace boost {
49 namespace atomics {
50 namespace detail {
51 
52 /*!
53  * The function converts \c boost::memory_order values to the compiler-specific constants.
54  *
55  * NOTE: The intention is that the function is optimized away by the compiler, and the
56  *       compiler-specific constants are passed to the intrinsics. I know constexpr doesn't
57  *       work in this case because the standard atomics interface require memory ordering
58  *       constants to be passed as function arguments, at which point they stop being constexpr.
59  *       However it is crucial that the compiler sees constants and not runtime values,
60  *       because otherwise it just ignores the ordering value and always uses seq_cst.
61  *       This is the case with Intel C++ Compiler 14.0.3 (Composer XE 2013 SP1, update 3) and
62  *       gcc 4.8.2. Intel Compiler issues a warning in this case:
63  *
64  *       warning #32013: Invalid memory order specified. Defaulting to seq_cst memory order.
65  *
66  *       while gcc acts silently.
67  *
68  *       To mitigate the problem ALL functions, including the atomic<> members must be
69  *       declared with BOOST_FORCEINLINE. In this case the compilers are able to see that
70  *       all functions are called with constant orderings and call intrinstcts properly.
71  *
72  *       Unfortunately, this still doesn't work in debug mode as the compiler doesn't
73  *       inline functions even when marked with BOOST_FORCEINLINE. In this case all atomic
74  *       operaions will be executed with seq_cst semantics.
75  */
convert_memory_order_to_gcc(memory_order order)76 BOOST_FORCEINLINE BOOST_CONSTEXPR int convert_memory_order_to_gcc(memory_order order) BOOST_NOEXCEPT
77 {
78     return (order == memory_order_relaxed ? __ATOMIC_RELAXED : (order == memory_order_consume ? __ATOMIC_CONSUME :
79         (order == memory_order_acquire ? __ATOMIC_ACQUIRE : (order == memory_order_release ? __ATOMIC_RELEASE :
80         (order == memory_order_acq_rel ? __ATOMIC_ACQ_REL : __ATOMIC_SEQ_CST)))));
81 }
82 
83 template< typename T >
84 struct gcc_atomic_operations
85 {
86     typedef T storage_type;
87 
storeboost::atomics::detail::gcc_atomic_operations88     static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
89     {
90         __atomic_store_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
91     }
92 
loadboost::atomics::detail::gcc_atomic_operations93     static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
94     {
95         return __atomic_load_n(&storage, atomics::detail::convert_memory_order_to_gcc(order));
96     }
97 
fetch_addboost::atomics::detail::gcc_atomic_operations98     static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
99     {
100         return __atomic_fetch_add(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
101     }
102 
fetch_subboost::atomics::detail::gcc_atomic_operations103     static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
104     {
105         return __atomic_fetch_sub(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
106     }
107 
exchangeboost::atomics::detail::gcc_atomic_operations108     static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
109     {
110         return __atomic_exchange_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
111     }
112 
compare_exchange_strongboost::atomics::detail::gcc_atomic_operations113     static BOOST_FORCEINLINE bool compare_exchange_strong(
114         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
115     {
116         return __atomic_compare_exchange_n
117         (
118             &storage, &expected, desired, false,
119             atomics::detail::convert_memory_order_to_gcc(success_order),
120             atomics::detail::convert_memory_order_to_gcc(failure_order)
121         );
122     }
123 
compare_exchange_weakboost::atomics::detail::gcc_atomic_operations124     static BOOST_FORCEINLINE bool compare_exchange_weak(
125         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
126     {
127         return __atomic_compare_exchange_n
128         (
129             &storage, &expected, desired, true,
130             atomics::detail::convert_memory_order_to_gcc(success_order),
131             atomics::detail::convert_memory_order_to_gcc(failure_order)
132         );
133     }
134 
fetch_andboost::atomics::detail::gcc_atomic_operations135     static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
136     {
137         return __atomic_fetch_and(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
138     }
139 
fetch_orboost::atomics::detail::gcc_atomic_operations140     static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
141     {
142         return __atomic_fetch_or(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
143     }
144 
fetch_xorboost::atomics::detail::gcc_atomic_operations145     static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
146     {
147         return __atomic_fetch_xor(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
148     }
149 
test_and_setboost::atomics::detail::gcc_atomic_operations150     static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
151     {
152         return __atomic_test_and_set(&storage, atomics::detail::convert_memory_order_to_gcc(order));
153     }
154 
clearboost::atomics::detail::gcc_atomic_operations155     static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
156     {
157         __atomic_clear(const_cast< storage_type* >(&storage), atomics::detail::convert_memory_order_to_gcc(order));
158     }
159 
is_lock_freeboost::atomics::detail::gcc_atomic_operations160     static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile& storage) BOOST_NOEXCEPT
161     {
162         return __atomic_is_lock_free(sizeof(storage_type), &storage);
163     }
164 };
165 
166 #if BOOST_ATOMIC_INT128_LOCK_FREE > 0
167 #if defined(__clang__) && defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B)
168 
169 // Workaround for clang bug: http://llvm.org/bugs/show_bug.cgi?id=19149
170 // Clang 3.4 does not implement 128-bit __atomic* intrinsics even though it defines __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16
171 template< bool Signed >
172 struct operations< 16u, Signed > :
173     public cas_based_operations< gcc_dcas_x86_64< Signed > >
174 {
175 };
176 
177 #else
178 
179 template< bool Signed >
180 struct operations< 16u, Signed > :
181     public gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >
182 {
183 };
184 
185 #endif
186 #endif
187 
188 
189 #if BOOST_ATOMIC_INT64_LOCK_FREE > 0
190 #if defined(__clang__) && defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B)
191 
192 // Workaround for clang bug http://llvm.org/bugs/show_bug.cgi?id=19355
193 template< bool Signed >
194 struct operations< 8u, Signed > :
195     public cas_based_operations< gcc_dcas_x86< Signed > >
196 {
197 };
198 
199 #elif (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 8 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\
200     (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 8 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\
201     (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 8 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\
202     (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 8 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\
203     (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 8 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE)
204 
205 #define BOOST_ATOMIC_DETAIL_INT64_EXTENDED
206 
207 template< bool Signed >
208 struct operations< 8u, Signed > :
209     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 8u, Signed >
210 {
211 };
212 
213 #else
214 
215 template< bool Signed >
216 struct operations< 8u, Signed > :
217     public gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >
218 {
219 };
220 
221 #endif
222 #endif
223 
224 #if BOOST_ATOMIC_INT32_LOCK_FREE > 0
225 #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 4 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\
226     (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 4 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\
227     (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 4 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\
228     (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 4 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\
229     (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 4 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE)
230 
231 #define BOOST_ATOMIC_DETAIL_INT32_EXTENDED
232 
233 #if !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
234 
235 template< bool Signed >
236 struct operations< 4u, Signed > :
237     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 4u, Signed >
238 {
239 };
240 
241 #else // !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
242 
243 template< bool Signed >
244 struct operations< 4u, Signed > :
245     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 4u, Signed >
246 {
247 };
248 
249 #endif // !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
250 
251 #else
252 
253 template< bool Signed >
254 struct operations< 4u, Signed > :
255     public gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type >
256 {
257 };
258 
259 #endif
260 #endif
261 
262 #if BOOST_ATOMIC_INT16_LOCK_FREE > 0
263 #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 2 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\
264     (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 2 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\
265     (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 2 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\
266     (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 2 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\
267     (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 2 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE)
268 
269 #define BOOST_ATOMIC_DETAIL_INT16_EXTENDED
270 
271 #if !defined(BOOST_ATOMIC_DETAIL_INT32_EXTENDED)
272 
273 template< bool Signed >
274 struct operations< 2u, Signed > :
275     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type >, 2u, Signed >
276 {
277 };
278 
279 #elif !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
280 
281 template< bool Signed >
282 struct operations< 2u, Signed > :
283     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 2u, Signed >
284 {
285 };
286 
287 #else
288 
289 template< bool Signed >
290 struct operations< 2u, Signed > :
291     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 2u, Signed >
292 {
293 };
294 
295 #endif
296 
297 #else
298 
299 template< bool Signed >
300 struct operations< 2u, Signed > :
301     public gcc_atomic_operations< typename make_storage_type< 2u, Signed >::type >
302 {
303 };
304 
305 #endif
306 #endif
307 
308 #if BOOST_ATOMIC_INT8_LOCK_FREE > 0
309 #if (BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 1 && __GCC_ATOMIC_LLONG_LOCK_FREE != BOOST_ATOMIC_LLONG_LOCK_FREE) ||\
310     (BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 1 && __GCC_ATOMIC_LONG_LOCK_FREE != BOOST_ATOMIC_LONG_LOCK_FREE) ||\
311     (BOOST_ATOMIC_DETAIL_SIZEOF_INT == 1 && __GCC_ATOMIC_INT_LOCK_FREE != BOOST_ATOMIC_INT_LOCK_FREE) ||\
312     (BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 1 && __GCC_ATOMIC_SHORT_LOCK_FREE != BOOST_ATOMIC_SHORT_LOCK_FREE) ||\
313     (BOOST_ATOMIC_DETAIL_SIZEOF_WCHAR_T == 1 && __GCC_ATOMIC_WCHAR_T_LOCK_FREE != BOOST_ATOMIC_WCHAR_T_LOCK_FREE) ||\
314     (__GCC_ATOMIC_CHAR_LOCK_FREE != BOOST_ATOMIC_CHAR_LOCK_FREE) ||\
315     (__GCC_ATOMIC_BOOL_LOCK_FREE != BOOST_ATOMIC_BOOL_LOCK_FREE)
316 
317 #if !defined(BOOST_ATOMIC_DETAIL_INT16_EXTENDED)
318 
319 template< bool Signed >
320 struct operations< 1u, Signed > :
321     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 2u, Signed >::type >, 1u, Signed >
322 {
323 };
324 
325 #elif !defined(BOOST_ATOMIC_DETAIL_INT32_EXTENDED)
326 
327 template< bool Signed >
328 struct operations< 1u, Signed > :
329     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 4u, Signed >::type >, 1u, Signed >
330 {
331 };
332 
333 #elif !defined(BOOST_ATOMIC_DETAIL_INT64_EXTENDED)
334 
335 template< bool Signed >
336 struct operations< 1u, Signed > :
337     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 8u, Signed >::type >, 1u, Signed >
338 {
339 };
340 
341 #else
342 
343 template< bool Signed >
344 struct operations< 1u, Signed > :
345     public extending_cas_based_operations< gcc_atomic_operations< typename make_storage_type< 16u, Signed >::type >, 1u, Signed >
346 {
347 };
348 
349 #endif
350 
351 #else
352 
353 template< bool Signed >
354 struct operations< 1u, Signed > :
355     public gcc_atomic_operations< typename make_storage_type< 1u, Signed >::type >
356 {
357 };
358 
359 #endif
360 #endif
361 
362 #undef BOOST_ATOMIC_DETAIL_INT16_EXTENDED
363 #undef BOOST_ATOMIC_DETAIL_INT32_EXTENDED
364 #undef BOOST_ATOMIC_DETAIL_INT64_EXTENDED
365 
thread_fence(memory_order order)366 BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
367 {
368     __atomic_thread_fence(atomics::detail::convert_memory_order_to_gcc(order));
369 }
370 
signal_fence(memory_order order)371 BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
372 {
373     __atomic_signal_fence(atomics::detail::convert_memory_order_to_gcc(order));
374 }
375 
376 } // namespace detail
377 } // namespace atomics
378 } // namespace boost
379 
380 #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_ATOMIC_HPP_INCLUDED_
381