File indexing completed on 2025-01-30 09:33:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef BOOST_ATOMIC_DETAIL_CORE_OPS_GCC_SYNC_HPP_INCLUDED_
0017 #define BOOST_ATOMIC_DETAIL_CORE_OPS_GCC_SYNC_HPP_INCLUDED_
0018
0019 #include <cstddef>
0020 #include <boost/memory_order.hpp>
0021 #include <boost/atomic/detail/config.hpp>
0022 #include <boost/atomic/detail/storage_traits.hpp>
0023 #include <boost/atomic/detail/core_operations_fwd.hpp>
0024 #include <boost/atomic/detail/extending_cas_based_arithmetic.hpp>
0025 #include <boost/atomic/detail/type_traits/integral_constant.hpp>
0026 #include <boost/atomic/detail/capabilities.hpp>
0027 #include <boost/atomic/detail/header.hpp>
0028
0029 #ifdef BOOST_HAS_PRAGMA_ONCE
0030 #pragma once
0031 #endif
0032
0033 namespace boost {
0034 namespace atomics {
0035 namespace detail {
0036
0037 struct core_operations_gcc_sync_base
0038 {
0039 static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
0040 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
0041
0042 static BOOST_FORCEINLINE void fence_before_store(memory_order order) BOOST_NOEXCEPT
0043 {
0044 if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
0045 __sync_synchronize();
0046 }
0047
0048 static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT
0049 {
0050 if (order == memory_order_seq_cst)
0051 __sync_synchronize();
0052 }
0053
0054 static BOOST_FORCEINLINE void fence_after_load(memory_order order) BOOST_NOEXCEPT
0055 {
0056 if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_acquire) | static_cast< unsigned int >(memory_order_consume))) != 0u)
0057 __sync_synchronize();
0058 }
0059 };
0060
0061 template< std::size_t Size, bool Signed, bool Interprocess >
0062 struct core_operations_gcc_sync :
0063 public core_operations_gcc_sync_base
0064 {
0065 typedef typename storage_traits< Size >::type storage_type;
0066
0067 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
0068 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = storage_traits< storage_size >::alignment;
0069 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
0070 static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
0071
0072
0073
0074
0075
0076 typedef atomics::detail::integral_constant< bool, storage_size <= sizeof(void*) > plain_stores_loads_are_atomic;
0077
0078 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0079 {
0080 store(storage, v, order, plain_stores_loads_are_atomic());
0081 }
0082
0083 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order, atomics::detail::true_type) BOOST_NOEXCEPT
0084 {
0085 fence_before_store(order);
0086 storage = v;
0087 fence_after_store(order);
0088 }
0089
0090 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order, atomics::detail::false_type) BOOST_NOEXCEPT
0091 {
0092 exchange(storage, v, order);
0093 }
0094
0095 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
0096 {
0097 return load(storage, order, plain_stores_loads_are_atomic());
0098 }
0099
0100 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order, atomics::detail::true_type) BOOST_NOEXCEPT
0101 {
0102 storage_type v = storage;
0103 fence_after_load(order);
0104 return v;
0105 }
0106
0107 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order, atomics::detail::false_type) BOOST_NOEXCEPT
0108 {
0109
0110 storage_type expected = storage_type();
0111 storage_type desired = expected;
0112
0113 return __sync_val_compare_and_swap(const_cast< storage_type volatile* >(&storage), expected, desired);
0114 }
0115
0116 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0117 {
0118 return __sync_fetch_and_add(&storage, v);
0119 }
0120
0121 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0122 {
0123 return __sync_fetch_and_sub(&storage, v);
0124 }
0125
0126 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0127 {
0128
0129
0130
0131 if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
0132 __sync_synchronize();
0133 return __sync_lock_test_and_set(&storage, v);
0134 }
0135
0136 static BOOST_FORCEINLINE bool compare_exchange_strong(
0137 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
0138 {
0139 storage_type expected2 = expected;
0140 storage_type old_val = __sync_val_compare_and_swap(&storage, expected2, desired);
0141
0142 if (old_val == expected2)
0143 {
0144 return true;
0145 }
0146 else
0147 {
0148 expected = old_val;
0149 return false;
0150 }
0151 }
0152
0153 static BOOST_FORCEINLINE bool compare_exchange_weak(
0154 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
0155 {
0156 return compare_exchange_strong(storage, expected, desired, success_order, failure_order);
0157 }
0158
0159 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0160 {
0161 return __sync_fetch_and_and(&storage, v);
0162 }
0163
0164 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0165 {
0166 return __sync_fetch_and_or(&storage, v);
0167 }
0168
0169 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
0170 {
0171 return __sync_fetch_and_xor(&storage, v);
0172 }
0173
0174 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0175 {
0176 if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
0177 __sync_synchronize();
0178 return !!__sync_lock_test_and_set(&storage, 1);
0179 }
0180
0181 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0182 {
0183 __sync_lock_release(&storage);
0184 if (order == memory_order_seq_cst)
0185 __sync_synchronize();
0186 }
0187 };
0188
0189 #if BOOST_ATOMIC_INT8_LOCK_FREE > 0
0190 template< bool Signed, bool Interprocess >
0191 struct core_operations< 1u, Signed, Interprocess > :
0192 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1)
0193 public core_operations_gcc_sync< 1u, Signed, Interprocess >
0194 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
0195 public extending_cas_based_arithmetic< core_operations_gcc_sync< 2u, Signed, Interprocess >, 1u, Signed >
0196 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
0197 public extending_cas_based_arithmetic< core_operations_gcc_sync< 4u, Signed, Interprocess >, 1u, Signed >
0198 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
0199 public extending_cas_based_arithmetic< core_operations_gcc_sync< 8u, Signed, Interprocess >, 1u, Signed >
0200 #else
0201 public extending_cas_based_arithmetic< core_operations_gcc_sync< 16u, Signed, Interprocess >, 1u, Signed >
0202 #endif
0203 {
0204 };
0205 #endif
0206
0207 #if BOOST_ATOMIC_INT16_LOCK_FREE > 0
0208 template< bool Signed, bool Interprocess >
0209 struct core_operations< 2u, Signed, Interprocess > :
0210 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
0211 public core_operations_gcc_sync< 2u, Signed, Interprocess >
0212 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
0213 public extending_cas_based_arithmetic< core_operations_gcc_sync< 4u, Signed, Interprocess >, 2u, Signed >
0214 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
0215 public extending_cas_based_arithmetic< core_operations_gcc_sync< 8u, Signed, Interprocess >, 2u, Signed >
0216 #else
0217 public extending_cas_based_arithmetic< core_operations_gcc_sync< 16u, Signed, Interprocess >, 2u, Signed >
0218 #endif
0219 {
0220 };
0221 #endif
0222
0223 #if BOOST_ATOMIC_INT32_LOCK_FREE > 0
0224 template< bool Signed, bool Interprocess >
0225 struct core_operations< 4u, Signed, Interprocess > :
0226 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
0227 public core_operations_gcc_sync< 4u, Signed, Interprocess >
0228 #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
0229 public extending_cas_based_arithmetic< core_operations_gcc_sync< 8u, Signed, Interprocess >, 4u, Signed >
0230 #else
0231 public extending_cas_based_arithmetic< core_operations_gcc_sync< 16u, Signed, Interprocess >, 4u, Signed >
0232 #endif
0233 {
0234 };
0235 #endif
0236
0237 #if BOOST_ATOMIC_INT64_LOCK_FREE > 0
0238 template< bool Signed, bool Interprocess >
0239 struct core_operations< 8u, Signed, Interprocess > :
0240 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
0241 public core_operations_gcc_sync< 8u, Signed, Interprocess >
0242 #else
0243 public extending_cas_based_arithmetic< core_operations_gcc_sync< 16u, Signed, Interprocess >, 8u, Signed >
0244 #endif
0245 {
0246 };
0247 #endif
0248
0249 #if BOOST_ATOMIC_INT128_LOCK_FREE > 0
0250 template< bool Signed, bool Interprocess >
0251 struct core_operations< 16u, Signed, Interprocess > :
0252 public core_operations_gcc_sync< 16u, Signed, Interprocess >
0253 {
0254 };
0255 #endif
0256
0257 }
0258 }
0259 }
0260
0261 #include <boost/atomic/detail/footer.hpp>
0262
0263 #endif