Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/port/alarm.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname port/alarm.c 1990/1211
## diff -e /dev/null /n/bootesdump/1990/1211/sys/src/9/port/alarm.c
0a
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"

Alarm	*alarmtab;

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]);
	}
}

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

	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);
	}
}
.
## diffname port/alarm.c 1991/0513
## diff -e /n/bootesdump/1990/1211/sys/src/9/port/alarm.c /n/bootesdump/1991/0513/sys/src/9/port/alarm.c
107a

	if(m == MACHP(0) && canqlock(&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;
		}
	}
	qunlock(&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;

	qlock(&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;
	qunlock(&alarms);

	return TK2MS(old);			
.
84a
	ulong now;
	Proc *rp;
.
8a
Alarms	alarms;
.
## diffname port/alarm.c 1991/0603
## diff -e /n/bootesdump/1991/0513/sys/src/9/port/alarm.c /n/bootesdump/1991/0603/sys/src/9/port/alarm.c
127d
125a
		qunlock(&alarms);
.
75a
	qlock(&alarms);
	qunlock(&alarms);
.
## diffname port/alarm.c 1991/0710
## diff -e /n/bootesdump/1991/0603/sys/src/9/port/alarm.c /n/bootesdump/1991/0710/sys/src/9/port/alarm.c
177a

/*
 * 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;
}

.
## diffname port/alarm.c 1991/0726
## diff -e /n/bootesdump/1991/0710/sys/src/9/port/alarm.c /n/bootesdump/1991/0726/sys/src/9/port/alarm.c
160,161c
		for(f = *l; f; f = f->palarm){
			if(f->alarm > when){
.
158c
	if(alarms.head){
.
149,150c
	for(f = *l; f; f = f->palarm){
		if(u->p == f){
.
140c
	if(when == 0){
.
122,123c
				}else
.
116,118c
		while((rp = alarms.head) && rp->alarm <= now){
			if(rp->alarm != 0L){
				if(canlock(&rp->debug)){
.
114c
	if(m == MACHP(0) && canqlock(&alarms)){
.
110c
		}else
.
## diffname port/alarm.c 1991/0727
## diff -e /n/bootesdump/1991/0726/sys/src/9/port/alarm.c /n/bootesdump/1991/0727/sys/src/9/port/alarm.c
173c
	unlock(&alarms);
.
146c
	lock(&alarms);
.
127c
		unlock(&alarms);
.
114c
	if(m == MACHP(0) && canlock(&alarms)){
.
76,77c
	lock(&alarms);
	unlock(&alarms);
.
## diffname port/alarm.c 1991/1113
## diff -e /n/bootesdump/1991/0727/sys/src/9/port/alarm.c /n/bootesdump/1991/1113/sys/src/9/port/alarm.c
175c
	return old;			
.
143,144c
	when = MS2TK(time);
	when += MACHP(0)->ticks;
.
141c
		return old;
.
137,139c
	old = TK2MS(u->p->alarm - MACHP(0)->ticks);
	if(time == 0){
.
## diffname port/alarm.c 1991/1206
## diff -e /n/bootesdump/1991/1113/sys/src/9/port/alarm.c /n/bootesdump/1991/1206/sys/src/9/port/alarm.c
73c
		lock(&alarmtab[i]);
.
65a
/* allocate locks here since you can't allocate them at interrupt time (on the SGI) */
.
## diffname port/alarm.c 1991/1216
## diff -e /n/bootesdump/1991/1206/sys/src/9/port/alarm.c /n/bootesdump/1991/1216/sys/src/9/port/alarm.c
121c
					qunlock(&rp->debug);
.
119c
				if(canqlock(&rp->debug)){
.
## diffname port/alarm.c 1992/0321
## diff -e /n/bootesdump/1991/1216/sys/src/9/port/alarm.c /n/bootesdump/1992/0321/sys/src/9/port/alarm.c
2c
#include	"../port/lib.h"
.
## diffname port/alarm.c 1992/0516
## diff -e /n/bootesdump/1992/0321/sys/src/9/port/alarm.c /n/bootesdump/1992/0516/sys/src/9/port/alarm.c
173c
	qunlock(&alarms);
.
146c
	qlock(&alarms);
.
131a
/*
 *  called every clock tick
 */
void
checkalarms(void)
{
	ulong now;
	Proc *p;
	Alarm *a;

	if(m != MACHP(0))
		return;
	a = alarmalloc.list;
	p = alarms.head;
	now = MACHP(0)->ticks;

	if((a && a->when <= now) || (p && p->alarm <= now))
		wakeup(&alarmalloc.r);
}

.
128c
		qunlock(&alarms);

		sleep(&alarmalloc.r, return0, 0);
.
120c
					if(!waserror()){
						postnote(rp, 0, "alarm", NUser);
						poperror();
					}
.
115,116c
		qlock(&alarms);
.
112,113c
			qunlock(&alarmalloc);
.
104,109c
			/*
			 *  execute alarm functions outside the lock since they
			 *  might call alarm().
			 */
			f = 0;
			if(waserror()){
				print("alarm func %lux caused error %s\n", f, u->error);
			} else {
				for(i = 0; i < n; i++){
					f = alist[i]->f; /* avoid race with cancel */
					if(f)
						(*f)(alist[i]);
					alist[i]->busy = 0;
				}
				poperror();
.
100,102c
			qunlock(&alarmalloc);
.
97,98c
				delete(&alarmalloc.list, 0, a);
				a = alarmalloc.list;
.
95c
			for(n=0; a && a->when<=now && n<NA; n++){
.
92,93c
	USED(arg);

	for(;;){
		now = MACHP(0)->ticks;

		qlock(&alarmalloc);
		a = alarmalloc.list;
.
83c
alarmkproc(void *arg)
.
81c
#define NA 10		/* alarms per wakeup */
.
77,78c
	qlock(&alarmalloc);
	qunlock(&alarmalloc);
	qlock(&alarms);
	qunlock(&alarms);
.
74,75c
		lock(&alarmalloc.tab[i]);
		unlock(&alarmalloc.tab[i]);
.
72c
	alarmalloc.tab = ialloc(conf.nalarm*sizeof(Alarm), 0);
.
63a
	return 0;	/* not reached */
.
51c
	for(i=0,a=alarmalloc.tab; i<conf.nalarm; i++,a++)
.
33,35c
	insert(&alarmalloc.list, pw, a);
	qunlock(&alarmalloc);
.
25,31c
	for(w=alarmalloc.list; w; pw=w, w=w->next){
		if(w->when > a->when)
			break;
.
22,23c
	qlock(&alarmalloc);
.
19c
	a->when = MACHP(0)->ticks+MS2TK(ms);
.
15c

.
8c
struct {
	QLock;
	Alarm	*tab;	/* table of all alarm structures */
	void	*list;	/* busy alarms */
	Rendez	r;
} alarmalloc;

.
## diffname port/alarm.c 1992/0517
## diff -e /n/bootesdump/1992/0516/sys/src/9/port/alarm.c /n/bootesdump/1992/0517/sys/src/9/port/alarm.c
160,161d
127c
			unlock(&alarmalloc);
.
108c
			unlock(&alarmalloc);
.
100c
		lock(&alarmalloc);
.
78,79c
	lock(&alarmalloc);
	unlock(&alarmalloc);
.
35c
	unlock(&alarmalloc);
.
28c
	lock(&alarmalloc);
.
9c
	Lock;
.
## diffname port/alarm.c 1992/0520
## diff -e /n/bootesdump/1992/0517/sys/src/9/port/alarm.c /n/bootesdump/1992/0520/sys/src/9/port/alarm.c
127a
			splx(s);
		}
.
126c
		}else{
.
108a
			splx(s);
.
99a
		s = splhi();
.
93a
	int s;
.
35a
	splx(s);
.
27a
	s = splhi();
.
20a
	int s;
.
## diffname port/alarm.c 1992/0602
## diff -e /n/bootesdump/1992/0520/sys/src/9/port/alarm.c /n/bootesdump/1992/0602/sys/src/9/port/alarm.c
221,250d
216c
	p->alarm = when;
.
214c
		alarms.head = p;
.
211c
		*l = p;
.
203,206c
		for(f = *l; f; f = f->palarm) {
			if(f->alarm > when) {
				p->palarm = f;
				*l = p;
.
200,201c
	p->palarm = 0;
	if(alarms.head) {
.
192,193c
	for(f = *l; f; f = f->palarm) {
		if(p == f){
.
187,188c
	when = MS2TK(time)+MACHP(0)->ticks;
.
182,184c
	p = u->p;
	old = TK2MS(p->alarm - MACHP(0)->ticks);
	if(time == 0) {
		p->alarm = 0;
.
179c
	Proc **l, *f, *p;
.
172,173c
	if(p && p->alarm <= now)
		wakeup(&alarmr);

	if(talarm.list == 0 || canlock(&talarm) == 0)
		return;

	for(;;) {
		p = talarm.list;
		if(p == 0)
			break;

		if(p->twhen == 0) {
			talarm.list = p->tlink;
			continue;
		}
		if(now < p->twhen)
			break;
		talarm.list = p->tlink;
		wakeup(p->trend);
	}

	unlock(&talarm);
.
168d
166c
	ulong now;
.
164d
154c
		sleep(&alarmr, return0, 0);
.
103,136d
92,94d
43,88d
17,41d
15a
Rendez	alarmr;
Talarm	talarm;
.
8,14d
## diffname port/alarm.c 1992/0701
## diff -e /n/bootesdump/1992/0602/sys/src/9/port/alarm.c /n/bootesdump/1992/0701/sys/src/9/port/alarm.c
75a
		talarm.list = p->tlink;
		p->trend = 0;
.
74d
69a
			p->trend = 0;
.
18c
	ulong now;
	int i, n, s;
.
15,16d
## diffname port/alarm.c 1992/0711
## diff -e /n/bootesdump/1992/0701/sys/src/9/port/alarm.c /n/bootesdump/1992/0711/sys/src/9/port/alarm.c
17d
## diffname port/alarm.c 1993/0501
## diff -e /n/bootesdump/1992/0711/sys/src/9/port/alarm.c /n/fornaxdump/1993/0501/sys/src/brazil/port/alarm.c
121c
	up->alarm = when;
.
119c
		alarms.head = up;
.
116c
		*l = up;
.
110,111c
				up->palarm = f;
				*l = up;
.
105c
	up->palarm = 0;
.
98c
		if(up == f){
.
90c
		up->alarm = 0;
.
87,88c
	old = TK2MS(up->alarm - MACHP(0)->ticks);
.
84c
	Proc **l, *f;
.
## diffname port/alarm.c 1994/0429
## diff -e /n/fornaxdump/1993/0501/sys/src/brazil/port/alarm.c /n/fornaxdump/1994/0429/sys/src/brazil/port/alarm.c
87c
	if(up->alarm)
		old = TK2MS(up->alarm - MACHP(0)->ticks);
	else
		old = 0;
.
## diffname port/alarm.c 1994/0624
## diff -e /n/fornaxdump/1994/0429/sys/src/brazil/port/alarm.c /n/fornaxdump/1994/0624/sys/src/brazil/port/alarm.c
6d
## diffname port/alarm.c 1995/0804
## diff -e /n/fornaxdump/1994/0624/sys/src/brazil/port/alarm.c /n/fornaxdump/1995/0804/sys/src/brazil/port/alarm.c
16,17d
12c
alarmkproc(void*)
.
## diffname port/alarm.c 1997/0327
## diff -e /n/fornaxdump/1995/0804/sys/src/brazil/port/alarm.c /n/emeliedump/1997/0327/sys/src/brazil/port/alarm.c
55c
	if(talarm.list == 0 || !canlock(&talarm))
.
## diffname port/alarm.c 1998/0512
## diff -e /n/emeliedump/1997/0327/sys/src/brazil/port/alarm.c /n/emeliedump/1998/0512/sys/src/brazil/port/alarm.c
123c
	return old;
.
## diffname port/alarm.c 2002/0326
## diff -e /n/emeliedump/1998/0512/sys/src/brazil/port/alarm.c /n/emeliedump/2002/0326/sys/src/9/port/alarm.c
92c
	when = ms2tk(time)+MACHP(0)->ticks;
.
## diffname port/alarm.c 2002/0710
## diff -e /n/emeliedump/2002/0326/sys/src/9/port/alarm.c /n/emeliedump/2002/0710/sys/src/9/port/alarm.c
85c
		old = tk2ms(up->alarm - MACHP(0)->ticks);
.
## diffname port/alarm.c 2002/0730
## diff -e /n/emeliedump/2002/0710/sys/src/9/port/alarm.c /n/emeliedump/2002/0730/sys/src/9/port/alarm.c
16a
print("alarmkproc\n");
.
## diffname port/alarm.c 2002/0731
## diff -e /n/emeliedump/2002/0730/sys/src/9/port/alarm.c /n/emeliedump/2002/0731/sys/src/9/port/alarm.c
17d

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].