wait_ops_emulated.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_emulated.hpp
  10. *
  11. * This header contains emulated (lock-based) implementation of the waiting and notifying atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_EMULATED_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_WAIT_OPS_EMULATED_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <chrono>
  17. #include <boost/memory_order.hpp>
  18. #include <boost/atomic/detail/config.hpp>
  19. #if !defined(BOOST_WINDOWS)
  20. #include <time.h>
  21. #include <type_traits>
  22. #include <boost/atomic/posix_clock_traits_fwd.hpp>
  23. #include <boost/atomic/detail/has_posix_clock_traits.hpp>
  24. #endif
  25. #include <boost/atomic/detail/chrono.hpp>
  26. #include <boost/atomic/detail/lock_pool.hpp>
  27. #include <boost/atomic/detail/wait_operations_fwd.hpp>
  28. #include <boost/atomic/detail/header.hpp>
  29. #ifdef BOOST_HAS_PRAGMA_ONCE
  30. #pragma once
  31. #endif
  32. namespace boost {
  33. namespace atomics {
  34. namespace detail {
  35. //! Emulated implementation of waiting and notifying operations
  36. template< typename Base >
  37. struct wait_operations_emulated :
  38. public Base
  39. {
  40. using base_type = Base;
  41. using storage_type = typename base_type::storage_type;
  42. using scoped_lock = lock_pool::scoped_lock< base_type::storage_alignment, true >;
  43. using scoped_wait_state = lock_pool::scoped_wait_state< base_type::storage_alignment >;
  44. static constexpr bool always_has_native_wait_notify = false;
  45. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) noexcept
  46. {
  47. return false;
  48. }
  49. static storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order) noexcept
  50. {
  51. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  52. storage_type const& s = const_cast< storage_type const& >(storage);
  53. scoped_wait_state wait_state(&storage);
  54. storage_type new_val = s;
  55. while (new_val == old_val)
  56. {
  57. wait_state.wait();
  58. new_val = s;
  59. }
  60. return new_val;
  61. }
  62. private:
  63. template< typename Clock >
  64. static BOOST_FORCEINLINE storage_type wait_until_fallback
  65. (
  66. storage_type const volatile& storage,
  67. storage_type old_val,
  68. typename Clock::time_point timeout,
  69. typename Clock::time_point now,
  70. bool& timed_out
  71. ) noexcept(noexcept(Clock::now()))
  72. {
  73. storage_type const& s = const_cast< storage_type const& >(storage);
  74. scoped_wait_state wait_state(&storage);
  75. storage_type new_val = s;
  76. while (new_val == old_val)
  77. {
  78. const std::chrono::nanoseconds nsec = atomics::detail::chrono::ceil< std::chrono::nanoseconds >(timeout - now);
  79. if (nsec.count() <= 0)
  80. {
  81. timed_out = true;
  82. break;
  83. }
  84. wait_state.wait_for(nsec);
  85. now = Clock::now();
  86. new_val = s;
  87. }
  88. return new_val;
  89. }
  90. #if !defined(BOOST_WINDOWS)
  91. template< typename Clock >
  92. static BOOST_FORCEINLINE storage_type wait_until_abs_timeout
  93. (
  94. storage_type const volatile& storage,
  95. storage_type old_val,
  96. typename Clock::time_point timeout,
  97. bool& timed_out
  98. ) noexcept
  99. {
  100. storage_type const& s = const_cast< storage_type const& >(storage);
  101. scoped_wait_state wait_state(&storage);
  102. storage_type new_val = s;
  103. const timespec abs_timeout(posix_clock_traits< Clock >::to_timespec(timeout));
  104. if (BOOST_LIKELY(abs_timeout.tv_sec >= 0))
  105. {
  106. while (new_val == old_val)
  107. {
  108. const bool wait_timed_out = wait_state.wait_until(posix_clock_traits< Clock >::clock_id, abs_timeout);
  109. new_val = s;
  110. if (wait_timed_out)
  111. goto timeout_expired;
  112. }
  113. }
  114. else
  115. {
  116. timeout_expired:
  117. timed_out = new_val == old_val;
  118. }
  119. return new_val;
  120. }
  121. template< typename Clock >
  122. static BOOST_FORCEINLINE storage_type wait_until_dispatch
  123. (
  124. storage_type const volatile& storage,
  125. storage_type old_val,
  126. typename Clock::time_point timeout,
  127. bool& timed_out,
  128. std::true_type
  129. ) noexcept
  130. {
  131. return wait_until_abs_timeout< Clock >(storage, old_val, timeout,timed_out);
  132. }
  133. template< typename Clock >
  134. static BOOST_FORCEINLINE storage_type wait_until_dispatch
  135. (
  136. storage_type const volatile& storage,
  137. storage_type old_val,
  138. typename Clock::time_point timeout,
  139. bool& timed_out,
  140. std::false_type
  141. ) noexcept(noexcept(Clock::now()))
  142. {
  143. return wait_until_fallback< Clock >(storage, old_val, timeout, Clock::now(), timed_out);
  144. }
  145. public:
  146. template< typename Clock, typename Duration >
  147. static BOOST_FORCEINLINE storage_type wait_until
  148. (
  149. storage_type const volatile& storage,
  150. storage_type old_val,
  151. std::chrono::time_point< Clock, Duration > timeout,
  152. memory_order,
  153. bool& timed_out
  154. ) noexcept(noexcept(wait_until_dispatch< Clock >(storage, old_val, timeout, timed_out, std::integral_constant< bool, has_posix_clock_traits< Clock >::value >())))
  155. {
  156. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  157. return wait_until_dispatch< Clock >(storage, old_val, timeout, timed_out, std::integral_constant< bool, has_posix_clock_traits< Clock >::value >());
  158. }
  159. #else // !defined(BOOST_WINDOWS)
  160. public:
  161. template< typename Clock, typename Duration >
  162. static BOOST_FORCEINLINE storage_type wait_until
  163. (
  164. storage_type const volatile& storage,
  165. storage_type old_val,
  166. std::chrono::time_point< Clock, Duration > timeout,
  167. memory_order,
  168. bool& timed_out
  169. ) noexcept(noexcept(wait_until_fallback< Clock >(storage, old_val, timeout, Clock::now(), timed_out)))
  170. {
  171. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  172. return wait_until_fallback< Clock >(storage, old_val, timeout, Clock::now(), timed_out);
  173. }
  174. #endif // !defined(BOOST_WINDOWS)
  175. template< typename Rep, typename Period >
  176. static BOOST_FORCEINLINE storage_type wait_for
  177. (
  178. storage_type const volatile& storage,
  179. storage_type old_val,
  180. std::chrono::duration< Rep, Period > timeout,
  181. memory_order,
  182. bool& timed_out
  183. ) noexcept
  184. {
  185. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  186. const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
  187. return wait_until_fallback< std::chrono::steady_clock >(storage, old_val, now + timeout, now, timed_out);
  188. }
  189. static void notify_one(storage_type volatile& storage) noexcept
  190. {
  191. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  192. scoped_lock lock(&storage);
  193. lock_pool::notify_one(lock.get_lock_state(), &storage);
  194. }
  195. static void notify_all(storage_type volatile& storage) noexcept
  196. {
  197. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  198. scoped_lock lock(&storage);
  199. lock_pool::notify_all(lock.get_lock_state(), &storage);
  200. }
  201. };
  202. template< typename Base, std::size_t Size, bool Interprocess >
  203. struct wait_operations< Base, Size, false, Interprocess > :
  204. public wait_operations_emulated< Base >
  205. {
  206. };
  207. } // namespace detail
  208. } // namespace atomics
  209. } // namespace boost
  210. #include <boost/atomic/detail/footer.hpp>
  211. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_EMULATED_HPP_INCLUDED_