deadline_timer_service.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // detail/deadline_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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_service.hpp>
  19. #include <boost/asio/detail/addressof.hpp>
  20. #include <boost/asio/detail/bind_handler.hpp>
  21. #include <boost/asio/detail/fenced_block.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_scheduler.hpp>
  27. #include <boost/asio/detail/wait_handler.hpp>
  28. #include <boost/asio/detail/wait_op.hpp>
  29. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  30. # include <chrono>
  31. # include <thread>
  32. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  33. #include <boost/asio/detail/push_options.hpp>
  34. namespace boost {
  35. namespace asio {
  36. namespace detail {
  37. template <typename Time_Traits>
  38. class deadline_timer_service
  39. {
  40. public:
  41. // The time type.
  42. typedef typename Time_Traits::time_type time_type;
  43. // The duration type.
  44. typedef typename Time_Traits::duration_type duration_type;
  45. // The implementation type of the timer. This type is dependent on the
  46. // underlying implementation of the timer service.
  47. struct implementation_type
  48. : private boost::asio::detail::noncopyable
  49. {
  50. time_type expiry;
  51. bool might_have_pending_waits;
  52. typename timer_queue<Time_Traits>::per_timer_data timer_data;
  53. };
  54. // Constructor.
  55. deadline_timer_service(boost::asio::io_service& io_service)
  56. : scheduler_(boost::asio::use_service<timer_scheduler>(io_service))
  57. {
  58. scheduler_.init_task();
  59. scheduler_.add_timer_queue(timer_queue_);
  60. }
  61. // Destructor.
  62. ~deadline_timer_service()
  63. {
  64. scheduler_.remove_timer_queue(timer_queue_);
  65. }
  66. // Destroy all user-defined handler objects owned by the service.
  67. void shutdown_service()
  68. {
  69. }
  70. // Construct a new timer implementation.
  71. void construct(implementation_type& impl)
  72. {
  73. impl.expiry = time_type();
  74. impl.might_have_pending_waits = false;
  75. }
  76. // Destroy a timer implementation.
  77. void destroy(implementation_type& impl)
  78. {
  79. boost::system::error_code ec;
  80. cancel(impl, ec);
  81. }
  82. // Cancel any asynchronous wait operations associated with the timer.
  83. std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
  84. {
  85. if (!impl.might_have_pending_waits)
  86. {
  87. ec = boost::system::error_code();
  88. return 0;
  89. }
  90. BOOST_ASIO_HANDLER_OPERATION(("deadline_timer", &impl, "cancel"));
  91. std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  92. impl.might_have_pending_waits = false;
  93. ec = boost::system::error_code();
  94. return count;
  95. }
  96. // Cancels one asynchronous wait operation associated with the timer.
  97. std::size_t cancel_one(implementation_type& impl,
  98. boost::system::error_code& ec)
  99. {
  100. if (!impl.might_have_pending_waits)
  101. {
  102. ec = boost::system::error_code();
  103. return 0;
  104. }
  105. BOOST_ASIO_HANDLER_OPERATION(("deadline_timer", &impl, "cancel_one"));
  106. std::size_t count = scheduler_.cancel_timer(
  107. timer_queue_, impl.timer_data, 1);
  108. if (count == 0)
  109. impl.might_have_pending_waits = false;
  110. ec = boost::system::error_code();
  111. return count;
  112. }
  113. // Get the expiry time for the timer as an absolute time.
  114. time_type expires_at(const implementation_type& impl) const
  115. {
  116. return impl.expiry;
  117. }
  118. // Set the expiry time for the timer as an absolute time.
  119. std::size_t expires_at(implementation_type& impl,
  120. const time_type& expiry_time, boost::system::error_code& ec)
  121. {
  122. std::size_t count = cancel(impl, ec);
  123. impl.expiry = expiry_time;
  124. ec = boost::system::error_code();
  125. return count;
  126. }
  127. // Get the expiry time for the timer relative to now.
  128. duration_type expires_from_now(const implementation_type& impl) const
  129. {
  130. return Time_Traits::subtract(expires_at(impl), Time_Traits::now());
  131. }
  132. // Set the expiry time for the timer relative to now.
  133. std::size_t expires_from_now(implementation_type& impl,
  134. const duration_type& expiry_time, boost::system::error_code& ec)
  135. {
  136. return expires_at(impl,
  137. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  138. }
  139. // Perform a blocking wait on the timer.
  140. void wait(implementation_type& impl, boost::system::error_code& ec)
  141. {
  142. time_type now = Time_Traits::now();
  143. ec = boost::system::error_code();
  144. while (Time_Traits::less_than(now, impl.expiry) && !ec)
  145. {
  146. this->do_wait(Time_Traits::to_posix_duration(
  147. Time_Traits::subtract(impl.expiry, now)), ec);
  148. now = Time_Traits::now();
  149. }
  150. }
  151. // Start an asynchronous wait on the timer.
  152. template <typename Handler>
  153. void async_wait(implementation_type& impl, Handler& handler)
  154. {
  155. // Allocate and construct an operation to wrap the handler.
  156. typedef wait_handler<Handler> op;
  157. typename op::ptr p = { boost::asio::detail::addressof(handler),
  158. boost_asio_handler_alloc_helpers::allocate(
  159. sizeof(op), handler), 0 };
  160. p.p = new (p.v) op(handler);
  161. impl.might_have_pending_waits = true;
  162. BOOST_ASIO_HANDLER_CREATION((p.p, "deadline_timer", &impl, "async_wait"));
  163. scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
  164. p.v = p.p = 0;
  165. }
  166. private:
  167. // Helper function to wait given a duration type. The duration type should
  168. // either be of type boost::posix_time::time_duration, or implement the
  169. // required subset of its interface.
  170. template <typename Duration>
  171. void do_wait(const Duration& timeout, boost::system::error_code& ec)
  172. {
  173. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  174. std::this_thread::sleep_for(
  175. std::chrono::seconds(timeout.total_seconds())
  176. + std::chrono::microseconds(timeout.total_microseconds()));
  177. ec = boost::system::error_code();
  178. #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  179. ::timeval tv;
  180. tv.tv_sec = timeout.total_seconds();
  181. tv.tv_usec = timeout.total_microseconds() % 1000000;
  182. socket_ops::select(0, 0, 0, 0, &tv, ec);
  183. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  184. }
  185. // The queue of timers.
  186. timer_queue<Time_Traits> timer_queue_;
  187. // The object that schedules and executes timers. Usually a reactor.
  188. timer_scheduler& scheduler_;
  189. };
  190. } // namespace detail
  191. } // namespace asio
  192. } // namespace boost
  193. #include <boost/asio/detail/pop_options.hpp>
  194. #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP