xref: /freebsd/sys/contrib/ck/include/ck_rwlock.h (revision 4f9d94bf6491250b649f5bc931b6d93e68373005)
1*1fb62fb0SOlivier Houchard /*
2*1fb62fb0SOlivier Houchard  * Copyright 2011-2015 Samy Al Bahra.
3*1fb62fb0SOlivier Houchard  * All rights reserved.
4*1fb62fb0SOlivier Houchard  *
5*1fb62fb0SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
6*1fb62fb0SOlivier Houchard  * modification, are permitted provided that the following conditions
7*1fb62fb0SOlivier Houchard  * are met:
8*1fb62fb0SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
9*1fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
10*1fb62fb0SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
11*1fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
12*1fb62fb0SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
13*1fb62fb0SOlivier Houchard  *
14*1fb62fb0SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*1fb62fb0SOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*1fb62fb0SOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*1fb62fb0SOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*1fb62fb0SOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*1fb62fb0SOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*1fb62fb0SOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*1fb62fb0SOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*1fb62fb0SOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*1fb62fb0SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*1fb62fb0SOlivier Houchard  * SUCH DAMAGE.
25*1fb62fb0SOlivier Houchard  */
26*1fb62fb0SOlivier Houchard 
27*1fb62fb0SOlivier Houchard #ifndef CK_RWLOCK_H
28*1fb62fb0SOlivier Houchard #define CK_RWLOCK_H
29*1fb62fb0SOlivier Houchard 
30*1fb62fb0SOlivier Houchard #include <ck_elide.h>
31*1fb62fb0SOlivier Houchard #include <ck_pr.h>
32*1fb62fb0SOlivier Houchard #include <ck_stdbool.h>
33*1fb62fb0SOlivier Houchard #include <ck_stddef.h>
34*1fb62fb0SOlivier Houchard 
35*1fb62fb0SOlivier Houchard struct ck_rwlock {
36*1fb62fb0SOlivier Houchard 	unsigned int writer;
37*1fb62fb0SOlivier Houchard 	unsigned int n_readers;
38*1fb62fb0SOlivier Houchard };
39*1fb62fb0SOlivier Houchard typedef struct ck_rwlock ck_rwlock_t;
40*1fb62fb0SOlivier Houchard 
41*1fb62fb0SOlivier Houchard #define CK_RWLOCK_INITIALIZER {0, 0}
42*1fb62fb0SOlivier Houchard 
43*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_rwlock_init(struct ck_rwlock * rw)44*1fb62fb0SOlivier Houchard ck_rwlock_init(struct ck_rwlock *rw)
45*1fb62fb0SOlivier Houchard {
46*1fb62fb0SOlivier Houchard 
47*1fb62fb0SOlivier Houchard 	rw->writer = 0;
48*1fb62fb0SOlivier Houchard 	rw->n_readers = 0;
49*1fb62fb0SOlivier Houchard 	ck_pr_barrier();
50*1fb62fb0SOlivier Houchard 	return;
51*1fb62fb0SOlivier Houchard }
52*1fb62fb0SOlivier Houchard 
53*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_rwlock_write_unlock(ck_rwlock_t * rw)54*1fb62fb0SOlivier Houchard ck_rwlock_write_unlock(ck_rwlock_t *rw)
55*1fb62fb0SOlivier Houchard {
56*1fb62fb0SOlivier Houchard 
57*1fb62fb0SOlivier Houchard 	ck_pr_fence_unlock();
58*1fb62fb0SOlivier Houchard 	ck_pr_store_uint(&rw->writer, 0);
59*1fb62fb0SOlivier Houchard 	return;
60*1fb62fb0SOlivier Houchard }
61*1fb62fb0SOlivier Houchard 
62*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_rwlock_locked_writer(ck_rwlock_t * rw)63*1fb62fb0SOlivier Houchard ck_rwlock_locked_writer(ck_rwlock_t *rw)
64*1fb62fb0SOlivier Houchard {
65*1fb62fb0SOlivier Houchard 	bool r;
66*1fb62fb0SOlivier Houchard 
67*1fb62fb0SOlivier Houchard 	r = ck_pr_load_uint(&rw->writer);
68*1fb62fb0SOlivier Houchard 	ck_pr_fence_acquire();
69*1fb62fb0SOlivier Houchard 	return r;
70*1fb62fb0SOlivier Houchard }
71*1fb62fb0SOlivier Houchard 
72*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_rwlock_write_downgrade(ck_rwlock_t * rw)73*1fb62fb0SOlivier Houchard ck_rwlock_write_downgrade(ck_rwlock_t *rw)
74*1fb62fb0SOlivier Houchard {
75*1fb62fb0SOlivier Houchard 
76*1fb62fb0SOlivier Houchard 	ck_pr_inc_uint(&rw->n_readers);
77*1fb62fb0SOlivier Houchard 	ck_rwlock_write_unlock(rw);
78*1fb62fb0SOlivier Houchard 	return;
79*1fb62fb0SOlivier Houchard }
80*1fb62fb0SOlivier Houchard 
81*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_rwlock_locked(ck_rwlock_t * rw)82*1fb62fb0SOlivier Houchard ck_rwlock_locked(ck_rwlock_t *rw)
83*1fb62fb0SOlivier Houchard {
84*1fb62fb0SOlivier Houchard 	bool l;
85*1fb62fb0SOlivier Houchard 
86*1fb62fb0SOlivier Houchard 	l = ck_pr_load_uint(&rw->n_readers) |
87*1fb62fb0SOlivier Houchard 	    ck_pr_load_uint(&rw->writer);
88*1fb62fb0SOlivier Houchard 	ck_pr_fence_acquire();
89*1fb62fb0SOlivier Houchard 	return l;
90*1fb62fb0SOlivier Houchard }
91*1fb62fb0SOlivier Houchard 
92*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_rwlock_write_trylock(ck_rwlock_t * rw)93*1fb62fb0SOlivier Houchard ck_rwlock_write_trylock(ck_rwlock_t *rw)
94*1fb62fb0SOlivier Houchard {
95*1fb62fb0SOlivier Houchard 
96*1fb62fb0SOlivier Houchard 	if (ck_pr_fas_uint(&rw->writer, 1) != 0)
97*1fb62fb0SOlivier Houchard 		return false;
98*1fb62fb0SOlivier Houchard 
99*1fb62fb0SOlivier Houchard 	ck_pr_fence_atomic_load();
100*1fb62fb0SOlivier Houchard 
101*1fb62fb0SOlivier Houchard 	if (ck_pr_load_uint(&rw->n_readers) != 0) {
102*1fb62fb0SOlivier Houchard 		ck_rwlock_write_unlock(rw);
103*1fb62fb0SOlivier Houchard 		return false;
104*1fb62fb0SOlivier Houchard 	}
105*1fb62fb0SOlivier Houchard 
106*1fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
107*1fb62fb0SOlivier Houchard 	return true;
108*1fb62fb0SOlivier Houchard }
109*1fb62fb0SOlivier Houchard 
CK_ELIDE_TRYLOCK_PROTOTYPE(ck_rwlock_write,ck_rwlock_t,ck_rwlock_locked,ck_rwlock_write_trylock)110*1fb62fb0SOlivier Houchard CK_ELIDE_TRYLOCK_PROTOTYPE(ck_rwlock_write, ck_rwlock_t,
111*1fb62fb0SOlivier Houchard     ck_rwlock_locked, ck_rwlock_write_trylock)
112*1fb62fb0SOlivier Houchard 
113*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
114*1fb62fb0SOlivier Houchard ck_rwlock_write_lock(ck_rwlock_t *rw)
115*1fb62fb0SOlivier Houchard {
116*1fb62fb0SOlivier Houchard 
117*1fb62fb0SOlivier Houchard 	while (ck_pr_fas_uint(&rw->writer, 1) != 0)
118*1fb62fb0SOlivier Houchard 		ck_pr_stall();
119*1fb62fb0SOlivier Houchard 
120*1fb62fb0SOlivier Houchard 	ck_pr_fence_atomic_load();
121*1fb62fb0SOlivier Houchard 
122*1fb62fb0SOlivier Houchard 	while (ck_pr_load_uint(&rw->n_readers) != 0)
123*1fb62fb0SOlivier Houchard 		ck_pr_stall();
124*1fb62fb0SOlivier Houchard 
125*1fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
126*1fb62fb0SOlivier Houchard 	return;
127*1fb62fb0SOlivier Houchard }
128*1fb62fb0SOlivier Houchard 
CK_ELIDE_PROTOTYPE(ck_rwlock_write,ck_rwlock_t,ck_rwlock_locked,ck_rwlock_write_lock,ck_rwlock_locked_writer,ck_rwlock_write_unlock)129*1fb62fb0SOlivier Houchard CK_ELIDE_PROTOTYPE(ck_rwlock_write, ck_rwlock_t,
130*1fb62fb0SOlivier Houchard     ck_rwlock_locked, ck_rwlock_write_lock,
131*1fb62fb0SOlivier Houchard     ck_rwlock_locked_writer, ck_rwlock_write_unlock)
132*1fb62fb0SOlivier Houchard 
133*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
134*1fb62fb0SOlivier Houchard ck_rwlock_read_trylock(ck_rwlock_t *rw)
135*1fb62fb0SOlivier Houchard {
136*1fb62fb0SOlivier Houchard 
137*1fb62fb0SOlivier Houchard 	if (ck_pr_load_uint(&rw->writer) != 0)
138*1fb62fb0SOlivier Houchard 		return false;
139*1fb62fb0SOlivier Houchard 
140*1fb62fb0SOlivier Houchard 	ck_pr_inc_uint(&rw->n_readers);
141*1fb62fb0SOlivier Houchard 
142*1fb62fb0SOlivier Houchard 	/*
143*1fb62fb0SOlivier Houchard 	 * Serialize with respect to concurrent write
144*1fb62fb0SOlivier Houchard 	 * lock operation.
145*1fb62fb0SOlivier Houchard 	 */
146*1fb62fb0SOlivier Houchard 	ck_pr_fence_atomic_load();
147*1fb62fb0SOlivier Houchard 
148*1fb62fb0SOlivier Houchard 	if (ck_pr_load_uint(&rw->writer) == 0) {
149*1fb62fb0SOlivier Houchard 		ck_pr_fence_lock();
150*1fb62fb0SOlivier Houchard 		return true;
151*1fb62fb0SOlivier Houchard 	}
152*1fb62fb0SOlivier Houchard 
153*1fb62fb0SOlivier Houchard 	ck_pr_dec_uint(&rw->n_readers);
154*1fb62fb0SOlivier Houchard 	return false;
155*1fb62fb0SOlivier Houchard }
156*1fb62fb0SOlivier Houchard 
CK_ELIDE_TRYLOCK_PROTOTYPE(ck_rwlock_read,ck_rwlock_t,ck_rwlock_locked_writer,ck_rwlock_read_trylock)157*1fb62fb0SOlivier Houchard CK_ELIDE_TRYLOCK_PROTOTYPE(ck_rwlock_read, ck_rwlock_t,
158*1fb62fb0SOlivier Houchard     ck_rwlock_locked_writer, ck_rwlock_read_trylock)
159*1fb62fb0SOlivier Houchard 
160*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
161*1fb62fb0SOlivier Houchard ck_rwlock_read_lock(ck_rwlock_t *rw)
162*1fb62fb0SOlivier Houchard {
163*1fb62fb0SOlivier Houchard 
164*1fb62fb0SOlivier Houchard 	for (;;) {
165*1fb62fb0SOlivier Houchard 		while (ck_pr_load_uint(&rw->writer) != 0)
166*1fb62fb0SOlivier Houchard 			ck_pr_stall();
167*1fb62fb0SOlivier Houchard 
168*1fb62fb0SOlivier Houchard 		ck_pr_inc_uint(&rw->n_readers);
169*1fb62fb0SOlivier Houchard 
170*1fb62fb0SOlivier Houchard 		/*
171*1fb62fb0SOlivier Houchard 		 * Serialize with respect to concurrent write
172*1fb62fb0SOlivier Houchard 		 * lock operation.
173*1fb62fb0SOlivier Houchard 		 */
174*1fb62fb0SOlivier Houchard 		ck_pr_fence_atomic_load();
175*1fb62fb0SOlivier Houchard 
176*1fb62fb0SOlivier Houchard 		if (ck_pr_load_uint(&rw->writer) == 0)
177*1fb62fb0SOlivier Houchard 			break;
178*1fb62fb0SOlivier Houchard 
179*1fb62fb0SOlivier Houchard 		ck_pr_dec_uint(&rw->n_readers);
180*1fb62fb0SOlivier Houchard 	}
181*1fb62fb0SOlivier Houchard 
182*1fb62fb0SOlivier Houchard 	/* Acquire semantics are necessary. */
183*1fb62fb0SOlivier Houchard 	ck_pr_fence_load();
184*1fb62fb0SOlivier Houchard 	return;
185*1fb62fb0SOlivier Houchard }
186*1fb62fb0SOlivier Houchard 
187*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_rwlock_locked_reader(ck_rwlock_t * rw)188*1fb62fb0SOlivier Houchard ck_rwlock_locked_reader(ck_rwlock_t *rw)
189*1fb62fb0SOlivier Houchard {
190*1fb62fb0SOlivier Houchard 
191*1fb62fb0SOlivier Houchard 	ck_pr_fence_load();
192*1fb62fb0SOlivier Houchard 	return ck_pr_load_uint(&rw->n_readers);
193*1fb62fb0SOlivier Houchard }
194*1fb62fb0SOlivier Houchard 
195*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_rwlock_read_unlock(ck_rwlock_t * rw)196*1fb62fb0SOlivier Houchard ck_rwlock_read_unlock(ck_rwlock_t *rw)
197*1fb62fb0SOlivier Houchard {
198*1fb62fb0SOlivier Houchard 
199*1fb62fb0SOlivier Houchard 	ck_pr_fence_load_atomic();
200*1fb62fb0SOlivier Houchard 	ck_pr_dec_uint(&rw->n_readers);
201*1fb62fb0SOlivier Houchard 	return;
202*1fb62fb0SOlivier Houchard }
203*1fb62fb0SOlivier Houchard 
204*1fb62fb0SOlivier Houchard CK_ELIDE_PROTOTYPE(ck_rwlock_read, ck_rwlock_t,
205*1fb62fb0SOlivier Houchard     ck_rwlock_locked_writer, ck_rwlock_read_lock,
206*1fb62fb0SOlivier Houchard     ck_rwlock_locked_reader, ck_rwlock_read_unlock)
207*1fb62fb0SOlivier Houchard 
208*1fb62fb0SOlivier Houchard /*
209*1fb62fb0SOlivier Houchard  * Recursive writer reader-writer lock implementation.
210*1fb62fb0SOlivier Houchard  */
211*1fb62fb0SOlivier Houchard struct ck_rwlock_recursive {
212*1fb62fb0SOlivier Houchard 	struct ck_rwlock rw;
213*1fb62fb0SOlivier Houchard 	unsigned int wc;
214*1fb62fb0SOlivier Houchard };
215*1fb62fb0SOlivier Houchard typedef struct ck_rwlock_recursive ck_rwlock_recursive_t;
216*1fb62fb0SOlivier Houchard 
217*1fb62fb0SOlivier Houchard #define CK_RWLOCK_RECURSIVE_INITIALIZER {CK_RWLOCK_INITIALIZER, 0}
218*1fb62fb0SOlivier Houchard 
219*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_rwlock_recursive_write_lock(ck_rwlock_recursive_t * rw,unsigned int tid)220*1fb62fb0SOlivier Houchard ck_rwlock_recursive_write_lock(ck_rwlock_recursive_t *rw, unsigned int tid)
221*1fb62fb0SOlivier Houchard {
222*1fb62fb0SOlivier Houchard 	unsigned int o;
223*1fb62fb0SOlivier Houchard 
224*1fb62fb0SOlivier Houchard 	o = ck_pr_load_uint(&rw->rw.writer);
225*1fb62fb0SOlivier Houchard 	if (o == tid)
226*1fb62fb0SOlivier Houchard 		goto leave;
227*1fb62fb0SOlivier Houchard 
228*1fb62fb0SOlivier Houchard 	while (ck_pr_cas_uint(&rw->rw.writer, 0, tid) == false)
229*1fb62fb0SOlivier Houchard 		ck_pr_stall();
230*1fb62fb0SOlivier Houchard 
231*1fb62fb0SOlivier Houchard 	ck_pr_fence_atomic_load();
232*1fb62fb0SOlivier Houchard 
233*1fb62fb0SOlivier Houchard 	while (ck_pr_load_uint(&rw->rw.n_readers) != 0)
234*1fb62fb0SOlivier Houchard 		ck_pr_stall();
235*1fb62fb0SOlivier Houchard 
236*1fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
237*1fb62fb0SOlivier Houchard leave:
238*1fb62fb0SOlivier Houchard 	rw->wc++;
239*1fb62fb0SOlivier Houchard 	return;
240*1fb62fb0SOlivier Houchard }
241*1fb62fb0SOlivier Houchard 
242*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_rwlock_recursive_write_trylock(ck_rwlock_recursive_t * rw,unsigned int tid)243*1fb62fb0SOlivier Houchard ck_rwlock_recursive_write_trylock(ck_rwlock_recursive_t *rw, unsigned int tid)
244*1fb62fb0SOlivier Houchard {
245*1fb62fb0SOlivier Houchard 	unsigned int o;
246*1fb62fb0SOlivier Houchard 
247*1fb62fb0SOlivier Houchard 	o = ck_pr_load_uint(&rw->rw.writer);
248*1fb62fb0SOlivier Houchard 	if (o == tid)
249*1fb62fb0SOlivier Houchard 		goto leave;
250*1fb62fb0SOlivier Houchard 
251*1fb62fb0SOlivier Houchard 	if (ck_pr_cas_uint(&rw->rw.writer, 0, tid) == false)
252*1fb62fb0SOlivier Houchard 		return false;
253*1fb62fb0SOlivier Houchard 
254*1fb62fb0SOlivier Houchard 	ck_pr_fence_atomic_load();
255*1fb62fb0SOlivier Houchard 
256*1fb62fb0SOlivier Houchard 	if (ck_pr_load_uint(&rw->rw.n_readers) != 0) {
257*1fb62fb0SOlivier Houchard 		ck_pr_store_uint(&rw->rw.writer, 0);
258*1fb62fb0SOlivier Houchard 		return false;
259*1fb62fb0SOlivier Houchard 	}
260*1fb62fb0SOlivier Houchard 
261*1fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
262*1fb62fb0SOlivier Houchard leave:
263*1fb62fb0SOlivier Houchard 	rw->wc++;
264*1fb62fb0SOlivier Houchard 	return true;
265*1fb62fb0SOlivier Houchard }
266*1fb62fb0SOlivier Houchard 
267*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_rwlock_recursive_write_unlock(ck_rwlock_recursive_t * rw)268*1fb62fb0SOlivier Houchard ck_rwlock_recursive_write_unlock(ck_rwlock_recursive_t *rw)
269*1fb62fb0SOlivier Houchard {
270*1fb62fb0SOlivier Houchard 
271*1fb62fb0SOlivier Houchard 	if (--rw->wc == 0) {
272*1fb62fb0SOlivier Houchard 		ck_pr_fence_unlock();
273*1fb62fb0SOlivier Houchard 		ck_pr_store_uint(&rw->rw.writer, 0);
274*1fb62fb0SOlivier Houchard 	}
275*1fb62fb0SOlivier Houchard 
276*1fb62fb0SOlivier Houchard 	return;
277*1fb62fb0SOlivier Houchard }
278*1fb62fb0SOlivier Houchard 
279*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_rwlock_recursive_read_lock(ck_rwlock_recursive_t * rw)280*1fb62fb0SOlivier Houchard ck_rwlock_recursive_read_lock(ck_rwlock_recursive_t *rw)
281*1fb62fb0SOlivier Houchard {
282*1fb62fb0SOlivier Houchard 
283*1fb62fb0SOlivier Houchard 	ck_rwlock_read_lock(&rw->rw);
284*1fb62fb0SOlivier Houchard 	return;
285*1fb62fb0SOlivier Houchard }
286*1fb62fb0SOlivier Houchard 
287*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_rwlock_recursive_read_trylock(ck_rwlock_recursive_t * rw)288*1fb62fb0SOlivier Houchard ck_rwlock_recursive_read_trylock(ck_rwlock_recursive_t *rw)
289*1fb62fb0SOlivier Houchard {
290*1fb62fb0SOlivier Houchard 
291*1fb62fb0SOlivier Houchard 	return ck_rwlock_read_trylock(&rw->rw);
292*1fb62fb0SOlivier Houchard }
293*1fb62fb0SOlivier Houchard 
294*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_rwlock_recursive_read_unlock(ck_rwlock_recursive_t * rw)295*1fb62fb0SOlivier Houchard ck_rwlock_recursive_read_unlock(ck_rwlock_recursive_t *rw)
296*1fb62fb0SOlivier Houchard {
297*1fb62fb0SOlivier Houchard 
298*1fb62fb0SOlivier Houchard 	ck_rwlock_read_unlock(&rw->rw);
299*1fb62fb0SOlivier Houchard 	return;
300*1fb62fb0SOlivier Houchard }
301*1fb62fb0SOlivier Houchard 
302*1fb62fb0SOlivier Houchard #endif /* CK_RWLOCK_H */
303