TimerQueue.h revision 344a2ce1
165c497a3SShuo Chen// excerpts from http://code.google.com/p/muduo/
265c497a3SShuo Chen//
365c497a3SShuo Chen// Use of this source code is governed by a BSD-style license
465c497a3SShuo Chen// that can be found in the License file.
565c497a3SShuo Chen//
665c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
765c497a3SShuo Chen
865c497a3SShuo Chen#ifndef MUDUO_NET_TIMERQUEUE_H
965c497a3SShuo Chen#define MUDUO_NET_TIMERQUEUE_H
1065c497a3SShuo Chen
1165c497a3SShuo Chen#include <set>
1265c497a3SShuo Chen#include <vector>
1365c497a3SShuo Chen
1465c497a3SShuo Chen#include <boost/noncopyable.hpp>
1565c497a3SShuo Chen
1665c497a3SShuo Chen#include "datetime/Timestamp.h"
1765c497a3SShuo Chen#include "thread/Mutex.h"
1865c497a3SShuo Chen#include "Callbacks.h"
1965c497a3SShuo Chen#include "Channel.h"
2065c497a3SShuo Chen
2165c497a3SShuo Chennamespace muduo
2265c497a3SShuo Chen{
2365c497a3SShuo Chen
2465c497a3SShuo Chenclass EventLoop;
2565c497a3SShuo Chenclass Timer;
2665c497a3SShuo Chenclass TimerId;
2765c497a3SShuo Chen
2865c497a3SShuo Chen///
2965c497a3SShuo Chen/// A best efforts timer queue.
3065c497a3SShuo Chen/// No guarantee that the callback will be on time.
3165c497a3SShuo Chen///
3265c497a3SShuo Chenclass TimerQueue : boost::noncopyable
3365c497a3SShuo Chen{
3465c497a3SShuo Chen public:
3565c497a3SShuo Chen  TimerQueue(EventLoop* loop);
3665c497a3SShuo Chen  ~TimerQueue();
3765c497a3SShuo Chen
3865c497a3SShuo Chen  ///
3965c497a3SShuo Chen  /// Schedules the callback to be run at given time,
4065c497a3SShuo Chen  /// repeats if @c interval > 0.0.
4165c497a3SShuo Chen  ///
4265c497a3SShuo Chen  /// Must be thread safe. Usually be called from other threads.
4365c497a3SShuo Chen  TimerId addTimer(const TimerCallback& cb,
4465c497a3SShuo Chen                   Timestamp when,
4565c497a3SShuo Chen                   double interval);
4665c497a3SShuo Chen
4765c497a3SShuo Chen  // void cancel(TimerId timerId);
4865c497a3SShuo Chen
4965c497a3SShuo Chen private:
5065c497a3SShuo Chen
5165c497a3SShuo Chen  // FIXME: use unique_ptr<Timer> instead of raw pointers.
5265c497a3SShuo Chen  typedef std::pair<Timestamp, Timer*> Entry;
5365c497a3SShuo Chen  typedef std::set<Entry> TimerList;
5465c497a3SShuo Chen
55344a2ce1SShuo Chen  void addTimerInLoop(Timer* timer);
56dee5abbcSShuo Chen  // called when timerfd alarms
5765c497a3SShuo Chen  void handleRead();
5865c497a3SShuo Chen  // move out all expired timers
5965c497a3SShuo Chen  std::vector<Entry> getExpired(Timestamp now);
6065c497a3SShuo Chen  void reset(const std::vector<Entry>& expired, Timestamp now);
6165c497a3SShuo Chen
6265c497a3SShuo Chen  bool insert(Timer* timer);
6365c497a3SShuo Chen
6465c497a3SShuo Chen  EventLoop* loop_;
6565c497a3SShuo Chen  const int timerfd_;
6665c497a3SShuo Chen  Channel timerfdChannel_;
6765c497a3SShuo Chen  // Timer list sorted by expiration
6865c497a3SShuo Chen  TimerList timers_;
6965c497a3SShuo Chen};
7065c497a3SShuo Chen
7165c497a3SShuo Chen}
7265c497a3SShuo Chen#endif  // MUDUO_NET_TIMERQUEUE_H
73