a
This commit is contained in:
39
kern/rwlock.c
Normal file
39
kern/rwlock.c
Normal file
@ -0,0 +1,39 @@
|
||||
#include "u.h"
|
||||
#include "lib.h"
|
||||
#include "dat.h"
|
||||
#include "fns.h"
|
||||
#include "error.h"
|
||||
|
||||
void
|
||||
rlock(RWlock *l)
|
||||
{
|
||||
qlock(&l->x); /* wait here for writers and exclusion */
|
||||
lock(&l->lk);
|
||||
l->readers++;
|
||||
canqlock(&l->k); /* block writers if we are the first reader */
|
||||
unlock(&l->lk);
|
||||
qunlock(&l->x);
|
||||
}
|
||||
|
||||
void
|
||||
runlock(RWlock *l)
|
||||
{
|
||||
lock(&l->lk);
|
||||
if(--l->readers == 0) /* last reader out allows writers */
|
||||
qunlock(&l->k);
|
||||
unlock(&l->lk);
|
||||
}
|
||||
|
||||
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->k);
|
||||
qunlock(&l->x);
|
||||
}
|
Reference in New Issue
Block a user