2 * Copyright 2011-2015 Samy Al Bahra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Big reader spinlocks provide cache-local contention-free read
32 * lock acquisition in the absence of writers. This comes at the
33 * cost of O(n) write lock acquisition. They were first implemented
34 * in the Linux kernel by Ingo Molnar and David S. Miller around the
37 * This implementation is thread-agnostic which comes at the cost
38 * of larger reader objects due to necessary linkage overhead. In
39 * order to cut down on TLB pressure, it is recommended to allocate
40 * these objects on the same page.
44 #include <ck_stdbool.h>
45 #include <ck_stddef.h>
47 struct ck_brlock_reader {
48 unsigned int n_readers;
49 struct ck_brlock_reader *previous;
50 struct ck_brlock_reader *next;
52 typedef struct ck_brlock_reader ck_brlock_reader_t;
54 #define CK_BRLOCK_READER_INITIALIZER {0}
57 struct ck_brlock_reader *readers;
60 typedef struct ck_brlock ck_brlock_t;
62 #define CK_BRLOCK_INITIALIZER {NULL, false}
64 CK_CC_INLINE static void
65 ck_brlock_init(struct ck_brlock *br)
74 CK_CC_INLINE static void
75 ck_brlock_write_lock(struct ck_brlock *br)
77 struct ck_brlock_reader *cursor;
80 * As the frequency of write acquisitions should be low,
81 * there is no point to more advanced contention avoidance.
83 while (ck_pr_fas_uint(&br->writer, true) == true)
86 ck_pr_fence_atomic_load();
88 /* The reader list is protected under the writer br. */
89 for (cursor = br->readers; cursor != NULL; cursor = cursor->next) {
90 while (ck_pr_load_uint(&cursor->n_readers) != 0)
98 CK_CC_INLINE static void
99 ck_brlock_write_unlock(struct ck_brlock *br)
102 ck_pr_fence_unlock();
103 ck_pr_store_uint(&br->writer, false);
107 CK_CC_INLINE static bool
108 ck_brlock_write_trylock(struct ck_brlock *br, unsigned int factor)
110 struct ck_brlock_reader *cursor;
111 unsigned int steps = 0;
113 while (ck_pr_fas_uint(&br->writer, true) == true) {
114 if (++steps >= factor)
121 * We do not require a strict fence here as atomic RMW operations
124 ck_pr_fence_atomic_load();
126 for (cursor = br->readers; cursor != NULL; cursor = cursor->next) {
127 while (ck_pr_load_uint(&cursor->n_readers) != 0) {
128 if (++steps >= factor) {
129 ck_brlock_write_unlock(br);
141 CK_CC_INLINE static void
142 ck_brlock_read_register(struct ck_brlock *br, struct ck_brlock_reader *reader)
145 reader->n_readers = 0;
146 reader->previous = NULL;
148 /* Implicit compiler barrier. */
149 ck_brlock_write_lock(br);
151 reader->next = ck_pr_load_ptr(&br->readers);
152 if (reader->next != NULL)
153 reader->next->previous = reader;
154 ck_pr_store_ptr(&br->readers, reader);
156 ck_brlock_write_unlock(br);
160 CK_CC_INLINE static void
161 ck_brlock_read_unregister(struct ck_brlock *br, struct ck_brlock_reader *reader)
164 ck_brlock_write_lock(br);
166 if (reader->next != NULL)
167 reader->next->previous = reader->previous;
169 if (reader->previous != NULL)
170 reader->previous->next = reader->next;
172 br->readers = reader->next;
174 ck_brlock_write_unlock(br);
178 CK_CC_INLINE static void
179 ck_brlock_read_lock(struct ck_brlock *br, struct ck_brlock_reader *reader)
182 if (reader->n_readers >= 1) {
183 ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1);
188 while (ck_pr_load_uint(&br->writer) == true)
191 #if defined(__x86__) || defined(__x86_64__)
192 ck_pr_fas_uint(&reader->n_readers, 1);
195 * Serialize reader counter update with respect to load of
198 ck_pr_fence_atomic_load();
200 ck_pr_store_uint(&reader->n_readers, 1);
203 * Serialize reader counter update with respect to load of
206 ck_pr_fence_store_load();
209 if (ck_pr_load_uint(&br->writer) == false)
212 ck_pr_store_uint(&reader->n_readers, 0);
219 CK_CC_INLINE static bool
220 ck_brlock_read_trylock(struct ck_brlock *br,
221 struct ck_brlock_reader *reader,
224 unsigned int steps = 0;
226 if (reader->n_readers >= 1) {
227 ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1);
232 while (ck_pr_load_uint(&br->writer) == true) {
233 if (++steps >= factor)
239 #if defined(__x86__) || defined(__x86_64__)
240 ck_pr_fas_uint(&reader->n_readers, 1);
243 * Serialize reader counter update with respect to load of
246 ck_pr_fence_atomic_load();
248 ck_pr_store_uint(&reader->n_readers, 1);
251 * Serialize reader counter update with respect to load of
254 ck_pr_fence_store_load();
257 if (ck_pr_load_uint(&br->writer) == false)
260 ck_pr_store_uint(&reader->n_readers, 0);
262 if (++steps >= factor)
270 CK_CC_INLINE static void
271 ck_brlock_read_unlock(struct ck_brlock_reader *reader)
274 ck_pr_fence_unlock();
275 ck_pr_store_uint(&reader->n_readers, reader->n_readers - 1);
279 #endif /* CK_BRLOCK_H */