-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtimer.h
More file actions
270 lines (208 loc) · 5.18 KB
/
timer.h
File metadata and controls
270 lines (208 loc) · 5.18 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifndef _TIMER_H
#define _TIMER_H
#include <sys/time.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
extern "C" {
#include "intrusiveList.h"
}
using namespace std;
#define loopus 200000
#define TotalBucket 60
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
/**
* @brief 秒级定时器
*/
template <class Owner, class Arg>
class SafeTimer {
public:
// 超时回调函数
typedef int32_t (*timeoutCallback)(Owner owner, Arg arg);
struct timerEvent {
/**
* @brief 链表结点,用于加入超时链表
*/
list_head_t timerNode;
/**
* @brief 回调函数
*/
timeoutCallback func;
/**
* @brief 超时时间戳,>ExpireTime将触发回调函数
*/
time_t expireTime;
/**
* @brief 回调函数作用对象
*/
Owner owner;
/**
* @brief 回调函数其他参数
*/
Arg arg;
timerEvent() {
// cout << "timerEvent create:" << time(NULL) << endl;
}
~timerEvent() {
// cout << "timerEvent delete:" << time(NULL) << endl;
}
};
public:
int32_t init();
int32_t unInit();
int32_t addEvent(timeoutCallback func, time_t expireTime, const Owner &owner, const Arg &arg);
int32_t checkEvent();
void initCurBucket();
void fixCurBucket();
static void *timerLoopProc(void *arg);
private:
int32_t delEvent(timerEvent *event);
private:
list_head_t timerHead[TotalBucket];//60秒时间桶
mutable list_head_t *curBucket;
mutable pthread_mutex_t m_mutex;
mutable uint32_t curTm; //当前时间
mutable uint32_t secStep; //已处理的时间step
#define LOCK pthread_mutex_lock(&m_mutex)
#define UNLOCK pthread_mutex_unlock(&m_mutex)
};
template <class Owner, class Arg>
int32_t SafeTimer<Owner,Arg>::init()
{
initCurBucket();
pthread_t tid;
int32_t ret = pthread_create(&tid, NULL, timerLoopProc, this);
if (ret != 0) {
return -1;
}
pthread_mutexattr_t attr;
ret = pthread_mutexattr_init(&attr);
if (ret != 0) {
return -1;
}
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
if (ret != 0) {
return -1;
}
ret = pthread_mutex_init(&m_mutex, &attr);
if (ret != 0) {
return -1;
}
return 0;
}
template <typename Owner, typename Arg>
int32_t SafeTimer<Owner,Arg>::unInit()
{
LOCK;
for (size_t i = 0; i < TotalBucket; i++) {
curBucket = &(timerHead[i]);
list_head_t *pos, *next;
list_for_each_safe(pos, next, curBucket) {
delEvent(reinterpret_cast<timerEvent *>(pos));
}
INIT_LIST_HEAD(curBucket);
}
UNLOCK;
pthread_mutex_destroy(&m_mutex);
return 0;
}
template <typename Owner, typename Arg>
int32_t SafeTimer<Owner,Arg>::addEvent(timeoutCallback func, time_t expireTime, const Owner &owner, const Arg &arg)
{
LOCK;
timerEvent *event = new timerEvent;
if (unlikely(!event)) return -1;
event->expireTime = expireTime;
event->func = func;
event->owner = owner;
event->arg = arg;
list_head_t *pos, *next;
list_for_each_safe(pos, next, curBucket) {
timerEvent *e = reinterpret_cast<timerEvent *>(pos);
if (e->expireTime > expireTime) {
list_add_tail(&event->timerNode, pos);
UNLOCK;
return 0;
}
}
list_add_tail(&event->timerNode, curBucket);
UNLOCK;
return 0;
}
template <typename Owner, typename Arg>
int32_t SafeTimer<Owner,Arg>::delEvent(timerEvent *event)
{
if (unlikely(event == NULL)) return -1;
list_del_init(&event->timerNode);
event->expireTime = 0;
event->func = NULL;
delete event;
event = NULL;
return 0;
}
template <typename Owner, typename Arg>
int32_t SafeTimer<Owner,Arg>::checkEvent()
{
LOCK;
list_head_t *pos, *next;
list_for_each_safe(pos, next, curBucket) {
timerEvent *e = reinterpret_cast<timerEvent *>(pos);
if (e->expireTime > curTm) {
break;
}
if (likely(e->func)) {
e->func(e->owner, e->arg);
}
delEvent(e);
}
UNLOCK;
return 0;
}
template <typename Owner, typename Arg>
void SafeTimer<Owner,Arg>::initCurBucket()
{
for (size_t i = 0; i < TotalBucket; i++) {
curBucket = &(timerHead[i]);
INIT_LIST_HEAD(curBucket);
}
timeval tv;
gettimeofday(&tv, 0);
secStep = tv.tv_sec;
curBucket = &(timerHead[secStep % TotalBucket]);
}
template <typename Owner, typename Arg>
void SafeTimer<Owner,Arg>::fixCurBucket()
{
static timeval tv;
static uint32_t secStep;
gettimeofday(&tv, 0);
curTm = tv.tv_sec;
LOCK;
/**
* 这里负责整个定时器的secStep追赶机制
*
* 产生原因:调用func耗时过长,会导致curTm的前后差距到几秒以上
*
* 这里处理方式:发现当前时间戳(curTm)大于我们桶指针(curBucket)正在处理的时间桶(secStep)
* 我们将时间桶加1,保证按顺序时间桶都会遍历到。
*
* 需要配合的:func应该注意不要放很重很占用时间的逻辑
*/
if (curTm > secStep) {
secStep ++;
curBucket = &(timerHead[secStep % TotalBucket]);
}
UNLOCK;
}
template <typename Owner, typename Arg>
void *SafeTimer<Owner,Arg>::timerLoopProc(void *arg)
{
for ( ; ; ) {
usleep(loopus);
(reinterpret_cast<SafeTimer *>(arg))->checkEvent();
(reinterpret_cast<SafeTimer *>(arg))->fixCurBucket();
}
pthread_exit(0);
}
#endif