caps_gcc_atomic.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) 2014, 2020 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/caps_gcc_atomic.hpp
  10. *
  11. * This header defines feature capabilities macros
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_CAPS_GCC_ATOMIC_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_CAPS_GCC_ATOMIC_HPP_INCLUDED_
  15. #include <boost/atomic/detail/config.hpp>
  16. #include <boost/atomic/detail/int_sizes.hpp>
  17. #if defined(BOOST_ATOMIC_DETAIL_CORE_ARCH_BACKEND_HEADER)
  18. #include BOOST_ATOMIC_DETAIL_CORE_ARCH_BACKEND_HEADER(boost/atomic/detail/caps_arch_)
  19. #endif
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. // Translate type-based lock-free macros to size-based ones
  24. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT8_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
  25. #if BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 2
  26. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
  27. #elif BOOST_ATOMIC_DETAIL_SIZEOF_INT == 2
  28. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
  29. #elif BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 2
  30. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
  31. #elif BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 2
  32. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
  33. #else
  34. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE 0
  35. #endif
  36. #if BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 4
  37. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
  38. #elif BOOST_ATOMIC_DETAIL_SIZEOF_INT == 4
  39. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
  40. #elif BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 4
  41. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
  42. #elif BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 4
  43. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
  44. #else
  45. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE 0
  46. #endif
  47. #if BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 8
  48. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
  49. #elif BOOST_ATOMIC_DETAIL_SIZEOF_INT == 8
  50. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
  51. #elif BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 8
  52. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
  53. #elif BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 8
  54. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
  55. #else
  56. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE 0
  57. #endif
  58. #if BOOST_ATOMIC_DETAIL_SIZEOF_SHORT == 16
  59. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
  60. #elif BOOST_ATOMIC_DETAIL_SIZEOF_INT == 16
  61. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
  62. #elif BOOST_ATOMIC_DETAIL_SIZEOF_LONG == 16
  63. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
  64. #elif BOOST_ATOMIC_DETAIL_SIZEOF_LLONG == 16
  65. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
  66. #else
  67. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE 0
  68. #endif
  69. // On x86-64, clang 3.4 does not implement 128-bit __atomic* intrinsics even though it defines __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16:
  70. // https://bugs.llvm.org/show_bug.cgi?id=19149
  71. // Another problem exists with gcc 7 and later, as it requires to link with libatomic to use 16-byte intrinsics:
  72. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80878
  73. // Both clang and gcc do generate cmpxchg16b for __sync_val_compare_and_swap though.
  74. #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B) &&\
  75. (\
  76. (defined(BOOST_CLANG) && (__clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 5))) ||\
  77. (defined(BOOST_GCC) && BOOST_GCC >= 70000)\
  78. )
  79. #undef BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE
  80. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE 0
  81. #endif
  82. // On 32-bit x86, there is a clang bug for 64-bit atomics: https://bugs.llvm.org/show_bug.cgi?id=19355. The compiler defines
  83. // __GCC_ATOMIC_LLONG_LOCK_FREE to 1 when the target architecture supports 64-bit atomic instructions (i.e. the value should be 2).
  84. // Additionally, any clang version requires to link with libatomic for 64-bit __atomic* intrinsics on x86. It does generate
  85. // cmpxchg8b for __sync_val_compare_and_swap though.
  86. #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) && defined(BOOST_CLANG)
  87. #undef BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE
  88. #define BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE 0
  89. #endif
  90. // Override arch-specific macros if atomic intrinsics provide better guarantees
  91. #if !defined(BOOST_ATOMIC_INT128_LOCK_FREE) || (BOOST_ATOMIC_INT128_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE)
  92. #undef BOOST_ATOMIC_INT128_LOCK_FREE
  93. #define BOOST_ATOMIC_INT128_LOCK_FREE BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT128_LOCK_FREE
  94. #endif
  95. #if !defined(BOOST_ATOMIC_INT64_LOCK_FREE) || (BOOST_ATOMIC_INT64_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE) || (BOOST_ATOMIC_INT64_LOCK_FREE < BOOST_ATOMIC_INT128_LOCK_FREE)
  96. #undef BOOST_ATOMIC_INT64_LOCK_FREE
  97. #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE >= BOOST_ATOMIC_INT128_LOCK_FREE
  98. #define BOOST_ATOMIC_INT64_LOCK_FREE BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT64_LOCK_FREE
  99. #else
  100. #define BOOST_ATOMIC_INT64_LOCK_FREE BOOST_ATOMIC_INT128_LOCK_FREE
  101. #endif
  102. #endif
  103. #if !defined(BOOST_ATOMIC_INT32_LOCK_FREE) || (BOOST_ATOMIC_INT32_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE) || (BOOST_ATOMIC_INT32_LOCK_FREE < BOOST_ATOMIC_INT64_LOCK_FREE)
  104. #undef BOOST_ATOMIC_INT32_LOCK_FREE
  105. #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE >= BOOST_ATOMIC_INT64_LOCK_FREE
  106. #define BOOST_ATOMIC_INT32_LOCK_FREE BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT32_LOCK_FREE
  107. #else
  108. #define BOOST_ATOMIC_INT32_LOCK_FREE BOOST_ATOMIC_INT64_LOCK_FREE
  109. #endif
  110. #endif
  111. #if !defined(BOOST_ATOMIC_INT16_LOCK_FREE) || (BOOST_ATOMIC_INT16_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE) || (BOOST_ATOMIC_INT16_LOCK_FREE < BOOST_ATOMIC_INT32_LOCK_FREE)
  112. #undef BOOST_ATOMIC_INT16_LOCK_FREE
  113. #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE >= BOOST_ATOMIC_INT32_LOCK_FREE
  114. #define BOOST_ATOMIC_INT16_LOCK_FREE BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT16_LOCK_FREE
  115. #else
  116. #define BOOST_ATOMIC_INT16_LOCK_FREE BOOST_ATOMIC_INT32_LOCK_FREE
  117. #endif
  118. #endif
  119. #if !defined(BOOST_ATOMIC_INT8_LOCK_FREE) || (BOOST_ATOMIC_INT8_LOCK_FREE < BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT8_LOCK_FREE) || (BOOST_ATOMIC_INT8_LOCK_FREE < BOOST_ATOMIC_INT16_LOCK_FREE)
  120. #undef BOOST_ATOMIC_INT8_LOCK_FREE
  121. #if BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT8_LOCK_FREE >= BOOST_ATOMIC_INT16_LOCK_FREE
  122. #define BOOST_ATOMIC_INT8_LOCK_FREE BOOST_ATOMIC_DETAIL_GCC_ATOMIC_INT8_LOCK_FREE
  123. #else
  124. #define BOOST_ATOMIC_INT8_LOCK_FREE BOOST_ATOMIC_INT16_LOCK_FREE
  125. #endif
  126. #endif
  127. #if !defined(BOOST_ATOMIC_POINTER_LOCK_FREE) || (BOOST_ATOMIC_POINTER_LOCK_FREE < __GCC_ATOMIC_POINTER_LOCK_FREE)
  128. #undef BOOST_ATOMIC_POINTER_LOCK_FREE
  129. #define BOOST_ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
  130. #endif
  131. #if !defined(BOOST_ATOMIC_THREAD_FENCE) || (BOOST_ATOMIC_THREAD_FENCE < 2)
  132. #undef BOOST_ATOMIC_THREAD_FENCE
  133. #define BOOST_ATOMIC_THREAD_FENCE 2
  134. #endif
  135. #if !defined(BOOST_ATOMIC_SIGNAL_FENCE) || (BOOST_ATOMIC_SIGNAL_FENCE < 2)
  136. #undef BOOST_ATOMIC_SIGNAL_FENCE
  137. #define BOOST_ATOMIC_SIGNAL_FENCE 2
  138. #endif
  139. #endif // BOOST_ATOMIC_DETAIL_CAPS_GCC_ATOMIC_HPP_INCLUDED_