urcu-qsbr: use linked list instead of array for registry
[urcu.git] / urcu-qsbr.c
1 /*
2 * urcu-qsbr.c
3 *
4 * Userspace RCU QSBR library
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
24 */
25
26 #include <stdio.h>
27 #include <pthread.h>
28 #include <signal.h>
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <poll.h>
34
35 #define BUILD_QSBR_LIB
36 #include "urcu-qsbr-static.h"
37 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
38 #include "urcu-qsbr.h"
39
40 static pthread_mutex_t urcu_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42 int gp_futex;
43
44 /*
45 * Global grace period counter.
46 */
47 unsigned long urcu_gp_ctr = RCU_GP_ONLINE;
48
49 /*
50 * Written to only by each individual reader. Read by both the reader and the
51 * writers.
52 */
53 struct urcu_reader __thread urcu_reader;
54
55 #ifdef DEBUG_YIELD
56 unsigned int yield_active;
57 unsigned int __thread rand_yield;
58 #endif
59
60 static LIST_HEAD(registry);
61
62 static void internal_urcu_lock(void)
63 {
64 int ret;
65
66 #ifndef DISTRUST_SIGNALS_EXTREME
67 ret = pthread_mutex_lock(&urcu_mutex);
68 if (ret) {
69 perror("Error in pthread mutex lock");
70 exit(-1);
71 }
72 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
73 while ((ret = pthread_mutex_trylock(&urcu_mutex)) != 0) {
74 if (ret != EBUSY && ret != EINTR) {
75 printf("ret = %d, errno = %d\n", ret, errno);
76 perror("Error in pthread mutex lock");
77 exit(-1);
78 }
79 poll(NULL,0,10);
80 }
81 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
82 }
83
84 static void internal_urcu_unlock(void)
85 {
86 int ret;
87
88 ret = pthread_mutex_unlock(&urcu_mutex);
89 if (ret) {
90 perror("Error in pthread mutex unlock");
91 exit(-1);
92 }
93 }
94
95 /*
96 * synchronize_rcu() waiting. Single thread.
97 */
98 static void wait_gp(struct urcu_reader *index)
99 {
100 uatomic_dec(&gp_futex);
101 smp_mb(); /* Write futex before read reader_gp */
102 if (!rcu_gp_ongoing(&index->ctr)) {
103 /* Read reader_gp before write futex */
104 smp_mb();
105 /* Callbacks are queued, don't wait. */
106 uatomic_set(&gp_futex, 0);
107 } else {
108 /* Read reader_gp before read futex */
109 smp_rmb();
110 if (uatomic_read(&gp_futex) == -1)
111 futex(&gp_futex, FUTEX_WAIT, -1,
112 NULL, NULL, 0);
113 }
114 }
115
116 static void wait_for_quiescent_state(void)
117 {
118 struct urcu_reader *index;
119
120 if (list_empty(&registry))
121 return;
122 /*
123 * Wait for each thread rcu_reader_qs_gp count to become 0.
124 */
125 list_for_each_entry(index, &registry, head) {
126 int wait_loops = 0;
127
128 while (rcu_gp_ongoing(&index->ctr)) {
129 if (wait_loops++ == RCU_QS_ACTIVE_ATTEMPTS) {
130 wait_gp(index);
131 } else {
132 #ifndef HAS_INCOHERENT_CACHES
133 cpu_relax();
134 #else /* #ifndef HAS_INCOHERENT_CACHES */
135 smp_mb();
136 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
137 }
138 }
139 }
140 }
141
142 /*
143 * Using a two-subphases algorithm for architectures with smaller than 64-bit
144 * long-size to ensure we do not encounter an overflow bug.
145 */
146
147 #if (BITS_PER_LONG < 64)
148 /*
149 * called with urcu_mutex held.
150 */
151 static void switch_next_urcu_qparity(void)
152 {
153 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr ^ RCU_GP_CTR);
154 }
155
156 void synchronize_rcu(void)
157 {
158 unsigned long was_online;
159
160 was_online = urcu_reader.ctr;
161
162 /* All threads should read qparity before accessing data structure
163 * where new ptr points to.
164 */
165 /* Write new ptr before changing the qparity */
166 smp_mb();
167
168 /*
169 * Mark the writer thread offline to make sure we don't wait for
170 * our own quiescent state. This allows using synchronize_rcu() in
171 * threads registered as readers.
172 */
173 if (was_online)
174 STORE_SHARED(urcu_reader.ctr, 0);
175
176 internal_urcu_lock();
177
178 switch_next_urcu_qparity(); /* 0 -> 1 */
179
180 /*
181 * Must commit qparity update to memory before waiting for parity
182 * 0 quiescent state. Failure to do so could result in the writer
183 * waiting forever while new readers are always accessing data (no
184 * progress).
185 * Ensured by STORE_SHARED and LOAD_SHARED.
186 */
187
188 /*
189 * Wait for previous parity to be empty of readers.
190 */
191 wait_for_quiescent_state(); /* Wait readers in parity 0 */
192
193 /*
194 * Must finish waiting for quiescent state for parity 0 before
195 * committing qparity update to memory. Failure to do so could result in
196 * the writer waiting forever while new readers are always accessing
197 * data (no progress).
198 * Ensured by STORE_SHARED and LOAD_SHARED.
199 */
200
201 switch_next_urcu_qparity(); /* 1 -> 0 */
202
203 /*
204 * Must commit qparity update to memory before waiting for parity
205 * 1 quiescent state. Failure to do so could result in the writer
206 * waiting forever while new readers are always accessing data (no
207 * progress).
208 * Ensured by STORE_SHARED and LOAD_SHARED.
209 */
210
211 /*
212 * Wait for previous parity to be empty of readers.
213 */
214 wait_for_quiescent_state(); /* Wait readers in parity 1 */
215
216 internal_urcu_unlock();
217
218 /*
219 * Finish waiting for reader threads before letting the old ptr being
220 * freed.
221 */
222 if (was_online)
223 _STORE_SHARED(urcu_reader.ctr, LOAD_SHARED(urcu_gp_ctr));
224 smp_mb();
225 }
226 #else /* !(BITS_PER_LONG < 64) */
227 void synchronize_rcu(void)
228 {
229 unsigned long was_online;
230
231 was_online = urcu_reader.ctr;
232
233 /*
234 * Mark the writer thread offline to make sure we don't wait for
235 * our own quiescent state. This allows using synchronize_rcu() in
236 * threads registered as readers.
237 */
238 smp_mb();
239 if (was_online)
240 STORE_SHARED(urcu_reader.ctr, 0);
241
242 internal_urcu_lock();
243 STORE_SHARED(urcu_gp_ctr, urcu_gp_ctr + RCU_GP_CTR);
244 wait_for_quiescent_state();
245 internal_urcu_unlock();
246
247 if (was_online)
248 _STORE_SHARED(urcu_reader.ctr, LOAD_SHARED(urcu_gp_ctr));
249 smp_mb();
250 }
251 #endif /* !(BITS_PER_LONG < 64) */
252
253 /*
254 * library wrappers to be used by non-LGPL compatible source code.
255 */
256
257 void rcu_read_lock(void)
258 {
259 _rcu_read_lock();
260 }
261
262 void rcu_read_unlock(void)
263 {
264 _rcu_read_unlock();
265 }
266
267 void *rcu_dereference(void *p)
268 {
269 return _rcu_dereference(p);
270 }
271
272 void *rcu_assign_pointer_sym(void **p, void *v)
273 {
274 wmb();
275 return STORE_SHARED(p, v);
276 }
277
278 void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new)
279 {
280 wmb();
281 return uatomic_cmpxchg(p, old, _new);
282 }
283
284 void *rcu_xchg_pointer_sym(void **p, void *v)
285 {
286 wmb();
287 return uatomic_xchg(p, v);
288 }
289
290 void *rcu_publish_content_sym(void **p, void *v)
291 {
292 void *oldptr;
293
294 oldptr = _rcu_xchg_pointer(p, v);
295 synchronize_rcu();
296 return oldptr;
297 }
298
299 void rcu_quiescent_state(void)
300 {
301 _rcu_quiescent_state();
302 }
303
304 void rcu_thread_offline(void)
305 {
306 _rcu_thread_offline();
307 }
308
309 void rcu_thread_online(void)
310 {
311 _rcu_thread_online();
312 }
313
314 void rcu_register_thread(void)
315 {
316 urcu_reader.tid = pthread_self();
317 assert(urcu_reader.ctr == 0);
318
319 internal_urcu_lock();
320 list_add(&urcu_reader.head, &registry);
321 internal_urcu_unlock();
322 _rcu_thread_online();
323 }
324
325 void rcu_unregister_thread(void)
326 {
327 /*
328 * We have to make the thread offline otherwise we end up dealocking
329 * with a waiting writer.
330 */
331 _rcu_thread_offline();
332 internal_urcu_lock();
333 list_del(&urcu_reader.head);
334 internal_urcu_unlock();
335 }
This page took 0.035365 seconds and 5 git commands to generate.