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