Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer_queue.hpp 9.5KB

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