deadline_timer_service.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // detail/deadline_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <boost/asio/associated_cancellation_slot.hpp>
  18. #include <boost/asio/cancellation_type.hpp>
  19. #include <boost/asio/config.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/execution_context.hpp>
  22. #include <boost/asio/detail/bind_handler.hpp>
  23. #include <boost/asio/detail/fenced_block.hpp>
  24. #include <boost/asio/detail/memory.hpp>
  25. #include <boost/asio/detail/noncopyable.hpp>
  26. #include <boost/asio/detail/socket_ops.hpp>
  27. #include <boost/asio/detail/socket_types.hpp>
  28. #include <boost/asio/detail/timer_queue.hpp>
  29. #include <boost/asio/detail/timer_scheduler.hpp>
  30. #include <boost/asio/detail/wait_handler.hpp>
  31. #include <boost/asio/detail/wait_op.hpp>
  32. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  33. # include <chrono>
  34. # include <thread>
  35. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  36. #include <boost/asio/detail/push_options.hpp>
  37. namespace boost {
  38. namespace asio {
  39. namespace detail {
  40. template <typename TimeTraits>
  41. class deadline_timer_service
  42. : public execution_context_service_base<deadline_timer_service<TimeTraits>>
  43. {
  44. public:
  45. // The time type.
  46. typedef typename TimeTraits::time_type time_type;
  47. // The duration type.
  48. typedef typename TimeTraits::duration_type duration_type;
  49. // The allocator type.
  50. typedef execution_context::allocator<void> allocator_type;
  51. // The implementation type of the timer. This type is dependent on the
  52. // underlying implementation of the timer service.
  53. struct implementation_type
  54. : private boost::asio::detail::noncopyable
  55. {
  56. time_type expiry;
  57. bool might_have_pending_waits;
  58. typename timer_queue<TimeTraits, allocator_type>::per_timer_data timer_data;
  59. };
  60. // Constructor.
  61. deadline_timer_service(execution_context& context)
  62. : execution_context_service_base<
  63. deadline_timer_service<TimeTraits>>(context),
  64. timer_queue_(allocator_type(context),
  65. config(context).get("timer", "heap_reserve", 0U)),
  66. scheduler_(boost::asio::use_service<timer_scheduler>(context))
  67. {
  68. scheduler_.init_task();
  69. scheduler_.add_timer_queue(timer_queue_);
  70. }
  71. // Destructor.
  72. ~deadline_timer_service()
  73. {
  74. scheduler_.remove_timer_queue(timer_queue_);
  75. }
  76. // Destroy all user-defined handler objects owned by the service.
  77. void shutdown()
  78. {
  79. }
  80. // Construct a new timer implementation.
  81. void construct(implementation_type& impl)
  82. {
  83. impl.expiry = time_type();
  84. impl.might_have_pending_waits = false;
  85. }
  86. // Destroy a timer implementation.
  87. void destroy(implementation_type& impl)
  88. {
  89. boost::system::error_code ec;
  90. cancel(impl, ec);
  91. }
  92. // Move-construct a new timer implementation.
  93. void move_construct(implementation_type& impl,
  94. implementation_type& other_impl)
  95. {
  96. if (other_impl.might_have_pending_waits)
  97. {
  98. scheduler_.move_timer(timer_queue_,
  99. impl.timer_data, other_impl.timer_data);
  100. }
  101. impl.expiry = other_impl.expiry;
  102. other_impl.expiry = time_type();
  103. impl.might_have_pending_waits = other_impl.might_have_pending_waits;
  104. other_impl.might_have_pending_waits = false;
  105. }
  106. // Move-assign from another timer implementation.
  107. void move_assign(implementation_type& impl,
  108. deadline_timer_service& other_service,
  109. implementation_type& other_impl)
  110. {
  111. if (this != &other_service)
  112. if (impl.might_have_pending_waits)
  113. scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  114. other_service.scheduler_.move_timer(other_service.timer_queue_,
  115. impl.timer_data, other_impl.timer_data);
  116. impl.expiry = other_impl.expiry;
  117. other_impl.expiry = time_type();
  118. impl.might_have_pending_waits = other_impl.might_have_pending_waits;
  119. other_impl.might_have_pending_waits = false;
  120. }
  121. // Move-construct a new timer implementation.
  122. void converting_move_construct(implementation_type& impl,
  123. deadline_timer_service&, implementation_type& other_impl)
  124. {
  125. move_construct(impl, other_impl);
  126. }
  127. // Move-assign from another timer implementation.
  128. void converting_move_assign(implementation_type& impl,
  129. deadline_timer_service& other_service,
  130. implementation_type& other_impl)
  131. {
  132. move_assign(impl, other_service, other_impl);
  133. }
  134. // Cancel any asynchronous wait operations associated with the timer.
  135. std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
  136. {
  137. if (!impl.might_have_pending_waits)
  138. {
  139. ec = boost::system::error_code();
  140. return 0;
  141. }
  142. BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
  143. "deadline_timer", &impl, 0, "cancel"));
  144. std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  145. impl.might_have_pending_waits = false;
  146. ec = boost::system::error_code();
  147. return count;
  148. }
  149. // Cancels one asynchronous wait operation associated with the timer.
  150. std::size_t cancel_one(implementation_type& impl,
  151. boost::system::error_code& ec)
  152. {
  153. if (!impl.might_have_pending_waits)
  154. {
  155. ec = boost::system::error_code();
  156. return 0;
  157. }
  158. BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
  159. "deadline_timer", &impl, 0, "cancel_one"));
  160. std::size_t count = scheduler_.cancel_timer(
  161. timer_queue_, impl.timer_data, 1);
  162. if (count == 0)
  163. impl.might_have_pending_waits = false;
  164. ec = boost::system::error_code();
  165. return count;
  166. }
  167. // Get the expiry time for the timer as an absolute time.
  168. time_type expiry(const implementation_type& impl) const
  169. {
  170. return impl.expiry;
  171. }
  172. // Get the expiry time for the timer as an absolute time.
  173. time_type expires_at(const implementation_type& impl) const
  174. {
  175. return impl.expiry;
  176. }
  177. // Get the expiry time for the timer relative to now.
  178. duration_type expires_from_now(const implementation_type& impl) const
  179. {
  180. return TimeTraits::subtract(this->expiry(impl), TimeTraits::now());
  181. }
  182. // Set the expiry time for the timer as an absolute time.
  183. std::size_t expires_at(implementation_type& impl,
  184. const time_type& expiry_time, boost::system::error_code& ec)
  185. {
  186. std::size_t count = cancel(impl, ec);
  187. impl.expiry = expiry_time;
  188. ec = boost::system::error_code();
  189. return count;
  190. }
  191. // Set the expiry time for the timer relative to now.
  192. std::size_t expires_after(implementation_type& impl,
  193. const duration_type& expiry_time, boost::system::error_code& ec)
  194. {
  195. return expires_at(impl,
  196. TimeTraits::add(TimeTraits::now(), expiry_time), ec);
  197. }
  198. // Set the expiry time for the timer relative to now.
  199. std::size_t expires_from_now(implementation_type& impl,
  200. const duration_type& expiry_time, boost::system::error_code& ec)
  201. {
  202. return expires_at(impl,
  203. TimeTraits::add(TimeTraits::now(), expiry_time), ec);
  204. }
  205. // Perform a blocking wait on the timer.
  206. void wait(implementation_type& impl, boost::system::error_code& ec)
  207. {
  208. time_type now = TimeTraits::now();
  209. ec = boost::system::error_code();
  210. while (TimeTraits::less_than(now, impl.expiry) && !ec)
  211. {
  212. this->do_wait(TimeTraits::to_posix_duration(
  213. TimeTraits::subtract(impl.expiry, now)), ec);
  214. now = TimeTraits::now();
  215. }
  216. }
  217. // Start an asynchronous wait on the timer.
  218. template <typename Handler, typename IoExecutor>
  219. void async_wait(implementation_type& impl,
  220. Handler& handler, const IoExecutor& io_ex)
  221. {
  222. associated_cancellation_slot_t<Handler> slot
  223. = boost::asio::get_associated_cancellation_slot(handler);
  224. // Allocate and construct an operation to wrap the handler.
  225. typedef wait_handler<Handler, IoExecutor> op;
  226. typename op::ptr p = { boost::asio::detail::addressof(handler),
  227. op::ptr::allocate(handler), 0 };
  228. p.p = new (p.v) op(handler, io_ex);
  229. // Optionally register for per-operation cancellation.
  230. if (slot.is_connected())
  231. {
  232. p.p->cancellation_key_ =
  233. &slot.template emplace<op_cancellation>(this, &impl.timer_data);
  234. }
  235. impl.might_have_pending_waits = true;
  236. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  237. *p.p, "deadline_timer", &impl, 0, "async_wait"));
  238. scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
  239. p.v = p.p = 0;
  240. }
  241. private:
  242. // Helper function to wait given a duration type. The duration type should
  243. // either be of type boost::posix_time::time_duration, or implement the
  244. // required subset of its interface.
  245. template <typename Duration>
  246. void do_wait(const Duration& timeout, boost::system::error_code& ec)
  247. {
  248. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  249. std::this_thread::sleep_for(
  250. std::chrono::seconds(timeout.total_seconds())
  251. + std::chrono::microseconds(timeout.total_microseconds()));
  252. ec = boost::system::error_code();
  253. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  254. ::timeval tv;
  255. tv.tv_sec = timeout.total_seconds();
  256. tv.tv_usec = timeout.total_microseconds() % 1000000;
  257. socket_ops::select(0, 0, 0, 0, &tv, ec);
  258. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  259. }
  260. // Helper class used to implement per-operation cancellation.
  261. class op_cancellation
  262. {
  263. public:
  264. op_cancellation(deadline_timer_service* s,
  265. typename timer_queue<TimeTraits, allocator_type>::per_timer_data* p)
  266. : service_(s),
  267. timer_data_(p)
  268. {
  269. }
  270. void operator()(cancellation_type_t type)
  271. {
  272. if (!!(type &
  273. (cancellation_type::terminal
  274. | cancellation_type::partial
  275. | cancellation_type::total)))
  276. {
  277. service_->scheduler_.cancel_timer_by_key(
  278. service_->timer_queue_, timer_data_, this);
  279. }
  280. }
  281. private:
  282. deadline_timer_service* service_;
  283. typename timer_queue<TimeTraits, allocator_type>::per_timer_data*
  284. timer_data_;
  285. };
  286. // The queue of timers.
  287. timer_queue<TimeTraits, allocator_type> timer_queue_;
  288. // The object that schedules and executes timers. Usually a reactor.
  289. timer_scheduler& scheduler_;
  290. };
  291. } // namespace detail
  292. } // namespace asio
  293. } // namespace boost
  294. #include <boost/asio/detail/pop_options.hpp>
  295. #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP