deadline_timer_service.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // detail/deadline_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include "asio/error.hpp"
  18. #include "asio/io_service.hpp"
  19. #include "asio/detail/bind_handler.hpp"
  20. #include "asio/detail/fenced_block.hpp"
  21. #include "asio/detail/noncopyable.hpp"
  22. #include "asio/detail/socket_ops.hpp"
  23. #include "asio/detail/socket_types.hpp"
  24. #include "asio/detail/timer_op.hpp"
  25. #include "asio/detail/timer_queue.hpp"
  26. #include "asio/detail/timer_scheduler.hpp"
  27. #include "asio/detail/wait_handler.hpp"
  28. #include "asio/detail/push_options.hpp"
  29. #include <boost/date_time/posix_time/posix_time_types.hpp>
  30. #include "asio/detail/pop_options.hpp"
  31. #include "asio/detail/push_options.hpp"
  32. namespace asio {
  33. namespace detail {
  34. template <typename Time_Traits>
  35. class deadline_timer_service
  36. {
  37. public:
  38. // The time type.
  39. typedef typename Time_Traits::time_type time_type;
  40. // The duration type.
  41. typedef typename Time_Traits::duration_type duration_type;
  42. // The implementation type of the timer. This type is dependent on the
  43. // underlying implementation of the timer service.
  44. struct implementation_type
  45. : private asio::detail::noncopyable
  46. {
  47. time_type expiry;
  48. bool might_have_pending_waits;
  49. typename timer_queue<Time_Traits>::per_timer_data timer_data;
  50. };
  51. // Constructor.
  52. deadline_timer_service(asio::io_service& io_service)
  53. : scheduler_(asio::use_service<timer_scheduler>(io_service))
  54. {
  55. scheduler_.init_task();
  56. scheduler_.add_timer_queue(timer_queue_);
  57. }
  58. // Destructor.
  59. ~deadline_timer_service()
  60. {
  61. scheduler_.remove_timer_queue(timer_queue_);
  62. }
  63. // Destroy all user-defined handler objects owned by the service.
  64. void shutdown_service()
  65. {
  66. }
  67. // Construct a new timer implementation.
  68. void construct(implementation_type& impl)
  69. {
  70. impl.expiry = time_type();
  71. impl.might_have_pending_waits = false;
  72. }
  73. // Destroy a timer implementation.
  74. void destroy(implementation_type& impl)
  75. {
  76. asio::error_code ec;
  77. cancel(impl, ec);
  78. }
  79. // Cancel any asynchronous wait operations associated with the timer.
  80. std::size_t cancel(implementation_type& impl, asio::error_code& ec)
  81. {
  82. if (!impl.might_have_pending_waits)
  83. {
  84. ec = asio::error_code();
  85. return 0;
  86. }
  87. std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
  88. impl.might_have_pending_waits = false;
  89. ec = asio::error_code();
  90. return count;
  91. }
  92. // Get the expiry time for the timer as an absolute time.
  93. time_type expires_at(const implementation_type& impl) const
  94. {
  95. return impl.expiry;
  96. }
  97. // Set the expiry time for the timer as an absolute time.
  98. std::size_t expires_at(implementation_type& impl,
  99. const time_type& expiry_time, asio::error_code& ec)
  100. {
  101. std::size_t count = cancel(impl, ec);
  102. impl.expiry = expiry_time;
  103. ec = asio::error_code();
  104. return count;
  105. }
  106. // Get the expiry time for the timer relative to now.
  107. duration_type expires_from_now(const implementation_type& impl) const
  108. {
  109. return Time_Traits::subtract(expires_at(impl), Time_Traits::now());
  110. }
  111. // Set the expiry time for the timer relative to now.
  112. std::size_t expires_from_now(implementation_type& impl,
  113. const duration_type& expiry_time, asio::error_code& ec)
  114. {
  115. return expires_at(impl,
  116. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  117. }
  118. // Perform a blocking wait on the timer.
  119. void wait(implementation_type& impl, asio::error_code& ec)
  120. {
  121. time_type now = Time_Traits::now();
  122. while (Time_Traits::less_than(now, impl.expiry))
  123. {
  124. boost::posix_time::time_duration timeout =
  125. Time_Traits::to_posix_duration(Time_Traits::subtract(impl.expiry, now));
  126. ::timeval tv;
  127. tv.tv_sec = timeout.total_seconds();
  128. tv.tv_usec = timeout.total_microseconds() % 1000000;
  129. asio::error_code ec;
  130. socket_ops::select(0, 0, 0, 0, &tv, ec);
  131. now = Time_Traits::now();
  132. }
  133. ec = asio::error_code();
  134. }
  135. // Start an asynchronous wait on the timer.
  136. template <typename Handler>
  137. void async_wait(implementation_type& impl, Handler handler)
  138. {
  139. // Allocate and construct an operation to wrap the handler.
  140. typedef wait_handler<Handler> op;
  141. typename op::ptr p = { boost::addressof(handler),
  142. asio_handler_alloc_helpers::allocate(
  143. sizeof(op), handler), 0 };
  144. p.p = new (p.v) op(handler);
  145. impl.might_have_pending_waits = true;
  146. scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
  147. p.v = p.p = 0;
  148. }
  149. private:
  150. // The queue of timers.
  151. timer_queue<Time_Traits> timer_queue_;
  152. // The object that schedules and executes timers. Usually a reactor.
  153. timer_scheduler& scheduler_;
  154. };
  155. } // namespace detail
  156. } // namespace asio
  157. #include "asio/detail/pop_options.hpp"
  158. #endif // ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP