xref: /plan9-contrib/sys/src/cmd/unix/drawterm/kern/rwlock.c (revision 781103c4074deb8af160e8a0da2742ba6b29dc2b)
1 #include "u.h"
2 #include "lib.h"
3 #include "dat.h"
4 #include "fns.h"
5 #include "error.h"
6 
7 void
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
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
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
35 wunlock(RWlock *l)
36 {
37 	qunlock(&l->k);
38 	qunlock(&l->x);
39 }
40