1566406ccSShuo Chen // excerpts from http://code.google.com/p/muduo/ 2566406ccSShuo Chen // 3566406ccSShuo Chen // Use of this source code is governed by a BSD-style license 4566406ccSShuo Chen // that can be found in the License file. 5566406ccSShuo Chen // 6566406ccSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 7566406ccSShuo Chen 8566406ccSShuo Chen #ifndef MUDUO_NET_TIMERQUEUE_H 9566406ccSShuo Chen #define MUDUO_NET_TIMERQUEUE_H 10566406ccSShuo Chen 11566406ccSShuo Chen #include <set> 12566406ccSShuo Chen #include <vector> 13566406ccSShuo Chen 14566406ccSShuo Chen #include <boost/noncopyable.hpp> 15566406ccSShuo Chen 16566406ccSShuo Chen #include "datetime/Timestamp.h" 17566406ccSShuo Chen #include "thread/Mutex.h" 18566406ccSShuo Chen #include "Callbacks.h" 19566406ccSShuo Chen #include "Channel.h" 20566406ccSShuo Chen 21566406ccSShuo Chen namespace muduo 22566406ccSShuo Chen { 23566406ccSShuo Chen 24566406ccSShuo Chen class EventLoop; 25566406ccSShuo Chen class Timer; 26566406ccSShuo Chen class TimerId; 27566406ccSShuo Chen 28566406ccSShuo Chen /// 29566406ccSShuo Chen /// A best efforts timer queue. 30566406ccSShuo Chen /// No guarantee that the callback will be on time. 31566406ccSShuo Chen /// 32566406ccSShuo Chen class TimerQueue : boost::noncopyable 33566406ccSShuo Chen { 34566406ccSShuo Chen public: 35566406ccSShuo Chen TimerQueue(EventLoop* loop); 36566406ccSShuo Chen ~TimerQueue(); 37566406ccSShuo Chen 38566406ccSShuo Chen /// 39566406ccSShuo Chen /// Schedules the callback to be run at given time, 40566406ccSShuo Chen /// repeats if @c interval > 0.0. 41566406ccSShuo Chen /// 42566406ccSShuo Chen /// Must be thread safe. Usually be called from other threads. 43566406ccSShuo Chen TimerId addTimer(const TimerCallback& cb, 44566406ccSShuo Chen Timestamp when, 45566406ccSShuo Chen double interval); 46566406ccSShuo Chen 47566406ccSShuo Chen // void cancel(TimerId timerId); 48566406ccSShuo Chen 49566406ccSShuo Chen private: 50566406ccSShuo Chen 51566406ccSShuo Chen // FIXME: use unique_ptr<Timer> instead of raw pointers. 52566406ccSShuo Chen typedef std::pair<Timestamp, Timer*> Entry; 53566406ccSShuo Chen typedef std::set<Entry> TimerList; 54566406ccSShuo Chen 55344a2ce1SShuo Chen+ void addTimerInLoop(Timer* timer); 56dee5abbcSShuo Chen // called when timerfd alarms 57566406ccSShuo Chen void handleRead(); 58566406ccSShuo Chen // move out all expired timers 59566406ccSShuo Chen std::vector<Entry> getExpired(Timestamp now); 60566406ccSShuo Chen void reset(const std::vector<Entry>& expired, Timestamp now); 61566406ccSShuo Chen 62566406ccSShuo Chen bool insert(Timer* timer); 63566406ccSShuo Chen 64566406ccSShuo Chen EventLoop* loop_; 65566406ccSShuo Chen const int timerfd_; 66566406ccSShuo Chen Channel timerfdChannel_; 67566406ccSShuo Chen // Timer list sorted by expiration 68566406ccSShuo Chen TimerList timers_; 69566406ccSShuo Chen }; 70566406ccSShuo Chen 71566406ccSShuo Chen } 72566406ccSShuo Chen #endif // MUDUO_NET_TIMERQUEUE_H 73