deadline_timer_service.hpp 10 KB

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