linux-arm.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef BOOST_ATOMIC_DETAIL_LINUX_ARM_HPP
  2. #define BOOST_ATOMIC_DETAIL_LINUX_ARM_HPP
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Copyright (c) 2009, 2011 Helge Bahmann
  8. // Copyright (c) 2009 Phil Endecott
  9. // Copyright (c) 2013 Tim Blechmann
  10. // Linux-specific code by Phil Endecott
  11. // Different ARM processors have different atomic instructions. In particular,
  12. // architecture versions before v6 (which are still in widespread use, e.g. the
  13. // Intel/Marvell XScale chips like the one in the NSLU2) have only atomic swap.
  14. // On Linux the kernel provides some support that lets us abstract away from
  15. // these differences: it provides emulated CAS and barrier functions at special
  16. // addresses that are guaranteed not to be interrupted by the kernel. Using
  17. // this facility is slightly slower than inline assembler would be, but much
  18. // faster than a system call.
  19. //
  20. // While this emulated CAS is "strong" in the sense that it does not fail
  21. // "spuriously" (i.e.: it never fails to perform the exchange when the value
  22. // found equals the value expected), it does not return the found value on
  23. // failure. To satisfy the atomic API, compare_exchange_{weak|strong} must
  24. // return the found value on failure, and we have to manually load this value
  25. // after the emulated CAS reports failure. This in turn introduces a race
  26. // between the CAS failing (due to the "wrong" value being found) and subsequently
  27. // loading (which might turn up the "right" value). From an application's
  28. // point of view this looks like "spurious failure", and therefore the
  29. // emulated CAS is only good enough to provide compare_exchange_weak
  30. // semantics.
  31. #include <cstddef>
  32. #include <boost/cstdint.hpp>
  33. #include <boost/memory_order.hpp>
  34. #include <boost/atomic/detail/config.hpp>
  35. #ifdef BOOST_HAS_PRAGMA_ONCE
  36. #pragma once
  37. #endif
  38. namespace boost {
  39. namespace atomics {
  40. namespace detail {
  41. inline void
  42. arm_barrier(void)
  43. {
  44. void (*kernel_dmb)(void) = (void (*)(void)) 0xffff0fa0;
  45. kernel_dmb();
  46. }
  47. inline void
  48. platform_fence_before(memory_order order)
  49. {
  50. switch(order) {
  51. case memory_order_release:
  52. case memory_order_acq_rel:
  53. case memory_order_seq_cst:
  54. arm_barrier();
  55. case memory_order_consume:
  56. default:;
  57. }
  58. }
  59. inline void
  60. platform_fence_after(memory_order order)
  61. {
  62. switch(order) {
  63. case memory_order_acquire:
  64. case memory_order_acq_rel:
  65. case memory_order_seq_cst:
  66. arm_barrier();
  67. default:;
  68. }
  69. }
  70. inline void
  71. platform_fence_before_store(memory_order order)
  72. {
  73. platform_fence_before(order);
  74. }
  75. inline void
  76. platform_fence_after_store(memory_order order)
  77. {
  78. if (order == memory_order_seq_cst)
  79. arm_barrier();
  80. }
  81. inline void
  82. platform_fence_after_load(memory_order order)
  83. {
  84. platform_fence_after(order);
  85. }
  86. template<typename T>
  87. inline bool
  88. platform_cmpxchg32(T & expected, T desired, volatile T * ptr)
  89. {
  90. typedef T (*kernel_cmpxchg32_t)(T oldval, T newval, volatile T * ptr);
  91. if (((kernel_cmpxchg32_t) 0xffff0fc0)(expected, desired, ptr) == 0) {
  92. return true;
  93. } else {
  94. expected = *ptr;
  95. return false;
  96. }
  97. }
  98. }
  99. }
  100. #define BOOST_ATOMIC_THREAD_FENCE 2
  101. inline void
  102. atomic_thread_fence(memory_order order)
  103. {
  104. switch(order) {
  105. case memory_order_acquire:
  106. case memory_order_release:
  107. case memory_order_acq_rel:
  108. case memory_order_seq_cst:
  109. atomics::detail::arm_barrier();
  110. default:;
  111. }
  112. }
  113. #define BOOST_ATOMIC_SIGNAL_FENCE 2
  114. inline void
  115. atomic_signal_fence(memory_order)
  116. {
  117. __asm__ __volatile__ ("" ::: "memory");
  118. }
  119. class atomic_flag
  120. {
  121. private:
  122. atomic_flag(const atomic_flag &) /* = delete */ ;
  123. atomic_flag & operator=(const atomic_flag &) /* = delete */ ;
  124. uint32_t v_;
  125. public:
  126. BOOST_CONSTEXPR atomic_flag(void) BOOST_NOEXCEPT : v_(0) {}
  127. void
  128. clear(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  129. {
  130. atomics::detail::platform_fence_before_store(order);
  131. const_cast<volatile uint32_t &>(v_) = 0;
  132. atomics::detail::platform_fence_after_store(order);
  133. }
  134. bool
  135. test_and_set(memory_order order = memory_order_seq_cst) volatile BOOST_NOEXCEPT
  136. {
  137. atomics::detail::platform_fence_before(order);
  138. uint32_t expected = v_;
  139. do {
  140. if (expected == 1)
  141. break;
  142. } while (!atomics::detail::platform_cmpxchg32(expected, (uint32_t)1, &v_));
  143. atomics::detail::platform_fence_after(order);
  144. return expected;
  145. }
  146. };
  147. #define BOOST_ATOMIC_FLAG_LOCK_FREE 2
  148. }
  149. #include <boost/atomic/detail/base.hpp>
  150. #if !defined(BOOST_ATOMIC_FORCE_FALLBACK)
  151. #define BOOST_ATOMIC_CHAR_LOCK_FREE 2
  152. #define BOOST_ATOMIC_CHAR16_T_LOCK_FREE 2
  153. #define BOOST_ATOMIC_CHAR32_T_LOCK_FREE 2
  154. #define BOOST_ATOMIC_WCHAR_T_LOCK_FREE 2
  155. #define BOOST_ATOMIC_SHORT_LOCK_FREE 2
  156. #define BOOST_ATOMIC_INT_LOCK_FREE 2
  157. #define BOOST_ATOMIC_LONG_LOCK_FREE 2
  158. #define BOOST_ATOMIC_LLONG_LOCK_FREE 0
  159. #define BOOST_ATOMIC_POINTER_LOCK_FREE 2
  160. #define BOOST_ATOMIC_BOOL_LOCK_FREE 2
  161. #include <boost/atomic/detail/cas32weak.hpp>
  162. #endif /* !defined(BOOST_ATOMIC_FORCE_FALLBACK) */
  163. #endif