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