deadline_timer_service.hpp 10 KB

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