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) 2015 Andrey Semashev
9 * \file atomic/detail/extra_ops_generic.hpp
11 * This header contains generic implementation of the extra atomic operations.
14 #ifndef BOOST_ATOMIC_DETAIL_EXTRA_OPS_GENERIC_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_EXTRA_OPS_GENERIC_HPP_INCLUDED_
18 #include <boost/memory_order.hpp>
19 #include <boost/atomic/detail/config.hpp>
20 #include <boost/atomic/detail/storage_type.hpp>
21 #include <boost/atomic/detail/extra_operations_fwd.hpp>
22 #include <boost/atomic/capabilities.hpp>
24 #ifdef BOOST_HAS_PRAGMA_ONCE
28 #if defined(BOOST_MSVC)
30 // unary minus operator applied to unsigned type, result still unsigned
31 #pragma warning(disable: 4146)
38 //! Generic implementation of extra operations
39 template< typename Base, std::size_t Size, bool Signed >
40 struct generic_extra_operations :
43 typedef Base base_type;
44 typedef typename base_type::storage_type storage_type;
45 typedef typename make_storage_type< Size, Signed >::type emulated_storage_type;
46 typedef typename make_storage_type< Size, false >::type unsigned_emulated_storage_type;
48 static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
51 atomics::detail::non_atomic_load(storage, old_val);
52 while (!Base::compare_exchange_weak(storage, old_val, static_cast< storage_type >(-static_cast< emulated_storage_type >(old_val)), order, memory_order_relaxed)) {}
56 static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
58 return Base::fetch_xor(storage, static_cast< storage_type >(~static_cast< emulated_storage_type >(0)), order);
61 static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
63 Base::fetch_add(storage, v, order);
66 static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
68 Base::fetch_sub(storage, v, order);
71 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
73 fetch_negate(storage, order);
76 static BOOST_FORCEINLINE void opaque_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
78 Base::fetch_and(storage, v, order);
81 static BOOST_FORCEINLINE void opaque_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
83 Base::fetch_or(storage, v, order);
86 static BOOST_FORCEINLINE void opaque_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
88 Base::fetch_xor(storage, v, order);
91 static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
93 fetch_complement(storage, order);
96 static BOOST_FORCEINLINE bool add_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
98 storage_type old_val = Base::fetch_add(storage, v, order);
99 emulated_storage_type new_val = static_cast< emulated_storage_type >(old_val) + static_cast< emulated_storage_type >(v);
103 static BOOST_FORCEINLINE bool sub_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
105 storage_type old_val = Base::fetch_sub(storage, v, order);
106 emulated_storage_type new_val = static_cast< emulated_storage_type >(old_val) - static_cast< emulated_storage_type >(v);
110 static BOOST_FORCEINLINE bool and_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
112 return !(Base::fetch_and(storage, v, order) & v);
115 static BOOST_FORCEINLINE bool or_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
117 return !(Base::fetch_or(storage, v, order) | v);
120 static BOOST_FORCEINLINE bool xor_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
122 return !(Base::fetch_xor(storage, v, order) ^ v);
125 static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
127 storage_type mask = storage_type(((unsigned_emulated_storage_type)1u) << bit_number);
128 storage_type old_val = Base::fetch_or(storage, mask, order);
129 return !!(old_val & mask);
132 static BOOST_FORCEINLINE bool bit_test_and_reset(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
134 storage_type mask = storage_type(((unsigned_emulated_storage_type)1u) << bit_number);
135 storage_type old_val = Base::fetch_and(storage, ~mask, order);
136 return !!(old_val & mask);
139 static BOOST_FORCEINLINE bool bit_test_and_complement(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
141 storage_type mask = storage_type(((unsigned_emulated_storage_type)1u) << bit_number);
142 storage_type old_val = Base::fetch_xor(storage, mask, order);
143 return !!(old_val & mask);
147 // Default extra_operations template definition will be used unless specialized for a specific platform
148 template< typename Base, std::size_t Size, bool Signed >
149 struct extra_operations :
150 public generic_extra_operations< Base, Size, Signed >
154 } // namespace detail
155 } // namespace atomics
158 #if defined(BOOST_MSVC)
162 #endif // BOOST_ATOMIC_DETAIL_EXTRA_OPS_GENERIC_HPP_INCLUDED_