deadline_timer_service.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // detail/deadline_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/io_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 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(boost::asio::io_context& io_context)
  58. : service_base<deadline_timer_service<Time_Traits> >(io_context),
  59. scheduler_(boost::asio::use_service<timer_scheduler>(io_context))
  60. {
  61. scheduler_.init_task();
  62. scheduler_.add_timer_queue(timer_queue_);
  63. }
  64. // Destructor.
  65. ~deadline_timer_service()
  66. {
  67. scheduler_.remove_timer_queue(timer_queue_);
  68. }
  69. // Destroy all user-defined handler objects owned by the service.
  70. void shutdown()
  71. {
  72. }
  73. // Construct a new timer implementation.
  74. void construct(implementation_type& impl)
  75. {
  76. impl.expiry = time_type();
  77. impl.might_have_pending_waits = false;
  78. }
  79. // Destroy a timer implementation.
  80. void destroy(implementation_type& impl)
  81. {
  82. boost::system::error_code ec;
  83. cancel(impl, ec);
  84. }
  85. // Move-construct a new serial port implementation.
  86. void move_construct(implementation_type& impl,
  87. implementation_type& other_impl)
  88. {
  89. scheduler_.move_timer(timer_queue_, impl.timer_data, other_impl.timer_data);
  90. impl.expiry = other_impl.expiry;
  91. other_impl.expiry = time_type();
  92. impl.might_have_pending_waits = other_impl.might_have_pending_waits;
  93. other_impl.might_have_pending_waits = false;
  94. }
  95. // Move-assign from another serial port implementation.
  96. void move_assign(implementation_type& impl,
  97. deadline_timer_service& other_service,
  98. implementation_type& other_impl)
  99. {
  100. if (this != &other_service)
  101. if (impl.might_have_pending_waits)
  102. scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  103. other_service.scheduler_.move_timer(other_service.timer_queue_,
  104. impl.timer_data, other_impl.timer_data);
  105. impl.expiry = other_impl.expiry;
  106. other_impl.expiry = time_type();
  107. impl.might_have_pending_waits = other_impl.might_have_pending_waits;
  108. other_impl.might_have_pending_waits = false;
  109. }
  110. // Cancel any asynchronous wait operations associated with the timer.
  111. std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
  112. {
  113. if (!impl.might_have_pending_waits)
  114. {
  115. ec = boost::system::error_code();
  116. return 0;
  117. }
  118. BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
  119. "deadline_timer", &impl, 0, "cancel"));
  120. std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  121. impl.might_have_pending_waits = false;
  122. ec = boost::system::error_code();
  123. return count;
  124. }
  125. // Cancels one asynchronous wait operation associated with the timer.
  126. std::size_t cancel_one(implementation_type& impl,
  127. 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_one"));
  136. std::size_t count = scheduler_.cancel_timer(
  137. timer_queue_, impl.timer_data, 1);
  138. if (count == 0)
  139. impl.might_have_pending_waits = false;
  140. ec = boost::system::error_code();
  141. return count;
  142. }
  143. // Get the expiry time for the timer as an absolute time.
  144. time_type expiry(const implementation_type& impl) const
  145. {
  146. return impl.expiry;
  147. }
  148. // Get the expiry time for the timer as an absolute time.
  149. time_type expires_at(const implementation_type& impl) const
  150. {
  151. return impl.expiry;
  152. }
  153. // Get the expiry time for the timer relative to now.
  154. duration_type expires_from_now(const implementation_type& impl) const
  155. {
  156. return Time_Traits::subtract(this->expiry(impl), Time_Traits::now());
  157. }
  158. // Set the expiry time for the timer as an absolute time.
  159. std::size_t expires_at(implementation_type& impl,
  160. const time_type& expiry_time, boost::system::error_code& ec)
  161. {
  162. std::size_t count = cancel(impl, ec);
  163. impl.expiry = expiry_time;
  164. ec = boost::system::error_code();
  165. return count;
  166. }
  167. // Set the expiry time for the timer relative to now.
  168. std::size_t expires_after(implementation_type& impl,
  169. const duration_type& expiry_time, boost::system::error_code& ec)
  170. {
  171. return expires_at(impl,
  172. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  173. }
  174. // Set the expiry time for the timer relative to now.
  175. std::size_t expires_from_now(implementation_type& impl,
  176. const duration_type& expiry_time, boost::system::error_code& ec)
  177. {
  178. return expires_at(impl,
  179. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  180. }
  181. // Perform a blocking wait on the timer.
  182. void wait(implementation_type& impl, boost::system::error_code& ec)
  183. {
  184. time_type now = Time_Traits::now();
  185. ec = boost::system::error_code();
  186. while (Time_Traits::less_than(now, impl.expiry) && !ec)
  187. {
  188. this->do_wait(Time_Traits::to_posix_duration(
  189. Time_Traits::subtract(impl.expiry, now)), ec);
  190. now = Time_Traits::now();
  191. }
  192. }
  193. // Start an asynchronous wait on the timer.
  194. template <typename Handler>
  195. void async_wait(implementation_type& impl, Handler& handler)
  196. {
  197. // Allocate and construct an operation to wrap the handler.
  198. typedef wait_handler<Handler> op;
  199. typename op::ptr p = { boost::asio::detail::addressof(handler),
  200. op::ptr::allocate(handler), 0 };
  201. p.p = new (p.v) op(handler);
  202. impl.might_have_pending_waits = true;
  203. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  204. *p.p, "deadline_timer", &impl, 0, "async_wait"));
  205. scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
  206. p.v = p.p = 0;
  207. }
  208. private:
  209. // Helper function to wait given a duration type. The duration type should
  210. // either be of type boost::posix_time::time_duration, or implement the
  211. // required subset of its interface.
  212. template <typename Duration>
  213. void do_wait(const Duration& timeout, boost::system::error_code& ec)
  214. {
  215. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  216. std::this_thread::sleep_for(
  217. std::chrono::seconds(timeout.total_seconds())
  218. + std::chrono::microseconds(timeout.total_microseconds()));
  219. ec = boost::system::error_code();
  220. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  221. ::timeval tv;
  222. tv.tv_sec = timeout.total_seconds();
  223. tv.tv_usec = timeout.total_microseconds() % 1000000;
  224. socket_ops::select(0, 0, 0, 0, &tv, ec);
  225. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  226. }
  227. // The queue of timers.
  228. timer_queue<Time_Traits> timer_queue_;
  229. // The object that schedules and executes timers. Usually a reactor.
  230. timer_scheduler& scheduler_;
  231. };
  232. } // namespace detail
  233. } // namespace asio
  234. } // namespace boost
  235. #include <boost/asio/detail/pop_options.hpp>
  236. #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP