Version 0.8.11
[userspace-rcu.git] / urcu-bp.c
CommitLineData
fdee2e6d
MD
1/*
2 * urcu-bp.c
3 *
4 * Userspace RCU library, "bulletproof" version.
5 *
6982d6d7 6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fdee2e6d
MD
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
0617bf4c 26#define _GNU_SOURCE
71c811bf 27#define _LGPL_SOURCE
fdee2e6d
MD
28#include <stdio.h>
29#include <pthread.h>
30#include <signal.h>
31#include <assert.h>
32#include <stdlib.h>
33#include <string.h>
34#include <errno.h>
35#include <poll.h>
36#include <unistd.h>
37#include <sys/mman.h>
38
d73fb81f 39#include "urcu/wfcqueue.h"
57760d44 40#include "urcu/map/urcu-bp.h"
af7c2dbe 41#include "urcu/static/urcu-bp.h"
618b2595 42#include "urcu-pointer.h"
bd252a04 43#include "urcu/tls-compat.h"
71c811bf 44
4a6d7378
MD
45#include "urcu-die.h"
46
fdee2e6d 47/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 48#undef _LGPL_SOURCE
fdee2e6d 49#include "urcu-bp.h"
71c811bf 50#define _LGPL_SOURCE
fdee2e6d 51
4c1ae2ea
MD
52#ifndef MAP_ANONYMOUS
53#define MAP_ANONYMOUS MAP_ANON
54#endif
55
c7eaf61c
MD
56#ifdef __linux__
57static
58void *mremap_wrapper(void *old_address, size_t old_size,
59 size_t new_size, int flags)
60{
61 return mremap(old_address, old_size, new_size, flags);
62}
63#else
45a4872f
MD
64
65#define MREMAP_MAYMOVE 1
66#define MREMAP_FIXED 2
67
68/*
a0d529ec 69 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
45a4872f
MD
70 * This is not generic.
71*/
c7eaf61c
MD
72static
73void *mremap_wrapper(void *old_address, size_t old_size,
74 size_t new_size, int flags)
45a4872f 75{
a0d529ec
MD
76 assert(!(flags & MREMAP_MAYMOVE));
77
78 return MAP_FAILED;
45a4872f
MD
79}
80#endif
81
e198fb6a
MD
82/* Sleep delay in ms */
83#define RCU_SLEEP_DELAY_MS 10
a0d529ec
MD
84#define INIT_NR_THREADS 8
85#define ARENA_INIT_ALLOC \
86 sizeof(struct registry_chunk) \
87 + INIT_NR_THREADS * sizeof(struct rcu_reader)
fdee2e6d 88
b7b6a8f5
PB
89/*
90 * Active attempts to check for reader Q.S. before calling sleep().
91 */
92#define RCU_QS_ACTIVE_ATTEMPTS 100
93
c2c36f69
MD
94static
95int rcu_bp_refcount;
96
8c1bd078
MD
97static
98void __attribute__((constructor)) rcu_bp_init(void);
99static
8a901d94 100void __attribute__((destructor)) _rcu_bp_exit(void);
fdee2e6d 101
3c29ca5d
MD
102/*
103 * rcu_gp_lock ensures mutual exclusion between threads calling
104 * synchronize_rcu().
105 */
6abb4bd5 106static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
3c29ca5d
MD
107/*
108 * rcu_registry_lock ensures mutual exclusion between threads
109 * registering and unregistering themselves to/from the registry, and
110 * with threads reading that registry from synchronize_rcu(). However,
111 * this lock is not held all the way through the completion of awaiting
112 * for the grace period. It is sporadically released between iterations
113 * on the registry.
114 * rcu_registry_lock may nest inside rcu_gp_lock.
115 */
116static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
fdee2e6d 117
8c1bd078
MD
118static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
119static int initialized;
120
121static pthread_key_t urcu_bp_key;
122
fdee2e6d 123#ifdef DEBUG_YIELD
1de4df4b 124unsigned int rcu_yield_active;
1182f6f0 125__DEFINE_URCU_TLS_GLOBAL(unsigned int, rcu_rand_yield);
fdee2e6d
MD
126#endif
127
c13c2e55 128struct rcu_gp rcu_gp = { .ctr = RCU_GP_COUNT };
fdee2e6d
MD
129
130/*
131 * Pointer to registry elements. Written to only by each individual reader. Read
132 * by both the reader and the writers.
133 */
1182f6f0 134__DEFINE_URCU_TLS_GLOBAL(struct rcu_reader *, rcu_reader);
fdee2e6d 135
16aa9ee8 136static CDS_LIST_HEAD(registry);
fdee2e6d 137
a0d529ec
MD
138struct registry_chunk {
139 size_t data_len; /* data length */
8c1bd078 140 size_t used; /* amount of data used */
a0d529ec
MD
141 struct cds_list_head node; /* chunk_list node */
142 char data[];
143};
144
fdee2e6d 145struct registry_arena {
a0d529ec 146 struct cds_list_head chunk_list;
fdee2e6d
MD
147};
148
a0d529ec
MD
149static struct registry_arena registry_arena = {
150 .chunk_list = CDS_LIST_HEAD_INIT(registry_arena.chunk_list),
151};
fdee2e6d 152
4cf1675f
MD
153/* Saved fork signal mask, protected by rcu_gp_lock */
154static sigset_t saved_fork_signal_mask;
155
6abb4bd5 156static void mutex_lock(pthread_mutex_t *mutex)
fdee2e6d
MD
157{
158 int ret;
159
160#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 161 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
162 if (ret)
163 urcu_die(ret);
fdee2e6d 164#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 165 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
166 if (ret != EBUSY && ret != EINTR)
167 urcu_die(ret);
fdee2e6d
MD
168 poll(NULL,0,10);
169 }
170#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
171}
172
6abb4bd5 173static void mutex_unlock(pthread_mutex_t *mutex)
fdee2e6d
MD
174{
175 int ret;
176
6abb4bd5 177 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
178 if (ret)
179 urcu_die(ret);
fdee2e6d
MD
180}
181
3c29ca5d
MD
182/*
183 * Always called with rcu_registry lock held. Releases this lock between
184 * iterations and grabs it again. Holds the lock when it returns.
185 */
52c75091
MD
186static void wait_for_readers(struct cds_list_head *input_readers,
187 struct cds_list_head *cur_snap_readers,
188 struct cds_list_head *qsreaders)
fdee2e6d 189{
e198fb6a 190 unsigned int wait_loops = 0;
02be5561 191 struct rcu_reader *index, *tmp;
fdee2e6d 192
fdee2e6d 193 /*
dd61d077
MD
194 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
195 * indicate quiescence (not nested), or observe the current
c13c2e55 196 * rcu_gp.ctr value.
fdee2e6d
MD
197 */
198 for (;;) {
e198fb6a
MD
199 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
200 wait_loops++;
201
52c75091
MD
202 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
203 switch (rcu_reader_state(&index->ctr)) {
204 case RCU_READER_ACTIVE_CURRENT:
205 if (cur_snap_readers) {
206 cds_list_move(&index->node,
207 cur_snap_readers);
208 break;
209 }
210 /* Fall-through */
211 case RCU_READER_INACTIVE:
212 cds_list_move(&index->node, qsreaders);
213 break;
214 case RCU_READER_ACTIVE_OLD:
215 /*
216 * Old snapshot. Leaving node in
217 * input_readers will make us busy-loop
218 * until the snapshot becomes current or
219 * the reader becomes inactive.
220 */
221 break;
222 }
fdee2e6d
MD
223 }
224
52c75091 225 if (cds_list_empty(input_readers)) {
fdee2e6d
MD
226 break;
227 } else {
3c29ca5d
MD
228 /* Temporarily unlock the registry lock. */
229 mutex_unlock(&rcu_registry_lock);
e198fb6a
MD
230 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS)
231 (void) poll(NULL, 0, RCU_SLEEP_DELAY_MS);
fdee2e6d 232 else
06f22bdb 233 caa_cpu_relax();
3c29ca5d
MD
234 /* Re-lock the registry lock before the next loop. */
235 mutex_lock(&rcu_registry_lock);
fdee2e6d
MD
236 }
237 }
fdee2e6d
MD
238}
239
240void synchronize_rcu(void)
241{
52c75091
MD
242 CDS_LIST_HEAD(cur_snap_readers);
243 CDS_LIST_HEAD(qsreaders);
fdee2e6d
MD
244 sigset_t newmask, oldmask;
245 int ret;
246
6ed4b2e6 247 ret = sigfillset(&newmask);
fdee2e6d 248 assert(!ret);
6ed4b2e6 249 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
fdee2e6d
MD
250 assert(!ret);
251
6abb4bd5 252 mutex_lock(&rcu_gp_lock);
fdee2e6d 253
3c29ca5d
MD
254 mutex_lock(&rcu_registry_lock);
255
16aa9ee8 256 if (cds_list_empty(&registry))
2dfb8b5e 257 goto out;
fdee2e6d
MD
258
259 /* All threads should read qparity before accessing data structure
2dfb8b5e 260 * where new ptr points to. */
fdee2e6d 261 /* Write new ptr before changing the qparity */
5481ddb3 262 cmm_smp_mb();
fdee2e6d 263
fdee2e6d 264 /*
dd61d077 265 * Wait for readers to observe original parity or be quiescent.
3c29ca5d
MD
266 * wait_for_readers() can release and grab again rcu_registry_lock
267 * interally.
dd61d077 268 */
52c75091 269 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
dd61d077
MD
270
271 /*
272 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
273 * model easier to understand. It does not have a big performance impact
274 * anyway, given this is the write-side.
275 */
276 cmm_smp_mb();
277
278 /* Switch parity: 0 -> 1, 1 -> 0 */
c13c2e55 279 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR_PHASE);
dd61d077
MD
280
281 /*
282 * Must commit qparity update to memory before waiting for other parity
283 * quiescent state. Failure to do so could result in the writer waiting
284 * forever while new readers are always accessing data (no progress).
285 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
fdee2e6d 286 */
fdee2e6d
MD
287
288 /*
5481ddb3 289 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
fdee2e6d
MD
290 * model easier to understand. It does not have a big performance impact
291 * anyway, given this is the write-side.
292 */
5481ddb3 293 cmm_smp_mb();
fdee2e6d 294
fdee2e6d 295 /*
dd61d077 296 * Wait for readers to observe new parity or be quiescent.
3c29ca5d
MD
297 * wait_for_readers() can release and grab again rcu_registry_lock
298 * interally.
fdee2e6d 299 */
52c75091
MD
300 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
301
302 /*
303 * Put quiescent reader list back into registry.
304 */
305 cds_list_splice(&qsreaders, &registry);
fdee2e6d
MD
306
307 /*
2dfb8b5e
MD
308 * Finish waiting for reader threads before letting the old ptr being
309 * freed.
fdee2e6d 310 */
5481ddb3 311 cmm_smp_mb();
2dfb8b5e 312out:
3c29ca5d 313 mutex_unlock(&rcu_registry_lock);
6abb4bd5 314 mutex_unlock(&rcu_gp_lock);
fdee2e6d
MD
315 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
316 assert(!ret);
317}
318
319/*
320 * library wrappers to be used by non-LGPL compatible source code.
321 */
322
323void rcu_read_lock(void)
324{
325 _rcu_read_lock();
326}
327
328void rcu_read_unlock(void)
329{
330 _rcu_read_unlock();
331}
332
882f3357
MD
333int rcu_read_ongoing(void)
334{
335 return _rcu_read_ongoing();
336}
337
fdee2e6d 338/*
a0d529ec
MD
339 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
340 * Else, try expanding the last chunk. If this fails, allocate a new
341 * chunk twice as big as the last chunk.
342 * Memory used by chunks _never_ moves. A chunk could theoretically be
343 * freed when all "used" slots are released, but we don't do it at this
344 * point.
fdee2e6d 345 */
a0d529ec
MD
346static
347void expand_arena(struct registry_arena *arena)
fdee2e6d 348{
a0d529ec
MD
349 struct registry_chunk *new_chunk, *last_chunk;
350 size_t old_chunk_len, new_chunk_len;
351
352 /* No chunk. */
353 if (cds_list_empty(&arena->chunk_list)) {
354 assert(ARENA_INIT_ALLOC >=
355 sizeof(struct registry_chunk)
356 + sizeof(struct rcu_reader));
357 new_chunk_len = ARENA_INIT_ALLOC;
5cedb854
MJ
358 new_chunk = (struct registry_chunk *) mmap(NULL,
359 new_chunk_len,
39be94ed
MD
360 PROT_READ | PROT_WRITE,
361 MAP_ANONYMOUS | MAP_PRIVATE,
362 -1, 0);
a0d529ec
MD
363 if (new_chunk == MAP_FAILED)
364 abort();
365 bzero(new_chunk, new_chunk_len);
366 new_chunk->data_len =
367 new_chunk_len - sizeof(struct registry_chunk);
368 cds_list_add_tail(&new_chunk->node, &arena->chunk_list);
369 return; /* We're done. */
370 }
39be94ed 371
a0d529ec
MD
372 /* Try expanding last chunk. */
373 last_chunk = cds_list_entry(arena->chunk_list.prev,
374 struct registry_chunk, node);
375 old_chunk_len =
376 last_chunk->data_len + sizeof(struct registry_chunk);
377 new_chunk_len = old_chunk_len << 1;
378
379 /* Don't allow memory mapping to move, just expand. */
380 new_chunk = mremap_wrapper(last_chunk, old_chunk_len,
381 new_chunk_len, 0);
382 if (new_chunk != MAP_FAILED) {
383 /* Should not have moved. */
384 assert(new_chunk == last_chunk);
385 bzero((char *) last_chunk + old_chunk_len,
386 new_chunk_len - old_chunk_len);
387 last_chunk->data_len =
388 new_chunk_len - sizeof(struct registry_chunk);
389 return; /* We're done. */
390 }
0617bf4c 391
a0d529ec 392 /* Remap did not succeed, we need to add a new chunk. */
5cedb854
MJ
393 new_chunk = (struct registry_chunk *) mmap(NULL,
394 new_chunk_len,
a0d529ec
MD
395 PROT_READ | PROT_WRITE,
396 MAP_ANONYMOUS | MAP_PRIVATE,
397 -1, 0);
398 if (new_chunk == MAP_FAILED)
399 abort();
400 bzero(new_chunk, new_chunk_len);
401 new_chunk->data_len =
402 new_chunk_len - sizeof(struct registry_chunk);
403 cds_list_add_tail(&new_chunk->node, &arena->chunk_list);
404}
fdee2e6d 405
a0d529ec
MD
406static
407struct rcu_reader *arena_alloc(struct registry_arena *arena)
408{
409 struct registry_chunk *chunk;
410 struct rcu_reader *rcu_reader_reg;
411 int expand_done = 0; /* Only allow to expand once per alloc */
412 size_t len = sizeof(struct rcu_reader);
413
414retry:
415 cds_list_for_each_entry(chunk, &arena->chunk_list, node) {
416 if (chunk->data_len - chunk->used < len)
417 continue;
418 /* Find spot */
419 for (rcu_reader_reg = (struct rcu_reader *) &chunk->data[0];
420 rcu_reader_reg < (struct rcu_reader *) &chunk->data[chunk->data_len];
421 rcu_reader_reg++) {
422 if (!rcu_reader_reg->alloc) {
423 rcu_reader_reg->alloc = 1;
424 chunk->used += len;
425 return rcu_reader_reg;
426 }
427 }
428 }
429
430 if (!expand_done) {
431 expand_arena(arena);
432 expand_done = 1;
433 goto retry;
434 }
435
436 return NULL;
fdee2e6d
MD
437}
438
439/* Called with signals off and mutex locked */
a0d529ec
MD
440static
441void add_thread(void)
fdee2e6d 442{
02be5561 443 struct rcu_reader *rcu_reader_reg;
8c1bd078 444 int ret;
fdee2e6d 445
a0d529ec
MD
446 rcu_reader_reg = arena_alloc(&registry_arena);
447 if (!rcu_reader_reg)
448 abort();
8c1bd078
MD
449 ret = pthread_setspecific(urcu_bp_key, rcu_reader_reg);
450 if (ret)
451 abort();
fdee2e6d
MD
452
453 /* Add to registry */
02be5561
MD
454 rcu_reader_reg->tid = pthread_self();
455 assert(rcu_reader_reg->ctr == 0);
16aa9ee8 456 cds_list_add(&rcu_reader_reg->node, &registry);
a0d529ec
MD
457 /*
458 * Reader threads are pointing to the reader registry. This is
459 * why its memory should never be relocated.
460 */
bd252a04 461 URCU_TLS(rcu_reader) = rcu_reader_reg;
fdee2e6d
MD
462}
463
8c1bd078
MD
464/* Called with mutex locked */
465static
466void cleanup_thread(struct registry_chunk *chunk,
467 struct rcu_reader *rcu_reader_reg)
468{
469 rcu_reader_reg->ctr = 0;
470 cds_list_del(&rcu_reader_reg->node);
471 rcu_reader_reg->tid = 0;
472 rcu_reader_reg->alloc = 0;
473 chunk->used -= sizeof(struct rcu_reader);
474}
475
476static
477struct registry_chunk *find_chunk(struct rcu_reader *rcu_reader_reg)
fdee2e6d 478{
a0d529ec 479 struct registry_chunk *chunk;
fdee2e6d 480
a0d529ec 481 cds_list_for_each_entry(chunk, &registry_arena.chunk_list, node) {
8c1bd078
MD
482 if (rcu_reader_reg < (struct rcu_reader *) &chunk->data[0])
483 continue;
484 if (rcu_reader_reg >= (struct rcu_reader *) &chunk->data[chunk->data_len])
485 continue;
486 return chunk;
487 }
488 return NULL;
489}
a0d529ec 490
8c1bd078
MD
491/* Called with signals off and mutex locked */
492static
c2c36f69 493void remove_thread(struct rcu_reader *rcu_reader_reg)
8c1bd078 494{
8c1bd078
MD
495 cleanup_thread(find_chunk(rcu_reader_reg), rcu_reader_reg);
496 URCU_TLS(rcu_reader) = NULL;
fdee2e6d
MD
497}
498
499/* Disable signals, take mutex, add to registry */
500void rcu_bp_register(void)
501{
502 sigset_t newmask, oldmask;
503 int ret;
504
6ed4b2e6 505 ret = sigfillset(&newmask);
8c1bd078
MD
506 if (ret)
507 abort();
6ed4b2e6 508 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
8c1bd078
MD
509 if (ret)
510 abort();
fdee2e6d
MD
511
512 /*
513 * Check if a signal concurrently registered our thread since
8c1bd078
MD
514 * the check in rcu_read_lock().
515 */
bd252a04 516 if (URCU_TLS(rcu_reader))
fdee2e6d
MD
517 goto end;
518
8c1bd078
MD
519 /*
520 * Take care of early registration before urcu_bp constructor.
521 */
522 rcu_bp_init();
523
3c29ca5d 524 mutex_lock(&rcu_registry_lock);
fdee2e6d 525 add_thread();
3c29ca5d 526 mutex_unlock(&rcu_registry_lock);
fdee2e6d
MD
527end:
528 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
8c1bd078
MD
529 if (ret)
530 abort();
531}
532
533/* Disable signals, take mutex, remove from registry */
534static
c2c36f69 535void rcu_bp_unregister(struct rcu_reader *rcu_reader_reg)
8c1bd078
MD
536{
537 sigset_t newmask, oldmask;
538 int ret;
539
540 ret = sigfillset(&newmask);
541 if (ret)
542 abort();
543 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
544 if (ret)
545 abort();
546
3c29ca5d 547 mutex_lock(&rcu_registry_lock);
c2c36f69 548 remove_thread(rcu_reader_reg);
3c29ca5d 549 mutex_unlock(&rcu_registry_lock);
8c1bd078
MD
550 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
551 if (ret)
552 abort();
8a901d94 553 _rcu_bp_exit();
8c1bd078
MD
554}
555
556/*
557 * Remove thread from the registry when it exits, and flag it as
558 * destroyed so garbage collection can take care of it.
559 */
560static
561void urcu_bp_thread_exit_notifier(void *rcu_key)
562{
c2c36f69 563 rcu_bp_unregister(rcu_key);
8c1bd078
MD
564}
565
566static
567void rcu_bp_init(void)
568{
569 mutex_lock(&init_lock);
c2c36f69 570 if (!rcu_bp_refcount++) {
8c1bd078
MD
571 int ret;
572
573 ret = pthread_key_create(&urcu_bp_key,
574 urcu_bp_thread_exit_notifier);
575 if (ret)
576 abort();
577 initialized = 1;
578 }
579 mutex_unlock(&init_lock);
fdee2e6d
MD
580}
581
8c1bd078 582static
8a901d94 583void _rcu_bp_exit(void)
fdee2e6d 584{
c2c36f69
MD
585 mutex_lock(&init_lock);
586 if (!--rcu_bp_refcount) {
587 struct registry_chunk *chunk, *tmp;
588 int ret;
a0d529ec 589
c2c36f69
MD
590 cds_list_for_each_entry_safe(chunk, tmp,
591 &registry_arena.chunk_list, node) {
5cedb854 592 munmap((void *) chunk, chunk->data_len
c2c36f69
MD
593 + sizeof(struct registry_chunk));
594 }
f4d5fce7 595 CDS_INIT_LIST_HEAD(&registry_arena.chunk_list);
c2c36f69
MD
596 ret = pthread_key_delete(urcu_bp_key);
597 if (ret)
598 abort();
a0d529ec 599 }
c2c36f69 600 mutex_unlock(&init_lock);
fdee2e6d 601}
4cf1675f 602
8a901d94
MD
603/*
604 * Keep ABI compability within stable versions. This has never been
605 * exposed through a header, but needs to stay in the .so until the
606 * soname is bumped.
607 */
608void rcu_bp_exit(void)
609{
610}
611
4cf1675f 612/*
3c29ca5d
MD
613 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
614 * sure we fork() don't race with a concurrent thread executing with
615 * any of those locks held. This ensures that the registry and data
616 * protected by rcu_gp_lock are in a coherent state in the child.
4cf1675f
MD
617 */
618void rcu_bp_before_fork(void)
619{
620 sigset_t newmask, oldmask;
621 int ret;
622
6ed4b2e6 623 ret = sigfillset(&newmask);
4cf1675f 624 assert(!ret);
6ed4b2e6 625 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
4cf1675f
MD
626 assert(!ret);
627 mutex_lock(&rcu_gp_lock);
3c29ca5d 628 mutex_lock(&rcu_registry_lock);
4cf1675f
MD
629 saved_fork_signal_mask = oldmask;
630}
631
632void rcu_bp_after_fork_parent(void)
633{
634 sigset_t oldmask;
635 int ret;
636
637 oldmask = saved_fork_signal_mask;
3c29ca5d 638 mutex_unlock(&rcu_registry_lock);
4cf1675f
MD
639 mutex_unlock(&rcu_gp_lock);
640 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
641 assert(!ret);
642}
643
8c1bd078
MD
644/*
645 * Prune all entries from registry except our own thread. Fits the Linux
3c29ca5d 646 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
8c1bd078
MD
647 */
648static
649void urcu_bp_prune_registry(void)
650{
651 struct registry_chunk *chunk;
652 struct rcu_reader *rcu_reader_reg;
653
654 cds_list_for_each_entry(chunk, &registry_arena.chunk_list, node) {
655 for (rcu_reader_reg = (struct rcu_reader *) &chunk->data[0];
656 rcu_reader_reg < (struct rcu_reader *) &chunk->data[chunk->data_len];
657 rcu_reader_reg++) {
658 if (!rcu_reader_reg->alloc)
659 continue;
660 if (rcu_reader_reg->tid == pthread_self())
661 continue;
662 cleanup_thread(chunk, rcu_reader_reg);
663 }
664 }
665}
666
4cf1675f
MD
667void rcu_bp_after_fork_child(void)
668{
669 sigset_t oldmask;
670 int ret;
671
8c1bd078 672 urcu_bp_prune_registry();
4cf1675f 673 oldmask = saved_fork_signal_mask;
3c29ca5d 674 mutex_unlock(&rcu_registry_lock);
4cf1675f
MD
675 mutex_unlock(&rcu_gp_lock);
676 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
677 assert(!ret);
678}
5e77fc1f 679
9b7981bb
MD
680void *rcu_dereference_sym_bp(void *p)
681{
682 return _rcu_dereference(p);
683}
684
5efd3cd2
MD
685void *rcu_set_pointer_sym_bp(void **p, void *v)
686{
687 cmm_wmb();
424d4ed5
MD
688 uatomic_set(p, v);
689 return v;
5efd3cd2
MD
690}
691
692void *rcu_xchg_pointer_sym_bp(void **p, void *v)
693{
694 cmm_wmb();
695 return uatomic_xchg(p, v);
696}
697
698void *rcu_cmpxchg_pointer_sym_bp(void **p, void *old, void *_new)
699{
700 cmm_wmb();
701 return uatomic_cmpxchg(p, old, _new);
702}
703
5e6b23a6 704DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 705
5e77fc1f 706#include "urcu-call-rcu-impl.h"
0376e7b2 707#include "urcu-defer-impl.h"
This page took 0.065759 seconds and 4 git commands to generate.