wait_ops_futex.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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_futex.hpp
  10. *
  11. * This header contains implementation of the waiting/notifying atomic operations based on futexes.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_
  15. #include <unistd.h> // _POSIX_MONOTONIC_CLOCK
  16. #include <time.h>
  17. #include <cstdint>
  18. #include <cerrno>
  19. #include <limits>
  20. #include <chrono>
  21. #include <type_traits>
  22. #include <boost/memory_order.hpp>
  23. #include <boost/atomic/posix_clock_traits_fwd.hpp>
  24. #include <boost/atomic/detail/config.hpp>
  25. #include <boost/atomic/detail/chrono.hpp>
  26. #include <boost/atomic/detail/futex.hpp>
  27. #include <boost/atomic/detail/has_posix_clock_traits.hpp>
  28. #include <boost/atomic/detail/wait_operations_fwd.hpp>
  29. #include <boost/atomic/detail/header.hpp>
  30. #ifdef BOOST_HAS_PRAGMA_ONCE
  31. #pragma once
  32. #endif
  33. namespace boost {
  34. namespace atomics {
  35. namespace detail {
  36. #if defined(BOOST_ATOMIC_DETAIL_FUTEX_WAIT_BITSET)
  37. struct futex_wait_fallback {};
  38. //! The type trait selects futex timed wait implementation tag
  39. template< typename Clock, bool = has_posix_clock_traits< Clock >::value >
  40. struct select_futex_wait
  41. {
  42. using type = futex_wait_fallback;
  43. };
  44. template< clockid_t ClockId >
  45. struct futex_wait_clock;
  46. template< clockid_t ClockId >
  47. struct select_futex_wait_impl
  48. {
  49. using type = futex_wait_fallback;
  50. };
  51. #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
  52. template< >
  53. struct futex_wait_clock< CLOCK_MONOTONIC >
  54. {
  55. static constexpr int futex_flags = 0;
  56. };
  57. template< >
  58. struct select_futex_wait_impl< CLOCK_MONOTONIC >
  59. {
  60. using type = futex_wait_clock< CLOCK_MONOTONIC >;
  61. };
  62. #endif // defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
  63. #if defined(BOOST_ATOMIC_DETAIL_FUTEX_CLOCK_REALTIME)
  64. template< >
  65. struct futex_wait_clock< CLOCK_REALTIME >
  66. {
  67. static constexpr int futex_flags = BOOST_ATOMIC_DETAIL_FUTEX_CLOCK_REALTIME;
  68. };
  69. template< >
  70. struct select_futex_wait_impl< CLOCK_REALTIME >
  71. {
  72. using type = futex_wait_clock< CLOCK_REALTIME >;
  73. };
  74. #endif // defined(BOOST_ATOMIC_DETAIL_FUTEX_CLOCK_REALTIME)
  75. template< typename Clock >
  76. struct select_futex_wait< Clock, true >
  77. {
  78. using type = typename select_futex_wait_impl< posix_clock_traits< Clock >::clock_id >::type;
  79. };
  80. #endif // defined(BOOST_ATOMIC_DETAIL_FUTEX_WAIT_BITSET)
  81. template< typename Base, bool Interprocess >
  82. struct wait_operations< Base, 4u, true, Interprocess > :
  83. public Base
  84. {
  85. using base_type = Base;
  86. using storage_type = typename base_type::storage_type;
  87. static constexpr bool always_has_native_wait_notify = true;
  88. private:
  89. static constexpr int futex_private_flag = Interprocess ? 0 : BOOST_ATOMIC_DETAIL_FUTEX_PRIVATE_FLAG;
  90. public:
  91. static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) noexcept
  92. {
  93. return true;
  94. }
  95. static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) noexcept
  96. {
  97. storage_type new_val = base_type::load(storage, order);
  98. while (new_val == old_val)
  99. {
  100. atomics::detail::futex_wait(const_cast< storage_type* >(&storage), old_val, futex_private_flag);
  101. new_val = base_type::load(storage, order);
  102. }
  103. return new_val;
  104. }
  105. private:
  106. template< typename Clock >
  107. static BOOST_FORCEINLINE storage_type wait_until_fallback
  108. (
  109. storage_type const volatile& storage,
  110. storage_type old_val,
  111. typename Clock::time_point timeout,
  112. typename Clock::time_point now,
  113. memory_order order,
  114. bool& timed_out
  115. ) noexcept(noexcept(Clock::now()))
  116. {
  117. futex_timespec ts{};
  118. storage_type new_val = base_type::load(storage, order);
  119. while (new_val == old_val)
  120. {
  121. const std::int64_t nsec = atomics::detail::chrono::ceil< std::chrono::nanoseconds >(timeout - now).count();
  122. if (nsec <= 0)
  123. {
  124. timed_out = true;
  125. break;
  126. }
  127. const std::int64_t sec = nsec / 1000000000;
  128. if (BOOST_LIKELY(sec <= (std::numeric_limits< decltype(ts.tv_sec) >::max)()))
  129. {
  130. ts.tv_sec = static_cast< decltype(ts.tv_sec) >(sec);
  131. ts.tv_nsec = static_cast< decltype(ts.tv_nsec) >(nsec % 1000000000);
  132. }
  133. else
  134. {
  135. ts.tv_sec = (std::numeric_limits< decltype(ts.tv_sec) >::max)();
  136. ts.tv_nsec = static_cast< decltype(ts.tv_nsec) >(999999999);
  137. }
  138. atomics::detail::futex_wait_for(const_cast< storage_type* >(&storage), old_val, ts, futex_private_flag);
  139. now = Clock::now();
  140. new_val = base_type::load(storage, order);
  141. }
  142. return new_val;
  143. }
  144. #if defined(BOOST_ATOMIC_DETAIL_FUTEX_WAIT_BITSET)
  145. template< typename Clock, clockid_t ClockId >
  146. static BOOST_FORCEINLINE storage_type wait_until_abs_timeout
  147. (
  148. storage_type const volatile& storage,
  149. storage_type old_val,
  150. typename Clock::time_point timeout,
  151. memory_order order,
  152. bool& timed_out
  153. ) noexcept(noexcept(Clock::now()))
  154. {
  155. futex_timespec ts(posix_clock_traits< Clock >::to_timespec(timeout));
  156. storage_type new_val = base_type::load(storage, order);
  157. if (BOOST_LIKELY(ts.tv_sec >= 0))
  158. {
  159. while (new_val == old_val)
  160. {
  161. int err = atomics::detail::futex_wait_until
  162. (
  163. const_cast< storage_type* >(&storage),
  164. old_val,
  165. ts,
  166. futex_private_flag | futex_wait_clock< ClockId >::futex_flags
  167. );
  168. if (err < 0)
  169. {
  170. err = errno;
  171. if (err == ETIMEDOUT)
  172. {
  173. new_val = base_type::load(storage, order);
  174. timed_out = new_val == old_val;
  175. break;
  176. }
  177. if (BOOST_UNLIKELY(err == ENOSYS))
  178. return wait_until_fallback< Clock >(storage, old_val, timeout, Clock::now(), order, timed_out);
  179. }
  180. new_val = base_type::load(storage, order);
  181. }
  182. }
  183. else
  184. {
  185. timed_out = new_val == old_val;
  186. }
  187. return new_val;
  188. }
  189. template< typename Clock >
  190. static BOOST_FORCEINLINE storage_type wait_until_dispatch
  191. (
  192. storage_type const volatile& storage,
  193. storage_type old_val,
  194. typename Clock::time_point timeout,
  195. memory_order order,
  196. bool& timed_out,
  197. futex_wait_fallback
  198. ) noexcept(noexcept(Clock::now()))
  199. {
  200. return wait_until_fallback< Clock >(storage, old_val, timeout, Clock::now(), order, timed_out);
  201. }
  202. template< typename Clock, clockid_t ClockId >
  203. static BOOST_FORCEINLINE storage_type wait_until_dispatch
  204. (
  205. storage_type const volatile& storage,
  206. storage_type old_val,
  207. typename Clock::time_point timeout,
  208. memory_order order,
  209. bool& timed_out,
  210. futex_wait_clock< ClockId >
  211. ) noexcept(noexcept(Clock::now()))
  212. {
  213. return wait_until_abs_timeout< Clock, ClockId >(storage, old_val, timeout, order, timed_out);
  214. }
  215. public:
  216. template< typename Clock, typename Duration >
  217. static BOOST_FORCEINLINE storage_type wait_until
  218. (
  219. storage_type const volatile& storage,
  220. storage_type old_val,
  221. std::chrono::time_point< Clock, Duration > timeout,
  222. memory_order order,
  223. bool& timed_out
  224. ) noexcept(noexcept(wait_until_dispatch< Clock >(storage, old_val, timeout, order, timed_out, typename select_futex_wait< Clock >::type())))
  225. {
  226. return wait_until_dispatch< Clock >(storage, old_val, timeout, order, timed_out, typename select_futex_wait< Clock >::type());
  227. }
  228. #else // defined(BOOST_ATOMIC_DETAIL_FUTEX_WAIT_BITSET)
  229. public:
  230. template< typename Clock, typename Duration >
  231. static BOOST_FORCEINLINE storage_type wait_until
  232. (
  233. storage_type const volatile& storage,
  234. storage_type old_val,
  235. std::chrono::time_point< Clock, Duration > timeout,
  236. memory_order order,
  237. bool& timed_out
  238. ) noexcept(noexcept(wait_until_fallback< Clock >(storage, old_val, timeout, Clock::now(), order, timed_out)))
  239. {
  240. return wait_until_fallback< Clock >(storage, old_val, timeout, Clock::now(), order, timed_out);
  241. }
  242. #endif // defined(BOOST_ATOMIC_DETAIL_FUTEX_WAIT_BITSET)
  243. template< typename Rep, typename Period >
  244. static BOOST_FORCEINLINE storage_type wait_for
  245. (
  246. storage_type const volatile& storage,
  247. storage_type old_val,
  248. std::chrono::duration< Rep, Period > timeout,
  249. memory_order order,
  250. bool& timed_out
  251. ) noexcept
  252. {
  253. #if defined(BOOST_ATOMIC_DETAIL_FUTEX_WAIT_BITSET) && defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
  254. if (BOOST_LIKELY(timeout.count() >= 0))
  255. {
  256. timespec now{};
  257. if (BOOST_LIKELY(clock_gettime(CLOCK_MONOTONIC, &now) == 0))
  258. {
  259. const std::int64_t nsec = static_cast< std::int64_t >(now.tv_nsec) + atomics::detail::chrono::ceil< std::chrono::nanoseconds >(timeout).count();
  260. const std::int64_t sec = static_cast< std::int64_t >(now.tv_sec) + nsec / 1000000000;
  261. if (BOOST_LIKELY(sec <= (std::numeric_limits< decltype(futex_timespec::tv_sec) >::max)()))
  262. {
  263. futex_timespec ts{};
  264. ts.tv_sec = static_cast< decltype(ts.tv_sec) >(sec);
  265. ts.tv_nsec = static_cast< decltype(ts.tv_nsec) >(nsec % 1000000000);
  266. storage_type new_val = base_type::load(storage, order);
  267. while (new_val == old_val)
  268. {
  269. int err = atomics::detail::futex_wait_until(const_cast< storage_type* >(&storage), old_val, ts, futex_private_flag);
  270. if (err < 0)
  271. {
  272. err = errno;
  273. if (err == ETIMEDOUT)
  274. {
  275. new_val = base_type::load(storage, order);
  276. timed_out = new_val == old_val;
  277. break;
  278. }
  279. if (BOOST_UNLIKELY(err == ENOSYS))
  280. goto use_wait_until_fallback;
  281. }
  282. new_val = base_type::load(storage, order);
  283. }
  284. return new_val;
  285. }
  286. }
  287. }
  288. use_wait_until_fallback:
  289. #endif // defined(BOOST_ATOMIC_DETAIL_FUTEX_WAIT_BITSET) && defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
  290. const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
  291. return wait_until_fallback< std::chrono::steady_clock >(storage, old_val, now + timeout, now, order, timed_out);
  292. }
  293. static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) noexcept
  294. {
  295. atomics::detail::futex_signal(const_cast< storage_type* >(&storage), futex_private_flag);
  296. }
  297. static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) noexcept
  298. {
  299. atomics::detail::futex_broadcast(const_cast< storage_type* >(&storage), futex_private_flag);
  300. }
  301. };
  302. } // namespace detail
  303. } // namespace atomics
  304. } // namespace boost
  305. #include <boost/atomic/detail/footer.hpp>
  306. #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_FUTEX_HPP_INCLUDED_