xref: /freebsd/sys/contrib/ck/include/ck_brlock.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_BRLOCK_H
28*1fb62fb0SOlivier Houchard #define CK_BRLOCK_H
29*1fb62fb0SOlivier Houchard 
30*1fb62fb0SOlivier Houchard /*
31*1fb62fb0SOlivier Houchard  * Big reader spinlocks provide cache-local contention-free read
32*1fb62fb0SOlivier Houchard  * lock acquisition in the absence of writers. This comes at the
33*1fb62fb0SOlivier Houchard  * cost of O(n) write lock acquisition. They were first implemented
34*1fb62fb0SOlivier Houchard  * in the Linux kernel by Ingo Molnar and David S. Miller around the
35*1fb62fb0SOlivier Houchard  * year 2000.
36*1fb62fb0SOlivier Houchard  *
37*1fb62fb0SOlivier Houchard  * This implementation is thread-agnostic which comes at the cost
38*1fb62fb0SOlivier Houchard  * of larger reader objects due to necessary linkage overhead. In
39*1fb62fb0SOlivier Houchard  * order to cut down on TLB pressure, it is recommended to allocate
40*1fb62fb0SOlivier Houchard  * these objects on the same page.
41*1fb62fb0SOlivier Houchard  */
42*1fb62fb0SOlivier Houchard 
43*1fb62fb0SOlivier Houchard #include <ck_pr.h>
44*1fb62fb0SOlivier Houchard #include <ck_stdbool.h>
45*1fb62fb0SOlivier Houchard #include <ck_stddef.h>
46*1fb62fb0SOlivier Houchard 
47*1fb62fb0SOlivier Houchard struct ck_brlock_reader {
48*1fb62fb0SOlivier Houchard 	unsigned int n_readers;
49*1fb62fb0SOlivier Houchard 	struct ck_brlock_reader *previous;
50*1fb62fb0SOlivier Houchard 	struct ck_brlock_reader *next;
51*1fb62fb0SOlivier Houchard };
52*1fb62fb0SOlivier Houchard typedef struct ck_brlock_reader ck_brlock_reader_t;
53*1fb62fb0SOlivier Houchard 
54*1fb62fb0SOlivier Houchard #define CK_BRLOCK_READER_INITIALIZER {0}
55*1fb62fb0SOlivier Houchard 
56*1fb62fb0SOlivier Houchard struct ck_brlock {
57*1fb62fb0SOlivier Houchard 	struct ck_brlock_reader *readers;
58*1fb62fb0SOlivier Houchard 	unsigned int writer;
59*1fb62fb0SOlivier Houchard };
60*1fb62fb0SOlivier Houchard typedef struct ck_brlock ck_brlock_t;
61*1fb62fb0SOlivier Houchard 
62*1fb62fb0SOlivier Houchard #define CK_BRLOCK_INITIALIZER {NULL, false}
63*1fb62fb0SOlivier Houchard 
64*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_brlock_init(struct ck_brlock * br)65*1fb62fb0SOlivier Houchard ck_brlock_init(struct ck_brlock *br)
66*1fb62fb0SOlivier Houchard {
67*1fb62fb0SOlivier Houchard 
68*1fb62fb0SOlivier Houchard 	br->readers = NULL;
69*1fb62fb0SOlivier Houchard 	br->writer = false;
70*1fb62fb0SOlivier Houchard 	ck_pr_barrier();
71*1fb62fb0SOlivier Houchard 	return;
72*1fb62fb0SOlivier Houchard }
73*1fb62fb0SOlivier Houchard 
74*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_brlock_write_lock(struct ck_brlock * br)75*1fb62fb0SOlivier Houchard ck_brlock_write_lock(struct ck_brlock *br)
76*1fb62fb0SOlivier Houchard {
77*1fb62fb0SOlivier Houchard 	struct ck_brlock_reader *cursor;
78*1fb62fb0SOlivier Houchard 
79*1fb62fb0SOlivier Houchard 	/*
80*1fb62fb0SOlivier Houchard 	 * As the frequency of write acquisitions should be low,
81*1fb62fb0SOlivier Houchard 	 * there is no point to more advanced contention avoidance.
82*1fb62fb0SOlivier Houchard 	 */
83*1fb62fb0SOlivier Houchard 	while (ck_pr_fas_uint(&br->writer, true) == true)
84*1fb62fb0SOlivier Houchard 		ck_pr_stall();
85*1fb62fb0SOlivier Houchard 
86*1fb62fb0SOlivier Houchard 	ck_pr_fence_atomic_load();
87*1fb62fb0SOlivier Houchard 
88*1fb62fb0SOlivier Houchard 	/* The reader list is protected under the writer br. */
89*1fb62fb0SOlivier Houchard 	for (cursor = br->readers; cursor != NULL; cursor = cursor->next) {
90*1fb62fb0SOlivier Houchard 		while (ck_pr_load_uint(&cursor->n_readers) != 0)
91*1fb62fb0SOlivier Houchard 			ck_pr_stall();
92*1fb62fb0SOlivier Houchard 	}
93*1fb62fb0SOlivier Houchard 
94*1fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
95*1fb62fb0SOlivier Houchard 	return;
96*1fb62fb0SOlivier Houchard }
97*1fb62fb0SOlivier Houchard 
98*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_brlock_write_unlock(struct ck_brlock * br)99*1fb62fb0SOlivier Houchard ck_brlock_write_unlock(struct ck_brlock *br)
100*1fb62fb0SOlivier Houchard {
101*1fb62fb0SOlivier Houchard 
102*1fb62fb0SOlivier Houchard 	ck_pr_fence_unlock();
103*1fb62fb0SOlivier Houchard 	ck_pr_store_uint(&br->writer, false);
104*1fb62fb0SOlivier Houchard 	return;
105*1fb62fb0SOlivier Houchard }
106*1fb62fb0SOlivier Houchard 
107*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_brlock_write_trylock(struct ck_brlock * br,unsigned int factor)108*1fb62fb0SOlivier Houchard ck_brlock_write_trylock(struct ck_brlock *br, unsigned int factor)
109*1fb62fb0SOlivier Houchard {
110*1fb62fb0SOlivier Houchard 	struct ck_brlock_reader *cursor;
111*1fb62fb0SOlivier Houchard 	unsigned int steps = 0;
112*1fb62fb0SOlivier Houchard 
113*1fb62fb0SOlivier Houchard 	while (ck_pr_fas_uint(&br->writer, true) == true) {
114*1fb62fb0SOlivier Houchard 		if (++steps >= factor)
115*1fb62fb0SOlivier Houchard 			return false;
116*1fb62fb0SOlivier Houchard 
117*1fb62fb0SOlivier Houchard 		ck_pr_stall();
118*1fb62fb0SOlivier Houchard 	}
119*1fb62fb0SOlivier Houchard 
120*1fb62fb0SOlivier Houchard 	/*
121*1fb62fb0SOlivier Houchard 	 * We do not require a strict fence here as atomic RMW operations
122*1fb62fb0SOlivier Houchard 	 * are serializing.
123*1fb62fb0SOlivier Houchard 	 */
124*1fb62fb0SOlivier Houchard 	ck_pr_fence_atomic_load();
125*1fb62fb0SOlivier Houchard 
126*1fb62fb0SOlivier Houchard 	for (cursor = br->readers; cursor != NULL; cursor = cursor->next) {
127*1fb62fb0SOlivier Houchard 		while (ck_pr_load_uint(&cursor->n_readers) != 0) {
128*1fb62fb0SOlivier Houchard 			if (++steps >= factor) {
129*1fb62fb0SOlivier Houchard 				ck_brlock_write_unlock(br);
130*1fb62fb0SOlivier Houchard 				return false;
131*1fb62fb0SOlivier Houchard 			}
132*1fb62fb0SOlivier Houchard 
133*1fb62fb0SOlivier Houchard 			ck_pr_stall();
134*1fb62fb0SOlivier Houchard 		}
135*1fb62fb0SOlivier Houchard 	}
136*1fb62fb0SOlivier Houchard 
137*1fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
138*1fb62fb0SOlivier Houchard 	return true;
139*1fb62fb0SOlivier Houchard }
140*1fb62fb0SOlivier Houchard 
141*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_brlock_read_register(struct ck_brlock * br,struct ck_brlock_reader * reader)142*1fb62fb0SOlivier Houchard ck_brlock_read_register(struct ck_brlock *br, struct ck_brlock_reader *reader)
143*1fb62fb0SOlivier Houchard {
144*1fb62fb0SOlivier Houchard 
145*1fb62fb0SOlivier Houchard 	reader->n_readers = 0;
146*1fb62fb0SOlivier Houchard 	reader->previous = NULL;
147*1fb62fb0SOlivier Houchard 
148*1fb62fb0SOlivier Houchard 	/* Implicit compiler barrier. */
149*1fb62fb0SOlivier Houchard 	ck_brlock_write_lock(br);
150*1fb62fb0SOlivier Houchard 
151*1fb62fb0SOlivier Houchard 	reader->next = ck_pr_load_ptr(&br->readers);
152*1fb62fb0SOlivier Houchard 	if (reader->next != NULL)
153*1fb62fb0SOlivier Houchard 		reader->next->previous = reader;
154*1fb62fb0SOlivier Houchard 	ck_pr_store_ptr(&br->readers, reader);
155*1fb62fb0SOlivier Houchard 
156*1fb62fb0SOlivier Houchard 	ck_brlock_write_unlock(br);
157*1fb62fb0SOlivier Houchard 	return;
158*1fb62fb0SOlivier Houchard }
159*1fb62fb0SOlivier Houchard 
160*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_brlock_read_unregister(struct ck_brlock * br,struct ck_brlock_reader * reader)161*1fb62fb0SOlivier Houchard ck_brlock_read_unregister(struct ck_brlock *br, struct ck_brlock_reader *reader)
162*1fb62fb0SOlivier Houchard {
163*1fb62fb0SOlivier Houchard 
164*1fb62fb0SOlivier Houchard 	ck_brlock_write_lock(br);
165*1fb62fb0SOlivier Houchard 
166*1fb62fb0SOlivier Houchard 	if (reader->next != NULL)
167*1fb62fb0SOlivier Houchard 		reader->next->previous = reader->previous;
168*1fb62fb0SOlivier Houchard 
169*1fb62fb0SOlivier Houchard 	if (reader->previous != NULL)
170*1fb62fb0SOlivier Houchard 		reader->previous->next = reader->next;
171*1fb62fb0SOlivier Houchard 	else
172*1fb62fb0SOlivier Houchard 		br->readers = reader->next;
173*1fb62fb0SOlivier Houchard 
174*1fb62fb0SOlivier Houchard 	ck_brlock_write_unlock(br);
175*1fb62fb0SOlivier Houchard 	return;
176*1fb62fb0SOlivier Houchard }
177*1fb62fb0SOlivier Houchard 
178*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_brlock_read_lock(struct ck_brlock * br,struct ck_brlock_reader * reader)179*1fb62fb0SOlivier Houchard ck_brlock_read_lock(struct ck_brlock *br, struct ck_brlock_reader *reader)
180*1fb62fb0SOlivier Houchard {
181*1fb62fb0SOlivier Houchard 
182*1fb62fb0SOlivier Houchard 	if (reader->n_readers >= 1) {
183*1fb62fb0SOlivier Houchard 		ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1);
184*1fb62fb0SOlivier Houchard 		return;
185*1fb62fb0SOlivier Houchard 	}
186*1fb62fb0SOlivier Houchard 
187*1fb62fb0SOlivier Houchard 	for (;;) {
188*1fb62fb0SOlivier Houchard 		while (ck_pr_load_uint(&br->writer) == true)
189*1fb62fb0SOlivier Houchard 			ck_pr_stall();
190*1fb62fb0SOlivier Houchard 
191*1fb62fb0SOlivier Houchard #if defined(__x86__) || defined(__x86_64__)
192*1fb62fb0SOlivier Houchard 		ck_pr_fas_uint(&reader->n_readers, 1);
193*1fb62fb0SOlivier Houchard 
194*1fb62fb0SOlivier Houchard 		/*
195*1fb62fb0SOlivier Houchard 		 * Serialize reader counter update with respect to load of
196*1fb62fb0SOlivier Houchard 		 * writer.
197*1fb62fb0SOlivier Houchard 		 */
198*1fb62fb0SOlivier Houchard 		ck_pr_fence_atomic_load();
199*1fb62fb0SOlivier Houchard #else
200*1fb62fb0SOlivier Houchard 		ck_pr_store_uint(&reader->n_readers, 1);
201*1fb62fb0SOlivier Houchard 
202*1fb62fb0SOlivier Houchard 		/*
203*1fb62fb0SOlivier Houchard 		 * Serialize reader counter update with respect to load of
204*1fb62fb0SOlivier Houchard 		 * writer.
205*1fb62fb0SOlivier Houchard 		 */
206*1fb62fb0SOlivier Houchard 		ck_pr_fence_store_load();
207*1fb62fb0SOlivier Houchard #endif
208*1fb62fb0SOlivier Houchard 
209*1fb62fb0SOlivier Houchard 		if (ck_pr_load_uint(&br->writer) == false)
210*1fb62fb0SOlivier Houchard 			break;
211*1fb62fb0SOlivier Houchard 
212*1fb62fb0SOlivier Houchard 		ck_pr_store_uint(&reader->n_readers, 0);
213*1fb62fb0SOlivier Houchard 	}
214*1fb62fb0SOlivier Houchard 
215*1fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
216*1fb62fb0SOlivier Houchard 	return;
217*1fb62fb0SOlivier Houchard }
218*1fb62fb0SOlivier Houchard 
219*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_brlock_read_trylock(struct ck_brlock * br,struct ck_brlock_reader * reader,unsigned int factor)220*1fb62fb0SOlivier Houchard ck_brlock_read_trylock(struct ck_brlock *br,
221*1fb62fb0SOlivier Houchard 		       struct ck_brlock_reader *reader,
222*1fb62fb0SOlivier Houchard 		       unsigned int factor)
223*1fb62fb0SOlivier Houchard {
224*1fb62fb0SOlivier Houchard 	unsigned int steps = 0;
225*1fb62fb0SOlivier Houchard 
226*1fb62fb0SOlivier Houchard 	if (reader->n_readers >= 1) {
227*1fb62fb0SOlivier Houchard 		ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1);
228*1fb62fb0SOlivier Houchard 		return true;
229*1fb62fb0SOlivier Houchard 	}
230*1fb62fb0SOlivier Houchard 
231*1fb62fb0SOlivier Houchard 	for (;;) {
232*1fb62fb0SOlivier Houchard 		while (ck_pr_load_uint(&br->writer) == true) {
233*1fb62fb0SOlivier Houchard 			if (++steps >= factor)
234*1fb62fb0SOlivier Houchard 				return false;
235*1fb62fb0SOlivier Houchard 
236*1fb62fb0SOlivier Houchard 			ck_pr_stall();
237*1fb62fb0SOlivier Houchard 		}
238*1fb62fb0SOlivier Houchard 
239*1fb62fb0SOlivier Houchard #if defined(__x86__) || defined(__x86_64__)
240*1fb62fb0SOlivier Houchard 		ck_pr_fas_uint(&reader->n_readers, 1);
241*1fb62fb0SOlivier Houchard 
242*1fb62fb0SOlivier Houchard 		/*
243*1fb62fb0SOlivier Houchard 		 * Serialize reader counter update with respect to load of
244*1fb62fb0SOlivier Houchard 		 * writer.
245*1fb62fb0SOlivier Houchard 		 */
246*1fb62fb0SOlivier Houchard 		ck_pr_fence_atomic_load();
247*1fb62fb0SOlivier Houchard #else
248*1fb62fb0SOlivier Houchard 		ck_pr_store_uint(&reader->n_readers, 1);
249*1fb62fb0SOlivier Houchard 
250*1fb62fb0SOlivier Houchard 		/*
251*1fb62fb0SOlivier Houchard 		 * Serialize reader counter update with respect to load of
252*1fb62fb0SOlivier Houchard 		 * writer.
253*1fb62fb0SOlivier Houchard 		 */
254*1fb62fb0SOlivier Houchard 		ck_pr_fence_store_load();
255*1fb62fb0SOlivier Houchard #endif
256*1fb62fb0SOlivier Houchard 
257*1fb62fb0SOlivier Houchard 		if (ck_pr_load_uint(&br->writer) == false)
258*1fb62fb0SOlivier Houchard 			break;
259*1fb62fb0SOlivier Houchard 
260*1fb62fb0SOlivier Houchard 		ck_pr_store_uint(&reader->n_readers, 0);
261*1fb62fb0SOlivier Houchard 
262*1fb62fb0SOlivier Houchard 		if (++steps >= factor)
263*1fb62fb0SOlivier Houchard 			return false;
264*1fb62fb0SOlivier Houchard 	}
265*1fb62fb0SOlivier Houchard 
266*1fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
267*1fb62fb0SOlivier Houchard 	return true;
268*1fb62fb0SOlivier Houchard }
269*1fb62fb0SOlivier Houchard 
270*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_brlock_read_unlock(struct ck_brlock_reader * reader)271*1fb62fb0SOlivier Houchard ck_brlock_read_unlock(struct ck_brlock_reader *reader)
272*1fb62fb0SOlivier Houchard {
273*1fb62fb0SOlivier Houchard 
274*1fb62fb0SOlivier Houchard 	ck_pr_fence_unlock();
275*1fb62fb0SOlivier Houchard 	ck_pr_store_uint(&reader->n_readers, reader->n_readers - 1);
276*1fb62fb0SOlivier Houchard 	return;
277*1fb62fb0SOlivier Houchard }
278*1fb62fb0SOlivier Houchard 
279*1fb62fb0SOlivier Houchard #endif /* CK_BRLOCK_H */
280