deadline_timer_service.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // detail/deadline_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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 serial port 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 serial port 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. // Cancel any asynchronous wait operations associated with the timer.
  112. std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
  113. {
  114. if (!impl.might_have_pending_waits)
  115. {
  116. ec = boost::system::error_code();
  117. return 0;
  118. }
  119. BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
  120. "deadline_timer", &impl, 0, "cancel"));
  121. std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  122. impl.might_have_pending_waits = false;
  123. ec = boost::system::error_code();
  124. return count;
  125. }
  126. // Cancels one asynchronous wait operation associated with the timer.
  127. std::size_t cancel_one(implementation_type& impl,
  128. boost::system::error_code& ec)
  129. {
  130. if (!impl.might_have_pending_waits)
  131. {
  132. ec = boost::system::error_code();
  133. return 0;
  134. }
  135. BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
  136. "deadline_timer", &impl, 0, "cancel_one"));
  137. std::size_t count = scheduler_.cancel_timer(
  138. timer_queue_, impl.timer_data, 1);
  139. if (count == 0)
  140. impl.might_have_pending_waits = false;
  141. ec = boost::system::error_code();
  142. return count;
  143. }
  144. // Get the expiry time for the timer as an absolute time.
  145. time_type expiry(const implementation_type& impl) const
  146. {
  147. return impl.expiry;
  148. }
  149. // Get the expiry time for the timer as an absolute time.
  150. time_type expires_at(const implementation_type& impl) const
  151. {
  152. return impl.expiry;
  153. }
  154. // Get the expiry time for the timer relative to now.
  155. duration_type expires_from_now(const implementation_type& impl) const
  156. {
  157. return Time_Traits::subtract(this->expiry(impl), Time_Traits::now());
  158. }
  159. // Set the expiry time for the timer as an absolute time.
  160. std::size_t expires_at(implementation_type& impl,
  161. const time_type& expiry_time, boost::system::error_code& ec)
  162. {
  163. std::size_t count = cancel(impl, ec);
  164. impl.expiry = expiry_time;
  165. ec = boost::system::error_code();
  166. return count;
  167. }
  168. // Set the expiry time for the timer relative to now.
  169. std::size_t expires_after(implementation_type& impl,
  170. const duration_type& expiry_time, boost::system::error_code& ec)
  171. {
  172. return expires_at(impl,
  173. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  174. }
  175. // Set the expiry time for the timer relative to now.
  176. std::size_t expires_from_now(implementation_type& impl,
  177. const duration_type& expiry_time, boost::system::error_code& ec)
  178. {
  179. return expires_at(impl,
  180. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  181. }
  182. // Perform a blocking wait on the timer.
  183. void wait(implementation_type& impl, boost::system::error_code& ec)
  184. {
  185. time_type now = Time_Traits::now();
  186. ec = boost::system::error_code();
  187. while (Time_Traits::less_than(now, impl.expiry) && !ec)
  188. {
  189. this->do_wait(Time_Traits::to_posix_duration(
  190. Time_Traits::subtract(impl.expiry, now)), ec);
  191. now = Time_Traits::now();
  192. }
  193. }
  194. // Start an asynchronous wait on the timer.
  195. template <typename Handler, typename IoExecutor>
  196. void async_wait(implementation_type& impl,
  197. Handler& handler, const IoExecutor& io_ex)
  198. {
  199. // Allocate and construct an operation to wrap the handler.
  200. typedef wait_handler<Handler, IoExecutor> op;
  201. typename op::ptr p = { boost::asio::detail::addressof(handler),
  202. op::ptr::allocate(handler), 0 };
  203. p.p = new (p.v) op(handler, io_ex);
  204. impl.might_have_pending_waits = true;
  205. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  206. *p.p, "deadline_timer", &impl, 0, "async_wait"));
  207. scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
  208. p.v = p.p = 0;
  209. }
  210. private:
  211. // Helper function to wait given a duration type. The duration type should
  212. // either be of type boost::posix_time::time_duration, or implement the
  213. // required subset of its interface.
  214. template <typename Duration>
  215. void do_wait(const Duration& timeout, boost::system::error_code& ec)
  216. {
  217. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  218. std::this_thread::sleep_for(
  219. std::chrono::seconds(timeout.total_seconds())
  220. + std::chrono::microseconds(timeout.total_microseconds()));
  221. ec = boost::system::error_code();
  222. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  223. ::timeval tv;
  224. tv.tv_sec = timeout.total_seconds();
  225. tv.tv_usec = timeout.total_microseconds() % 1000000;
  226. socket_ops::select(0, 0, 0, 0, &tv, ec);
  227. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  228. }
  229. // The queue of timers.
  230. timer_queue<Time_Traits> timer_queue_;
  231. // The object that schedules and executes timers. Usually a reactor.
  232. timer_scheduler& scheduler_;
  233. };
  234. } // namespace detail
  235. } // namespace asio
  236. } // namespace boost
  237. #include <boost/asio/detail/pop_options.hpp>
  238. #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP