~kris/9p

9hist

ref: ff2bbb664f3cd3e0481e4448bd8ea9b21e7eebd4 9hist/port/alarm.c -rw-r--r-- 3.0 KiB
ff2bbb66 — David du Colombier Plan 9 from Bell Labs 1991-09-13 35 years ago
                                                                                
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
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"

Alarm	*alarmtab;
Alarms	alarms;

Alarm*
alarm(int ms, void (*f)(Alarm*), void *arg)
{
	Alarm *a, *w, *pw;
	ulong s;
	if(ms < 0)
		ms = 0;
	a = newalarm();
	a->dt = MS2TK(ms);
	a->f = f;
	a->arg = arg;
	s = splhi();
	lock(&m->alarmlock);
	pw = 0;
	for(w=m->alarm; w; pw=w, w=w->next){
		if(w->dt <= a->dt){
			a->dt -= w->dt;
			continue;
		}
		w->dt -= a->dt;
		break;
	}
	insert(&m->alarm, pw, a);
	unlock(&m->alarmlock);
	splx(s);
	return a;
}

void
cancel(Alarm *a)
{
	a->f = 0;
}

Alarm*
newalarm(void)
{
	int i;
	Alarm *a;

	for(i=0,a=alarmtab; i<conf.nalarm; i++,a++)
		if(a->busy==0 && a->f==0 && canlock(a)){
			if(a->busy){
				unlock(a);
				continue;
			}
			a->f = 0;
			a->arg = 0;
			a->busy = 1;
			unlock(a);
			return a;
		}
	panic("newalarm");
}

void
alarminit(void)
{
	int i;

	alarmtab = ialloc(conf.nalarm*sizeof(Alarm), 0);
	for(i=0; i<conf.nalarm; i++){
		lock(&alarmtab[i]);	/* allocate locks, as they are used at interrupt time */
		unlock(&alarmtab[i]);
	}
	lock(&alarms);
	unlock(&alarms);
}

#define NA 10		/* alarms per clock tick */
void
checkalarms(void)
{
	int i, n;
	Alarm *a;
	void (*f)(void*);
	Alarm *alist[NA];
	ulong now;
	Proc *rp;

	if(canlock(&m->alarmlock)){
		a = m->alarm;
		if(a){
			for(n=0; a && a->dt<=0 && n<NA; n++){
				alist[n] = a;
				delete(&m->alarm, 0, a);
				a = m->alarm;
			}
			if(a)
				a->dt--;
			unlock(&m->alarmlock);

			/*  execute alarm functions outside the lock */
			for(i = 0; i < n; i++){
				f = alist[i]->f;	/* avoid race with cancel */
				if(f)
					(*f)(alist[i]);
				alist[i]->busy = 0;
			}
		}else
			unlock(&m->alarmlock);
	}

	if(m == MACHP(0) && canlock(&alarms)){
		now = MACHP(0)->ticks;
		while((rp = alarms.head) && rp->alarm <= now){
			if(rp->alarm != 0L){
				if(canlock(&rp->debug)){
					postnote(rp, 0, "alarm", NUser);
					unlock(&rp->debug);
					rp->alarm = 0L;
				}else
					break;
			}
			alarms.head = rp->palarm;
		}
		unlock(&alarms);
	}
}

ulong
procalarm(ulong time)
{
	Proc **l, *f;
	ulong when, old;

	when = MS2TK(time);
	old = u->p->alarm - MACHP(0)->ticks;
	if(when == 0){
		u->p->alarm = 0;
		return TK2MS(old);
	}
	else
		when += MACHP(0)->ticks;

	lock(&alarms);
	l = &alarms.head;
	for(f = *l; f; f = f->palarm){
		if(u->p == f){
			*l = f->palarm;
			break;
		}
		l = &f->palarm;
	}

	u->p->palarm = 0;
	if(alarms.head){
		l = &alarms.head;
		for(f = *l; f; f = f->palarm){
			if(f->alarm > when){
				u->p->palarm = f;
				*l = u->p;
				goto done;
			}
			l = &f->palarm;
		}
		*l = u->p;
	}
	else
		alarms.head = u->p;
done:
	u->p->alarm = when;
	unlock(&alarms);

	return TK2MS(old);			
}

/*
 * Insert new into list after where
 */
void
insert(List **head, List *where, List *new)
{
	if(where == 0){
		new->next = *head;
		*head = new;
	}else{
		new->next = where->next;
		where->next = new;
	}
		
}

/*
 * Delete old from list.  where->next is known to be old.
 */
void
delete(List **head, List *where, List *old)
{
	if(where == 0){
		*head = old->next;
		return;
	}
	where->next = old->next;
}