ops_linux_arm.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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) 2009, 2011 Helge Bahmann
  7. * Copyright (c) 2009 Phil Endecott
  8. * Copyright (c) 2013 Tim Blechmann
  9. * Linux-specific code by Phil Endecott
  10. * Copyright (c) 2014 Andrey Semashev
  11. */
  12. /*!
  13. * \file atomic/detail/ops_linux_arm.hpp
  14. *
  15. * This header contains implementation of the \c operations template.
  16. */
  17. #ifndef BOOST_ATOMIC_DETAIL_OPS_LINUX_ARM_HPP_INCLUDED_
  18. #define BOOST_ATOMIC_DETAIL_OPS_LINUX_ARM_HPP_INCLUDED_
  19. #include <cstddef>
  20. #include <boost/memory_order.hpp>
  21. #include <boost/atomic/detail/config.hpp>
  22. #include <boost/atomic/detail/storage_type.hpp>
  23. #include <boost/atomic/detail/operations_fwd.hpp>
  24. #include <boost/atomic/capabilities.hpp>
  25. #include <boost/atomic/detail/ops_cas_based.hpp>
  26. #include <boost/atomic/detail/ops_extending_cas_based.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. namespace boost {
  31. namespace atomics {
  32. namespace detail {
  33. // Different ARM processors have different atomic instructions. In particular,
  34. // architecture versions before v6 (which are still in widespread use, e.g. the
  35. // Intel/Marvell XScale chips like the one in the NSLU2) have only atomic swap.
  36. // On Linux the kernel provides some support that lets us abstract away from
  37. // these differences: it provides emulated CAS and barrier functions at special
  38. // addresses that are guaranteed not to be interrupted by the kernel. Using
  39. // this facility is slightly slower than inline assembler would be, but much
  40. // faster than a system call.
  41. //
  42. // While this emulated CAS is "strong" in the sense that it does not fail
  43. // "spuriously" (i.e.: it never fails to perform the exchange when the value
  44. // found equals the value expected), it does not return the found value on
  45. // failure. To satisfy the atomic API, compare_exchange_{weak|strong} must
  46. // return the found value on failure, and we have to manually load this value
  47. // after the emulated CAS reports failure. This in turn introduces a race
  48. // between the CAS failing (due to the "wrong" value being found) and subsequently
  49. // loading (which might turn up the "right" value). From an application's
  50. // point of view this looks like "spurious failure", and therefore the
  51. // emulated CAS is only good enough to provide compare_exchange_weak
  52. // semantics.
  53. struct linux_arm_cas_base
  54. {
  55. static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = true;
  56. static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
  57. static BOOST_FORCEINLINE void fence_before_store(memory_order order) BOOST_NOEXCEPT
  58. {
  59. if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
  60. hardware_full_fence();
  61. }
  62. static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT
  63. {
  64. if (order == memory_order_seq_cst)
  65. hardware_full_fence();
  66. }
  67. static BOOST_FORCEINLINE void fence_after_load(memory_order order) BOOST_NOEXCEPT
  68. {
  69. if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_consume) | static_cast< unsigned int >(memory_order_acquire))) != 0u)
  70. hardware_full_fence();
  71. }
  72. static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT
  73. {
  74. typedef void (*kernel_dmb_t)(void);
  75. ((kernel_dmb_t)0xffff0fa0)();
  76. }
  77. };
  78. template< bool Signed >
  79. struct linux_arm_cas :
  80. public linux_arm_cas_base
  81. {
  82. typedef typename make_storage_type< 4u >::type storage_type;
  83. typedef typename make_storage_type< 4u >::aligned aligned_storage_type;
  84. static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 4u;
  85. static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
  86. static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  87. {
  88. fence_before_store(order);
  89. storage = v;
  90. fence_after_store(order);
  91. }
  92. static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
  93. {
  94. storage_type v = storage;
  95. fence_after_load(order);
  96. return v;
  97. }
  98. static BOOST_FORCEINLINE bool compare_exchange_strong(
  99. storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  100. {
  101. while (true)
  102. {
  103. storage_type tmp = expected;
  104. if (compare_exchange_weak(storage, tmp, desired, success_order, failure_order))
  105. return true;
  106. if (tmp != expected)
  107. {
  108. expected = tmp;
  109. return false;
  110. }
  111. }
  112. }
  113. static BOOST_FORCEINLINE bool compare_exchange_weak(
  114. storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
  115. {
  116. typedef storage_type (*kernel_cmpxchg32_t)(storage_type oldval, storage_type newval, volatile storage_type* ptr);
  117. if (((kernel_cmpxchg32_t)0xffff0fc0)(expected, desired, &storage) == 0)
  118. {
  119. return true;
  120. }
  121. else
  122. {
  123. expected = storage;
  124. return false;
  125. }
  126. }
  127. };
  128. template< bool Signed >
  129. struct operations< 1u, Signed > :
  130. public extending_cas_based_operations< cas_based_operations< cas_based_exchange< linux_arm_cas< Signed > > >, 1u, Signed >
  131. {
  132. };
  133. template< bool Signed >
  134. struct operations< 2u, Signed > :
  135. public extending_cas_based_operations< cas_based_operations< cas_based_exchange< linux_arm_cas< Signed > > >, 2u, Signed >
  136. {
  137. };
  138. template< bool Signed >
  139. struct operations< 4u, Signed > :
  140. public cas_based_operations< cas_based_exchange< linux_arm_cas< Signed > > >
  141. {
  142. };
  143. BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
  144. {
  145. if (order != memory_order_relaxed)
  146. linux_arm_cas_base::hardware_full_fence();
  147. }
  148. BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
  149. {
  150. if (order != memory_order_relaxed)
  151. __asm__ __volatile__ ("" ::: "memory");
  152. }
  153. } // namespace detail
  154. } // namespace atomics
  155. } // namespace boost
  156. #endif // BOOST_ATOMIC_DETAIL_OPS_LINUX_ARM_HPP_INCLUDED_