9fans archive / 1996 / 08 / 51 /    prev next

From: philw@pla... philw@pla...
Subject: No subject
Date: Fri, 23 Aug 1996 18:03:29 -0400

Put this in the library ...
philw.

#include <alef.h>

/*
 * Multiple readers/Single Write lock package
*/

void
RWlock.Rlock(RWlock *l)
{
	l->x.lock();		/* wait here for writers and exclusion */
	l->lock();
	l->readers++;
	l->k.canlock();		/* block writers if we are the first reader */
	l->unlock();
	l->x.unlock();
}

void
RWlock.Runlock(RWlock *l)
{
	l->lock();
	if(--l->readers == 0)	/* last reader out allows writers */
		l->k.unlock();
	l->unlock();
}

void
RWlock.Wlock(RWlock *l)
{
	l->x.lock();		/* wait here for writers and exclusion */
	l->k.lock();		/* wait here for last reader */
}

void
RWlock.Wunlock(RWlock *l)
{
	l->k.unlock();
	l->x.unlock();
}