]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/atomic/detail/extra_ops_generic.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / atomic / detail / extra_ops_generic.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) 2015 Andrey Semashev
7 */
8 /*!
9 * \file atomic/detail/extra_ops_generic.hpp
10 *
11 * This header contains generic implementation of the extra atomic operations.
12 */
13
14 #ifndef BOOST_ATOMIC_DETAIL_EXTRA_OPS_GENERIC_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_EXTRA_OPS_GENERIC_HPP_INCLUDED_
16
17 #include <cstddef>
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>
23
24 #ifdef BOOST_HAS_PRAGMA_ONCE
25 #pragma once
26 #endif
27
28 #if defined(BOOST_MSVC)
29 #pragma warning(push)
30 // unary minus operator applied to unsigned type, result still unsigned
31 #pragma warning(disable: 4146)
32 #endif
33
34 namespace boost {
35 namespace atomics {
36 namespace detail {
37
38 //! Generic implementation of extra operations
39 template< typename Base, std::size_t Size, bool Signed >
40 struct generic_extra_operations :
41 public Base
42 {
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;
47
48 static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
49 {
50 storage_type old_val;
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)) {}
53 return old_val;
54 }
55
56 static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
57 {
58 return Base::fetch_xor(storage, static_cast< storage_type >(~static_cast< emulated_storage_type >(0)), order);
59 }
60
61 static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
62 {
63 Base::fetch_add(storage, v, order);
64 }
65
66 static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
67 {
68 Base::fetch_sub(storage, v, order);
69 }
70
71 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
72 {
73 fetch_negate(storage, order);
74 }
75
76 static BOOST_FORCEINLINE void opaque_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
77 {
78 Base::fetch_and(storage, v, order);
79 }
80
81 static BOOST_FORCEINLINE void opaque_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
82 {
83 Base::fetch_or(storage, v, order);
84 }
85
86 static BOOST_FORCEINLINE void opaque_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
87 {
88 Base::fetch_xor(storage, v, order);
89 }
90
91 static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
92 {
93 fetch_complement(storage, order);
94 }
95
96 static BOOST_FORCEINLINE bool add_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
97 {
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);
100 return !new_val;
101 }
102
103 static BOOST_FORCEINLINE bool sub_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
104 {
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);
107 return !new_val;
108 }
109
110 static BOOST_FORCEINLINE bool and_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
111 {
112 return !(Base::fetch_and(storage, v, order) & v);
113 }
114
115 static BOOST_FORCEINLINE bool or_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
116 {
117 return !(Base::fetch_or(storage, v, order) | v);
118 }
119
120 static BOOST_FORCEINLINE bool xor_and_test(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
121 {
122 return !(Base::fetch_xor(storage, v, order) ^ v);
123 }
124
125 static BOOST_FORCEINLINE bool bit_test_and_set(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
126 {
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);
130 }
131
132 static BOOST_FORCEINLINE bool bit_test_and_reset(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
133 {
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);
137 }
138
139 static BOOST_FORCEINLINE bool bit_test_and_complement(storage_type volatile& storage, unsigned int bit_number, memory_order order) BOOST_NOEXCEPT
140 {
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);
144 }
145 };
146
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 >
151 {
152 };
153
154 } // namespace detail
155 } // namespace atomics
156 } // namespace boost
157
158 #if defined(BOOST_MSVC)
159 #pragma warning(pop)
160 #endif
161
162 #endif // BOOST_ATOMIC_DETAIL_EXTRA_OPS_GENERIC_HPP_INCLUDED_