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