Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:30:05

0001 /*
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * Copyright (c) 2014, 2020 Andrey Semashev
0007  */
0008 /*!
0009  * \file   atomic/detail/core_ops_gcc_atomic.hpp
0010  *
0011  * This header contains implementation of the \c core_operations template.
0012  */
0013 
0014 #ifndef BOOST_ATOMIC_DETAIL_CORE_OPS_GCC_ATOMIC_HPP_INCLUDED_
0015 #define BOOST_ATOMIC_DETAIL_CORE_OPS_GCC_ATOMIC_HPP_INCLUDED_
0016 
0017 #include <cstddef>
0018 #include <boost/memory_order.hpp>
0019 #include <boost/atomic/detail/config.hpp>
0020 #include <boost/atomic/detail/storage_traits.hpp>
0021 #include <boost/atomic/detail/core_operations_fwd.hpp>
0022 #include <boost/atomic/detail/core_arch_operations.hpp>
0023 #include <boost/atomic/detail/capabilities.hpp>
0024 #include <boost/atomic/detail/gcc_atomic_memory_order_utils.hpp>
0025 
0026 #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT8_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE || BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE ||\
0027     BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE || BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE
0028 // There are platforms where we need to use larger storage types
0029 #include <boost/atomic/detail/int_sizes.hpp>
0030 #include <boost/atomic/detail/extending_cas_based_arithmetic.hpp>
0031 #endif
0032 #include <boost/atomic/detail/header.hpp>
0033 
0034 #ifdef BOOST_HAS_PRAGMA_ONCE
0035 #pragma once
0036 #endif
0037 
0038 #if defined(__INTEL_COMPILER)
0039 // This is used to suppress warning #32013 described in gcc_atomic_memory_order_utils.hpp
0040 // for Intel Compiler.
0041 // In debug builds the compiler does not inline any functions, so basically
0042 // every atomic function call results in this warning. I don't know any other
0043 // way to selectively disable just this one warning.
0044 #pragma system_header
0045 #endif
0046 
0047 namespace boost {
0048 namespace atomics {
0049 namespace detail {
0050 
0051 template< std::size_t Size, bool Signed, bool Interprocess >
0052 struct core_operations_gcc_atomic
0053 {
0054     typedef typename storage_traits< Size >::type storage_type;
0055 
0056     static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
0057     static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = storage_traits< Size >::alignment;
0058     static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
0059     static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
0060     static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
0061 
0062     // Note: In the current implementation, core_operations_gcc_atomic are used only when the particularly sized __atomic
0063     // intrinsics are always lock-free (i.e. the corresponding LOCK_FREE macro is 2). Therefore it is safe to
0064     // always set is_always_lock_free to true here.
0065     static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
0066 
0067     static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0068     {
0069 #if defined(BOOST_GCC) && BOOST_GCC < 100100 && (defined(__x86_64__) || defined(__i386__))
0070         // gcc up to 10.1 generates mov + mfence for seq_cst stores, which is slower than xchg
0071         if (order != memory_order_seq_cst)
0072             __atomic_store_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
0073         else
0074             __atomic_exchange_n(&storage, v, __ATOMIC_SEQ_CST);
0075 #else
0076         __atomic_store_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
0077 #endif
0078     }
0079 
0080     static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
0081     {
0082 #if defined(BOOST_ATOMIC_DETAIL_AARCH64_HAS_RCPC) && !((defined(BOOST_GCC) && BOOST_GCC >= 130100) || (defined(BOOST_CLANG) && BOOST_CLANG_VERSION >= 160000))
0083         // At least gcc 9.3 and clang 10 do not generate relaxed ldapr instructions that are available in ARMv8.3-RCPC extension.
0084         // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95751
0085         // This was fixed in gcc 13.1 and clang 16.
0086         typedef atomics::detail::core_arch_operations< storage_size, is_signed, is_interprocess > core_arch_operations;
0087         return core_arch_operations::load(storage, order);
0088 #else
0089         return __atomic_load_n(&storage, atomics::detail::convert_memory_order_to_gcc(order));
0090 #endif
0091     }
0092 
0093     static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0094     {
0095         return __atomic_fetch_add(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
0096     }
0097 
0098     static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0099     {
0100         return __atomic_fetch_sub(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
0101     }
0102 
0103     static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0104     {
0105         return __atomic_exchange_n(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
0106     }
0107 
0108     static BOOST_FORCEINLINE bool compare_exchange_strong(
0109         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
0110     {
0111         return __atomic_compare_exchange_n
0112         (
0113             &storage, &expected, desired, false,
0114             atomics::detail::convert_memory_order_to_gcc(success_order),
0115             atomics::detail::convert_memory_order_to_gcc(failure_order)
0116         );
0117     }
0118 
0119     static BOOST_FORCEINLINE bool compare_exchange_weak(
0120         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
0121     {
0122         return __atomic_compare_exchange_n
0123         (
0124             &storage, &expected, desired, true,
0125             atomics::detail::convert_memory_order_to_gcc(success_order),
0126             atomics::detail::convert_memory_order_to_gcc(failure_order)
0127         );
0128     }
0129 
0130     static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0131     {
0132         return __atomic_fetch_and(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
0133     }
0134 
0135     static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0136     {
0137         return __atomic_fetch_or(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
0138     }
0139 
0140     static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
0141     {
0142         return __atomic_fetch_xor(&storage, v, atomics::detail::convert_memory_order_to_gcc(order));
0143     }
0144 
0145     static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0146     {
0147         return __atomic_test_and_set(&storage, atomics::detail::convert_memory_order_to_gcc(order));
0148     }
0149 
0150     static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
0151     {
0152         __atomic_clear(const_cast< storage_type* >(&storage), atomics::detail::convert_memory_order_to_gcc(order));
0153     }
0154 };
0155 
0156 // We want to only enable __atomic* intrinsics when the corresponding BOOST_ATOMIC_DETAIL_GCC_ATOMIC_*_LOCK_FREE macro indicates
0157 // the same or better lock-free guarantees as the BOOST_ATOMIC_*_LOCK_FREE macro. Otherwise, we want to leave core_operations
0158 // unspecialized, so that core_arch_operations is used instead.
0159 
0160 #if BOOST_ATOMIC_INT128_LOCK_FREE > 0 && BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE >= BOOST_ATOMIC_INT128_LOCK_FREE
0161 
0162 template< bool Signed, bool Interprocess >
0163 struct core_operations< 16u, Signed, Interprocess > :
0164     public core_operations_gcc_atomic< 16u, Signed, Interprocess >
0165 {
0166 };
0167 
0168 #endif
0169 
0170 #if BOOST_ATOMIC_INT64_LOCK_FREE > 0
0171 #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE >= BOOST_ATOMIC_INT64_LOCK_FREE
0172 
0173 template< bool Signed, bool Interprocess >
0174 struct core_operations< 8u, Signed, Interprocess > :
0175     public core_operations_gcc_atomic< 8u, Signed, Interprocess >
0176 {
0177 };
0178 
0179 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE >= BOOST_ATOMIC_INT64_LOCK_FREE
0180 
0181 template< bool Signed, bool Interprocess >
0182 struct core_operations< 8u, Signed, Interprocess > :
0183     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 16u, Signed, Interprocess >, 8u, Signed >
0184 {
0185 };
0186 
0187 #endif
0188 #endif // BOOST_ATOMIC_INT64_LOCK_FREE > 0
0189 
0190 
0191 #if BOOST_ATOMIC_INT32_LOCK_FREE > 0
0192 #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE >= BOOST_ATOMIC_INT32_LOCK_FREE
0193 
0194 template< bool Signed, bool Interprocess >
0195 struct core_operations< 4u, Signed, Interprocess > :
0196     public core_operations_gcc_atomic< 4u, Signed, Interprocess >
0197 {
0198 };
0199 
0200 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE >= BOOST_ATOMIC_INT32_LOCK_FREE
0201 
0202 template< bool Signed, bool Interprocess >
0203 struct core_operations< 4u, Signed, Interprocess > :
0204     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 8u, Signed, Interprocess >, 4u, Signed >
0205 {
0206 };
0207 
0208 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE >= BOOST_ATOMIC_INT32_LOCK_FREE
0209 
0210 template< bool Signed, bool Interprocess >
0211 struct core_operations< 8u, Signed, Interprocess > :
0212     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 16u, Signed, Interprocess >, 4u, Signed >
0213 {
0214 };
0215 
0216 #endif
0217 #endif // BOOST_ATOMIC_INT32_LOCK_FREE > 0
0218 
0219 
0220 #if BOOST_ATOMIC_INT16_LOCK_FREE > 0
0221 #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE >= BOOST_ATOMIC_INT16_LOCK_FREE
0222 
0223 template< bool Signed, bool Interprocess >
0224 struct core_operations< 2u, Signed, Interprocess > :
0225     public core_operations_gcc_atomic< 2u, Signed, Interprocess >
0226 {
0227 };
0228 
0229 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE >= BOOST_ATOMIC_INT16_LOCK_FREE
0230 
0231 template< bool Signed, bool Interprocess >
0232 struct core_operations< 2u, Signed, Interprocess > :
0233     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 4u, Signed, Interprocess >, 2u, Signed >
0234 {
0235 };
0236 
0237 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE >= BOOST_ATOMIC_INT16_LOCK_FREE
0238 
0239 template< bool Signed, bool Interprocess >
0240 struct core_operations< 2u, Signed, Interprocess > :
0241     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 8u, Signed, Interprocess >, 2u, Signed >
0242 {
0243 };
0244 
0245 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE >= BOOST_ATOMIC_INT16_LOCK_FREE
0246 
0247 template< bool Signed, bool Interprocess >
0248 struct core_operations< 2u, Signed, Interprocess > :
0249     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 16u, Signed, Interprocess >, 2u, Signed >
0250 {
0251 };
0252 
0253 #endif
0254 #endif // BOOST_ATOMIC_INT16_LOCK_FREE > 0
0255 
0256 
0257 #if BOOST_ATOMIC_INT8_LOCK_FREE > 0
0258 #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT8_LOCK_FREE >= BOOST_ATOMIC_INT8_LOCK_FREE
0259 
0260 template< bool Signed, bool Interprocess >
0261 struct core_operations< 1u, Signed, Interprocess > :
0262     public core_operations_gcc_atomic< 1u, Signed, Interprocess >
0263 {
0264 };
0265 
0266 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE >= BOOST_ATOMIC_INT8_LOCK_FREE
0267 
0268 template< bool Signed, bool Interprocess >
0269 struct core_operations< 1u, Signed, Interprocess > :
0270     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 2u, Signed, Interprocess >, 1u, Signed >
0271 {
0272 };
0273 
0274 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE >= BOOST_ATOMIC_INT8_LOCK_FREE
0275 
0276 template< bool Signed, bool Interprocess >
0277 struct core_operations< 1u, Signed, Interprocess > :
0278     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 4u, Signed, Interprocess >, 1u, Signed >
0279 {
0280 };
0281 
0282 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE >= BOOST_ATOMIC_INT8_LOCK_FREE
0283 
0284 template< bool Signed, bool Interprocess >
0285 struct core_operations< 1u, Signed, Interprocess > :
0286     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 8u, Signed, Interprocess >, 1u, Signed >
0287 {
0288 };
0289 
0290 #elif BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE >= BOOST_ATOMIC_INT8_LOCK_FREE
0291 
0292 template< bool Signed, bool Interprocess >
0293 struct core_operations< 1u, Signed, Interprocess > :
0294     public extending_cas_based_arithmetic< core_operations_gcc_atomic< 16u, Signed, Interprocess >, 1u, Signed >
0295 {
0296 };
0297 
0298 #endif
0299 #endif // BOOST_ATOMIC_INT8_LOCK_FREE > 0
0300 
0301 } // namespace detail
0302 } // namespace atomics
0303 } // namespace boost
0304 
0305 #include <boost/atomic/detail/footer.hpp>
0306 
0307 #endif // BOOST_ATOMIC_DETAIL_CORE_OPS_GCC_ATOMIC_HPP_INCLUDED_