deadline_timer_service.hpp 9.0 KB

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