atomic_sync.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <__threading_support>
  19. #include <__type_traits/decay.h>
  20. #include <cstring>
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. #ifndef _LIBCPP_HAS_NO_THREADS
  26. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile*);
  27. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile*);
  28. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(void const volatile*);
  29. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __libcpp_atomic_wait(void const volatile*, __cxx_contention_t);
  30. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile*);
  31. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile*);
  32. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile*);
  33. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __libcpp_atomic_wait(__cxx_atomic_contention_t const volatile*, __cxx_contention_t);
  34. template <class _Atp, class _Fn>
  35. struct __libcpp_atomic_wait_backoff_impl {
  36. _Atp* __a;
  37. _Fn __test_fn;
  38. _LIBCPP_AVAILABILITY_SYNC
  39. _LIBCPP_HIDE_FROM_ABI bool operator()(chrono::nanoseconds __elapsed) const
  40. {
  41. if(__elapsed > chrono::microseconds(64))
  42. {
  43. auto const __monitor = std::__libcpp_atomic_monitor(__a);
  44. if(__test_fn())
  45. return true;
  46. std::__libcpp_atomic_wait(__a, __monitor);
  47. }
  48. else if(__elapsed > chrono::microseconds(4))
  49. __libcpp_thread_yield();
  50. else
  51. {} // poll
  52. return false;
  53. }
  54. };
  55. template <class _Atp, class _Fn>
  56. _LIBCPP_AVAILABILITY_SYNC
  57. _LIBCPP_HIDE_FROM_ABI bool __cxx_atomic_wait(_Atp* __a, _Fn && __test_fn)
  58. {
  59. __libcpp_atomic_wait_backoff_impl<_Atp, __decay_t<_Fn> > __backoff_fn = {__a, __test_fn};
  60. return std::__libcpp_thread_poll_with_backoff(__test_fn, __backoff_fn);
  61. }
  62. #else // _LIBCPP_HAS_NO_THREADS
  63. template <class _Tp>
  64. _LIBCPP_HIDE_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_impl<_Tp> const volatile*) { }
  65. template <class _Tp>
  66. _LIBCPP_HIDE_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_impl<_Tp> const volatile*) { }
  67. template <class _Atp, class _Fn>
  68. _LIBCPP_HIDE_FROM_ABI bool __cxx_atomic_wait(_Atp*, _Fn && __test_fn)
  69. {
  70. return std::__libcpp_thread_poll_with_backoff(__test_fn, __spinning_backoff_policy());
  71. }
  72. #endif // _LIBCPP_HAS_NO_THREADS
  73. template <typename _Tp> _LIBCPP_HIDE_FROM_ABI
  74. bool __cxx_nonatomic_compare_equal(_Tp const& __lhs, _Tp const& __rhs) {
  75. return std::memcmp(std::addressof(__lhs), std::addressof(__rhs), sizeof(_Tp)) == 0;
  76. }
  77. template <class _Atp, class _Tp>
  78. struct __cxx_atomic_wait_test_fn_impl {
  79. _Atp* __a;
  80. _Tp __val;
  81. memory_order __order;
  82. _LIBCPP_HIDE_FROM_ABI bool operator()() const
  83. {
  84. return !std::__cxx_nonatomic_compare_equal(std::__cxx_atomic_load(__a, __order), __val);
  85. }
  86. };
  87. template <class _Atp, class _Tp>
  88. _LIBCPP_AVAILABILITY_SYNC
  89. _LIBCPP_HIDE_FROM_ABI bool __cxx_atomic_wait(_Atp* __a, _Tp const __val, memory_order __order)
  90. {
  91. __cxx_atomic_wait_test_fn_impl<_Atp, _Tp> __test_fn = {__a, __val, __order};
  92. return std::__cxx_atomic_wait(__a, __test_fn);
  93. }
  94. _LIBCPP_END_NAMESPACE_STD
  95. #endif // _LIBCPP___ATOMIC_ATOMIC_SYNC_H