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

1999/0501/port/taslock.c (diff list | history)

port/taslock.c on 1992/0222
1992/0222    
#include "u.h" 
1992/0321    
#include "../port/lib.h" 
1992/0222    
#include "mem.h" 
#include "dat.h" 
#include "fns.h" 
#include "../port/error.h" 
 
1998/0606    
struct { 
	ulong	locks; 
	ulong	glare; 
	ulong	inglare; 
} lockstats; 
 
1992/0222    
void 
1995/1030    
lockloop(Lock *l, ulong pc) 
{ 
1998/0606    
	Proc *p; 
 
	p = l->p; 
1998/0825    
	print("lock loop key 0x%lux pc 0x%lux held by pc 0x%lux proc %lud\n", 
1998/0606    
		l->key, pc, l->pc, p ? p->pid : 0); 
1995/1030    
	dumpaproc(up); 
1998/0606    
	if(p != nil) 
		dumpaproc(p); 
1996/0523    
 
1998/0604    
	if(up && up->state == Running && islo()) 
1996/0523    
		sched(); 
1995/1030    
} 
 
void 
1992/0222    
lock(Lock *l) 
{ 
1998/0606    
	int i, cansched; 
	ulong pc, oldpri; 
1995/1009    
 
1999/0501    
	pc = getcallerpc(&l); 
1995/1009    
 
1998/0606    
lockstats.locks++; 
1996/0522    
	if(tas(&l->key) == 0){ 
		l->pc = pc; 
1998/0604    
		l->p = up; 
		l->isilock = 0; 
1995/0110    
		return; 
1995/1009    
	} 
1995/0108    
 
1998/0606    
lockstats.glare++; 
	cansched = up != nil && up->state == Running; 
	if(cansched){ 
		oldpri = up->priority; 
		up->lockwait = 1; 
		up->priority = PriLock; 
	} else 
		oldpri = 0; 
 
1996/0522    
	for(;;){ 
1998/0606    
lockstats.inglare++; 
1996/0522    
		i = 0; 
1998/0604    
		while(l->key){ 
1998/0606    
			if(conf.nmach < 2 && cansched){ 
1998/0604    
				if(i++ > 1000){ 
					i = 0; 
					lockloop(l, pc); 
				} 
				sched(); 
			} else { 
				if(i++ > 100000000){ 
					i = 0; 
					lockloop(l, pc); 
				} 
1996/0522    
			} 
1998/0604    
		} 
1995/1009    
		if(tas(&l->key) == 0){ 
			l->pc = pc; 
1998/0604    
			l->p = up; 
			l->isilock = 0; 
1998/0606    
			if(cansched){ 
				up->lockwait = 0; 
				up->priority = oldpri; 
1998/0604    
			} 
1995/0108    
			return; 
1995/1009    
		} 
1995/0108    
	} 
1995/0110    
} 
1995/0108    
 
1995/0110    
void 
ilock(Lock *l) 
{ 
	ulong x; 
1998/0606    
	ulong pc, oldpri; 
	int cansched; 
1995/0110    
 
1999/0501    
	pc = getcallerpc(&l); 
1998/0606    
lockstats.locks++; 
1995/1009    
 
1995/0110    
	x = splhi(); 
	if(tas(&l->key) == 0){ 
		l->sr = x; 
1995/1009    
		l->pc = pc; 
1998/0604    
		l->p = up; 
		l->isilock = 1; 
1993/1204    
		return; 
1995/0110    
	} 
 
1998/0606    
lockstats.glare++; 
	cansched = up != nil && up->state == Running; 
	if(cansched){ 
		oldpri = up->priority; 
		up->lockwait = 1; 
		up->priority = PriLock; 
	} else 
		oldpri = 0; 
1998/0522    
	if(conf.nmach < 2) 
1999/0402    
		panic("ilock: no way out: pc %uX\n", pc); 
1998/0522    
 
1993/0830    
	for(;;){ 
1998/0606    
lockstats.inglare++; 
1998/0228    
		splx(x); 
1994/0808    
		while(l->key) 
			; 
1998/0228    
		x = splhi(); 
1995/0110    
		if(tas(&l->key) == 0){ 
			l->sr = x; 
1995/1009    
			l->pc = pc; 
1998/0604    
			l->p = up; 
			l->isilock = 1; 
1998/0606    
			if(cansched){ 
				up->lockwait = 0; 
				up->priority = oldpri; 
			} 
1992/0222    
			return; 
1995/0110    
		} 
1992/0222    
	} 
} 
 
1995/0109    
int 
canlock(Lock *l) 
1994/0322    
{ 
1996/0522    
	if(tas(&l->key)) 
1996/0511    
		return 0; 
1996/0522    
 
1999/0501    
	l->pc = getcallerpc(&l); 
1998/0604    
	l->p = up; 
	l->isilock = 0; 
1992/0222    
	return 1; 
} 
 
void 
unlock(Lock *l) 
{ 
1998/0604    
 
1998/0522    
	if(l->key == 0) 
1999/0501    
		print("unlock: not locked: pc %luX\n", getcallerpc(&l)); 
1998/0604    
	if(l->isilock) 
1999/0501    
		print("iunlock of lock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc); 
1996/0522    
	l->pc = 0; 
1997/0327    
	l->key = 0; 
1997/0220    
	coherence(); 
1994/0322    
} 
 
void 
iunlock(Lock *l) 
{ 
	ulong sr; 
 
1998/0603    
	if(l->key == 0) 
1999/0501    
		print("iunlock: not locked: pc %luX\n", getcallerpc(&l)); 
1998/0604    
	if(!l->isilock) 
1999/0501    
		print("unlock of ilock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc); 
1998/0603    
 
1994/0322    
	sr = l->sr; 
1995/1014    
	l->pc = 0; 
1997/0327    
	l->key = 0; 
1997/0220    
	coherence(); 
1998/0606    
 
1999/0501    
	m->splpc = getcallerpc(&l); 
1998/0606    
	splxpc(sr); 
1992/0222    
} 


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