-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
68 lines (61 loc) · 1.17 KB
/
Timer.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef __TIMTR_H__
#define __TIMER_H__
#include <vector>
#include <functional>
namespace common
{
using TIMER_CALLBACK_t = std::function<void(void)>;
typedef struct tag_TimerNode{
uint64_t ms;
int cycle;
TIMER_CALLBACK_t cb;
tag_TimerNode* next;
tag_TimerNode* prev;
tag_TimerNode():next(nullptr), prev(nullptr) {
}
} TimerNode_t;
typedef struct tag_TimerInfo {
TimerNode_t head;
TimerNode_t tail;
tag_TimerInfo()
{
head.prev = nullptr;
head.next = &tail;
tail.prev = &head;
tail.next = nullptr;
}
void Add(TimerNode_t* node)
{
node->next = head.next;
head.next->prev = node;
node->prev = &head;
head.next = node;
}
void Delete(TimerNode_t* node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
bool Empty() const {
return head.next == &tail ? true : false;
}
} TimerInfo_t;
class Timer {
public:
Timer(int n=60);
~Timer();
int Add(int ms, TIMER_CALLBACK_t cb);
int Run();
void Cancel();
private:
int adjust(TimerInfo_t* tlist);
private:
int epfd_;
int timerfd_;
int cur_;
int total_;
int quit_;
std::vector<TimerInfo_t*> timer_list_;
};
}
#endif //__TIMER_H__