plan 9 kernel history: overview | file list | diff list

1990/1012/gnot/clock.c (diff list | history)

1990/1012/sys/src/9/gnot/clock.c:1,1361990/1106/sys/src/9/gnot/clock.c:1,137 (short | long | prev | next)
1990/03091    
#include	"u.h" 
#include	"lib.h" 
#include	"mem.h" 
#include	"dat.h" 
#include	"fns.h" 
#include	"io.h" 
 
#include	"ureg.h" 
 
Alarm	*alarmtab; 
 
Alarm* 
alarm(int ms, void (*f)(Alarm*), void *arg) 
{ 
	Alarm *a, *w, *pw; 
	ulong s; 
	a = newalarm(); 
1990/06111    
	a->dt = MS2TK(ms); 
1990/1012    
	if(a->dt < 0) 
		a->dt = 0; 
1990/03091    
	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]); 
	} 
} 
 
void 
delay(int ms) 
{ 
	ulong t, *p; 
	int i; 
 
	ms *= 1000;	/* experimentally determined */ 
	for(i=0; i<ms; i++) 
		; 
} 
 
1990/0728    
#define NA 10 
1990/03091    
void 
clock(Ureg *ur) 
{ 
1990/0728    
	int i, n; 
1990/03091    
	Alarm *a; 
	void (*f)(void*); 
	Proc *p; 
1990/0728    
	Alarm *alist[NA]; 
1990/03091    
 
	SYNCREG[1] = 0x5F;	/* clear interrupt */ 
	m->ticks++; 
	p = m->proc; 
1990/0312    
	if(p){ 
		p->pc = ur->pc; 
1990/1004    
		if (p->state==Running) 
			p->time[p->insyscall]++; 
1990/0312    
	} 
1990/03091    
	if(canlock(&m->alarmlock)){ 
		if(m->alarm){ 
			a = m->alarm; 
			a->dt--; 
1990/0728    
			for(n = 0; a && a->dt<=0 && n<NA; n++){ 
1990/1106    
		a = m->alarm; 
		if(a){ 
			for(n=0; a && a->dt<=0 && n<NA; n++){ 
1990/0728    
				alist[n] = a; 
1990/03091    
				delete(&m->alarm, 0, a); 
				a = m->alarm; 
			} 
1990/1106    
			if(a) 
				a->dt--; 
1990/0728    
			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); 
1990/03091    
	} 
1990/0321    
	kbdclock(); 
1990/0504    
	mouseclock(); 
1990/1004    
	if((ur->sr&SPL(7)) == 0 && p && p->state==Running){ 
		sched(); 
		if(u->nnote && (ur->sr&SUPER)==0) 
			notify(ur); 
1990/03091    
	} 
} 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)