fix: explicitly include urcu/config.h in files using CONFIG_RCU_ defines
[urcu.git] / src / urcu-bp.c
1 /*
2 * urcu-bp.c
3 *
4 * Userspace RCU library, "bulletproof" version.
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
26 #define URCU_NO_COMPAT_IDENTIFIERS
27 #define _LGPL_SOURCE
28 #include <stdio.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <poll.h>
36 #include <unistd.h>
37 #include <stdbool.h>
38 #include <sys/mman.h>
39
40 #include <urcu/config.h>
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>
47
48 #include "urcu-die.h"
49 #include "urcu-utils.h"
50
51 #define URCU_API_MAP
52 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
53 #undef _LGPL_SOURCE
54 #include <urcu/urcu-bp.h>
55 #define _LGPL_SOURCE
56
57 #ifndef MAP_ANONYMOUS
58 #define MAP_ANONYMOUS MAP_ANON
59 #endif
60
61 #ifdef __linux__
62 static
63 void *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
69
70 #define MREMAP_MAYMOVE 1
71 #define MREMAP_FIXED 2
72
73 /*
74 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
75 * This is not generic.
76 */
77 static
78 void *mremap_wrapper(void *old_address, size_t old_size,
79 size_t new_size, int flags)
80 {
81 assert(!(flags & MREMAP_MAYMOVE));
82
83 return MAP_FAILED;
84 }
85 #endif
86
87 /* Sleep delay in ms */
88 #define RCU_SLEEP_DELAY_MS 10
89 #define INIT_NR_THREADS 8
90 #define ARENA_INIT_ALLOC \
91 sizeof(struct registry_chunk) \
92 + INIT_NR_THREADS * sizeof(struct urcu_bp_reader)
93
94 /*
95 * Active attempts to check for reader Q.S. before calling sleep().
96 */
97 #define RCU_QS_ACTIVE_ATTEMPTS 100
98
99 static
100 int urcu_bp_refcount;
101
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__)
105 #else
106 # define membarrier(...) -ENOSYS
107 #endif
108
109 enum membarrier_cmd {
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),
116 };
117
118 static
119 void __attribute__((constructor)) _urcu_bp_init(void);
120 static
121 void __attribute__((destructor)) urcu_bp_exit(void);
122
123 #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
124 int urcu_bp_has_sys_membarrier;
125 #endif
126
127 /*
128 * rcu_gp_lock ensures mutual exclusion between threads calling
129 * synchronize_rcu().
130 */
131 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
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 */
141 static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
142
143 static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
144 static int initialized;
145
146 static pthread_key_t urcu_bp_key;
147
148 struct urcu_bp_gp urcu_bp_gp = { .ctr = URCU_BP_GP_COUNT };
149 URCU_ATTR_ALIAS("urcu_bp_gp") extern struct urcu_bp_gp rcu_gp_bp;
150
151 /*
152 * Pointer to registry elements. Written to only by each individual reader. Read
153 * by both the reader and the writers.
154 */
155 DEFINE_URCU_TLS(struct urcu_bp_reader *, urcu_bp_reader);
156 DEFINE_URCU_TLS_ALIAS(struct urcu_bp_reader *, urcu_bp_reader, rcu_reader_bp);
157
158 static CDS_LIST_HEAD(registry);
159
160 struct registry_chunk {
161 size_t data_len; /* data length */
162 size_t used; /* amount of data used */
163 struct cds_list_head node; /* chunk_list node */
164 char data[];
165 };
166
167 struct registry_arena {
168 struct cds_list_head chunk_list;
169 };
170
171 static struct registry_arena registry_arena = {
172 .chunk_list = CDS_LIST_HEAD_INIT(registry_arena.chunk_list),
173 };
174
175 /* Saved fork signal mask, protected by rcu_gp_lock */
176 static sigset_t saved_fork_signal_mask;
177
178 static void mutex_lock(pthread_mutex_t *mutex)
179 {
180 int ret;
181
182 #ifndef DISTRUST_SIGNALS_EXTREME
183 ret = pthread_mutex_lock(mutex);
184 if (ret)
185 urcu_die(ret);
186 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
187 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
188 if (ret != EBUSY && ret != EINTR)
189 urcu_die(ret);
190 poll(NULL,0,10);
191 }
192 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
193 }
194
195 static void mutex_unlock(pthread_mutex_t *mutex)
196 {
197 int ret;
198
199 ret = pthread_mutex_unlock(mutex);
200 if (ret)
201 urcu_die(ret);
202 }
203
204 static void smp_mb_master(void)
205 {
206 if (caa_likely(urcu_bp_has_sys_membarrier)) {
207 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0))
208 urcu_die(errno);
209 } else {
210 cmm_smp_mb();
211 }
212 }
213
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 */
218 static void wait_for_readers(struct cds_list_head *input_readers,
219 struct cds_list_head *cur_snap_readers,
220 struct cds_list_head *qsreaders)
221 {
222 unsigned int wait_loops = 0;
223 struct urcu_bp_reader *index, *tmp;
224
225 /*
226 * Wait for each thread URCU_TLS(urcu_bp_reader).ctr to either
227 * indicate quiescence (not nested), or observe the current
228 * rcu_gp.ctr value.
229 */
230 for (;;) {
231 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
232 wait_loops++;
233
234 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
235 switch (urcu_bp_reader_state(&index->ctr)) {
236 case URCU_BP_READER_ACTIVE_CURRENT:
237 if (cur_snap_readers) {
238 cds_list_move(&index->node,
239 cur_snap_readers);
240 break;
241 }
242 /* Fall-through */
243 case URCU_BP_READER_INACTIVE:
244 cds_list_move(&index->node, qsreaders);
245 break;
246 case URCU_BP_READER_ACTIVE_OLD:
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 }
255 }
256
257 if (cds_list_empty(input_readers)) {
258 break;
259 } else {
260 /* Temporarily unlock the registry lock. */
261 mutex_unlock(&rcu_registry_lock);
262 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS)
263 (void) poll(NULL, 0, RCU_SLEEP_DELAY_MS);
264 else
265 caa_cpu_relax();
266 /* Re-lock the registry lock before the next loop. */
267 mutex_lock(&rcu_registry_lock);
268 }
269 }
270 }
271
272 void urcu_bp_synchronize_rcu(void)
273 {
274 CDS_LIST_HEAD(cur_snap_readers);
275 CDS_LIST_HEAD(qsreaders);
276 sigset_t newmask, oldmask;
277 int ret;
278
279 ret = sigfillset(&newmask);
280 assert(!ret);
281 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
282 assert(!ret);
283
284 mutex_lock(&rcu_gp_lock);
285
286 mutex_lock(&rcu_registry_lock);
287
288 if (cds_list_empty(&registry))
289 goto out;
290
291 /* All threads should read qparity before accessing data structure
292 * where new ptr points to. */
293 /* Write new ptr before changing the qparity */
294 smp_mb_master();
295
296 /*
297 * Wait for readers to observe original parity or be quiescent.
298 * wait_for_readers() can release and grab again rcu_registry_lock
299 * interally.
300 */
301 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
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 */
311 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ URCU_BP_GP_CTR_PHASE);
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.
318 */
319
320 /*
321 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
322 * model easier to understand. It does not have a big performance impact
323 * anyway, given this is the write-side.
324 */
325 cmm_smp_mb();
326
327 /*
328 * Wait for readers to observe new parity or be quiescent.
329 * wait_for_readers() can release and grab again rcu_registry_lock
330 * interally.
331 */
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);
338
339 /*
340 * Finish waiting for reader threads before letting the old ptr being
341 * freed.
342 */
343 smp_mb_master();
344 out:
345 mutex_unlock(&rcu_registry_lock);
346 mutex_unlock(&rcu_gp_lock);
347 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
348 assert(!ret);
349 }
350 URCU_ATTR_ALIAS("urcu_bp_synchronize_rcu") void synchronize_rcu_bp();
351
352 /*
353 * library wrappers to be used by non-LGPL compatible source code.
354 */
355
356 void urcu_bp_read_lock(void)
357 {
358 _urcu_bp_read_lock();
359 }
360 URCU_ATTR_ALIAS("urcu_bp_read_lock") void rcu_read_lock_bp();
361
362 void urcu_bp_read_unlock(void)
363 {
364 _urcu_bp_read_unlock();
365 }
366 URCU_ATTR_ALIAS("urcu_bp_read_unlock") void rcu_read_unlock_bp();
367
368 int urcu_bp_read_ongoing(void)
369 {
370 return _urcu_bp_read_ongoing();
371 }
372 URCU_ATTR_ALIAS("urcu_bp_read_ongoing") int rcu_read_ongoing_bp();
373
374 /*
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.
381 */
382 static
383 void expand_arena(struct registry_arena *arena)
384 {
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;
394 new_chunk = (struct registry_chunk *) mmap(NULL,
395 new_chunk_len,
396 PROT_READ | PROT_WRITE,
397 MAP_ANONYMOUS | MAP_PRIVATE,
398 -1, 0);
399 if (new_chunk == MAP_FAILED)
400 abort();
401 memset(new_chunk, 0, new_chunk_len);
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 }
407
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);
421 memset((char *) last_chunk + old_chunk_len, 0,
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 }
427
428 /* Remap did not succeed, we need to add a new chunk. */
429 new_chunk = (struct registry_chunk *) mmap(NULL,
430 new_chunk_len,
431 PROT_READ | PROT_WRITE,
432 MAP_ANONYMOUS | MAP_PRIVATE,
433 -1, 0);
434 if (new_chunk == MAP_FAILED)
435 abort();
436 memset(new_chunk, 0, new_chunk_len);
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 }
441
442 static
443 struct 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
450 retry:
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;
473 }
474
475 /* Called with signals off and mutex locked */
476 static
477 void add_thread(void)
478 {
479 struct rcu_reader *rcu_reader_reg;
480 int ret;
481
482 rcu_reader_reg = arena_alloc(&registry_arena);
483 if (!rcu_reader_reg)
484 abort();
485 ret = pthread_setspecific(urcu_bp_key, rcu_reader_reg);
486 if (ret)
487 abort();
488
489 /* Add to registry */
490 rcu_reader_reg->tid = pthread_self();
491 assert(rcu_reader_reg->ctr == 0);
492 cds_list_add(&rcu_reader_reg->node, &registry);
493 /*
494 * Reader threads are pointing to the reader registry. This is
495 * why its memory should never be relocated.
496 */
497 URCU_TLS(urcu_bp_reader) = rcu_reader_reg;
498 }
499
500 /* Called with mutex locked */
501 static
502 void 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
512 static
513 struct registry_chunk *find_chunk(struct rcu_reader *rcu_reader_reg)
514 {
515 struct registry_chunk *chunk;
516
517 cds_list_for_each_entry(chunk, &registry_arena.chunk_list, node) {
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 }
526
527 /* Called with signals off and mutex locked */
528 static
529 void remove_thread(struct rcu_reader *rcu_reader_reg)
530 {
531 cleanup_thread(find_chunk(rcu_reader_reg), rcu_reader_reg);
532 URCU_TLS(urcu_bp_reader) = NULL;
533 }
534
535 /* Disable signals, take mutex, add to registry */
536 void urcu_bp_register(void)
537 {
538 sigset_t newmask, oldmask;
539 int ret;
540
541 ret = sigfillset(&newmask);
542 if (ret)
543 abort();
544 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
545 if (ret)
546 abort();
547
548 /*
549 * Check if a signal concurrently registered our thread since
550 * the check in rcu_read_lock().
551 */
552 if (URCU_TLS(urcu_bp_reader))
553 goto end;
554
555 /*
556 * Take care of early registration before urcu_bp constructor.
557 */
558 _urcu_bp_init();
559
560 mutex_lock(&rcu_registry_lock);
561 add_thread();
562 mutex_unlock(&rcu_registry_lock);
563 end:
564 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
565 if (ret)
566 abort();
567 }
568 URCU_ATTR_ALIAS("urcu_bp_register") void rcu_bp_register();
569
570 void 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
576 /* Disable signals, take mutex, remove from registry */
577 static
578 void urcu_bp_unregister(struct rcu_reader *rcu_reader_reg)
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
590 mutex_lock(&rcu_registry_lock);
591 remove_thread(rcu_reader_reg);
592 mutex_unlock(&rcu_registry_lock);
593 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
594 if (ret)
595 abort();
596 urcu_bp_exit();
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 */
603 static
604 void urcu_bp_thread_exit_notifier(void *rcu_key)
605 {
606 urcu_bp_unregister(rcu_key);
607 }
608
609 #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
610 static
611 void urcu_bp_sys_membarrier_status(bool available)
612 {
613 if (!available)
614 abort();
615 }
616 #else
617 static
618 void urcu_bp_sys_membarrier_status(bool available)
619 {
620 if (!available)
621 return;
622 urcu_bp_has_sys_membarrier = 1;
623 }
624 #endif
625
626 static
627 void urcu_bp_sys_membarrier_init(void)
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 }
640 urcu_bp_sys_membarrier_status(available);
641 }
642
643 static
644 void _urcu_bp_init(void)
645 {
646 mutex_lock(&init_lock);
647 if (!urcu_bp_refcount++) {
648 int ret;
649
650 ret = pthread_key_create(&urcu_bp_key,
651 urcu_bp_thread_exit_notifier);
652 if (ret)
653 abort();
654 urcu_bp_sys_membarrier_init();
655 initialized = 1;
656 }
657 mutex_unlock(&init_lock);
658 }
659
660 static
661 void urcu_bp_exit(void)
662 {
663 mutex_lock(&init_lock);
664 if (!--urcu_bp_refcount) {
665 struct registry_chunk *chunk, *tmp;
666 int ret;
667
668 cds_list_for_each_entry_safe(chunk, tmp,
669 &registry_arena.chunk_list, node) {
670 munmap((void *) chunk, chunk->data_len
671 + sizeof(struct registry_chunk));
672 }
673 CDS_INIT_LIST_HEAD(&registry_arena.chunk_list);
674 ret = pthread_key_delete(urcu_bp_key);
675 if (ret)
676 abort();
677 }
678 mutex_unlock(&init_lock);
679 }
680
681 /*
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.
686 */
687 void urcu_bp_before_fork(void)
688 {
689 sigset_t newmask, oldmask;
690 int ret;
691
692 ret = sigfillset(&newmask);
693 assert(!ret);
694 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
695 assert(!ret);
696 mutex_lock(&rcu_gp_lock);
697 mutex_lock(&rcu_registry_lock);
698 saved_fork_signal_mask = oldmask;
699 }
700 URCU_ATTR_ALIAS("urcu_bp_before_fork") void rcu_bp_before_fork();
701
702 void urcu_bp_after_fork_parent(void)
703 {
704 sigset_t oldmask;
705 int ret;
706
707 oldmask = saved_fork_signal_mask;
708 mutex_unlock(&rcu_registry_lock);
709 mutex_unlock(&rcu_gp_lock);
710 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
711 assert(!ret);
712 }
713 URCU_ATTR_ALIAS("urcu_bp_after_fork_parent")
714 void rcu_bp_after_fork_parent(void);
715
716 /*
717 * Prune all entries from registry except our own thread. Fits the Linux
718 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
719 */
720 static
721 void urcu_bp_prune_registry(void)
722 {
723 struct registry_chunk *chunk;
724 struct urcu_bp_reader *rcu_reader_reg;
725
726 cds_list_for_each_entry(chunk, &registry_arena.chunk_list, node) {
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];
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
739 void urcu_bp_after_fork_child(void)
740 {
741 sigset_t oldmask;
742 int ret;
743
744 urcu_bp_prune_registry();
745 oldmask = saved_fork_signal_mask;
746 mutex_unlock(&rcu_registry_lock);
747 mutex_unlock(&rcu_gp_lock);
748 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
749 assert(!ret);
750 }
751 URCU_ATTR_ALIAS("urcu_bp_after_fork_child")
752 void rcu_bp_after_fork_child(void);
753
754 void *urcu_bp_dereference_sym(void *p)
755 {
756 return _rcu_dereference(p);
757 }
758 URCU_ATTR_ALIAS("urcu_bp_dereference_sym")
759 void *rcu_dereference_sym_bp();
760
761 void *urcu_bp_set_pointer_sym(void **p, void *v)
762 {
763 cmm_wmb();
764 uatomic_set(p, v);
765 return v;
766 }
767 URCU_ATTR_ALIAS("urcu_bp_set_pointer_sym")
768 void *rcu_set_pointer_sym_bp();
769
770 void *urcu_bp_xchg_pointer_sym(void **p, void *v)
771 {
772 cmm_wmb();
773 return uatomic_xchg(p, v);
774 }
775 URCU_ATTR_ALIAS("urcu_bp_xchg_pointer_sym")
776 void *rcu_xchg_pointer_sym_bp();
777
778 void *urcu_bp_cmpxchg_pointer_sym(void **p, void *old, void *_new)
779 {
780 cmm_wmb();
781 return uatomic_cmpxchg(p, old, _new);
782 }
783 URCU_ATTR_ALIAS("urcu_bp_cmpxchg_pointer_sym")
784 void *rcu_cmpxchg_pointer_sym_bp();
785
786 DEFINE_RCU_FLAVOR(rcu_flavor);
787 DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor, alias_rcu_flavor);
788
789 #include "urcu-call-rcu-impl.h"
790 #include "urcu-defer-impl.h"
This page took 0.043721 seconds and 5 git commands to generate.