forked from voidccc/mini-muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerQueue.h
51 lines (41 loc) · 1.28 KB
/
TimerQueue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// author voidccc
#ifndef TIMERQUEUE_H
#define TIMERQUEUE_H
#include "Declear.h"
#include "IChannelCallback.h"
#include "IRun.h"
#include "Timestamp.h"
#include <vector>
#include <set>
using namespace std;
class TimerQueue : public IChannelCallback
, public IRun2
{
public:
TimerQueue(EventLoop* pLoop);
~TimerQueue();
void doAddTimer(Timer* timer);
void doCancelTimer(Timer* timer);
long addTimer(IRun0* pRun,
Timestamp when,
double interval);
void cancelTimer(long timerId);
virtual void run2(const string& str, void* timer);
virtual void handleRead();
virtual void handleWrite();
private:
typedef std::pair<Timestamp, Timer*> Entry;
typedef std::set<Entry> TimerList;
int createTimerfd();
vector<TimerQueue::Entry> getExpired(Timestamp now);
void readTimerfd(int timerfd, Timestamp now);
void reset(const vector<Entry>& expired, Timestamp now);
void resetTimerfd(int timerfd, Timestamp stamp);
bool insert(Timer* pItem);
struct timespec howMuchTimeFromNow(Timestamp when);
int _timerfd;
TimerList _pTimers;
EventLoop* _pLoop;
Channel* _pTimerfdChannel;
};
#endif