Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/ss/lock.c

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


## diffname ss/lock.c 1990/1223
## diff -e /dev/null /n/bootesdump/1990/1223/sys/src/9/sparc/lock.c
0a
#include "u.h"
#include "lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"

#define PCOFF -1

/*
 *  N.B.  Ken's compiler generates a TAS instruction for the sequence:
 *
 *  	if(l->key >= 0){
 *		l->key |= 0x80;
 *		...
 *
 *	DO NOT TAKE THE ADDRESS OF l->key or the TAS will disappear.
 */
void
lock(Lock *l)
{
	Lock *ll = l;	/* do NOT take the address of l */
	int i;

	/*
	 * Try the fast grab first
	 */
    	if(ll->key >= 0){
		ll->key |= 0x80;
		ll->pc = ((ulong*)&l)[PCOFF];
		return;
	}
	for(i=0; i<10000000; i++)
    		if(ll->key >= 0){
			ll->key |= 0x80;
			ll->pc = ((ulong*)&l)[PCOFF];
			return;
		}
	ll->key = 0;
dumpstack();
	panic("lock loop %lux pc %lux held by pc %lux\n", l, ((ulong*)&l)[PCOFF], l->pc);
}

int
canlock(Lock *l)
{
	Lock *ll = l;	/* do NOT take the address of l */
	if(ll->key >= 0){
		ll->key |= 0x80;
		ll->pc = ((ulong*)&l)[PCOFF];
		return 1;
	}
	return 0;
}

void
unlock(Lock *l)
{
	l->pc = 0;
	l->key = 0;
}

void
qlock(QLock *q)
{
	Proc *p;

	if(canlock(&q->use))
		return;
	lock(&q->queue);
	if(canlock(&q->use)){
		unlock(&q->queue);
		return;
	}
	p = q->tail;
	if(p == 0)
		q->head = u->p;
	else
		p->qnext = u->p;
	q->tail = u->p;
	u->p->qnext = 0;
	u->p->state = Queueing;
	u->p->qlock = q;	/* DEBUG */
	unlock(&q->queue);
	sched();
}

int
canqlock(QLock *q)
{
	return canlock(&q->use);
}

void
qunlock(QLock *q)
{
	Proc *p;

	lock(&q->queue);
	u->p->qlock = 0;
	if(q->head){
		p = q->head;
		q->head = p->qnext;
		if(q->head == 0)
			q->tail = 0;
		unlock(&q->queue);
		ready(p);
	}else{
		unlock(&q->use);
		unlock(&q->queue);
	}
}
.
## diffname ss/lock.c 1990/1226
## diff -e /n/bootesdump/1990/1223/sys/src/9/sparc/lock.c /n/bootesdump/1990/1226/sys/src/9/sparc/lock.c
99c
/*	u->p->qlock = 0; /* DEBUG */
.
82c
/*	u->p->qlock = q;	/* DEBUG */
.
46,48c
	Lock *ll = l;		/* so can take address of l */

	if(swap1(&ll->key) == 0){
.
33,34c
    		if(swap1(&ll->key) == 0){
.
31a
reset();
.
27,28c
    	if(swap1(&ll->key) == 0){
.
21c
	Lock *ll = l;		/* so can take address of l */
.
9,17d
## diffname ss/lock.c 1991/0109
## diff -e /n/bootesdump/1990/1226/sys/src/9/sparc/lock.c /n/bootesdump/1991/0109/sys/src/9/sparc/lock.c
22d
## diffname ss/lock.c 1991/0201 # deleted
## diff -e /n/bootesdump/1991/0109/sys/src/9/sparc/lock.c /n/bootesdump/1991/0201/sys/src/9/sparc/lock.c
1,100d

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].