1 #include "u.h"
2 #include "lib.h"
3 #include "dat.h"
4 #include "fns.h"
5 #include "error.h"
6 
7 void
rlock(RWlock * l)8 rlock(RWlock *l)
9 {
10 	qlock(&l->x);		/* wait here for writers and exclusion */
11 	lock(&l->lk);
12 	l->readers++;
13 	canqlock(&l->k);	/* block writers if we are the first reader */
14 	unlock(&l->lk);
15 	qunlock(&l->x);
16 }
17 
18 void
runlock(RWlock * l)19 runlock(RWlock *l)
20 {
21 	lock(&l->lk);
22 	if(--l->readers == 0)	/* last reader out allows writers */
23 		qunlock(&l->k);
24 	unlock(&l->lk);
25 }
26 
27 void
wlock(RWlock * l)28 wlock(RWlock *l)
29 {
30 	qlock(&l->x);		/* wait here for writers and exclusion */
31 	qlock(&l->k);		/* wait here for last reader */
32 }
33 
34 void
wunlock(RWlock * l)35 wunlock(RWlock *l)
36 {
37 	qunlock(&l->k);
38 	qunlock(&l->x);
39 }
40