-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
356 lines (321 loc) · 7.19 KB
/
timer.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Timer and Clock meta-driver for MOKE.
*
* A arch/platform-specific backend driver (with arch_ prefix) is required.
*
*/
#include <type.h>
#include <kprint.h>
#include <mem.h>
#include <panic.h>
#include <list.h>
#include <string.h>
#define S_PER_NS 1000000000
#define MS_PER_NS 1000000
#define US_PER_NS 1000
#define TICK_MS 10
struct time_spec {
u32 sec;
i32 nsec; //Senseless for now.
};
struct time_spec now;
u32 ticks;
u32 epoch; //Time starts from 1970.
/*
* 4-level timing wheel.
* 100ms - 10s - 1000s - 10*6 s
*
*/
struct time_spec time_diff(struct time_spec *a,struct time_spec *b)
{
struct time_spec ret;
ret.sec=a->sec-b->sec;
ret.nsec=a->nsec-b->nsec;
if (ret.nsec / S_PER_NS )
{
ret.sec +=ret.nsec / S_PER_NS;
ret.nsec =ret.nsec % S_PER_NS;
}
return ret;
}
int time_diff_ms(struct time_spec *a,struct time_spec *b)
{
return (a->sec-b->sec)*1000+(a->nsec-b->nsec)/MS_PER_NS;
}
void time_add_ms(struct time_spec *dst,u32 msec)
{
dst->nsec+=(msec%1000)*MS_PER_NS;
dst->sec+=(msec/1000);
if (dst->nsec / S_PER_NS)
{
dst->sec += dst->nsec / S_PER_NS;
dst->nsec = dst->nsec % S_PER_NS;
}
return ;
}
struct time_spec time_add(struct time_spec *a,struct time_spec *b)
{
struct time_spec ret;
ret.sec=a->sec+b->sec;
ret.nsec=a->nsec+b->nsec;
if (ret.nsec / S_PER_NS)
{
ret.sec += ret.nsec / S_PER_NS;
ret.nsec = ret.nsec % S_PER_NS;
}
printk("%ds%dns+%ds%dns=%ds%dns\n",a->sec,a->nsec,b->sec,b->nsec,ret.sec,ret.nsec);
return ret;
}
struct timer {
struct time_spec time;
struct time_spec interval;
void (*func)();
int type;
struct timer *prev;
struct timer *next;
};
void timer_dump(struct timer *p)
{
//printk("expire %d s,%d ms\n",p->time.sec,p->time.nsec/MS_PER_NS);
return;
}
/*
* timer.type schema:
* bit 0: Enable bit
* - bit 1: Periodical bit(interval)
*
*/
struct timer_item {
struct timer *timer;
struct timer_item *prev;
struct timer_item *next;
};
#define TIMER_MAGIC 0x632f632f
struct time_layer {
int unit; //in microseconds.
struct timer *timer_list[100];
int now;
} wheel[4];
struct timer_backend {
u32 (*tick_set)(struct time_spec *);
struct time_spec *(*time_get)(struct time_spec *);
u32 tick_perc;
u32 time_perc;
u32 type;
};
struct timer *timer_create(void)
{
struct timer *p=kalloc(sizeof(struct timer),0);
//p->prev=NULL;
//p->next=NULL;
clear(p,sizeof(struct timer));
return p;
}
int timer_insert(struct timer *t)
{
printk("adding timer %x at %d s %d ms\n",t,t->time.sec,t->time.nsec/MS_PER_NS);
//Check the right position to insert.
for (int i=3;i>=0;i--)
{
if (time_diff_ms(&t->time,&now)<0)
break;
if (time_diff_ms(&t->time,&now)<wheel[i].unit)
{
continue;
}
else
{
//Append to the wheel list.
int where = time_diff_ms(&t->time, &now) / wheel[i].unit +wheel[i].now>= 100 ? time_diff_ms(&t->time,&now)/wheel[i].unit+wheel[i].now-100 : time_diff_ms(&t->time, &now) / wheel[i].unit+wheel[i].now;
printk("insert timer %x at level %d %d\n",t,i,where);
if (!wheel[i].timer_list[where])
{
wheel[i].timer_list[where]=t;
}
else
FOR_ITEM(wheel[i].timer_list[where],j)
{
if (!j->next)
{
APPEND_ITEM(j,t);
break;
}
}
break;
}
}
}
int timer_set(struct timer *t,struct time_spec *time,void (*func)(void) )
{
t->time=time_add(&now,time);
t->func=func;
//timer_dump(t);
timer_insert(t);
}
int timer_at(struct timer *t,struct time_spec *time,void (*func)(void))
{
t->time=*time;
t->func=func;
timer_insert(t);
}
int timer_pause(struct timer *t)
{
//Remove from timing-wheel.
DEL_ITEM(t);
}
int periodical_set(struct timer *t,struct time_spec *interval,void (*func)(void))
{
t->time=time_add(&now,interval);
t->interval=*interval;
t->func=func;
timer_insert(t);
return 0;
}
int timer_stop(struct timer *t)
{
//Set target time to 0.
//DEL_ITEM(t);
t->time.sec=0;
t->time.nsec=0;
t->interval.sec=0;
t->interval.nsec=0;
}
int timer_del(struct timer *t)
{
//Remove from linked-list.
if (t->next)
t->next->prev=t->prev;
if (t->prev)
t->prev->next=t->next;
kfree(t);
}
void timer_handler(void)
{
//timing-wheel handler.
//Check the wheel for deadlines.
//struct timer_item *p=wheel->timer_list[wheel->now];
for (int i=0;i<=TICK_MS-1;i++)
{
for (struct timer *p=wheel->timer_list[wheel->now+i>=100 ? wheel->now+i-100 : wheel->now+i];p;p=p->next)
{
if (time_diff_ms(&p->time,&now)<TICK_MS)
{
printk("now %ds %dms,this clock %ds %dms\n",now.sec,now.nsec/MS_PER_NS,p->time.sec,p->time.nsec/MS_PER_NS);
p->func(); //Do callback.
if (wheel->timer_list[wheel->now+i>=100 ? wheel->now+i-100 : wheel->now+i]==p)
{
wheel->timer_list[wheel->now+i>=100 ? wheel->now+i-100 : wheel->now+i]=NULL;
}
else
{
DEL_ITEM(p)
}
if (p->interval.sec || p->interval.nsec)
//Re-insert timer.
{
timer_set(p,&p->interval,p->func);
}
else
{
timer_stop(p);
}
//Invalidate timer.
}
}
}
if (!((ticks*TICK_MS)%wheel[0].unit))
{
wheel->now++;
if (wheel->now>=100)
wheel->now=0;
}
//wheel->now+=10;
//if (wheel->now>=100)
//wheel->now=0;
//Check upper wheels.
for (int i=1;i<=3;i++)
{
struct timer *k;
//If a deadline is not longer than lower wheel,the we move it.
FOR_ITEM(wheel[i].timer_list[wheel[i].now],j)
{
if (!j)
break;
k=j;
if (time_diff_ms(&k->time,&now)<wheel[i].unit)
{
DEL_ITEM(k)
if (wheel[i].timer_list[wheel[i].now]==k)
wheel[i].timer_list[wheel[i].now]=NULL;
//Append
int where=(time_diff_ms(&k->time,&now)/wheel[i-1].unit+wheel[i-1].now);
printk("move to %d - %d\n",i,where);
if (where>=100)
where-=100;
if (!wheel[i-1].timer_list[where])
{
wheel[i-1].timer_list[where]=k;
k->prev=NULL;
k->next=NULL;
}
else
{
FOR_ITEM(wheel[i-1].timer_list[where],tmp)
{
if (!tmp->next)
{
tmp->next=k;
k->prev=tmp;
k->next=NULL;
}
}
}
}
}
//Increase 'now' value when needed.
if (!((ticks*TICK_MS)%wheel[i].unit))
{
wheel[i].now++;
if (wheel[i].now>=100)
wheel[i].now=0;
}
}
return;
}
void tick_handler(void) //Called in interrupt handler. No scheduling allowed.
{
ticks++;
time_add_ms(&now,TICK_MS);
timer_handler();
}
void clock_handler(void) //For non-periodical timer shots. Currently unavailable on x86.
{
return;
}
void timer_init(void)
{
wheel[0].unit=100;
wheel[1].unit=10000;
wheel[2].unit=1000000;
wheel[3].unit=100000000;
return ;
}
#ifndef PRODUCTION
void callback(void)
{
printk("Called by timer at %d sec %d ms!\n",now.sec,now.nsec/MS_PER_NS);
}
void timer_test()
{
struct timer *a=timer_create();
struct timer *b=timer_create();
struct timer *c=timer_create();
timer_set(a,&(struct time_spec){.sec=2,.nsec=0},callback);
timer_set(b,&(struct time_spec){.sec=10001,.nsec=0},callback);
timer_set(c,&(struct time_spec){.sec=100,.nsec=0},callback);
//printk("%d ms\n",time_diff_ms(&(struct time_spec){.sec=12000,.nsec=10},&(struct time_spec){.sec=200,.nsec=1000}));
//struct time_spec d=time_add(&(struct time_spec){.sec=100,.nsec=3000000},&(struct time_spec){.sec=1000,.nsec=30000000});
//printk("%d s %d ns\n",d.sec,d.nsec);
return;
}
#endif