TimerQueue.h revision bfe73648
1bfe73648SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2bfe73648SShuo Chen// 3bfe73648SShuo Chen// Use of this source code is governed by a BSD-style license 4bfe73648SShuo Chen// that can be found in the License file. 5bfe73648SShuo Chen// 6bfe73648SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7bfe73648SShuo Chen 8bfe73648SShuo Chen#ifndef MUDUO_NET_TIMERQUEUE_H 9bfe73648SShuo Chen#define MUDUO_NET_TIMERQUEUE_H 10bfe73648SShuo Chen 11bfe73648SShuo Chen#include <set> 12bfe73648SShuo Chen#include <vector> 13bfe73648SShuo Chen 14bfe73648SShuo Chen#include <boost/noncopyable.hpp> 15bfe73648SShuo Chen 16bfe73648SShuo Chen#include "datetime/Timestamp.h" 17bfe73648SShuo Chen#include "thread/Mutex.h" 18bfe73648SShuo Chen#include "Callbacks.h" 19bfe73648SShuo Chen#include "Channel.h" 20bfe73648SShuo Chen 21bfe73648SShuo Chennamespace muduo 22bfe73648SShuo Chen{ 23bfe73648SShuo Chen 24bfe73648SShuo Chenclass EventLoop; 25bfe73648SShuo Chenclass Timer; 26bfe73648SShuo Chenclass TimerId; 27bfe73648SShuo Chen 28bfe73648SShuo Chen/// 29bfe73648SShuo Chen/// A best efforts timer queue. 30bfe73648SShuo Chen/// No guarantee that the callback will be on time. 31bfe73648SShuo Chen/// 32bfe73648SShuo Chenclass TimerQueue : boost::noncopyable 33bfe73648SShuo Chen{ 34bfe73648SShuo Chen public: 35bfe73648SShuo Chen TimerQueue(EventLoop* loop); 36bfe73648SShuo Chen ~TimerQueue(); 37bfe73648SShuo Chen 38bfe73648SShuo Chen /// 39bfe73648SShuo Chen /// Schedules the callback to be run at given time, 40bfe73648SShuo Chen /// repeats if @c interval > 0.0. 41bfe73648SShuo Chen /// 42bfe73648SShuo Chen /// Must be thread safe. Usually be called from other threads. 43bfe73648SShuo Chen TimerId addTimer(const TimerCallback& cb, 44bfe73648SShuo Chen Timestamp when, 45bfe73648SShuo Chen double interval); 46bfe73648SShuo Chen 47bfe73648SShuo Chen // void cancel(TimerId timerId); 48bfe73648SShuo Chen 49bfe73648SShuo Chen private: 50bfe73648SShuo Chen 51bfe73648SShuo Chen // FIXME: use unique_ptr<Timer> instead of raw pointers. 52bfe73648SShuo Chen typedef std::pair<Timestamp, Timer*> Entry; 53bfe73648SShuo Chen typedef std::set<Entry> TimerList; 54bfe73648SShuo Chen 55bfe73648SShuo Chen void scheduleInLoop(Timer* timer); 56bfe73648SShuo Chen // called when timerfd arms 57bfe73648SShuo Chen void handleRead(); 58bfe73648SShuo Chen // move out all expired timers 59bfe73648SShuo Chen std::vector<Entry> getExpired(Timestamp now); 60bfe73648SShuo Chen void reset(const std::vector<Entry>& expired, Timestamp now); 61bfe73648SShuo Chen 62bfe73648SShuo Chen bool insert(Timer* timer); 63bfe73648SShuo Chen 64bfe73648SShuo Chen EventLoop* loop_; 65bfe73648SShuo Chen const int timerfd_; 66bfe73648SShuo Chen Channel timerfdChannel_; 67bfe73648SShuo Chen // Timer list sorted by expiration 68bfe73648SShuo Chen TimerList timers_; 69bfe73648SShuo Chen}; 70bfe73648SShuo Chen 71bfe73648SShuo Chen} 72bfe73648SShuo Chen#endif // MUDUO_NET_TIMERQUEUE_H 73