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