atomic_sync.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ATOMIC_ATOMIC_SYNC_H
  9. #define _LIBCPP___ATOMIC_ATOMIC_SYNC_H
  10. #include <__atomic/contention_t.h>
  11. #include <__atomic/cxx_atomic_impl.h>
  12. #include <__atomic/memory_order.h>
  13. #include <__availability>
  14. #include <__chrono/duration.h>
  15. #include <__config>
  16. #include <__memory/addressof.h>
  17. #include <__thread/poll_with_backoff.h>
  18. #include <__thread/support.h>
  19. #include <__type_traits/conjunction.h>
  20. #include <__type_traits/decay.h>
  21. #include <__type_traits/invoke.h>
  22. #include <__type_traits/void_t.h>
  23. #include <__utility/declval.h>
  24. #include <cstring>
  25. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  26. # pragma GCC system_header
  27. #endif
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. // The customisation points to enable the following functions:
  30. // - __atomic_wait
  31. // - __atomic_wait_unless
  32. // - __atomic_notify_one
  33. // - __atomic_notify_all
  34. // Note that std::atomic<T>::wait was back-ported to C++03
  35. // The below implementations look ugly to support C++03
  36. template <class _Tp, class = void>
  37. struct __atomic_waitable_traits {
  38. template <class _AtomicWaitable>
  39. static void __atomic_load(_AtomicWaitable&&, memory_order) = delete;
  40. template <class _AtomicWaitable>
  41. static void __atomic_contention_address(_AtomicWaitable&&) = delete;
  42. };
  43. template <class _Tp, class = void>
  44. struct __atomic_waitable : false_type {};
  45. template <class _Tp>
  46. struct __atomic_waitable< _Tp,
  47. __void_t<decltype(__atomic_waitable_traits<__decay_t<_Tp> >::__atomic_load(
  48. std::declval<const _Tp&>(), std::declval<memory_order>())),
  49. decltype(__atomic_waitable_traits<__decay_t<_Tp> >::__atomic_contention_address(
  50. std::declval<const _Tp&>()))> > : true_type {};
  51. template <class _AtomicWaitable, class _Poll>
  52. struct __atomic_wait_poll_impl {
  53. const _AtomicWaitable& __a_;
  54. _Poll __poll_;
  55. memory_order __order_;
  56. _LIBCPP_HIDE_FROM_ABI bool operator()() const {
  57. auto __current_val = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_load(__a_, __order_);
  58. return __poll_(__current_val);
  59. }
  60. };
  61. #ifndef _LIBCPP_HAS_NO_THREADS
  62. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile*);
  63. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile*);
  64. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(void const volatile*);
  65. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __libcpp_atomic_wait(void const volatile*, __cxx_contention_t);
  66. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void
  67. __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile*);
  68. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void
  69. __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile*);
  70. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t
  71. __libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile*);
  72. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void
  73. __libcpp_atomic_wait(__cxx_atomic_contention_t const volatile*, __cxx_contention_t);
  74. template <class _AtomicWaitable, class _Poll>
  75. struct __atomic_wait_backoff_impl {
  76. const _AtomicWaitable& __a_;
  77. _Poll __poll_;
  78. memory_order __order_;
  79. using __waitable_traits = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >;
  80. _LIBCPP_AVAILABILITY_SYNC
  81. _LIBCPP_HIDE_FROM_ABI bool
  82. __update_monitor_val_and_poll(__cxx_atomic_contention_t const volatile*, __cxx_contention_t& __monitor_val) const {
  83. // In case the contention type happens to be __cxx_atomic_contention_t, i.e. __cxx_atomic_impl<int64_t>,
  84. // the platform wait is directly monitoring the atomic value itself.
  85. // `__poll_` takes the current value of the atomic as an in-out argument
  86. // to potentially modify it. After it returns, `__monitor` has a value
  87. // which can be safely waited on by `std::__libcpp_atomic_wait` without any
  88. // ABA style issues.
  89. __monitor_val = __waitable_traits::__atomic_load(__a_, __order_);
  90. return __poll_(__monitor_val);
  91. }
  92. _LIBCPP_AVAILABILITY_SYNC
  93. _LIBCPP_HIDE_FROM_ABI bool
  94. __update_monitor_val_and_poll(void const volatile* __contention_address, __cxx_contention_t& __monitor_val) const {
  95. // In case the contention type is anything else, platform wait is monitoring a __cxx_atomic_contention_t
  96. // from the global pool, the monitor comes from __libcpp_atomic_monitor
  97. __monitor_val = std::__libcpp_atomic_monitor(__contention_address);
  98. auto __current_val = __waitable_traits::__atomic_load(__a_, __order_);
  99. return __poll_(__current_val);
  100. }
  101. _LIBCPP_AVAILABILITY_SYNC
  102. _LIBCPP_HIDE_FROM_ABI bool operator()(chrono::nanoseconds __elapsed) const {
  103. if (__elapsed > chrono::microseconds(64)) {
  104. auto __contention_address = __waitable_traits::__atomic_contention_address(__a_);
  105. __cxx_contention_t __monitor_val;
  106. if (__update_monitor_val_and_poll(__contention_address, __monitor_val))
  107. return true;
  108. std::__libcpp_atomic_wait(__contention_address, __monitor_val);
  109. } else if (__elapsed > chrono::microseconds(4))
  110. __libcpp_thread_yield();
  111. else {
  112. } // poll
  113. return false;
  114. }
  115. };
  116. // The semantics of this function are similar to `atomic`'s
  117. // `.wait(T old, std::memory_order order)`, but instead of having a hardcoded
  118. // predicate (is the loaded value unequal to `old`?), the predicate function is
  119. // specified as an argument. The loaded value is given as an in-out argument to
  120. // the predicate. If the predicate function returns `true`,
  121. // `__atomic_wait_unless` will return. If the predicate function returns
  122. // `false`, it must set the argument to its current understanding of the atomic
  123. // value. The predicate function must not return `false` spuriously.
  124. template <class _AtomicWaitable, class _Poll>
  125. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
  126. __atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __order) {
  127. static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
  128. __atomic_wait_poll_impl<_AtomicWaitable, __decay_t<_Poll> > __poll_impl = {__a, __poll, __order};
  129. __atomic_wait_backoff_impl<_AtomicWaitable, __decay_t<_Poll> > __backoff_fn = {__a, __poll, __order};
  130. std::__libcpp_thread_poll_with_backoff(__poll_impl, __backoff_fn);
  131. }
  132. template <class _AtomicWaitable>
  133. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable& __a) {
  134. static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
  135. std::__cxx_atomic_notify_one(__atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_contention_address(__a));
  136. }
  137. template <class _AtomicWaitable>
  138. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable& __a) {
  139. static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
  140. std::__cxx_atomic_notify_all(__atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_contention_address(__a));
  141. }
  142. #else // _LIBCPP_HAS_NO_THREADS
  143. template <class _AtomicWaitable, class _Poll>
  144. _LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __order) {
  145. __atomic_wait_poll_impl<_AtomicWaitable, __decay_t<_Poll> > __poll_fn = {__a, __poll, __order};
  146. std::__libcpp_thread_poll_with_backoff(__poll_fn, __spinning_backoff_policy());
  147. }
  148. template <class _AtomicWaitable>
  149. _LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable&) {}
  150. template <class _AtomicWaitable>
  151. _LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable&) {}
  152. #endif // _LIBCPP_HAS_NO_THREADS
  153. template <typename _Tp>
  154. _LIBCPP_HIDE_FROM_ABI bool __cxx_nonatomic_compare_equal(_Tp const& __lhs, _Tp const& __rhs) {
  155. return std::memcmp(std::addressof(__lhs), std::addressof(__rhs), sizeof(_Tp)) == 0;
  156. }
  157. template <class _Tp>
  158. struct __atomic_compare_unequal_to {
  159. _Tp __val_;
  160. _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __arg) const {
  161. return !std::__cxx_nonatomic_compare_equal(__arg, __val_);
  162. }
  163. };
  164. template <class _AtomicWaitable, class _Up>
  165. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void
  166. __atomic_wait(_AtomicWaitable& __a, _Up __val, memory_order __order) {
  167. static_assert(__atomic_waitable<_AtomicWaitable>::value, "");
  168. __atomic_compare_unequal_to<_Up> __nonatomic_equal = {__val};
  169. std::__atomic_wait_unless(__a, __nonatomic_equal, __order);
  170. }
  171. _LIBCPP_END_NAMESPACE_STD
  172. #endif // _LIBCPP___ATOMIC_ATOMIC_SYNC_H