12745a763SShuo Chen// excerpts from http://code.google.com/p/muduo/
22745a763SShuo Chen//
32745a763SShuo Chen// Use of this source code is governed by a BSD-style license
42745a763SShuo Chen// that can be found in the License file.
52745a763SShuo Chen//
62745a763SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
72745a763SShuo Chen
82745a763SShuo Chen#ifndef MUDUO_NET_TIMERQUEUE_H
92745a763SShuo Chen#define MUDUO_NET_TIMERQUEUE_H
102745a763SShuo Chen
112745a763SShuo Chen#include <set>
122745a763SShuo Chen#include <vector>
132745a763SShuo Chen
142745a763SShuo Chen#include <boost/noncopyable.hpp>
152745a763SShuo Chen
162745a763SShuo Chen#include "datetime/Timestamp.h"
172745a763SShuo Chen#include "thread/Mutex.h"
182745a763SShuo Chen#include "Callbacks.h"
192745a763SShuo Chen#include "Channel.h"
202745a763SShuo Chen
212745a763SShuo Chennamespace muduo
222745a763SShuo Chen{
232745a763SShuo Chen
242745a763SShuo Chenclass EventLoop;
252745a763SShuo Chenclass Timer;
262745a763SShuo Chenclass TimerId;
272745a763SShuo Chen
282745a763SShuo Chen///
292745a763SShuo Chen/// A best efforts timer queue.
302745a763SShuo Chen/// No guarantee that the callback will be on time.
312745a763SShuo Chen///
322745a763SShuo Chenclass TimerQueue : boost::noncopyable
332745a763SShuo Chen{
342745a763SShuo Chen public:
352745a763SShuo Chen  TimerQueue(EventLoop* loop);
362745a763SShuo Chen  ~TimerQueue();
372745a763SShuo Chen
382745a763SShuo Chen  ///
392745a763SShuo Chen  /// Schedules the callback to be run at given time,
402745a763SShuo Chen  /// repeats if @c interval > 0.0.
412745a763SShuo Chen  ///
422745a763SShuo Chen  /// Must be thread safe. Usually be called from other threads.
432745a763SShuo Chen  TimerId addTimer(const TimerCallback& cb,
442745a763SShuo Chen                   Timestamp when,
452745a763SShuo Chen                   double interval);
462745a763SShuo Chen
472745a763SShuo Chen  // void cancel(TimerId timerId);
482745a763SShuo Chen
492745a763SShuo Chen private:
502745a763SShuo Chen
512745a763SShuo Chen  // FIXME: use unique_ptr<Timer> instead of raw pointers.
522745a763SShuo Chen  typedef std::pair<Timestamp, Timer*> Entry;
532745a763SShuo Chen  typedef std::set<Entry> TimerList;
542745a763SShuo Chen
55344a2ce1SShuo Chen  void addTimerInLoop(Timer* timer);
56dee5abbcSShuo Chen  // called when timerfd alarms
572745a763SShuo Chen  void handleRead();
582745a763SShuo Chen  // move out all expired timers
592745a763SShuo Chen  std::vector<Entry> getExpired(Timestamp now);
602745a763SShuo Chen  void reset(const std::vector<Entry>& expired, Timestamp now);
612745a763SShuo Chen
622745a763SShuo Chen  bool insert(Timer* timer);
632745a763SShuo Chen
642745a763SShuo Chen  EventLoop* loop_;
652745a763SShuo Chen  const int timerfd_;
662745a763SShuo Chen  Channel timerfdChannel_;
672745a763SShuo Chen  // Timer list sorted by expiration
682745a763SShuo Chen  TimerList timers_;
692745a763SShuo Chen};
702745a763SShuo Chen
712745a763SShuo Chen}
722745a763SShuo Chen#endif  // MUDUO_NET_TIMERQUEUE_H
73