71e7a39e6f73795026e054bf8f0620772ec9a70f
[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@efficios.com>
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 #define _GNU_SOURCE
27 #define _LGPL_SOURCE
28 #include <stdio.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <poll.h>
37
38 #include "urcu/wfcqueue.h"
39 #include "urcu/map/urcu-qsbr.h"
40 #define BUILD_QSBR_LIB
41 #include "urcu/static/urcu-qsbr.h"
42 #include "urcu-pointer.h"
43 #include "urcu/tls-compat.h"
44
45 #include "urcu-die.h"
46 #include "urcu-wait.h"
47
48 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
49 #undef _LGPL_SOURCE
50 #include "urcu-qsbr.h"
51 #define _LGPL_SOURCE
52
53 void __attribute__((destructor)) rcu_exit(void);
54
55 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
56 struct rcu_gp rcu_gp = { .ctr = RCU_GP_ONLINE };
57
58 /*
59 * Active attempts to check for reader Q.S. before calling futex().
60 */
61 #define RCU_QS_ACTIVE_ATTEMPTS 100
62
63 /*
64 * Written to only by each individual reader. Read by both the reader and the
65 * writers.
66 */
67 DEFINE_URCU_TLS(struct rcu_reader, rcu_reader);
68
69 #ifdef DEBUG_YIELD
70 unsigned int rcu_yield_active;
71 DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
72 #endif
73
74 static CDS_LIST_HEAD(registry);
75
76 /*
77 * Queue keeping threads awaiting to wait for a grace period. Contains
78 * struct gp_waiters_thread objects.
79 */
80 static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
81
82 static void mutex_lock(pthread_mutex_t *mutex)
83 {
84 int ret;
85
86 #ifndef DISTRUST_SIGNALS_EXTREME
87 ret = pthread_mutex_lock(mutex);
88 if (ret)
89 urcu_die(ret);
90 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
91 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
92 if (ret != EBUSY && ret != EINTR)
93 urcu_die(ret);
94 poll(NULL,0,10);
95 }
96 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
97 }
98
99 static void mutex_unlock(pthread_mutex_t *mutex)
100 {
101 int ret;
102
103 ret = pthread_mutex_unlock(mutex);
104 if (ret)
105 urcu_die(ret);
106 }
107
108 /*
109 * synchronize_rcu() waiting. Single thread.
110 */
111 static void wait_gp(void)
112 {
113 /* Read reader_gp before read futex */
114 cmm_smp_rmb();
115 if (uatomic_read(&rcu_gp.futex) == -1)
116 futex_noasync(&rcu_gp.futex, FUTEX_WAIT, -1,
117 NULL, NULL, 0);
118 }
119
120 static void wait_for_readers(struct cds_list_head *input_readers,
121 struct cds_list_head *cur_snap_readers,
122 struct cds_list_head *qsreaders)
123 {
124 unsigned int wait_loops = 0;
125 struct rcu_reader *index, *tmp;
126
127 /*
128 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
129 * indicate quiescence (offline), or for them to observe the
130 * current rcu_gp.ctr value.
131 */
132 for (;;) {
133 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
134 wait_loops++;
135 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
136 uatomic_set(&rcu_gp.futex, -1);
137 /*
138 * Write futex before write waiting (the other side
139 * reads them in the opposite order).
140 */
141 cmm_smp_wmb();
142 cds_list_for_each_entry(index, input_readers, node) {
143 _CMM_STORE_SHARED(index->waiting, 1);
144 }
145 /* Write futex before read reader_gp */
146 cmm_smp_mb();
147 }
148 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
149 switch (rcu_reader_state(&index->ctr)) {
150 case RCU_READER_ACTIVE_CURRENT:
151 if (cur_snap_readers) {
152 cds_list_move(&index->node,
153 cur_snap_readers);
154 break;
155 }
156 /* Fall-through */
157 case RCU_READER_INACTIVE:
158 cds_list_move(&index->node, qsreaders);
159 break;
160 case RCU_READER_ACTIVE_OLD:
161 /*
162 * Old snapshot. Leaving node in
163 * input_readers will make us busy-loop
164 * until the snapshot becomes current or
165 * the reader becomes inactive.
166 */
167 break;
168 }
169 }
170
171 if (cds_list_empty(input_readers)) {
172 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
173 /* Read reader_gp before write futex */
174 cmm_smp_mb();
175 uatomic_set(&rcu_gp.futex, 0);
176 }
177 break;
178 } else {
179 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
180 wait_gp();
181 } else {
182 #ifndef HAS_INCOHERENT_CACHES
183 caa_cpu_relax();
184 #else /* #ifndef HAS_INCOHERENT_CACHES */
185 cmm_smp_mb();
186 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
187 }
188 }
189 }
190 }
191
192 /*
193 * Using a two-subphases algorithm for architectures with smaller than 64-bit
194 * long-size to ensure we do not encounter an overflow bug.
195 */
196
197 #if (CAA_BITS_PER_LONG < 64)
198 void synchronize_rcu(void)
199 {
200 CDS_LIST_HEAD(cur_snap_readers);
201 CDS_LIST_HEAD(qsreaders);
202 unsigned long was_online;
203 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
204 struct urcu_waiters waiters;
205
206 was_online = rcu_read_ongoing();
207
208 /* All threads should read qparity before accessing data structure
209 * where new ptr points to. In the "then" case, rcu_thread_offline
210 * includes a memory barrier.
211 *
212 * Mark the writer thread offline to make sure we don't wait for
213 * our own quiescent state. This allows using synchronize_rcu()
214 * in threads registered as readers.
215 */
216 if (was_online)
217 rcu_thread_offline();
218 else
219 cmm_smp_mb();
220
221 /*
222 * Add ourself to gp_waiters queue of threads awaiting to wait
223 * for a grace period. Proceed to perform the grace period only
224 * if we are the first thread added into the queue.
225 */
226 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
227 /* Not first in queue: will be awakened by another thread. */
228 urcu_adaptative_busy_wait(&wait);
229 goto gp_end;
230 }
231 /* We won't need to wake ourself up */
232 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
233
234 mutex_lock(&rcu_gp_lock);
235
236 /*
237 * Move all waiters into our local queue.
238 */
239 urcu_move_waiters(&waiters, &gp_waiters);
240
241 if (cds_list_empty(&registry))
242 goto out;
243
244 /*
245 * Wait for readers to observe original parity or be quiescent.
246 */
247 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
248
249 /*
250 * Must finish waiting for quiescent state for original parity
251 * before committing next rcu_gp.ctr update to memory. Failure
252 * to do so could result in the writer waiting forever while new
253 * readers are always accessing data (no progress). Enforce
254 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
255 * to rcu_gp.ctr.
256 */
257 cmm_barrier();
258
259 /*
260 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
261 * model easier to understand. It does not have a big performance impact
262 * anyway, given this is the write-side.
263 */
264 cmm_smp_mb();
265
266 /* Switch parity: 0 -> 1, 1 -> 0 */
267 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR);
268
269 /*
270 * Must commit rcu_gp.ctr update to memory before waiting for
271 * quiescent state. Failure to do so could result in the writer
272 * waiting forever while new readers are always accessing data
273 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
274 * before load URCU_TLS(rcu_reader).ctr.
275 */
276 cmm_barrier();
277
278 /*
279 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
280 * model easier to understand. It does not have a big performance impact
281 * anyway, given this is the write-side.
282 */
283 cmm_smp_mb();
284
285 /*
286 * Wait for readers to observe new parity or be quiescent.
287 */
288 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
289
290 /*
291 * Put quiescent reader list back into registry.
292 */
293 cds_list_splice(&qsreaders, &registry);
294 out:
295 mutex_unlock(&rcu_gp_lock);
296 urcu_wake_all_waiters(&waiters);
297 gp_end:
298 /*
299 * Finish waiting for reader threads before letting the old ptr being
300 * freed.
301 */
302 if (was_online)
303 rcu_thread_online();
304 else
305 cmm_smp_mb();
306 }
307 #else /* !(CAA_BITS_PER_LONG < 64) */
308 void synchronize_rcu(void)
309 {
310 CDS_LIST_HEAD(qsreaders);
311 unsigned long was_online;
312 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
313 struct urcu_waiters waiters;
314
315 was_online = rcu_read_ongoing();
316
317 /*
318 * Mark the writer thread offline to make sure we don't wait for
319 * our own quiescent state. This allows using synchronize_rcu()
320 * in threads registered as readers.
321 */
322 if (was_online)
323 rcu_thread_offline();
324 else
325 cmm_smp_mb();
326
327 /*
328 * Add ourself to gp_waiters queue of threads awaiting to wait
329 * for a grace period. Proceed to perform the grace period only
330 * if we are the first thread added into the queue.
331 */
332 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
333 /* Not first in queue: will be awakened by another thread. */
334 urcu_adaptative_busy_wait(&wait);
335 goto gp_end;
336 }
337 /* We won't need to wake ourself up */
338 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
339
340 mutex_lock(&rcu_gp_lock);
341
342 /*
343 * Move all waiters into our local queue.
344 */
345 urcu_move_waiters(&waiters, &gp_waiters);
346
347 if (cds_list_empty(&registry))
348 goto out;
349
350 /* Increment current G.P. */
351 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr + RCU_GP_CTR);
352
353 /*
354 * Must commit rcu_gp.ctr update to memory before waiting for
355 * quiescent state. Failure to do so could result in the writer
356 * waiting forever while new readers are always accessing data
357 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
358 * before load URCU_TLS(rcu_reader).ctr.
359 */
360 cmm_barrier();
361
362 /*
363 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
364 * model easier to understand. It does not have a big performance impact
365 * anyway, given this is the write-side.
366 */
367 cmm_smp_mb();
368
369 /*
370 * Wait for readers to observe new count of be quiescent.
371 */
372 wait_for_readers(&registry, NULL, &qsreaders);
373
374 /*
375 * Put quiescent reader list back into registry.
376 */
377 cds_list_splice(&qsreaders, &registry);
378 out:
379 mutex_unlock(&rcu_gp_lock);
380 urcu_wake_all_waiters(&waiters);
381 gp_end:
382 if (was_online)
383 rcu_thread_online();
384 else
385 cmm_smp_mb();
386 }
387 #endif /* !(CAA_BITS_PER_LONG < 64) */
388
389 /*
390 * library wrappers to be used by non-LGPL compatible source code.
391 */
392
393 void rcu_read_lock(void)
394 {
395 _rcu_read_lock();
396 }
397
398 void rcu_read_unlock(void)
399 {
400 _rcu_read_unlock();
401 }
402
403 int rcu_read_ongoing(void)
404 {
405 return _rcu_read_ongoing();
406 }
407
408 void rcu_quiescent_state(void)
409 {
410 _rcu_quiescent_state();
411 }
412
413 void rcu_thread_offline(void)
414 {
415 _rcu_thread_offline();
416 }
417
418 void rcu_thread_online(void)
419 {
420 _rcu_thread_online();
421 }
422
423 void rcu_register_thread(void)
424 {
425 URCU_TLS(rcu_reader).tid = pthread_self();
426 assert(URCU_TLS(rcu_reader).ctr == 0);
427
428 mutex_lock(&rcu_gp_lock);
429 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
430 mutex_unlock(&rcu_gp_lock);
431 _rcu_thread_online();
432 }
433
434 void rcu_unregister_thread(void)
435 {
436 /*
437 * We have to make the thread offline otherwise we end up dealocking
438 * with a waiting writer.
439 */
440 _rcu_thread_offline();
441 mutex_lock(&rcu_gp_lock);
442 cds_list_del(&URCU_TLS(rcu_reader).node);
443 mutex_unlock(&rcu_gp_lock);
444 }
445
446 void rcu_exit(void)
447 {
448 /*
449 * Assertion disabled because call_rcu threads are now rcu
450 * readers, and left running at exit.
451 * assert(cds_list_empty(&registry));
452 */
453 }
454
455 DEFINE_RCU_FLAVOR(rcu_flavor);
456
457 #include "urcu-call-rcu-impl.h"
458 #include "urcu-defer-impl.h"
This page took 0.037453 seconds and 4 git commands to generate.