timer_queue.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // detail/timer_queue.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_TIMER_QUEUE_HPP
  11. #define BOOST_ASIO_DETAIL_TIMER_QUEUE_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 <vector>
  18. #include <boost/asio/detail/cstdint.hpp>
  19. #include <boost/asio/detail/date_time_fwd.hpp>
  20. #include <boost/asio/detail/limits.hpp>
  21. #include <boost/asio/detail/op_queue.hpp>
  22. #include <boost/asio/detail/timer_queue_base.hpp>
  23. #include <boost/asio/detail/wait_op.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. template <typename Time_Traits>
  30. class timer_queue
  31. : public timer_queue_base
  32. {
  33. public:
  34. // The time type.
  35. typedef typename Time_Traits::time_type time_type;
  36. // The duration type.
  37. typedef typename Time_Traits::duration_type duration_type;
  38. // Per-timer data.
  39. class per_timer_data
  40. {
  41. public:
  42. per_timer_data() :
  43. heap_index_((std::numeric_limits<std::size_t>::max)()),
  44. next_(0), prev_(0)
  45. {
  46. }
  47. private:
  48. friend class timer_queue;
  49. // The operations waiting on the timer.
  50. op_queue<wait_op> op_queue_;
  51. // The index of the timer in the heap.
  52. std::size_t heap_index_;
  53. // Pointers to adjacent timers in a linked list.
  54. per_timer_data* next_;
  55. per_timer_data* prev_;
  56. };
  57. // Constructor.
  58. timer_queue()
  59. : timers_(),
  60. heap_()
  61. {
  62. }
  63. // Add a new timer to the queue. Returns true if this is the timer that is
  64. // earliest in the queue, in which case the reactor's event demultiplexing
  65. // function call may need to be interrupted and restarted.
  66. bool enqueue_timer(const time_type& time, per_timer_data& timer, wait_op* op)
  67. {
  68. // Enqueue the timer object.
  69. if (timer.prev_ == 0 && &timer != timers_)
  70. {
  71. if (this->is_positive_infinity(time))
  72. {
  73. // No heap entry is required for timers that never expire.
  74. timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
  75. }
  76. else
  77. {
  78. // Put the new timer at the correct position in the heap. This is done
  79. // first since push_back() can throw due to allocation failure.
  80. timer.heap_index_ = heap_.size();
  81. heap_entry entry = { time, &timer };
  82. heap_.push_back(entry);
  83. up_heap(heap_.size() - 1);
  84. }
  85. // Insert the new timer into the linked list of active timers.
  86. timer.next_ = timers_;
  87. timer.prev_ = 0;
  88. if (timers_)
  89. timers_->prev_ = &timer;
  90. timers_ = &timer;
  91. }
  92. // Enqueue the individual timer operation.
  93. timer.op_queue_.push(op);
  94. // Interrupt reactor only if newly added timer is first to expire.
  95. return timer.heap_index_ == 0 && timer.op_queue_.front() == op;
  96. }
  97. // Whether there are no timers in the queue.
  98. virtual bool empty() const
  99. {
  100. return timers_ == 0;
  101. }
  102. // Get the time for the timer that is earliest in the queue.
  103. virtual long wait_duration_msec(long max_duration) const
  104. {
  105. if (heap_.empty())
  106. return max_duration;
  107. return this->to_msec(
  108. Time_Traits::to_posix_duration(
  109. Time_Traits::subtract(heap_[0].time_, Time_Traits::now())),
  110. max_duration);
  111. }
  112. // Get the time for the timer that is earliest in the queue.
  113. virtual long wait_duration_usec(long max_duration) const
  114. {
  115. if (heap_.empty())
  116. return max_duration;
  117. return this->to_usec(
  118. Time_Traits::to_posix_duration(
  119. Time_Traits::subtract(heap_[0].time_, Time_Traits::now())),
  120. max_duration);
  121. }
  122. // Dequeue all timers not later than the current time.
  123. virtual void get_ready_timers(op_queue<operation>& ops)
  124. {
  125. if (!heap_.empty())
  126. {
  127. const time_type now = Time_Traits::now();
  128. while (!heap_.empty() && !Time_Traits::less_than(now, heap_[0].time_))
  129. {
  130. per_timer_data* timer = heap_[0].timer_;
  131. ops.push(timer->op_queue_);
  132. remove_timer(*timer);
  133. }
  134. }
  135. }
  136. // Dequeue all timers.
  137. virtual void get_all_timers(op_queue<operation>& ops)
  138. {
  139. while (timers_)
  140. {
  141. per_timer_data* timer = timers_;
  142. timers_ = timers_->next_;
  143. ops.push(timer->op_queue_);
  144. timer->next_ = 0;
  145. timer->prev_ = 0;
  146. }
  147. heap_.clear();
  148. }
  149. // Cancel and dequeue operations for the given timer.
  150. std::size_t cancel_timer(per_timer_data& timer, op_queue<operation>& ops,
  151. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)())
  152. {
  153. std::size_t num_cancelled = 0;
  154. if (timer.prev_ != 0 || &timer == timers_)
  155. {
  156. while (wait_op* op = (num_cancelled != max_cancelled)
  157. ? timer.op_queue_.front() : 0)
  158. {
  159. op->ec_ = boost::asio::error::operation_aborted;
  160. timer.op_queue_.pop();
  161. ops.push(op);
  162. ++num_cancelled;
  163. }
  164. if (timer.op_queue_.empty())
  165. remove_timer(timer);
  166. }
  167. return num_cancelled;
  168. }
  169. // Move operations from one timer to another, empty timer.
  170. void move_timer(per_timer_data& target, per_timer_data& source)
  171. {
  172. target.op_queue_.push(source.op_queue_);
  173. target.heap_index_ = source.heap_index_;
  174. source.heap_index_ = (std::numeric_limits<std::size_t>::max)();
  175. if (target.heap_index_ < heap_.size())
  176. heap_[target.heap_index_].timer_ = &target;
  177. if (timers_ == &source)
  178. timers_ = &target;
  179. if (source.prev_)
  180. source.prev_->next_ = &target;
  181. if (source.next_)
  182. source.next_->prev_= &target;
  183. target.next_ = source.next_;
  184. target.prev_ = source.prev_;
  185. source.next_ = 0;
  186. source.prev_ = 0;
  187. }
  188. private:
  189. // Move the item at the given index up the heap to its correct position.
  190. void up_heap(std::size_t index)
  191. {
  192. while (index > 0)
  193. {
  194. std::size_t parent = (index - 1) / 2;
  195. if (!Time_Traits::less_than(heap_[index].time_, heap_[parent].time_))
  196. break;
  197. swap_heap(index, parent);
  198. index = parent;
  199. }
  200. }
  201. // Move the item at the given index down the heap to its correct position.
  202. void down_heap(std::size_t index)
  203. {
  204. std::size_t child = index * 2 + 1;
  205. while (child < heap_.size())
  206. {
  207. std::size_t min_child = (child + 1 == heap_.size()
  208. || Time_Traits::less_than(
  209. heap_[child].time_, heap_[child + 1].time_))
  210. ? child : child + 1;
  211. if (Time_Traits::less_than(heap_[index].time_, heap_[min_child].time_))
  212. break;
  213. swap_heap(index, min_child);
  214. index = min_child;
  215. child = index * 2 + 1;
  216. }
  217. }
  218. // Swap two entries in the heap.
  219. void swap_heap(std::size_t index1, std::size_t index2)
  220. {
  221. heap_entry tmp = heap_[index1];
  222. heap_[index1] = heap_[index2];
  223. heap_[index2] = tmp;
  224. heap_[index1].timer_->heap_index_ = index1;
  225. heap_[index2].timer_->heap_index_ = index2;
  226. }
  227. // Remove a timer from the heap and list of timers.
  228. void remove_timer(per_timer_data& timer)
  229. {
  230. // Remove the timer from the heap.
  231. std::size_t index = timer.heap_index_;
  232. if (!heap_.empty() && index < heap_.size())
  233. {
  234. if (index == heap_.size() - 1)
  235. {
  236. timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
  237. heap_.pop_back();
  238. }
  239. else
  240. {
  241. swap_heap(index, heap_.size() - 1);
  242. timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
  243. heap_.pop_back();
  244. if (index > 0 && Time_Traits::less_than(
  245. heap_[index].time_, heap_[(index - 1) / 2].time_))
  246. up_heap(index);
  247. else
  248. down_heap(index);
  249. }
  250. }
  251. // Remove the timer from the linked list of active timers.
  252. if (timers_ == &timer)
  253. timers_ = timer.next_;
  254. if (timer.prev_)
  255. timer.prev_->next_ = timer.next_;
  256. if (timer.next_)
  257. timer.next_->prev_= timer.prev_;
  258. timer.next_ = 0;
  259. timer.prev_ = 0;
  260. }
  261. // Determine if the specified absolute time is positive infinity.
  262. template <typename Time_Type>
  263. static bool is_positive_infinity(const Time_Type&)
  264. {
  265. return false;
  266. }
  267. // Determine if the specified absolute time is positive infinity.
  268. template <typename T, typename TimeSystem>
  269. static bool is_positive_infinity(
  270. const boost::date_time::base_time<T, TimeSystem>& time)
  271. {
  272. return time.is_pos_infinity();
  273. }
  274. // Helper function to convert a duration into milliseconds.
  275. template <typename Duration>
  276. long to_msec(const Duration& d, long max_duration) const
  277. {
  278. if (d.ticks() <= 0)
  279. return 0;
  280. int64_t msec = d.total_milliseconds();
  281. if (msec == 0)
  282. return 1;
  283. if (msec > max_duration)
  284. return max_duration;
  285. return static_cast<long>(msec);
  286. }
  287. // Helper function to convert a duration into microseconds.
  288. template <typename Duration>
  289. long to_usec(const Duration& d, long max_duration) const
  290. {
  291. if (d.ticks() <= 0)
  292. return 0;
  293. int64_t usec = d.total_microseconds();
  294. if (usec == 0)
  295. return 1;
  296. if (usec > max_duration)
  297. return max_duration;
  298. return static_cast<long>(usec);
  299. }
  300. // The head of a linked list of all active timers.
  301. per_timer_data* timers_;
  302. struct heap_entry
  303. {
  304. // The time when the timer should fire.
  305. time_type time_;
  306. // The associated timer with enqueued operations.
  307. per_timer_data* timer_;
  308. };
  309. // The heap of timers, with the earliest timer at the front.
  310. std::vector<heap_entry> heap_;
  311. };
  312. } // namespace detail
  313. } // namespace asio
  314. } // namespace boost
  315. #include <boost/asio/detail/pop_options.hpp>
  316. #endif // BOOST_ASIO_DETAIL_TIMER_QUEUE_HPP