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