ops_gcc_sync.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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) 2011 Helge Bahmann
  7. * Copyright (c) 2013 Tim Blechmann
  8. * Copyright (c) 2014 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/ops_gcc_sync.hpp
  12. *
  13. * This header contains implementation of the \c operations template.
  14. */
  15. #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_
  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/operations_fwd.hpp>
  22. #include <boost/atomic/detail/ops_extending_cas_based.hpp>
  23. #include <boost/atomic/capabilities.hpp>
  24. #ifdef BOOST_HAS_PRAGMA_ONCE
  25. #pragma once
  26. #endif
  27. namespace boost {
  28. namespace atomics {
  29. namespace detail {
  30. struct gcc_sync_operations_base
  31. {
  32. static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
  33. static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
  34. static BOOST_FORCEINLINE void fence_before_store(memory_order order) BOOST_NOEXCEPT
  35. {
  36. if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
  37. __sync_synchronize();
  38. }
  39. static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT
  40. {
  41. if (order == memory_order_seq_cst)
  42. __sync_synchronize();
  43. }
  44. static BOOST_FORCEINLINE void fence_after_load(memory_order order) BOOST_NOEXCEPT
  45. {
  46. if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_acquire) | static_cast< unsigned int >(memory_order_consume))) != 0u)
  47. __sync_synchronize();
  48. }
  49. };
  50. template< std::size_t Size, bool Signed >
  51. struct gcc_sync_operations :
  52. public gcc_sync_operations_base
  53. {
  54. typedef typename make_storage_type< Size >::type storage_type;
  55. typedef typename make_storage_type< Size >::aligned aligned_storage_type;
  56. static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
  57. static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
  58. static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  59. {
  60. fence_before_store(order);
  61. storage = v;
  62. fence_after_store(order);
  63. }
  64. static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
  65. {
  66. storage_type v = storage;
  67. fence_after_load(order);
  68. return v;
  69. }
  70. static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  71. {
  72. return __sync_fetch_and_add(&storage, v);
  73. }
  74. static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  75. {
  76. return __sync_fetch_and_sub(&storage, v);
  77. }
  78. static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  79. {
  80. // GCC docs mention that not all architectures may support full exchange semantics for this intrinsic. However, GCC's implementation of
  81. // std::atomic<> uses this intrinsic unconditionally. We do so as well. In case if some architectures actually don't support this, we can always
  82. // add a check here and fall back to a CAS loop.
  83. if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
  84. __sync_synchronize();
  85. return __sync_lock_test_and_set(&storage, v);
  86. }
  87. static BOOST_FORCEINLINE bool compare_exchange_strong(
  88. storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
  89. {
  90. storage_type expected2 = expected;
  91. storage_type old_val = __sync_val_compare_and_swap(&storage, expected2, desired);
  92. if (old_val == expected2)
  93. {
  94. return true;
  95. }
  96. else
  97. {
  98. expected = old_val;
  99. return false;
  100. }
  101. }
  102. static BOOST_FORCEINLINE bool compare_exchange_weak(
  103. storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  104. {
  105. return compare_exchange_strong(storage, expected, desired, success_order, failure_order);
  106. }
  107. static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  108. {
  109. return __sync_fetch_and_and(&storage, v);
  110. }
  111. static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  112. {
  113. return __sync_fetch_and_or(&storage, v);
  114. }
  115. static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  116. {
  117. return __sync_fetch_and_xor(&storage, v);
  118. }
  119. static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  120. {
  121. if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
  122. __sync_synchronize();
  123. return !!__sync_lock_test_and_set(&storage, 1);
  124. }
  125. static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  126. {
  127. __sync_lock_release(&storage);
  128. if (order == memory_order_seq_cst)
  129. __sync_synchronize();
  130. }
  131. };
  132. #if BOOST_ATOMIC_INT8_LOCK_FREE > 0
  133. template< bool Signed >
  134. struct operations< 1u, Signed > :
  135. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1)
  136. public gcc_sync_operations< 1u, Signed >
  137. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
  138. public extending_cas_based_operations< gcc_sync_operations< 2u, Signed >, 1u, Signed >
  139. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  140. public extending_cas_based_operations< gcc_sync_operations< 4u, Signed >, 1u, Signed >
  141. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  142. public extending_cas_based_operations< gcc_sync_operations< 8u, Signed >, 1u, Signed >
  143. #else
  144. public extending_cas_based_operations< gcc_sync_operations< 16u, Signed >, 1u, Signed >
  145. #endif
  146. {
  147. };
  148. #endif
  149. #if BOOST_ATOMIC_INT16_LOCK_FREE > 0
  150. template< bool Signed >
  151. struct operations< 2u, Signed > :
  152. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
  153. public gcc_sync_operations< 2u, Signed >
  154. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  155. public extending_cas_based_operations< gcc_sync_operations< 4u, Signed >, 2u, Signed >
  156. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  157. public extending_cas_based_operations< gcc_sync_operations< 8u, Signed >, 2u, Signed >
  158. #else
  159. public extending_cas_based_operations< gcc_sync_operations< 16u, Signed >, 2u, Signed >
  160. #endif
  161. {
  162. };
  163. #endif
  164. #if BOOST_ATOMIC_INT32_LOCK_FREE > 0
  165. template< bool Signed >
  166. struct operations< 4u, Signed > :
  167. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  168. public gcc_sync_operations< 4u, Signed >
  169. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  170. public extending_cas_based_operations< gcc_sync_operations< 8u, Signed >, 4u, Signed >
  171. #else
  172. public extending_cas_based_operations< gcc_sync_operations< 16u, Signed >, 4u, Signed >
  173. #endif
  174. {
  175. };
  176. #endif
  177. #if BOOST_ATOMIC_INT64_LOCK_FREE > 0
  178. template< bool Signed >
  179. struct operations< 8u, Signed > :
  180. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  181. public gcc_sync_operations< 8u, Signed >
  182. #else
  183. public extending_cas_based_operations< gcc_sync_operations< 16u, Signed >, 8u, Signed >
  184. #endif
  185. {
  186. };
  187. #endif
  188. #if BOOST_ATOMIC_INT128_LOCK_FREE > 0
  189. template< bool Signed >
  190. struct operations< 16u, Signed > :
  191. public gcc_sync_operations< 16u, Signed >
  192. {
  193. };
  194. #endif
  195. BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
  196. {
  197. if (order != memory_order_relaxed)
  198. __sync_synchronize();
  199. }
  200. BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
  201. {
  202. if (order != memory_order_relaxed)
  203. __asm__ __volatile__ ("" ::: "memory");
  204. }
  205. } // namespace detail
  206. } // namespace atomics
  207. } // namespace boost
  208. #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_