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

1991/1030/port/qlock.c (diff list | history)

1991/1030/sys/src/9/port/qlock.c:1,961992/0321/sys/src/9/port/qlock.c:1,96 (short | long | prev | next)
Move lib.h to ../port.
rsc Fri Mar 4 12:44:25 2005
1991/0428    
#include "u.h" 
#include "lib.h" 
1992/0321    
#include "../port/lib.h" 
1991/0428    
#include "mem.h" 
#include "dat.h" 
#include "fns.h" 
 
 
void 
qlock(QLock *q) 
{ 
1991/1002    
	Proc *p, *mp; 
1991/0428    
 
1991/1002    
	lock(&q->use); 
	if(!q->locked) { 
		q->locked = 1; 
		unlock(&q->use); 
1991/0428    
		return; 
	} 
	p = q->tail; 
1991/1002    
	mp = u->p; 
1991/0428    
	if(p == 0) 
1991/1002    
		q->head = mp; 
1991/0428    
	else 
1991/1002    
		p->qnext = mp; 
	q->tail = mp; 
	mp->qnext = 0; 
	mp->state = Queueing; 
	unlock(&q->use); 
1991/0428    
	sched(); 
} 
 
int 
canqlock(QLock *q) 
{ 
1991/1002    
	lock(&q->use); 
1991/1030    
	if(q->locked){ 
1991/1002    
		unlock(&q->use); 
		return 0; 
	} 
	q->locked = 1; 
	unlock(&q->use); 
	return 1; 
1991/0428    
} 
 
void 
qunlock(QLock *q) 
{ 
	Proc *p; 
 
1991/1002    
	lock(&q->use); 
	p = q->head; 
	if(p) { 
1991/0428    
		q->head = p->qnext; 
		if(q->head == 0) 
			q->tail = 0; 
		unlock(&q->use); 
1991/1002    
		ready(p); 
		return; 
1991/0428    
	} 
1991/1002    
	q->locked = 0; 
	unlock(&q->use); 
1991/0428    
} 
1991/1011    
 
void 
rlock(RWlock *l) 
{ 
	qlock(&l->x);		/* wait here for writers and exclusion */ 
	lock(l); 
	l->readers++; 
	canqlock(&l->k);	/* block writers if we are the first reader */ 
	unlock(l); 
	qunlock(&l->x); 
} 
 
void 
runlock(RWlock *l) 
{ 
	lock(l); 
	if(--l->readers == 0)	/* last reader out allows writers */ 
		qunlock(&l->k); 
	unlock(l); 
} 
 
void 
wlock(RWlock *l) 
{ 
	qlock(&l->x);		/* wait here for writers and exclusion */ 
	qlock(&l->k);		/* wait here for last reader */ 
} 
 
void 
wunlock(RWlock *l) 
{ 
	qunlock(&l->x); 
	qunlock(&l->k); 
} 


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