|
|
1.1 root 1: #include "u.h"
2: #include "lib.h"
3: #include "mem.h"
4: #include "dat.h"
5: #include "fns.h"
6: #include "io.h"
7: #define nelem(x) (sizeof(x)/sizeof((x)[0]))
8: #define MAXALARM 10
9:
10: Alarm alarmtab[MAXALARM];
11:
12: /*
13: * Insert new into list after where
14: */
15: void
16: insert(List **head, List *where, List *new)
17: {
18: if(where == 0){
19: new->next = *head;
20: *head = new;
21: }else{
22: new->next = where->next;
23: where->next = new;
24: }
25:
26: }
27:
28: /*
29: * Delete old from list. where->next is known to be old.
30: */
31: void
32: delete(List **head, List *where, List *old)
33: {
34: if(where == 0){
35: *head = old->next;
36: return;
37: }
38: where->next = old->next;
39: }
40:
41: Alarm*
42: newalarm(void)
43: {
44: int i;
45: Alarm *a;
46:
47: for(i=0,a=alarmtab; i < nelem(alarmtab); i++,a++)
48: if(a->busy==0 && a->f==0){
49: a->f = 0;
50: a->arg = 0;
51: a->busy = 1;
52: return a;
53: }
54: panic("newalarm");
55: return 0; /* not reached */
56: }
57:
58: Alarm*
59: alarm(int ms, void (*f)(Alarm*), void *arg)
60: {
61: Alarm *a, *w, *pw;
62: ulong s;
63:
64: if(ms < 0)
65: ms = 0;
66: s = splhi();
67: a = newalarm();
68: a->dt = MS2TK(ms);
69: a->f = f;
70: a->arg = arg;
71: pw = 0;
72: for(w=m->alarm; w; pw=w, w=w->next){
73: if(w->dt <= a->dt){
74: a->dt -= w->dt;
75: continue;
76: }
77: w->dt -= a->dt;
78: break;
79: }
80: insert(&m->alarm, pw, a);
81: splx(s);
82: return a;
83: }
84:
85: void
86: cancel(Alarm *a)
87: {
88: a->f = 0;
89: }
90:
91: void
92: alarminit(void)
93: {
94: }
95:
96: #define NA 10 /* alarms per clock tick */
97: void
98: checkalarms(void)
99: {
100: int i, n, s;
101: Alarm *a;
102: void (*f)(void*);
103: Alarm *alist[NA];
104:
105: s = splhi();
106: a = m->alarm;
107: if(a){
108: for(n=0; a && a->dt<=0 && n<NA; n++){
109: alist[n] = a;
110: delete(&m->alarm, 0, a);
111: a = m->alarm;
112: }
113: if(a)
114: a->dt--;
115:
116: for(i = 0; i < n; i++){
117: f = alist[i]->f; /* avoid race with cancel */
118: if(f)
119: (*f)(alist[i]);
120: alist[i]->busy = 0;
121: }
122: }
123: splx(s);
124: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.