11 #ifndef ASIO_DETAIL_TIMER_QUEUE_HPP 12 #define ASIO_DETAIL_TIMER_QUEUE_HPP 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 18 #include "asio/detail/config.hpp" 21 #include "asio/detail/cstdint.hpp" 22 #include "asio/detail/date_time_fwd.hpp" 23 #include "asio/detail/limits.hpp" 24 #include "asio/detail/op_queue.hpp" 25 #include "asio/detail/timer_queue_base.hpp" 26 #include "asio/detail/wait_op.hpp" 27 #include "asio/error.hpp" 29 #include "asio/detail/push_options.hpp" 34 template <
typename Time_Traits>
40 typedef typename Time_Traits::time_type time_type;
43 typedef typename Time_Traits::duration_type duration_type;
50 heap_index_((std::numeric_limits<std::size_t>::max)()),
62 std::size_t heap_index_;
82 if (timer.prev_ == 0 && &timer != timers_)
84 if (this->is_positive_infinity(time))
87 timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
93 timer.heap_index_ = heap_.size();
94 heap_entry entry = { time, &timer };
95 heap_.push_back(entry);
96 up_heap(heap_.size() - 1);
100 timer.next_ = timers_;
103 timers_->prev_ = &timer;
108 timer.op_queue_.push(op);
111 return timer.heap_index_ == 0 && timer.op_queue_.front() == op;
115 virtual bool empty()
const 121 virtual long wait_duration_msec(
long max_duration)
const 126 return this->to_msec(
127 Time_Traits::to_posix_duration(
128 Time_Traits::subtract(heap_[0].time_, Time_Traits::now())),
133 virtual long wait_duration_usec(
long max_duration)
const 138 return this->to_usec(
139 Time_Traits::to_posix_duration(
140 Time_Traits::subtract(heap_[0].time_, Time_Traits::now())),
149 const time_type now = Time_Traits::now();
150 while (!heap_.empty() && !Time_Traits::less_than(now, heap_[0].time_))
153 ops.push(timer->op_queue_);
154 remove_timer(*timer);
165 timers_ = timers_->next_;
166 ops.push(timer->op_queue_);
176 std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)())
178 std::size_t num_cancelled = 0;
179 if (timer.prev_ != 0 || &timer == timers_)
181 while (
wait_op* op = (num_cancelled != max_cancelled)
182 ? timer.op_queue_.front() : 0)
184 op->ec_ = asio::error::operation_aborted;
185 timer.op_queue_.pop();
189 if (timer.op_queue_.empty())
192 return num_cancelled;
198 target.op_queue_.push(source.op_queue_);
200 target.heap_index_ = source.heap_index_;
201 source.heap_index_ = (std::numeric_limits<std::size_t>::max)();
203 if (target.heap_index_ < heap_.size())
204 heap_[target.heap_index_].timer_ = ⌖
206 if (timers_ == &source)
209 source.prev_->next_ = ⌖
211 source.next_->prev_= ⌖
212 target.next_ = source.next_;
213 target.prev_ = source.prev_;
220 void up_heap(std::size_t index)
224 std::size_t parent = (index - 1) / 2;
225 if (!Time_Traits::less_than(heap_[index].time_, heap_[parent].time_))
227 swap_heap(index, parent);
233 void down_heap(std::size_t index)
235 std::size_t child = index * 2 + 1;
236 while (child < heap_.size())
238 std::size_t min_child = (child + 1 == heap_.size()
239 || Time_Traits::less_than(
240 heap_[child].time_, heap_[child + 1].time_))
242 if (Time_Traits::less_than(heap_[index].time_, heap_[min_child].time_))
244 swap_heap(index, min_child);
246 child = index * 2 + 1;
251 void swap_heap(std::size_t index1, std::size_t index2)
253 heap_entry tmp = heap_[index1];
254 heap_[index1] = heap_[index2];
256 heap_[index1].timer_->heap_index_ = index1;
257 heap_[index2].timer_->heap_index_ = index2;
264 std::size_t index = timer.heap_index_;
265 if (!heap_.empty() && index < heap_.size())
267 if (index == heap_.size() - 1)
269 timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
274 swap_heap(index, heap_.size() - 1);
275 timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
277 if (index > 0 && Time_Traits::less_than(
278 heap_[index].time_, heap_[(index - 1) / 2].time_))
286 if (timers_ == &timer)
287 timers_ = timer.next_;
289 timer.prev_->next_ = timer.next_;
291 timer.next_->prev_= timer.prev_;
297 template <
typename Time_Type>
298 static bool is_positive_infinity(
const Time_Type&)
304 template <
typename T,
typename TimeSystem>
305 static bool is_positive_infinity(
308 return time.is_pos_infinity();
312 template <
typename Duration>
313 long to_msec(
const Duration& d,
long max_duration)
const 317 int64_t msec = d.total_milliseconds();
320 if (msec > max_duration)
322 return static_cast<long>(msec);
326 template <
typename Duration>
327 long to_usec(
const Duration& d,
long max_duration)
const 331 int64_t usec = d.total_microseconds();
334 if (usec > max_duration)
336 return static_cast<long>(usec);
352 std::vector<heap_entry> heap_;
358 #include "asio/detail/pop_options.hpp" 360 #endif // ASIO_DETAIL_TIMER_QUEUE_HPP Definition: date_time_fwd.hpp:24
Definition: op_queue.hpp:26
Definition: timer_queue.hpp:35
Definition: timer_queue_base.hpp:28
Definition: wait_op.hpp:26
Definition: timer_queue.hpp:46
Definition: any_io_executor.hpp:28