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