2 // detail/timer_queue.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_TIMER_QUEUE_HPP
12 #define BOOST_ASIO_DETAIL_TIMER_QUEUE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
21 #include <boost/asio/detail/cstdint.hpp>
22 #include <boost/asio/detail/date_time_fwd.hpp>
23 #include <boost/asio/detail/limits.hpp>
24 #include <boost/asio/detail/op_queue.hpp>
25 #include <boost/asio/detail/timer_queue_base.hpp>
26 #include <boost/asio/detail/wait_op.hpp>
27 #include <boost/asio/error.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 template <typename Time_Traits>
37 : public timer_queue_base
41 typedef typename Time_Traits::time_type time_type;
44 typedef typename Time_Traits::duration_type duration_type;
51 heap_index_((std::numeric_limits<std::size_t>::max)()),
57 friend class timer_queue;
59 // The operations waiting on the timer.
60 op_queue<wait_op> op_queue_;
62 // The index of the timer in the heap.
63 std::size_t heap_index_;
65 // Pointers to adjacent timers in a linked list.
66 per_timer_data* next_;
67 per_timer_data* prev_;
77 // Add a new timer to the queue. Returns true if this is the timer that is
78 // earliest in the queue, in which case the reactor's event demultiplexing
79 // function call may need to be interrupted and restarted.
80 bool enqueue_timer(const time_type& time, per_timer_data& timer, wait_op* op)
82 // Enqueue the timer object.
83 if (timer.prev_ == 0 && &timer != timers_)
85 if (this->is_positive_infinity(time))
87 // No heap entry is required for timers that never expire.
88 timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
92 // Put the new timer at the correct position in the heap. This is done
93 // first since push_back() can throw due to allocation failure.
94 timer.heap_index_ = heap_.size();
95 heap_entry entry = { time, &timer };
96 heap_.push_back(entry);
97 up_heap(heap_.size() - 1);
100 // Insert the new timer into the linked list of active timers.
101 timer.next_ = timers_;
104 timers_->prev_ = &timer;
108 // Enqueue the individual timer operation.
109 timer.op_queue_.push(op);
111 // Interrupt reactor only if newly added timer is first to expire.
112 return timer.heap_index_ == 0 && timer.op_queue_.front() == op;
115 // Whether there are no timers in the queue.
116 virtual bool empty() const
121 // Get the time for the timer that is earliest in the queue.
122 virtual long wait_duration_msec(long max_duration) const
127 return this->to_msec(
128 Time_Traits::to_posix_duration(
129 Time_Traits::subtract(heap_[0].time_, Time_Traits::now())),
133 // Get the time for the timer that is earliest in the queue.
134 virtual long wait_duration_usec(long max_duration) const
139 return this->to_usec(
140 Time_Traits::to_posix_duration(
141 Time_Traits::subtract(heap_[0].time_, Time_Traits::now())),
145 // Dequeue all timers not later than the current time.
146 virtual void get_ready_timers(op_queue<operation>& ops)
150 const time_type now = Time_Traits::now();
151 while (!heap_.empty() && !Time_Traits::less_than(now, heap_[0].time_))
153 per_timer_data* timer = heap_[0].timer_;
154 ops.push(timer->op_queue_);
155 remove_timer(*timer);
160 // Dequeue all timers.
161 virtual void get_all_timers(op_queue<operation>& ops)
165 per_timer_data* timer = timers_;
166 timers_ = timers_->next_;
167 ops.push(timer->op_queue_);
175 // Cancel and dequeue operations for the given timer.
176 std::size_t cancel_timer(per_timer_data& timer, op_queue<operation>& ops,
177 std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)())
179 std::size_t num_cancelled = 0;
180 if (timer.prev_ != 0 || &timer == timers_)
182 while (wait_op* op = (num_cancelled != max_cancelled)
183 ? timer.op_queue_.front() : 0)
185 op->ec_ = boost::asio::error::operation_aborted;
186 timer.op_queue_.pop();
190 if (timer.op_queue_.empty())
193 return num_cancelled;
196 // Move operations from one timer to another, empty timer.
197 void move_timer(per_timer_data& target, per_timer_data& source)
199 target.op_queue_.push(source.op_queue_);
201 target.heap_index_ = source.heap_index_;
202 source.heap_index_ = (std::numeric_limits<std::size_t>::max)();
204 if (target.heap_index_ < heap_.size())
205 heap_[target.heap_index_].timer_ = ⌖
207 if (timers_ == &source)
210 source.prev_->next_ = ⌖
212 source.next_->prev_= ⌖
213 target.next_ = source.next_;
214 target.prev_ = source.prev_;
220 // Move the item at the given index up the heap to its correct position.
221 void up_heap(std::size_t index)
225 std::size_t parent = (index - 1) / 2;
226 if (!Time_Traits::less_than(heap_[index].time_, heap_[parent].time_))
228 swap_heap(index, parent);
233 // Move the item at the given index down the heap to its correct position.
234 void down_heap(std::size_t index)
236 std::size_t child = index * 2 + 1;
237 while (child < heap_.size())
239 std::size_t min_child = (child + 1 == heap_.size()
240 || Time_Traits::less_than(
241 heap_[child].time_, heap_[child + 1].time_))
243 if (Time_Traits::less_than(heap_[index].time_, heap_[min_child].time_))
245 swap_heap(index, min_child);
247 child = index * 2 + 1;
251 // Swap two entries in the heap.
252 void swap_heap(std::size_t index1, std::size_t index2)
254 heap_entry tmp = heap_[index1];
255 heap_[index1] = heap_[index2];
257 heap_[index1].timer_->heap_index_ = index1;
258 heap_[index2].timer_->heap_index_ = index2;
261 // Remove a timer from the heap and list of timers.
262 void remove_timer(per_timer_data& timer)
264 // Remove the timer from the heap.
265 std::size_t index = timer.heap_index_;
266 if (!heap_.empty() && index < heap_.size())
268 if (index == heap_.size() - 1)
270 timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
275 swap_heap(index, heap_.size() - 1);
276 timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
278 if (index > 0 && Time_Traits::less_than(
279 heap_[index].time_, heap_[(index - 1) / 2].time_))
286 // Remove the timer from the linked list of active timers.
287 if (timers_ == &timer)
288 timers_ = timer.next_;
290 timer.prev_->next_ = timer.next_;
292 timer.next_->prev_= timer.prev_;
297 // Determine if the specified absolute time is positive infinity.
298 template <typename Time_Type>
299 static bool is_positive_infinity(const Time_Type&)
304 // Determine if the specified absolute time is positive infinity.
305 template <typename T, typename TimeSystem>
306 static bool is_positive_infinity(
307 const boost::date_time::base_time<T, TimeSystem>& time)
309 return time.is_pos_infinity();
312 // Helper function to convert a duration into milliseconds.
313 template <typename Duration>
314 long to_msec(const Duration& d, long max_duration) const
318 int64_t msec = d.total_milliseconds();
321 if (msec > max_duration)
323 return static_cast<long>(msec);
326 // Helper function to convert a duration into microseconds.
327 template <typename Duration>
328 long to_usec(const Duration& d, long max_duration) const
332 int64_t usec = d.total_microseconds();
335 if (usec > max_duration)
337 return static_cast<long>(usec);
340 // The head of a linked list of all active timers.
341 per_timer_data* timers_;
345 // The time when the timer should fire.
348 // The associated timer with enqueued operations.
349 per_timer_data* timer_;
352 // The heap of timers, with the earliest timer at the front.
353 std::vector<heap_entry> heap_;
356 } // namespace detail
360 #include <boost/asio/detail/pop_options.hpp>
362 #endif // BOOST_ASIO_DETAIL_TIMER_QUEUE_HPP