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