wait_ops_dragonfly_umtx.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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) 2020-2025 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/wait_ops_dragonfly_umtx.hpp
  10. *
  11. * This header contains implementation of the waiting/notifying atomic operations based on DragonFly BSD umtx.
  12. * https://man.dragonflybsd.org/?command=umtx&section=2
  13. */
  14. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_DRAGONFLY_UMTX_HPP_INCLUDED_
  15. #define BOOST_ATOMIC_DETAIL_WAIT_OPS_DRAGONFLY_UMTX_HPP_INCLUDED_
  16. #include <unistd.h>
  17. #include <cstdint>
  18. #include <limits>
  19. #include <chrono>
  20. #include <boost/memory_order.hpp>
  21. #include <boost/atomic/detail/config.hpp>
  22. #include <boost/atomic/detail/chrono.hpp>
  23. #include <boost/atomic/detail/wait_operations_fwd.hpp>
  24. #include <boost/atomic/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. namespace atomics {
  30. namespace detail {
  31. template< typename Base, bool Interprocess >
  32. struct wait_operations< Base, sizeof(int), true, Interprocess > :
  33. public Base
  34. {
  35. using base_type = Base;
  36. using storage_type = typename base_type::storage_type;
  37. public:
  38. static constexpr bool always_has_native_wait_notify = true;
  39. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) noexcept
  40. {
  41. return true;
  42. }
  43. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) noexcept
  44. {
  45. storage_type new_val = base_type::load(storage, order);
  46. while (new_val == old_val)
  47. {
  48. umtx_sleep(reinterpret_cast< int* >(const_cast< storage_type* >(&storage)), static_cast< int >(old_val), 0);
  49. new_val = base_type::load(storage, order);
  50. }
  51. return new_val;
  52. }
  53. private:
  54. template< typename Clock >
  55. static BOOST_FORCEINLINE storage_type wait_until_impl
  56. (
  57. storage_type const volatile& storage,
  58. storage_type old_val,
  59. typename Clock::time_point timeout,
  60. typename Clock::time_point now,
  61. memory_order order,
  62. bool& timed_out
  63. ) noexcept(noexcept(Clock::now()))
  64. {
  65. storage_type new_val = base_type::load(storage, order);
  66. while (new_val == old_val)
  67. {
  68. const std::int64_t usec = atomics::detail::chrono::ceil< std::chrono::microseconds >(timeout - now).count();
  69. if (usec <= 0)
  70. {
  71. timed_out = true;
  72. break;
  73. }
  74. umtx_sleep
  75. (
  76. reinterpret_cast< int* >(const_cast< storage_type* >(&storage)),
  77. static_cast< int >(old_val),
  78. usec <= (std::numeric_limits< int >::max)() ? static_cast< int >(usec) : (std::numeric_limits< int >::max)()
  79. );
  80. now = Clock::now();
  81. new_val = base_type::load(storage, order);
  82. }
  83. return new_val;
  84. }
  85. public:
  86. template< typename Clock, typename Duration >
  87. static BOOST_FORCEINLINE storage_type wait_until
  88. (
  89. storage_type const volatile& storage,
  90. storage_type old_val,
  91. std::chrono::time_point< Clock, Duration > timeout,
  92. memory_order order,
  93. bool& timed_out
  94. ) noexcept(noexcept(Clock::now()))
  95. {
  96. return wait_until_impl< Clock >(storage, old_val, timeout, Clock::now(), order, timed_out);
  97. }
  98. template< typename Rep, typename Period >
  99. static BOOST_FORCEINLINE storage_type wait_for
  100. (
  101. storage_type const volatile& storage,
  102. storage_type old_val,
  103. std::chrono::duration< Rep, Period > timeout,
  104. memory_order order,
  105. bool& timed_out
  106. ) noexcept
  107. {
  108. const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
  109. return wait_until_impl< std::chrono::steady_clock >(storage, old_val, now + timeout, now, order, timed_out);
  110. }
  111. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) noexcept
  112. {
  113. umtx_wakeup(reinterpret_cast< int* >(const_cast< storage_type* >(&storage)), 1);
  114. }
  115. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) noexcept
  116. {
  117. umtx_wakeup(reinterpret_cast< int* >(const_cast< storage_type* >(&storage)), 0);
  118. }
  119. };
  120. } // namespace detail
  121. } // namespace atomics
  122. } // namespace boost
  123. #include <boost/atomic/detail/footer.hpp>
  124. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_DRAGONFLY_UMTX_HPP_INCLUDED_