| 1 | //
|
|---|
| 2 | // timer_queue.hpp
|
|---|
| 3 | // ~~~~~~~~~~~~~~~
|
|---|
| 4 | //
|
|---|
| 5 | // Copyright (c) 2003-2008 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 |
|
|---|
| 11 | #ifndef BOOST_ASIO_DETAIL_TIMER_QUEUE_HPP
|
|---|
| 12 | #define BOOST_ASIO_DETAIL_TIMER_QUEUE_HPP
|
|---|
| 13 |
|
|---|
| 14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
|---|
| 15 | # pragma once
|
|---|
| 16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
|---|
| 17 |
|
|---|
| 18 | #include <boost/asio/detail/push_options.hpp>
|
|---|
| 19 |
|
|---|
| 20 | #include <boost/asio/detail/push_options.hpp>
|
|---|
| 21 | #include <cstddef>
|
|---|
| 22 | #include <functional>
|
|---|
| 23 | #include <limits>
|
|---|
| 24 | #include <memory>
|
|---|
| 25 | #include <vector>
|
|---|
| 26 | #include <boost/config.hpp>
|
|---|
| 27 | #include <boost/asio/detail/pop_options.hpp>
|
|---|
| 28 |
|
|---|
| 29 | #include <boost/asio/error.hpp>
|
|---|
| 30 | #include <boost/asio/detail/handler_alloc_helpers.hpp>
|
|---|
| 31 | #include <boost/asio/detail/hash_map.hpp>
|
|---|
| 32 | #include <boost/asio/detail/noncopyable.hpp>
|
|---|
| 33 | #include <boost/asio/detail/timer_queue_base.hpp>
|
|---|
| 34 |
|
|---|
| 35 | namespace boost {
|
|---|
| 36 | namespace asio {
|
|---|
| 37 | namespace detail {
|
|---|
| 38 |
|
|---|
| 39 | template <typename Time_Traits>
|
|---|
| 40 | class timer_queue
|
|---|
| 41 | : public timer_queue_base
|
|---|
| 42 | {
|
|---|
| 43 | public:
|
|---|
| 44 | // The time type.
|
|---|
| 45 | typedef typename Time_Traits::time_type time_type;
|
|---|
| 46 |
|
|---|
| 47 | // The duration type.
|
|---|
| 48 | typedef typename Time_Traits::duration_type duration_type;
|
|---|
| 49 |
|
|---|
| 50 | // Constructor.
|
|---|
| 51 | timer_queue()
|
|---|
| 52 | : timers_(),
|
|---|
| 53 | heap_(),
|
|---|
| 54 | cancelled_timers_(0),
|
|---|
| 55 | complete_timers_(0)
|
|---|
| 56 | {
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | // Add a new timer to the queue. Returns true if this is the timer that is
|
|---|
| 60 | // earliest in the queue, in which case the reactor's event demultiplexing
|
|---|
| 61 | // function call may need to be interrupted and restarted.
|
|---|
| 62 | template <typename Handler>
|
|---|
| 63 | bool enqueue_timer(const time_type& time, Handler handler, void* token)
|
|---|
| 64 | {
|
|---|
| 65 | // Ensure that there is space for the timer in the heap. We reserve here so
|
|---|
| 66 | // that the push_back below will not throw due to a reallocation failure.
|
|---|
| 67 | heap_.reserve(heap_.size() + 1);
|
|---|
| 68 |
|
|---|
| 69 | // Create a new timer object.
|
|---|
| 70 | typedef timer<Handler> timer_type;
|
|---|
| 71 | typedef handler_alloc_traits<Handler, timer_type> alloc_traits;
|
|---|
| 72 | raw_handler_ptr<alloc_traits> raw_ptr(handler);
|
|---|
| 73 | handler_ptr<alloc_traits> new_timer(raw_ptr, time, handler, token);
|
|---|
| 74 |
|
|---|
| 75 | //std::auto_ptr<timer<Handler> > new_timer(
|
|---|
| 76 | // new timer<Handler>(time, handler, token));
|
|---|
| 77 |
|
|---|
| 78 | // Insert the new timer into the hash.
|
|---|
| 79 | typedef typename hash_map<void*, timer_base*>::iterator iterator;
|
|---|
| 80 | typedef typename hash_map<void*, timer_base*>::value_type value_type;
|
|---|
| 81 | std::pair<iterator, bool> result =
|
|---|
| 82 | timers_.insert(value_type(token, new_timer.get()));
|
|---|
| 83 | if (!result.second)
|
|---|
| 84 | {
|
|---|
| 85 | result.first->second->prev_ = new_timer.get();
|
|---|
| 86 | new_timer.get()->next_ = result.first->second;
|
|---|
| 87 | result.first->second = new_timer.get();
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | // Put the timer at the correct position in the heap.
|
|---|
| 91 | new_timer.get()->heap_index_ = heap_.size();
|
|---|
| 92 | heap_.push_back(new_timer.get());
|
|---|
| 93 | up_heap(heap_.size() - 1);
|
|---|
| 94 | bool is_first = (heap_[0] == new_timer.get());
|
|---|
| 95 |
|
|---|
| 96 | // Ownership of the timer is transferred to the timer queue.
|
|---|
| 97 | new_timer.release();
|
|---|
| 98 |
|
|---|
| 99 | return is_first;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | // Whether there are no timers in the queue.
|
|---|
| 103 | virtual bool empty() const
|
|---|
| 104 | {
|
|---|
| 105 | return heap_.empty();
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | // Get the time for the timer that is earliest in the queue.
|
|---|
| 109 | virtual boost::posix_time::time_duration wait_duration() const
|
|---|
| 110 | {
|
|---|
| 111 | if (heap_.empty())
|
|---|
| 112 | return boost::posix_time::pos_infin;
|
|---|
| 113 | return Time_Traits::to_posix_duration(
|
|---|
| 114 | Time_Traits::subtract(heap_[0]->time_, Time_Traits::now()));
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | // Dispatch the timers that are earlier than the specified time.
|
|---|
| 118 | virtual void dispatch_timers()
|
|---|
| 119 | {
|
|---|
| 120 | const time_type now = Time_Traits::now();
|
|---|
| 121 | while (!heap_.empty() && !Time_Traits::less_than(now, heap_[0]->time_))
|
|---|
| 122 | {
|
|---|
| 123 | timer_base* t = heap_[0];
|
|---|
| 124 | remove_timer(t);
|
|---|
| 125 | t->result_ = boost::system::error_code();
|
|---|
| 126 | t->prev_ = 0;
|
|---|
| 127 | t->next_ = complete_timers_;
|
|---|
| 128 | complete_timers_ = t;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | // Cancel the timers with the given token. Any timers pending for the token
|
|---|
| 133 | // will be notified that they have been cancelled next time
|
|---|
| 134 | // dispatch_cancellations is called. Returns the number of timers that were
|
|---|
| 135 | // cancelled.
|
|---|
| 136 | std::size_t cancel_timer(void* timer_token)
|
|---|
| 137 | {
|
|---|
| 138 | std::size_t num_cancelled = 0;
|
|---|
| 139 | typedef typename hash_map<void*, timer_base*>::iterator iterator;
|
|---|
| 140 | iterator it = timers_.find(timer_token);
|
|---|
| 141 | if (it != timers_.end())
|
|---|
| 142 | {
|
|---|
| 143 | timer_base* t = it->second;
|
|---|
| 144 | while (t)
|
|---|
| 145 | {
|
|---|
| 146 | timer_base* next = t->next_;
|
|---|
| 147 | remove_timer(t);
|
|---|
| 148 | t->prev_ = 0;
|
|---|
| 149 | t->next_ = cancelled_timers_;
|
|---|
| 150 | cancelled_timers_ = t;
|
|---|
| 151 | t = next;
|
|---|
| 152 | ++num_cancelled;
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 | return num_cancelled;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | // Dispatch any pending cancels for timers.
|
|---|
| 159 | virtual void dispatch_cancellations()
|
|---|
| 160 | {
|
|---|
| 161 | while (cancelled_timers_)
|
|---|
| 162 | {
|
|---|
| 163 | timer_base* this_timer = cancelled_timers_;
|
|---|
| 164 | this_timer->result_ = boost::asio::error::operation_aborted;
|
|---|
| 165 | cancelled_timers_ = this_timer->next_;
|
|---|
| 166 | this_timer->next_ = complete_timers_;
|
|---|
| 167 | complete_timers_ = this_timer;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | // Complete any timers that are waiting to be completed.
|
|---|
| 172 | virtual void complete_timers()
|
|---|
| 173 | {
|
|---|
| 174 | while (complete_timers_)
|
|---|
| 175 | {
|
|---|
| 176 | timer_base* this_timer = complete_timers_;
|
|---|
| 177 | complete_timers_ = this_timer->next_;
|
|---|
| 178 | this_timer->next_ = 0;
|
|---|
| 179 | this_timer->complete();
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | // Destroy all timers.
|
|---|
| 184 | virtual void destroy_timers()
|
|---|
| 185 | {
|
|---|
| 186 | typename hash_map<void*, timer_base*>::iterator i = timers_.begin();
|
|---|
| 187 | typename hash_map<void*, timer_base*>::iterator end = timers_.end();
|
|---|
| 188 | while (i != end)
|
|---|
| 189 | {
|
|---|
| 190 | timer_base* t = i->second;
|
|---|
| 191 | typename hash_map<void*, timer_base*>::iterator old_i = i++;
|
|---|
| 192 | timers_.erase(old_i);
|
|---|
| 193 | destroy_timer_list(t);
|
|---|
| 194 | }
|
|---|
| 195 | heap_.clear();
|
|---|
| 196 | timers_.clear();
|
|---|
| 197 | destroy_timer_list(cancelled_timers_);
|
|---|
| 198 | destroy_timer_list(complete_timers_);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | private:
|
|---|
| 202 | // Base class for timer operations. Function pointers are used instead of
|
|---|
| 203 | // virtual functions to avoid the associated overhead.
|
|---|
| 204 | class timer_base
|
|---|
| 205 | {
|
|---|
| 206 | public:
|
|---|
| 207 | // Delete the timer and post the handler.
|
|---|
| 208 | void complete()
|
|---|
| 209 | {
|
|---|
| 210 | complete_func_(this, result_);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | // Delete the timer.
|
|---|
| 214 | void destroy()
|
|---|
| 215 | {
|
|---|
| 216 | destroy_func_(this);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | protected:
|
|---|
| 220 | typedef void (*complete_func_type)(timer_base*,
|
|---|
| 221 | const boost::system::error_code&);
|
|---|
| 222 | typedef void (*destroy_func_type)(timer_base*);
|
|---|
| 223 |
|
|---|
| 224 | // Constructor.
|
|---|
| 225 | timer_base(complete_func_type complete_func, destroy_func_type destroy_func,
|
|---|
| 226 | const time_type& time, void* token)
|
|---|
| 227 | : complete_func_(complete_func),
|
|---|
| 228 | destroy_func_(destroy_func),
|
|---|
| 229 | time_(time),
|
|---|
| 230 | token_(token),
|
|---|
| 231 | next_(0),
|
|---|
| 232 | prev_(0),
|
|---|
| 233 | heap_index_(
|
|---|
| 234 | std::numeric_limits<size_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION())
|
|---|
| 235 | {
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | // Prevent deletion through this type.
|
|---|
| 239 | ~timer_base()
|
|---|
| 240 | {
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | private:
|
|---|
| 244 | friend class timer_queue<Time_Traits>;
|
|---|
| 245 |
|
|---|
| 246 | // The function to be called to delete the timer and post the handler.
|
|---|
| 247 | complete_func_type complete_func_;
|
|---|
| 248 |
|
|---|
| 249 | // The function to be called to delete the timer.
|
|---|
| 250 | destroy_func_type destroy_func_;
|
|---|
| 251 |
|
|---|
| 252 | // The result of the timer operation.
|
|---|
| 253 | boost::system::error_code result_;
|
|---|
| 254 |
|
|---|
| 255 | // The time when the timer should fire.
|
|---|
| 256 | time_type time_;
|
|---|
| 257 |
|
|---|
| 258 | // The token associated with the timer.
|
|---|
| 259 | void* token_;
|
|---|
| 260 |
|
|---|
| 261 | // The next timer known to the queue.
|
|---|
| 262 | timer_base* next_;
|
|---|
| 263 |
|
|---|
| 264 | // The previous timer known to the queue.
|
|---|
| 265 | timer_base* prev_;
|
|---|
| 266 |
|
|---|
| 267 | // The index of the timer in the heap.
|
|---|
| 268 | size_t heap_index_;
|
|---|
| 269 | };
|
|---|
| 270 |
|
|---|
| 271 | // Adaptor class template for using handlers in timers.
|
|---|
| 272 | template <typename Handler>
|
|---|
| 273 | class timer
|
|---|
| 274 | : public timer_base
|
|---|
| 275 | {
|
|---|
| 276 | public:
|
|---|
| 277 | // Constructor.
|
|---|
| 278 | timer(const time_type& time, Handler handler, void* token)
|
|---|
| 279 | : timer_base(&timer<Handler>::complete_handler,
|
|---|
| 280 | &timer<Handler>::destroy_handler, time, token),
|
|---|
| 281 | handler_(handler)
|
|---|
| 282 | {
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | // Delete the timer and post the handler.
|
|---|
| 286 | static void complete_handler(timer_base* base,
|
|---|
| 287 | const boost::system::error_code& result)
|
|---|
| 288 | {
|
|---|
| 289 | // Take ownership of the timer object.
|
|---|
| 290 | typedef timer<Handler> this_type;
|
|---|
| 291 | this_type* this_timer(static_cast<this_type*>(base));
|
|---|
| 292 | typedef handler_alloc_traits<Handler, this_type> alloc_traits;
|
|---|
| 293 | handler_ptr<alloc_traits> ptr(this_timer->handler_, this_timer);
|
|---|
| 294 |
|
|---|
| 295 | // Make a copy of the error_code and the handler so that the memory can
|
|---|
| 296 | // be deallocated before the upcall is made.
|
|---|
| 297 | boost::system::error_code ec(result);
|
|---|
| 298 | Handler handler(this_timer->handler_);
|
|---|
| 299 |
|
|---|
| 300 | // Free the memory associated with the handler.
|
|---|
| 301 | ptr.reset();
|
|---|
| 302 |
|
|---|
| 303 | // Make the upcall.
|
|---|
| 304 | handler(ec);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | // Delete the timer.
|
|---|
| 308 | static void destroy_handler(timer_base* base)
|
|---|
| 309 | {
|
|---|
| 310 | // Take ownership of the timer object.
|
|---|
| 311 | typedef timer<Handler> this_type;
|
|---|
| 312 | this_type* this_timer(static_cast<this_type*>(base));
|
|---|
| 313 | typedef handler_alloc_traits<Handler, this_type> alloc_traits;
|
|---|
| 314 | handler_ptr<alloc_traits> ptr(this_timer->handler_, this_timer);
|
|---|
| 315 |
|
|---|
| 316 | // A sub-object of the handler may be the true owner of the memory
|
|---|
| 317 | // associated with the handler. Consequently, a local copy of the handler
|
|---|
| 318 | // is required to ensure that any owning sub-object remains valid until
|
|---|
| 319 | // after we have deallocated the memory here.
|
|---|
| 320 | Handler handler(this_timer->handler_);
|
|---|
| 321 | (void)handler;
|
|---|
| 322 |
|
|---|
| 323 | // Free the memory associated with the handler.
|
|---|
| 324 | ptr.reset();
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | private:
|
|---|
| 328 | Handler handler_;
|
|---|
| 329 | };
|
|---|
| 330 |
|
|---|
| 331 | // Move the item at the given index up the heap to its correct position.
|
|---|
| 332 | void up_heap(size_t index)
|
|---|
| 333 | {
|
|---|
| 334 | size_t parent = (index - 1) / 2;
|
|---|
| 335 | while (index > 0
|
|---|
| 336 | && Time_Traits::less_than(heap_[index]->time_, heap_[parent]->time_))
|
|---|
| 337 | {
|
|---|
| 338 | swap_heap(index, parent);
|
|---|
| 339 | index = parent;
|
|---|
| 340 | parent = (index - 1) / 2;
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | // Move the item at the given index down the heap to its correct position.
|
|---|
| 345 | void down_heap(size_t index)
|
|---|
| 346 | {
|
|---|
| 347 | size_t child = index * 2 + 1;
|
|---|
| 348 | while (child < heap_.size())
|
|---|
| 349 | {
|
|---|
| 350 | size_t min_child = (child + 1 == heap_.size()
|
|---|
| 351 | || Time_Traits::less_than(
|
|---|
| 352 | heap_[child]->time_, heap_[child + 1]->time_))
|
|---|
| 353 | ? child : child + 1;
|
|---|
| 354 | if (Time_Traits::less_than(heap_[index]->time_, heap_[min_child]->time_))
|
|---|
| 355 | break;
|
|---|
| 356 | swap_heap(index, min_child);
|
|---|
| 357 | index = min_child;
|
|---|
| 358 | child = index * 2 + 1;
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | // Swap two entries in the heap.
|
|---|
| 363 | void swap_heap(size_t index1, size_t index2)
|
|---|
| 364 | {
|
|---|
| 365 | timer_base* tmp = heap_[index1];
|
|---|
| 366 | heap_[index1] = heap_[index2];
|
|---|
| 367 | heap_[index2] = tmp;
|
|---|
| 368 | heap_[index1]->heap_index_ = index1;
|
|---|
| 369 | heap_[index2]->heap_index_ = index2;
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | // Remove a timer from the heap and list of timers.
|
|---|
| 373 | void remove_timer(timer_base* t)
|
|---|
| 374 | {
|
|---|
| 375 | // Remove the timer from the heap.
|
|---|
| 376 | size_t index = t->heap_index_;
|
|---|
| 377 | if (!heap_.empty() && index < heap_.size())
|
|---|
| 378 | {
|
|---|
| 379 | if (index == heap_.size() - 1)
|
|---|
| 380 | {
|
|---|
| 381 | heap_.pop_back();
|
|---|
| 382 | }
|
|---|
| 383 | else
|
|---|
| 384 | {
|
|---|
| 385 | swap_heap(index, heap_.size() - 1);
|
|---|
| 386 | heap_.pop_back();
|
|---|
| 387 | size_t parent = (index - 1) / 2;
|
|---|
| 388 | if (index > 0 && Time_Traits::less_than(
|
|---|
| 389 | heap_[index]->time_, heap_[parent]->time_))
|
|---|
| 390 | up_heap(index);
|
|---|
| 391 | else
|
|---|
| 392 | down_heap(index);
|
|---|
| 393 | }
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | // Remove the timer from the hash.
|
|---|
| 397 | typedef typename hash_map<void*, timer_base*>::iterator iterator;
|
|---|
| 398 | iterator it = timers_.find(t->token_);
|
|---|
| 399 | if (it != timers_.end())
|
|---|
| 400 | {
|
|---|
| 401 | if (it->second == t)
|
|---|
| 402 | it->second = t->next_;
|
|---|
| 403 | if (t->prev_)
|
|---|
| 404 | t->prev_->next_ = t->next_;
|
|---|
| 405 | if (t->next_)
|
|---|
| 406 | t->next_->prev_ = t->prev_;
|
|---|
| 407 | if (it->second == 0)
|
|---|
| 408 | timers_.erase(it);
|
|---|
| 409 | }
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | // Destroy all timers in a linked list.
|
|---|
| 413 | void destroy_timer_list(timer_base*& t)
|
|---|
| 414 | {
|
|---|
| 415 | while (t)
|
|---|
| 416 | {
|
|---|
| 417 | timer_base* next = t->next_;
|
|---|
| 418 | t->next_ = 0;
|
|---|
| 419 | t->destroy();
|
|---|
| 420 | t = next;
|
|---|
| 421 | }
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | // A hash of timer token to linked lists of timers.
|
|---|
| 425 | hash_map<void*, timer_base*> timers_;
|
|---|
| 426 |
|
|---|
| 427 | // The heap of timers, with the earliest timer at the front.
|
|---|
| 428 | std::vector<timer_base*> heap_;
|
|---|
| 429 |
|
|---|
| 430 | // The list of timers to be cancelled.
|
|---|
| 431 | timer_base* cancelled_timers_;
|
|---|
| 432 |
|
|---|
| 433 | // The list of timers waiting to be completed.
|
|---|
| 434 | timer_base* complete_timers_;
|
|---|
| 435 | };
|
|---|
| 436 |
|
|---|
| 437 | } // namespace detail
|
|---|
| 438 | } // namespace asio
|
|---|
| 439 | } // namespace boost
|
|---|
| 440 |
|
|---|
| 441 | #include <boost/asio/detail/pop_options.hpp>
|
|---|
| 442 |
|
|---|
| 443 | #endif // BOOST_ASIO_DETAIL_TIMER_QUEUE_HPP
|
|---|