-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapptimer.c
285 lines (219 loc) · 6.69 KB
/
apptimer.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "apptimer.h"
unsigned int regnum = 1; //counter
LIST_HEAD(timer_list);
/*
* 更新这个定时器的下一个到期时间如果他是个可重复的定时器
*/
void update_timer(struct app_timer *a)
{
if(a->t_last.tv_sec == 0 && a->t_last.tv_usec == 0) {
struct timeval t_now;
gettimeofday(&t_now, NULL);
a->t_last.tv_sec = t_now.tv_sec;
a->t_last.tv_usec = t_now.tv_usec;
a->t_next.tv_sec = t_now.tv_sec + a->t.tv_sec;
a->t_next.tv_usec = t_now.tv_usec + a->t.tv_usec;
while(a->t_next.tv_usec >= 1000000) {
a->t_next.tv_usec -= 1000000;
a->t_next.tv_sec += 1;
}
}
else if(a->t_next.tv_sec == 0 && a->t_next.tv_usec == 0) {
if(a->flags & SA_REPEAT) {
if(a->t.tv_sec == 0 && a->t.tv_usec == 0) {
app_timer_unregister(a->clientreg);
return;
}
a->t_next.tv_sec = a->t_last.tv_sec + a->t.tv_sec;
a->t_next.tv_usec = a->t_last.tv_usec + a->t.tv_usec;
while(a->t_next.tv_usec >= 1000000) {
a->t_next.tv_usec -= 1000000;
a->t_next.tv_sec += 1;
}
}
else {
app_timer_unregister(a->clientreg);
}
}
}
/*
* 注销回调函数从一个定时器注册链表
*/
void app_timer_unregister(unsigned int clientreg)
{
struct app_timer *sa_ptr;
struct list_head *pos;
/* sa_ptr通过遍历链表找到要注销的app_time结构体 */
list_for_each(pos, &timer_list){
sa_ptr = list_entry(pos,struct app_timer,list);
if(sa_ptr->clientreg == clientreg)
break;
}
if(sa_ptr != NULL) {
printf("注销定时器 %d 号\n", sa_ptr->clientreg);
list_del(&sa_ptr->list);
(sa_ptr)->t.tv_sec = 0;
(sa_ptr)->t.tv_usec = 0;
(sa_ptr)->t_last.tv_sec = 0;
(sa_ptr)->t_last.tv_usec = 0;
(sa_ptr)->t_next.tv_sec = 0;
(sa_ptr)->t_next.tv_usec = 0;
(sa_ptr)->flags = 0;
(sa_ptr)->clientarg = NULL;
(sa_ptr)->thecallback = NULL;
(sa_ptr)->clientreg = 0;
}
else {
}
}
struct app_timer *find_next_timer(void)
{
struct app_timer *a, *lowest = NULL;
struct list_head *pos;
list_for_each(pos, &timer_list){
a = list_entry(pos, struct app_timer, list);
if(lowest == NULL)
lowest = a;
else if(a->t_next.tv_sec == lowest->t_next.tv_sec) {
if(a->t_next.tv_usec < lowest->t_next.tv_usec) {
lowest = a;
}
}else if(a->t_next.tv_sec < lowest->t_next.tv_sec) {
lowest = a;
}
}
return lowest;
}
/* 按clientreg找到相应的定时器结构体 */
struct app_timer *sa_find_specific(unsigned int clientreg)
{
struct app_timer *sa_ptr;
struct list_head *pos;
list_for_each(pos,&timer_list){
sa_ptr = list_entry(pos, struct app_timer, list);
if(sa_ptr->clientreg == clientreg) {
return sa_ptr;
}
}
return NULL;
}
void run_timers(void)
{
int done = 0;
struct app_timer *a = NULL;
unsigned int clientreg;
struct timeval t_now;
while(!done) {
if((a = find_next_timer()) == NULL) {
return;
}
gettimeofday(&t_now, NULL);
if((a->t_next.tv_sec < t_now.tv_sec) ||
((a->t_next.tv_sec == t_now.tv_sec) &&
(a->t_next.tv_usec < t_now.tv_usec))) {
clientreg = a->clientreg;
(*(a->thecallback)) (clientreg, a->clientarg);
if((a = sa_find_specific(clientreg)) != NULL) {
a->t_last.tv_sec = t_now.tv_sec;
a->t_last.tv_usec = t_now.tv_usec;
a->t_next.tv_sec = 0;
a->t_next.tv_usec = 0;
update_timer(a);
}
else {
}
}
else {
done = 1;
}
}
}
/*
* 注意 timer_handler函数 是基础,这是其他定时器到期处理函数的来源,
* 因此在timer_handler中我们首先调用这个到期的定时器的处理函数,然后
* 选择另一个定时器在timer_list然后设置它的剩余时间,当然这个定时器
* 将成为下一个到期的。
*/
void timer_handler(int a)
{
/* 调用到期的定时器的回调函数*/
run_timers();
set_an_timer();
}
/*
* 获得最近到期定时器剩余时间写入delta
*/
int get_next_timer_delay_time(struct timeval *delta)
{
struct app_timer *sa_ptr;
struct timeval t_diff, t_now;
sa_ptr = find_next_timer();
if(sa_ptr) {
gettimeofday(&t_now, 0);
if((t_now.tv_sec > sa_ptr->t_next.tv_sec) ||
((t_now.tv_sec == sa_ptr->t_next.tv_sec) &&
(t_now.tv_usec > sa_ptr->t_next.tv_usec))) {
delta->tv_sec = 0;
delta->tv_usec = 1;
return sa_ptr->clientreg;
}
else {
t_diff.tv_sec = sa_ptr->t_next.tv_sec - t_now.tv_sec;
t_diff.tv_usec = sa_ptr->t_next.tv_usec - t_now.tv_usec;
while(t_diff.tv_usec < 0) {
t_diff.tv_sec -= 1;
t_diff.tv_usec += 1000000;
}
delta->tv_sec = t_diff.tv_sec;
delta->tv_usec = t_diff.tv_usec;
return sa_ptr->clientreg;
}
}
return 0;
}
void set_an_timer(void)
{
struct timeval delta;
int next_timer = get_next_timer_delay_time(&delta);
if(next_timer) {
struct itimerval it;
it.it_value.tv_sec = delta.tv_sec;//
it.it_value.tv_usec = delta.tv_usec;
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 0;
signal(SIGALRM, timer_handler);
/* kick off the timer */
setitimer(ITIMER_REAL, &it, NULL);
}
else {
/*printf("no timers found to schedule\n");*/
}
}
unsigned int
app_timer_register(struct app_timer *sa_ptr,unsigned int when, unsigned int flags,
APPTimerCallback * thecallback, void *clientarg)
{
struct list_head *pos;
list_add_tail(&sa_ptr->list, &timer_list);
if(sa_ptr == NULL)
return 0;
if(0 == when) {
(sa_ptr)->t.tv_sec = 0;
(sa_ptr)->t.tv_usec = 1;
}
else {
(sa_ptr)->t.tv_sec = when;
(sa_ptr)->t.tv_usec = 0;
}
(sa_ptr)->t_last.tv_sec = 0;
(sa_ptr)->t_last.tv_usec = 0;
(sa_ptr)->t_next.tv_sec = 0;
(sa_ptr)->t_next.tv_usec = 0;
(sa_ptr)->flags = flags;
(sa_ptr)->clientarg = clientarg;
(sa_ptr)->thecallback = thecallback;
(sa_ptr)->clientreg = regnum++;
update_timer(sa_ptr);
set_an_timer();
return (sa_ptr)->clientreg;
}