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) 2011 Helge Bahmann
7 * Copyright (c) 2013 Tim Blechmann
8 * Copyright (c) 2014 Andrey Semashev
11 * \file atomic/detail/atomic_template.hpp
13 * This header contains interface definition of \c atomic template.
16 #ifndef BOOST_ATOMIC_DETAIL_ATOMIC_TEMPLATE_HPP_INCLUDED_
17 #define BOOST_ATOMIC_DETAIL_ATOMIC_TEMPLATE_HPP_INCLUDED_
20 #include <boost/cstdint.hpp>
21 #include <boost/assert.hpp>
22 #include <boost/type_traits/is_signed.hpp>
23 #include <boost/type_traits/is_integral.hpp>
24 #include <boost/atomic/detail/config.hpp>
25 #include <boost/atomic/detail/bitwise_cast.hpp>
26 #include <boost/atomic/detail/operations_fwd.hpp>
28 #ifdef BOOST_HAS_PRAGMA_ONCE
32 #if defined(BOOST_MSVC)
34 // 'boost::atomics::atomic<T>' : multiple assignment operators specified
35 #pragma warning(disable: 4522)
39 * IMPLEMENTATION NOTE: All interface functions MUST be declared with BOOST_FORCEINLINE,
40 * see comment for convert_memory_order_to_gcc in ops_gcc_atomic.hpp.
47 BOOST_FORCEINLINE BOOST_CONSTEXPR memory_order deduce_failure_order(memory_order order) BOOST_NOEXCEPT
49 return order == memory_order_acq_rel ? memory_order_acquire : (order == memory_order_release ? memory_order_relaxed : order);
52 BOOST_FORCEINLINE BOOST_CONSTEXPR bool cas_failure_order_must_not_be_stronger_than_success_order(memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
54 // 15 == (memory_order_seq_cst | memory_order_consume), see memory_order.hpp
55 // Given the enum values we can test the strength of memory order requirements with this single condition.
56 return (failure_order & 15u) <= (success_order & 15u);
59 template< typename T, bool IsInt = boost::is_integral< T >::value >
65 template< typename T >
66 struct classify< T, true > { typedef int type; };
68 template< typename T >
69 struct classify< T*, false > { typedef void* type; };
71 template< typename T, typename Kind >
74 //! Implementation for integers
75 template< typename T >
76 class base_atomic< T, int >
80 typedef T difference_type;
83 typedef atomics::detail::operations< storage_size_of< value_type >::value, boost::is_signed< T >::value > operations;
84 typedef value_type value_arg_type;
87 typedef typename operations::storage_type storage_type;
90 typename operations::aligned_storage_type m_storage;
93 BOOST_DEFAULTED_FUNCTION(base_atomic(), {})
94 BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : m_storage(v) {}
96 BOOST_FORCEINLINE void store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
98 BOOST_ASSERT(order != memory_order_consume);
99 BOOST_ASSERT(order != memory_order_acquire);
100 BOOST_ASSERT(order != memory_order_acq_rel);
102 operations::store(m_storage.value, static_cast< storage_type >(v), order);
105 BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
107 BOOST_ASSERT(order != memory_order_release);
108 BOOST_ASSERT(order != memory_order_acq_rel);
110 return static_cast< value_type >(operations::load(m_storage.value, order));
113 BOOST_FORCEINLINE value_type fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
115 return static_cast< value_type >(operations::fetch_add(m_storage.value, static_cast< storage_type >(v), order));
118 BOOST_FORCEINLINE value_type fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
120 return static_cast< value_type >(operations::fetch_sub(m_storage.value, static_cast< storage_type >(v), order));
123 BOOST_FORCEINLINE value_type exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
125 return static_cast< value_type >(operations::exchange(m_storage.value, static_cast< storage_type >(v), order));
128 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
130 BOOST_ASSERT(failure_order != memory_order_release);
131 BOOST_ASSERT(failure_order != memory_order_acq_rel);
132 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
134 storage_type old_value = static_cast< storage_type >(expected);
135 const bool res = operations::compare_exchange_strong(m_storage.value, old_value, static_cast< storage_type >(desired), success_order, failure_order);
136 expected = static_cast< value_type >(old_value);
140 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
142 return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
145 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
147 BOOST_ASSERT(failure_order != memory_order_release);
148 BOOST_ASSERT(failure_order != memory_order_acq_rel);
149 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
151 storage_type old_value = static_cast< storage_type >(expected);
152 const bool res = operations::compare_exchange_weak(m_storage.value, old_value, static_cast< storage_type >(desired), success_order, failure_order);
153 expected = static_cast< value_type >(old_value);
157 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
159 return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
162 BOOST_FORCEINLINE value_type fetch_and(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
164 return static_cast< value_type >(operations::fetch_and(m_storage.value, static_cast< storage_type >(v), order));
167 BOOST_FORCEINLINE value_type fetch_or(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
169 return static_cast< value_type >(operations::fetch_or(m_storage.value, static_cast< storage_type >(v), order));
172 BOOST_FORCEINLINE value_type fetch_xor(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
174 return static_cast< value_type >(operations::fetch_xor(m_storage.value, static_cast< storage_type >(v), order));
177 BOOST_FORCEINLINE bool is_lock_free() const volatile BOOST_NOEXCEPT
179 return operations::is_lock_free(m_storage.value);
182 BOOST_FORCEINLINE value_type operator++(int) volatile BOOST_NOEXCEPT
187 BOOST_FORCEINLINE value_type operator++() volatile BOOST_NOEXCEPT
189 return fetch_add(1) + 1;
192 BOOST_FORCEINLINE value_type operator--(int) volatile BOOST_NOEXCEPT
197 BOOST_FORCEINLINE value_type operator--() volatile BOOST_NOEXCEPT
199 return fetch_sub(1) - 1;
202 BOOST_FORCEINLINE value_type operator+=(difference_type v) volatile BOOST_NOEXCEPT
204 return fetch_add(v) + v;
207 BOOST_FORCEINLINE value_type operator-=(difference_type v) volatile BOOST_NOEXCEPT
209 return fetch_sub(v) - v;
212 BOOST_FORCEINLINE value_type operator&=(value_type v) volatile BOOST_NOEXCEPT
214 return fetch_and(v) & v;
217 BOOST_FORCEINLINE value_type operator|=(value_type v) volatile BOOST_NOEXCEPT
219 return fetch_or(v) | v;
222 BOOST_FORCEINLINE value_type operator^=(value_type v) volatile BOOST_NOEXCEPT
224 return fetch_xor(v) ^ v;
227 BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
228 BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
231 //! Implementation for bool
233 class base_atomic< bool, int >
236 typedef bool value_type;
239 typedef atomics::detail::operations< 1u, false > operations;
240 typedef value_type value_arg_type;
243 typedef operations::storage_type storage_type;
246 operations::aligned_storage_type m_storage;
249 BOOST_DEFAULTED_FUNCTION(base_atomic(), {})
250 BOOST_CONSTEXPR explicit base_atomic(value_type v) BOOST_NOEXCEPT : m_storage(v) {}
252 BOOST_FORCEINLINE void store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
254 BOOST_ASSERT(order != memory_order_consume);
255 BOOST_ASSERT(order != memory_order_acquire);
256 BOOST_ASSERT(order != memory_order_acq_rel);
258 operations::store(m_storage.value, static_cast< storage_type >(v), order);
261 BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
263 BOOST_ASSERT(order != memory_order_release);
264 BOOST_ASSERT(order != memory_order_acq_rel);
266 return !!operations::load(m_storage.value, order);
269 BOOST_FORCEINLINE value_type exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
271 return !!operations::exchange(m_storage.value, static_cast< storage_type >(v), order);
274 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
276 BOOST_ASSERT(failure_order != memory_order_release);
277 BOOST_ASSERT(failure_order != memory_order_acq_rel);
278 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
280 storage_type old_value = static_cast< storage_type >(expected);
281 const bool res = operations::compare_exchange_strong(m_storage.value, old_value, static_cast< storage_type >(desired), success_order, failure_order);
282 expected = !!old_value;
286 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
288 return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
291 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
293 BOOST_ASSERT(failure_order != memory_order_release);
294 BOOST_ASSERT(failure_order != memory_order_acq_rel);
295 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
297 storage_type old_value = static_cast< storage_type >(expected);
298 const bool res = operations::compare_exchange_weak(m_storage.value, old_value, static_cast< storage_type >(desired), success_order, failure_order);
299 expected = !!old_value;
303 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
305 return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
308 BOOST_FORCEINLINE bool is_lock_free() const volatile BOOST_NOEXCEPT
310 return operations::is_lock_free(m_storage.value);
313 BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
314 BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
318 //! Implementation for user-defined types, such as structs and enums
319 template< typename T >
320 class base_atomic< T, void >
323 typedef T value_type;
326 typedef atomics::detail::operations< storage_size_of< value_type >::value, false > operations;
327 typedef value_type const& value_arg_type;
330 typedef typename operations::storage_type storage_type;
333 typename operations::aligned_storage_type m_storage;
336 BOOST_FORCEINLINE explicit base_atomic(value_type const& v = value_type()) BOOST_NOEXCEPT : m_storage(atomics::detail::bitwise_cast< storage_type >(v))
340 BOOST_FORCEINLINE void store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
342 BOOST_ASSERT(order != memory_order_consume);
343 BOOST_ASSERT(order != memory_order_acquire);
344 BOOST_ASSERT(order != memory_order_acq_rel);
346 operations::store(m_storage.value, atomics::detail::bitwise_cast< storage_type >(v), order);
349 BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
351 BOOST_ASSERT(order != memory_order_release);
352 BOOST_ASSERT(order != memory_order_acq_rel);
354 return atomics::detail::bitwise_cast< value_type >(operations::load(m_storage.value, order));
357 BOOST_FORCEINLINE value_type exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
359 return atomics::detail::bitwise_cast< value_type >(operations::exchange(m_storage.value, atomics::detail::bitwise_cast< storage_type >(v), order));
362 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
364 BOOST_ASSERT(failure_order != memory_order_release);
365 BOOST_ASSERT(failure_order != memory_order_acq_rel);
366 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
368 storage_type old_value = atomics::detail::bitwise_cast< storage_type >(expected);
369 const bool res = operations::compare_exchange_strong(m_storage.value, old_value, atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
370 expected = atomics::detail::bitwise_cast< value_type >(old_value);
374 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
376 return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
379 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
381 BOOST_ASSERT(failure_order != memory_order_release);
382 BOOST_ASSERT(failure_order != memory_order_acq_rel);
383 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
385 storage_type old_value = atomics::detail::bitwise_cast< storage_type >(expected);
386 const bool res = operations::compare_exchange_weak(m_storage.value, old_value, atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
387 expected = atomics::detail::bitwise_cast< value_type >(old_value);
391 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
393 return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
396 BOOST_FORCEINLINE bool is_lock_free() const volatile BOOST_NOEXCEPT
398 return operations::is_lock_free(m_storage.value);
401 BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
402 BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
406 //! Implementation for pointers
407 template< typename T >
408 class base_atomic< T*, void* >
411 typedef T* value_type;
412 typedef std::ptrdiff_t difference_type;
415 typedef atomics::detail::operations< storage_size_of< value_type >::value, false > operations;
416 typedef value_type value_arg_type;
419 typedef typename operations::storage_type storage_type;
422 typename operations::aligned_storage_type m_storage;
425 BOOST_DEFAULTED_FUNCTION(base_atomic(), {})
426 BOOST_FORCEINLINE explicit base_atomic(value_type const& v) BOOST_NOEXCEPT : m_storage(atomics::detail::bitwise_cast< storage_type >(v))
430 BOOST_FORCEINLINE void store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
432 BOOST_ASSERT(order != memory_order_consume);
433 BOOST_ASSERT(order != memory_order_acquire);
434 BOOST_ASSERT(order != memory_order_acq_rel);
436 operations::store(m_storage.value, atomics::detail::bitwise_cast< storage_type >(v), order);
439 BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
441 BOOST_ASSERT(order != memory_order_release);
442 BOOST_ASSERT(order != memory_order_acq_rel);
444 return atomics::detail::bitwise_cast< value_type >(operations::load(m_storage.value, order));
447 BOOST_FORCEINLINE value_type fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
449 return atomics::detail::bitwise_cast< value_type >(operations::fetch_add(m_storage.value, static_cast< storage_type >(v * sizeof(T)), order));
452 BOOST_FORCEINLINE value_type fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
454 return atomics::detail::bitwise_cast< value_type >(operations::fetch_sub(m_storage.value, static_cast< storage_type >(v * sizeof(T)), order));
457 BOOST_FORCEINLINE value_type exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
459 return atomics::detail::bitwise_cast< value_type >(operations::exchange(m_storage.value, atomics::detail::bitwise_cast< storage_type >(v), order));
462 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
464 BOOST_ASSERT(failure_order != memory_order_release);
465 BOOST_ASSERT(failure_order != memory_order_acq_rel);
466 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
468 storage_type old_value = atomics::detail::bitwise_cast< storage_type >(expected);
469 const bool res = operations::compare_exchange_strong(m_storage.value, old_value, atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
470 expected = atomics::detail::bitwise_cast< value_type >(old_value);
474 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
476 return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
479 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
481 BOOST_ASSERT(failure_order != memory_order_release);
482 BOOST_ASSERT(failure_order != memory_order_acq_rel);
483 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
485 storage_type old_value = atomics::detail::bitwise_cast< storage_type >(expected);
486 const bool res = operations::compare_exchange_weak(m_storage.value, old_value, atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
487 expected = atomics::detail::bitwise_cast< value_type >(old_value);
491 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
493 return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
496 BOOST_FORCEINLINE bool is_lock_free() const volatile BOOST_NOEXCEPT
498 return operations::is_lock_free(m_storage.value);
501 BOOST_FORCEINLINE value_type operator++(int) volatile BOOST_NOEXCEPT
506 BOOST_FORCEINLINE value_type operator++() volatile BOOST_NOEXCEPT
508 return fetch_add(1) + 1;
511 BOOST_FORCEINLINE value_type operator--(int) volatile BOOST_NOEXCEPT
516 BOOST_FORCEINLINE value_type operator--() volatile BOOST_NOEXCEPT
518 return fetch_sub(1) - 1;
521 BOOST_FORCEINLINE value_type operator+=(difference_type v) volatile BOOST_NOEXCEPT
523 return fetch_add(v) + v;
526 BOOST_FORCEINLINE value_type operator-=(difference_type v) volatile BOOST_NOEXCEPT
528 return fetch_sub(v) - v;
531 BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
532 BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
536 //! Implementation for void pointers
538 class base_atomic< void*, void* >
541 typedef void* value_type;
542 typedef std::ptrdiff_t difference_type;
545 typedef atomics::detail::operations< storage_size_of< value_type >::value, false > operations;
546 typedef value_type value_arg_type;
549 typedef operations::storage_type storage_type;
552 operations::aligned_storage_type m_storage;
555 BOOST_DEFAULTED_FUNCTION(base_atomic(), {})
556 BOOST_FORCEINLINE explicit base_atomic(value_type const& v) BOOST_NOEXCEPT : m_storage(atomics::detail::bitwise_cast< storage_type >(v))
560 BOOST_FORCEINLINE void store(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
562 BOOST_ASSERT(order != memory_order_consume);
563 BOOST_ASSERT(order != memory_order_acquire);
564 BOOST_ASSERT(order != memory_order_acq_rel);
566 operations::store(m_storage.value, atomics::detail::bitwise_cast< storage_type >(v), order);
569 BOOST_FORCEINLINE value_type load(memory_order order = memory_order_seq_cst) const volatile BOOST_NOEXCEPT
571 BOOST_ASSERT(order != memory_order_release);
572 BOOST_ASSERT(order != memory_order_acq_rel);
574 return atomics::detail::bitwise_cast< value_type >(operations::load(m_storage.value, order));
577 BOOST_FORCEINLINE value_type fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
579 return atomics::detail::bitwise_cast< value_type >(operations::fetch_add(m_storage.value, static_cast< storage_type >(v), order));
582 BOOST_FORCEINLINE value_type fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
584 return atomics::detail::bitwise_cast< value_type >(operations::fetch_sub(m_storage.value, static_cast< storage_type >(v), order));
587 BOOST_FORCEINLINE value_type exchange(value_type v, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
589 return atomics::detail::bitwise_cast< value_type >(operations::exchange(m_storage.value, atomics::detail::bitwise_cast< storage_type >(v), order));
592 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
594 BOOST_ASSERT(failure_order != memory_order_release);
595 BOOST_ASSERT(failure_order != memory_order_acq_rel);
596 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
598 storage_type old_value = atomics::detail::bitwise_cast< storage_type >(expected);
599 const bool res = operations::compare_exchange_strong(m_storage.value, old_value, atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
600 expected = atomics::detail::bitwise_cast< value_type >(old_value);
604 BOOST_FORCEINLINE bool compare_exchange_strong(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
606 return compare_exchange_strong(expected, desired, order, atomics::detail::deduce_failure_order(order));
609 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order success_order, memory_order failure_order) volatile BOOST_NOEXCEPT
611 BOOST_ASSERT(failure_order != memory_order_release);
612 BOOST_ASSERT(failure_order != memory_order_acq_rel);
613 BOOST_ASSERT(cas_failure_order_must_not_be_stronger_than_success_order(success_order, failure_order));
615 storage_type old_value = atomics::detail::bitwise_cast< storage_type >(expected);
616 const bool res = operations::compare_exchange_weak(m_storage.value, old_value, atomics::detail::bitwise_cast< storage_type >(desired), success_order, failure_order);
617 expected = atomics::detail::bitwise_cast< value_type >(old_value);
621 BOOST_FORCEINLINE bool compare_exchange_weak(value_type& expected, value_type desired, memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
623 return compare_exchange_weak(expected, desired, order, atomics::detail::deduce_failure_order(order));
626 BOOST_FORCEINLINE bool is_lock_free() const volatile BOOST_NOEXCEPT
628 return operations::is_lock_free(m_storage.value);
631 BOOST_FORCEINLINE value_type operator++(int) volatile BOOST_NOEXCEPT
636 BOOST_FORCEINLINE value_type operator++() volatile BOOST_NOEXCEPT
638 return (char*)fetch_add(1) + 1;
641 BOOST_FORCEINLINE value_type operator--(int) volatile BOOST_NOEXCEPT
646 BOOST_FORCEINLINE value_type operator--() volatile BOOST_NOEXCEPT
648 return (char*)fetch_sub(1) - 1;
651 BOOST_FORCEINLINE value_type operator+=(difference_type v) volatile BOOST_NOEXCEPT
653 return (char*)fetch_add(v) + v;
656 BOOST_FORCEINLINE value_type operator-=(difference_type v) volatile BOOST_NOEXCEPT
658 return (char*)fetch_sub(v) - v;
661 BOOST_DELETED_FUNCTION(base_atomic(base_atomic const&))
662 BOOST_DELETED_FUNCTION(base_atomic& operator=(base_atomic const&))
665 } // namespace detail
667 template< typename T >
669 public atomics::detail::base_atomic< T, typename atomics::detail::classify< T >::type >
672 typedef T value_type;
673 typedef atomics::detail::base_atomic< T, typename atomics::detail::classify< T >::type > base_type;
674 typedef typename base_type::value_arg_type value_arg_type;
677 typedef typename base_type::storage_type storage_type;
680 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = base_type::operations::is_always_lock_free;
683 BOOST_DEFAULTED_FUNCTION(atomic(), BOOST_NOEXCEPT {})
685 // NOTE: The constructor is made explicit because gcc 4.7 complains that
686 // operator=(value_arg_type) is considered ambiguous with operator=(atomic const&)
687 // in assignment expressions, even though conversion to atomic<> is less preferred
688 // than conversion to value_arg_type.
689 BOOST_FORCEINLINE explicit BOOST_CONSTEXPR atomic(value_arg_type v) BOOST_NOEXCEPT : base_type(v) {}
691 BOOST_FORCEINLINE value_type operator= (value_arg_type v) volatile BOOST_NOEXCEPT
697 BOOST_FORCEINLINE operator value_type() volatile const BOOST_NOEXCEPT
702 BOOST_FORCEINLINE storage_type& storage() BOOST_NOEXCEPT { return this->m_storage.value; }
703 BOOST_FORCEINLINE storage_type volatile& storage() volatile BOOST_NOEXCEPT { return this->m_storage.value; }
704 BOOST_FORCEINLINE storage_type const& storage() const BOOST_NOEXCEPT { return this->m_storage.value; }
705 BOOST_FORCEINLINE storage_type const volatile& storage() const volatile BOOST_NOEXCEPT { return this->m_storage.value; }
707 BOOST_DELETED_FUNCTION(atomic(atomic const&))
708 BOOST_DELETED_FUNCTION(atomic& operator= (atomic const&))
709 BOOST_DELETED_FUNCTION(atomic& operator= (atomic const&) volatile)
712 template< typename T >
713 BOOST_CONSTEXPR_OR_CONST bool atomic< T >::is_always_lock_free;
715 typedef atomic< char > atomic_char;
716 typedef atomic< unsigned char > atomic_uchar;
717 typedef atomic< signed char > atomic_schar;
718 typedef atomic< uint8_t > atomic_uint8_t;
719 typedef atomic< int8_t > atomic_int8_t;
720 typedef atomic< unsigned short > atomic_ushort;
721 typedef atomic< short > atomic_short;
722 typedef atomic< uint16_t > atomic_uint16_t;
723 typedef atomic< int16_t > atomic_int16_t;
724 typedef atomic< unsigned int > atomic_uint;
725 typedef atomic< int > atomic_int;
726 typedef atomic< uint32_t > atomic_uint32_t;
727 typedef atomic< int32_t > atomic_int32_t;
728 typedef atomic< unsigned long > atomic_ulong;
729 typedef atomic< long > atomic_long;
730 typedef atomic< uint64_t > atomic_uint64_t;
731 typedef atomic< int64_t > atomic_int64_t;
732 #ifdef BOOST_HAS_LONG_LONG
733 typedef atomic< boost::ulong_long_type > atomic_ullong;
734 typedef atomic< boost::long_long_type > atomic_llong;
736 typedef atomic< void* > atomic_address;
737 typedef atomic< bool > atomic_bool;
738 typedef atomic< wchar_t > atomic_wchar_t;
739 #if !defined(BOOST_NO_CXX11_CHAR16_T)
740 typedef atomic< char16_t > atomic_char16_t;
742 #if !defined(BOOST_NO_CXX11_CHAR32_T)
743 typedef atomic< char32_t > atomic_char32_t;
746 typedef atomic< int_least8_t > atomic_int_least8_t;
747 typedef atomic< uint_least8_t > atomic_uint_least8_t;
748 typedef atomic< int_least16_t > atomic_int_least16_t;
749 typedef atomic< uint_least16_t > atomic_uint_least16_t;
750 typedef atomic< int_least32_t > atomic_int_least32_t;
751 typedef atomic< uint_least32_t > atomic_uint_least32_t;
752 typedef atomic< int_least64_t > atomic_int_least64_t;
753 typedef atomic< uint_least64_t > atomic_uint_least64_t;
754 typedef atomic< int_fast8_t > atomic_int_fast8_t;
755 typedef atomic< uint_fast8_t > atomic_uint_fast8_t;
756 typedef atomic< int_fast16_t > atomic_int_fast16_t;
757 typedef atomic< uint_fast16_t > atomic_uint_fast16_t;
758 typedef atomic< int_fast32_t > atomic_int_fast32_t;
759 typedef atomic< uint_fast32_t > atomic_uint_fast32_t;
760 typedef atomic< int_fast64_t > atomic_int_fast64_t;
761 typedef atomic< uint_fast64_t > atomic_uint_fast64_t;
762 typedef atomic< intmax_t > atomic_intmax_t;
763 typedef atomic< uintmax_t > atomic_uintmax_t;
765 typedef atomic< std::size_t > atomic_size_t;
766 typedef atomic< std::ptrdiff_t > atomic_ptrdiff_t;
768 #if defined(BOOST_HAS_INTPTR_T)
769 typedef atomic< intptr_t > atomic_intptr_t;
770 typedef atomic< uintptr_t > atomic_uintptr_t;
773 } // namespace atomics
776 #if defined(BOOST_MSVC)
780 #endif // BOOST_ATOMIC_DETAIL_ATOMIC_TEMPLATE_HPP_INCLUDED_