4 * Userspace RCU library - batch memory reclamation with kernel API
6 * Copyright (c) 2010 Paul E. McKenney <paulmck@linux.vnet.ibm.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 #include "compat-getcpu.h"
38 #include <urcu/wfcqueue.h>
39 #include <urcu/call-rcu.h>
40 #include <urcu/pointer.h>
41 #include <urcu/list.h>
42 #include <urcu/futex.h>
43 #include <urcu/tls-compat.h>
46 #include "urcu-utils.h"
48 #define SET_AFFINITY_CHECK_PERIOD (1U << 8) /* 256 */
49 #define SET_AFFINITY_CHECK_PERIOD_MASK (SET_AFFINITY_CHECK_PERIOD - 1)
51 /* Data structure that identifies a call_rcu thread. */
53 struct call_rcu_data
{
55 * We do not align head on a different cache-line than tail
56 * mainly because call_rcu callback-invocation threads use
57 * batching ("splice") to get an entire list of callbacks, which
58 * effectively empties the queue, and requires to touch the tail
61 struct cds_wfcq_tail cbs_tail
;
62 struct cds_wfcq_head cbs_head
;
65 unsigned long qlen
; /* maintained for debugging. */
68 unsigned long gp_count
;
69 struct cds_list_head list
;
70 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
72 struct call_rcu_completion
{
78 struct call_rcu_completion_work
{
80 struct call_rcu_completion
*completion
;
84 * List of all call_rcu_data structures to keep valgrind happy.
85 * Protected by call_rcu_mutex.
88 static CDS_LIST_HEAD(call_rcu_data_list
);
90 /* Link a thread using call_rcu() to its call_rcu thread. */
92 static DEFINE_URCU_TLS(struct call_rcu_data
*, thread_call_rcu_data
);
95 * Guard call_rcu thread creation and atfork handlers.
97 static pthread_mutex_t call_rcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
99 /* If a given thread does not have its own call_rcu thread, this is default. */
101 static struct call_rcu_data
*default_call_rcu_data
;
103 static struct urcu_atfork
*registered_rculfhash_atfork
;
104 static unsigned long registered_rculfhash_atfork_refcount
;
107 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
108 * available, then we can have call_rcu threads assigned to individual
109 * CPUs rather than only to specific threads.
112 #if defined(HAVE_SYSCONF) && (defined(HAVE_SCHED_GETCPU) || defined(HAVE_GETCPUID))
115 * Pointer to array of pointers to per-CPU call_rcu_data structures
116 * and # CPUs. per_cpu_call_rcu_data is a RCU-protected pointer to an
117 * array of RCU-protected pointers to call_rcu_data. call_rcu acts as a
118 * RCU read-side and reads per_cpu_call_rcu_data and the per-cpu pointer
119 * without mutex. The call_rcu_mutex protects updates.
122 static struct call_rcu_data
**per_cpu_call_rcu_data
;
125 static void maxcpus_reset(void)
130 /* Allocate the array if it has not already been allocated. */
132 static void alloc_cpu_call_rcu_data(void)
134 struct call_rcu_data
**p
;
135 static int warned
= 0;
139 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
143 p
= malloc(maxcpus
* sizeof(*per_cpu_call_rcu_data
));
145 memset(p
, '\0', maxcpus
* sizeof(*per_cpu_call_rcu_data
));
146 rcu_set_pointer(&per_cpu_call_rcu_data
, p
);
149 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
155 #else /* #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
158 * per_cpu_call_rcu_data should be constant, but some functions below, used both
159 * for cases where cpu number is available and not available, assume it it not
162 static struct call_rcu_data
**per_cpu_call_rcu_data
= NULL
;
163 static const long maxcpus
= -1;
165 static void maxcpus_reset(void)
169 static void alloc_cpu_call_rcu_data(void)
173 #endif /* #else #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
175 /* Acquire the specified pthread mutex. */
177 static void call_rcu_lock(pthread_mutex_t
*pmp
)
181 ret
= pthread_mutex_lock(pmp
);
186 /* Release the specified pthread mutex. */
188 static void call_rcu_unlock(pthread_mutex_t
*pmp
)
192 ret
= pthread_mutex_unlock(pmp
);
198 * Periodically retry setting CPU affinity if we migrate.
199 * Losing affinity can be caused by CPU hotunplug/hotplug, or by
202 #ifdef HAVE_SCHED_SETAFFINITY
204 int set_thread_cpu_affinity(struct call_rcu_data
*crdp
)
209 if (crdp
->cpu_affinity
< 0)
211 if (++crdp
->gp_count
& SET_AFFINITY_CHECK_PERIOD_MASK
)
213 if (urcu_sched_getcpu() == crdp
->cpu_affinity
)
217 CPU_SET(crdp
->cpu_affinity
, &mask
);
218 #if SCHED_SETAFFINITY_ARGS == 2
219 ret
= sched_setaffinity(0, &mask
);
221 ret
= sched_setaffinity(0, sizeof(mask
), &mask
);
224 * EINVAL is fine: can be caused by hotunplugged CPUs, or by
225 * cpuset(7). This is why we should always retry if we detect
228 if (ret
&& errno
== EINVAL
) {
236 int set_thread_cpu_affinity(struct call_rcu_data
*crdp
__attribute__((unused
)))
242 static void call_rcu_wait(struct call_rcu_data
*crdp
)
244 /* Read call_rcu list before read futex */
246 while (uatomic_read(&crdp
->futex
) == -1) {
247 if (!futex_async(&crdp
->futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
249 * Prior queued wakeups queued by unrelated code
250 * using the same address can cause futex wait to
251 * return 0 even through the futex value is still
252 * -1 (spurious wakeups). Check the value again
253 * in user-space to validate whether it really
260 /* Value already changed. */
263 /* Retry if interrupted by signal. */
264 break; /* Get out of switch. Check again. */
266 /* Unexpected error. */
272 static void call_rcu_wake_up(struct call_rcu_data
*crdp
)
274 /* Write to call_rcu list before reading/writing futex */
276 if (caa_unlikely(uatomic_read(&crdp
->futex
) == -1)) {
277 uatomic_set(&crdp
->futex
, 0);
278 if (futex_async(&crdp
->futex
, FUTEX_WAKE
, 1,
284 static void call_rcu_completion_wait(struct call_rcu_completion
*completion
)
286 /* Read completion barrier count before read futex */
288 while (uatomic_read(&completion
->futex
) == -1) {
289 if (!futex_async(&completion
->futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
291 * Prior queued wakeups queued by unrelated code
292 * using the same address can cause futex wait to
293 * return 0 even through the futex value is still
294 * -1 (spurious wakeups). Check the value again
295 * in user-space to validate whether it really
302 /* Value already changed. */
305 /* Retry if interrupted by signal. */
306 break; /* Get out of switch. Check again. */
308 /* Unexpected error. */
314 static void call_rcu_completion_wake_up(struct call_rcu_completion
*completion
)
316 /* Write to completion barrier count before reading/writing futex */
318 if (caa_unlikely(uatomic_read(&completion
->futex
) == -1)) {
319 uatomic_set(&completion
->futex
, 0);
320 if (futex_async(&completion
->futex
, FUTEX_WAKE
, 1,
326 /* This is the code run by each call_rcu thread. */
328 static void *call_rcu_thread(void *arg
)
330 unsigned long cbcount
;
331 struct call_rcu_data
*crdp
= (struct call_rcu_data
*) arg
;
332 int rt
= !!(uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_RT
);
334 if (set_thread_cpu_affinity(crdp
))
338 * If callbacks take a read-side lock, we need to be registered.
340 rcu_register_thread();
342 URCU_TLS(thread_call_rcu_data
) = crdp
;
344 uatomic_dec(&crdp
->futex
);
345 /* Decrement futex before reading call_rcu list */
349 struct cds_wfcq_head cbs_tmp_head
;
350 struct cds_wfcq_tail cbs_tmp_tail
;
351 struct cds_wfcq_node
*cbs
, *cbs_tmp_n
;
352 enum cds_wfcq_ret splice_ret
;
354 if (set_thread_cpu_affinity(crdp
))
357 if (uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSE
) {
359 * Pause requested. Become quiescent: remove
360 * ourself from all global lists, and don't
361 * process any callback. The callback lists may
362 * still be non-empty though.
364 rcu_unregister_thread();
365 cmm_smp_mb__before_uatomic_or();
366 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_PAUSED
);
367 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSE
) != 0)
368 (void) poll(NULL
, 0, 1);
369 uatomic_and(&crdp
->flags
, ~URCU_CALL_RCU_PAUSED
);
370 cmm_smp_mb__after_uatomic_and();
371 rcu_register_thread();
374 cds_wfcq_init(&cbs_tmp_head
, &cbs_tmp_tail
);
375 splice_ret
= __cds_wfcq_splice_blocking(&cbs_tmp_head
,
376 &cbs_tmp_tail
, &crdp
->cbs_head
, &crdp
->cbs_tail
);
377 assert(splice_ret
!= CDS_WFCQ_RET_WOULDBLOCK
);
378 assert(splice_ret
!= CDS_WFCQ_RET_DEST_NON_EMPTY
);
379 if (splice_ret
!= CDS_WFCQ_RET_SRC_EMPTY
) {
382 __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head
,
383 &cbs_tmp_tail
, cbs
, cbs_tmp_n
) {
384 struct rcu_head
*rhp
;
386 rhp
= caa_container_of(cbs
,
387 struct rcu_head
, next
);
391 uatomic_sub(&crdp
->qlen
, cbcount
);
393 if (uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOP
)
395 rcu_thread_offline();
397 if (cds_wfcq_empty(&crdp
->cbs_head
,
400 (void) poll(NULL
, 0, 10);
401 uatomic_dec(&crdp
->futex
);
403 * Decrement futex before reading
408 (void) poll(NULL
, 0, 10);
411 (void) poll(NULL
, 0, 10);
417 * Read call_rcu list before write futex.
420 uatomic_set(&crdp
->futex
, 0);
422 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_STOPPED
);
423 rcu_unregister_thread();
428 * Create both a call_rcu thread and the corresponding call_rcu_data
429 * structure, linking the structure in as specified. Caller must hold
433 static void call_rcu_data_init(struct call_rcu_data
**crdpp
,
437 struct call_rcu_data
*crdp
;
440 crdp
= malloc(sizeof(*crdp
));
443 memset(crdp
, '\0', sizeof(*crdp
));
444 cds_wfcq_init(&crdp
->cbs_head
, &crdp
->cbs_tail
);
448 cds_list_add(&crdp
->list
, &call_rcu_data_list
);
449 crdp
->cpu_affinity
= cpu_affinity
;
451 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
453 ret
= pthread_create(&crdp
->tid
, NULL
, call_rcu_thread
, crdp
);
459 * Return a pointer to the call_rcu_data structure for the specified
460 * CPU, returning NULL if there is none. We cannot automatically
461 * created it because the platform we are running on might not define
462 * urcu_sched_getcpu().
464 * The call to this function and use of the returned call_rcu_data
465 * should be protected by RCU read-side lock.
468 struct call_rcu_data
*get_cpu_call_rcu_data(int cpu
)
470 static int warned
= 0;
471 struct call_rcu_data
**pcpu_crdp
;
473 pcpu_crdp
= rcu_dereference(per_cpu_call_rcu_data
);
474 if (pcpu_crdp
== NULL
)
476 if (!warned
&& maxcpus
> 0 && (cpu
< 0 || maxcpus
<= cpu
)) {
477 fprintf(stderr
, "[error] liburcu: get CPU # out of range\n");
480 if (cpu
< 0 || maxcpus
<= cpu
)
482 return rcu_dereference(pcpu_crdp
[cpu
]);
484 URCU_ATTR_ALIAS(urcu_stringify(get_cpu_call_rcu_data
))
485 struct call_rcu_data
*alias_get_cpu_call_rcu_data();
488 * Return the tid corresponding to the call_rcu thread whose
489 * call_rcu_data structure is specified.
492 pthread_t
get_call_rcu_thread(struct call_rcu_data
*crdp
)
496 URCU_ATTR_ALIAS(urcu_stringify(get_call_rcu_thread
))
497 pthread_t
alias_get_call_rcu_thread();
500 * Create a call_rcu_data structure (with thread) and return a pointer.
503 static struct call_rcu_data
*__create_call_rcu_data(unsigned long flags
,
506 struct call_rcu_data
*crdp
;
508 call_rcu_data_init(&crdp
, flags
, cpu_affinity
);
512 URCU_ATTR_ALIAS(urcu_stringify(create_call_rcu_data
))
513 struct call_rcu_data
*alias_create_call_rcu_data();
514 struct call_rcu_data
*create_call_rcu_data(unsigned long flags
,
517 struct call_rcu_data
*crdp
;
519 call_rcu_lock(&call_rcu_mutex
);
520 crdp
= __create_call_rcu_data(flags
, cpu_affinity
);
521 call_rcu_unlock(&call_rcu_mutex
);
526 * Set the specified CPU to use the specified call_rcu_data structure.
528 * Use NULL to remove a CPU's call_rcu_data structure, but it is
529 * the caller's responsibility to dispose of the removed structure.
530 * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure
531 * (prior to NULLing it out, of course).
533 * The caller must wait for a grace-period to pass between return from
534 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
535 * previous call rcu data as argument.
538 int set_cpu_call_rcu_data(int cpu
, struct call_rcu_data
*crdp
)
540 static int warned
= 0;
542 call_rcu_lock(&call_rcu_mutex
);
543 alloc_cpu_call_rcu_data();
544 if (cpu
< 0 || maxcpus
<= cpu
) {
546 fprintf(stderr
, "[error] liburcu: set CPU # out of range\n");
549 call_rcu_unlock(&call_rcu_mutex
);
554 if (per_cpu_call_rcu_data
== NULL
) {
555 call_rcu_unlock(&call_rcu_mutex
);
560 if (per_cpu_call_rcu_data
[cpu
] != NULL
&& crdp
!= NULL
) {
561 call_rcu_unlock(&call_rcu_mutex
);
566 rcu_set_pointer(&per_cpu_call_rcu_data
[cpu
], crdp
);
567 call_rcu_unlock(&call_rcu_mutex
);
570 URCU_ATTR_ALIAS(urcu_stringify(set_cpu_call_rcu_data
))
571 int alias_set_cpu_call_rcu_data();
574 * Return a pointer to the default call_rcu_data structure, creating
575 * one if need be. Because we never free call_rcu_data structures,
576 * we don't need to be in an RCU read-side critical section.
579 struct call_rcu_data
*get_default_call_rcu_data(void)
581 if (default_call_rcu_data
!= NULL
)
582 return rcu_dereference(default_call_rcu_data
);
583 call_rcu_lock(&call_rcu_mutex
);
584 if (default_call_rcu_data
!= NULL
) {
585 call_rcu_unlock(&call_rcu_mutex
);
586 return default_call_rcu_data
;
588 call_rcu_data_init(&default_call_rcu_data
, 0, -1);
589 call_rcu_unlock(&call_rcu_mutex
);
590 return default_call_rcu_data
;
592 URCU_ATTR_ALIAS(urcu_stringify(get_default_call_rcu_data
))
593 struct call_rcu_data
*alias_get_default_call_rcu_data();
596 * Return the call_rcu_data structure that applies to the currently
597 * running thread. Any call_rcu_data structure assigned specifically
598 * to this thread has first priority, followed by any call_rcu_data
599 * structure assigned to the CPU on which the thread is running,
600 * followed by the default call_rcu_data structure. If there is not
601 * yet a default call_rcu_data structure, one will be created.
603 * Calls to this function and use of the returned call_rcu_data should
604 * be protected by RCU read-side lock.
606 struct call_rcu_data
*get_call_rcu_data(void)
608 struct call_rcu_data
*crd
;
610 if (URCU_TLS(thread_call_rcu_data
) != NULL
)
611 return URCU_TLS(thread_call_rcu_data
);
614 crd
= get_cpu_call_rcu_data(urcu_sched_getcpu());
619 return get_default_call_rcu_data();
621 URCU_ATTR_ALIAS(urcu_stringify(get_call_rcu_data
))
622 struct call_rcu_data
*alias_get_call_rcu_data();
625 * Return a pointer to this task's call_rcu_data if there is one.
628 struct call_rcu_data
*get_thread_call_rcu_data(void)
630 return URCU_TLS(thread_call_rcu_data
);
632 URCU_ATTR_ALIAS(urcu_stringify(get_thread_call_rcu_data
))
633 struct call_rcu_data
*alias_get_thread_call_rcu_data();
636 * Set this task's call_rcu_data structure as specified, regardless
637 * of whether or not this task already had one. (This allows switching
638 * to and from real-time call_rcu threads, for example.)
640 * Use NULL to remove a thread's call_rcu_data structure, but it is
641 * the caller's responsibility to dispose of the removed structure.
642 * Use get_thread_call_rcu_data() to obtain a pointer to the old structure
643 * (prior to NULLing it out, of course).
646 void set_thread_call_rcu_data(struct call_rcu_data
*crdp
)
648 URCU_TLS(thread_call_rcu_data
) = crdp
;
650 URCU_ATTR_ALIAS(urcu_stringify(set_thread_call_rcu_data
))
651 void alias_set_thread_call_rcu_data();
654 * Create a separate call_rcu thread for each CPU. This does not
655 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
656 * function if you want that behavior. Should be paired with
657 * free_all_cpu_call_rcu_data() to teardown these call_rcu worker
661 int create_all_cpu_call_rcu_data(unsigned long flags
)
664 struct call_rcu_data
*crdp
;
667 call_rcu_lock(&call_rcu_mutex
);
668 alloc_cpu_call_rcu_data();
669 call_rcu_unlock(&call_rcu_mutex
);
674 if (per_cpu_call_rcu_data
== NULL
) {
678 for (i
= 0; i
< maxcpus
; i
++) {
679 call_rcu_lock(&call_rcu_mutex
);
680 if (get_cpu_call_rcu_data(i
)) {
681 call_rcu_unlock(&call_rcu_mutex
);
684 crdp
= __create_call_rcu_data(flags
, i
);
686 call_rcu_unlock(&call_rcu_mutex
);
690 call_rcu_unlock(&call_rcu_mutex
);
691 if ((ret
= set_cpu_call_rcu_data(i
, crdp
)) != 0) {
692 call_rcu_data_free(crdp
);
694 /* it has been created by other thread */
703 URCU_ATTR_ALIAS(urcu_stringify(create_all_cpu_call_rcu_data
))
704 int alias_create_all_cpu_call_rcu_data();
707 * Wake up the call_rcu thread corresponding to the specified
708 * call_rcu_data structure.
710 static void wake_call_rcu_thread(struct call_rcu_data
*crdp
)
712 if (!(_CMM_LOAD_SHARED(crdp
->flags
) & URCU_CALL_RCU_RT
))
713 call_rcu_wake_up(crdp
);
716 static void _call_rcu(struct rcu_head
*head
,
717 void (*func
)(struct rcu_head
*head
),
718 struct call_rcu_data
*crdp
)
720 cds_wfcq_node_init(&head
->next
);
722 cds_wfcq_enqueue(&crdp
->cbs_head
, &crdp
->cbs_tail
, &head
->next
);
723 uatomic_inc(&crdp
->qlen
);
724 wake_call_rcu_thread(crdp
);
728 * Schedule a function to be invoked after a following grace period.
729 * This is the only function that must be called -- the others are
730 * only present to allow applications to tune their use of RCU for
731 * maximum performance.
733 * Note that unless a call_rcu thread has not already been created,
734 * the first invocation of call_rcu() will create one. So, if you
735 * need the first invocation of call_rcu() to be fast, make sure
736 * to create a call_rcu thread first. One way to accomplish this is
737 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
739 * call_rcu must be called by registered RCU read-side threads.
741 void call_rcu(struct rcu_head
*head
,
742 void (*func
)(struct rcu_head
*head
))
744 struct call_rcu_data
*crdp
;
746 /* Holding rcu read-side lock across use of per-cpu crdp */
748 crdp
= get_call_rcu_data();
749 _call_rcu(head
, func
, crdp
);
752 URCU_ATTR_ALIAS(urcu_stringify(call_rcu
)) void alias_call_rcu();
755 * Free up the specified call_rcu_data structure, terminating the
756 * associated call_rcu thread. The caller must have previously
757 * removed the call_rcu_data structure from per-thread or per-CPU
758 * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU
759 * call_rcu_data structures or set_thread_call_rcu_data(NULL) for
760 * per-thread call_rcu_data structures.
762 * We silently refuse to free up the default call_rcu_data structure
763 * because that is where we put any leftover callbacks. Note that
764 * the possibility of self-spawning callbacks makes it impossible
765 * to execute all the callbacks in finite time without putting any
766 * newly spawned callbacks somewhere else. The "somewhere else" of
767 * last resort is the default call_rcu_data structure.
769 * We also silently refuse to free NULL pointers. This simplifies
772 * The caller must wait for a grace-period to pass between return from
773 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
774 * previous call rcu data as argument.
776 * Note: introducing __cds_wfcq_splice_blocking() in this function fixed
777 * a list corruption bug in the 0.7.x series. The equivalent fix
778 * appeared in 0.6.8 for the stable-0.6 branch.
780 void call_rcu_data_free(struct call_rcu_data
*crdp
)
782 if (crdp
== NULL
|| crdp
== default_call_rcu_data
) {
785 if ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOPPED
) == 0) {
786 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_STOP
);
787 wake_call_rcu_thread(crdp
);
788 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOPPED
) == 0)
789 (void) poll(NULL
, 0, 1);
791 call_rcu_lock(&call_rcu_mutex
);
792 if (!cds_wfcq_empty(&crdp
->cbs_head
, &crdp
->cbs_tail
)) {
793 call_rcu_unlock(&call_rcu_mutex
);
794 /* Create default call rcu data if need be. */
795 /* CBs queued here will be handed to the default list. */
796 (void) get_default_call_rcu_data();
797 call_rcu_lock(&call_rcu_mutex
);
798 __cds_wfcq_splice_blocking(&default_call_rcu_data
->cbs_head
,
799 &default_call_rcu_data
->cbs_tail
,
800 &crdp
->cbs_head
, &crdp
->cbs_tail
);
801 uatomic_add(&default_call_rcu_data
->qlen
,
802 uatomic_read(&crdp
->qlen
));
803 wake_call_rcu_thread(default_call_rcu_data
);
806 cds_list_del(&crdp
->list
);
807 call_rcu_unlock(&call_rcu_mutex
);
811 URCU_ATTR_ALIAS(urcu_stringify(call_rcu_data_free
))
812 void alias_call_rcu_data_free();
815 * Clean up all the per-CPU call_rcu threads.
817 void free_all_cpu_call_rcu_data(void)
820 struct call_rcu_data
**crdp
;
821 static int warned
= 0;
826 crdp
= malloc(sizeof(*crdp
) * maxcpus
);
829 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
835 for (cpu
= 0; cpu
< maxcpus
; cpu
++) {
836 crdp
[cpu
] = get_cpu_call_rcu_data(cpu
);
837 if (crdp
[cpu
] == NULL
)
839 set_cpu_call_rcu_data(cpu
, NULL
);
842 * Wait for call_rcu sites acting as RCU readers of the
843 * call_rcu_data to become quiescent.
846 for (cpu
= 0; cpu
< maxcpus
; cpu
++) {
847 if (crdp
[cpu
] == NULL
)
849 call_rcu_data_free(crdp
[cpu
]);
854 /* ABI6 has a non-namespaced free_all_cpu_call_rcu_data for qsbr */
855 #undef free_all_cpu_call_rcu_data
856 URCU_ATTR_ALIAS("urcu_qsbr_free_all_cpu_call_rcu_data")
857 void free_all_cpu_call_rcu_data();
858 #define free_all_cpu_call_rcu_data urcu_qsbr_free_all_cpu_call_rcu_data
860 URCU_ATTR_ALIAS(urcu_stringify(free_all_cpu_call_rcu_data
))
861 void alias_free_all_cpu_call_rcu_data();
865 void free_completion(struct urcu_ref
*ref
)
867 struct call_rcu_completion
*completion
;
869 completion
= caa_container_of(ref
, struct call_rcu_completion
, ref
);
874 void _rcu_barrier_complete(struct rcu_head
*head
)
876 struct call_rcu_completion_work
*work
;
877 struct call_rcu_completion
*completion
;
879 work
= caa_container_of(head
, struct call_rcu_completion_work
, head
);
880 completion
= work
->completion
;
881 if (!uatomic_sub_return(&completion
->barrier_count
, 1))
882 call_rcu_completion_wake_up(completion
);
883 urcu_ref_put(&completion
->ref
, free_completion
);
888 * Wait for all in-flight call_rcu callbacks to complete execution.
890 void rcu_barrier(void)
892 struct call_rcu_data
*crdp
;
893 struct call_rcu_completion
*completion
;
897 /* Put in offline state in QSBR. */
898 was_online
= _rcu_read_ongoing();
900 rcu_thread_offline();
902 * Calling a rcu_barrier() within a RCU read-side critical
903 * section is an error.
905 if (_rcu_read_ongoing()) {
906 static int warned
= 0;
909 fprintf(stderr
, "[error] liburcu: rcu_barrier() called from within RCU read-side critical section.\n");
915 completion
= calloc(sizeof(*completion
), 1);
919 call_rcu_lock(&call_rcu_mutex
);
920 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
)
923 /* Referenced by rcu_barrier() and each call_rcu thread. */
924 urcu_ref_set(&completion
->ref
, count
+ 1);
925 completion
->barrier_count
= count
;
927 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
928 struct call_rcu_completion_work
*work
;
930 work
= calloc(sizeof(*work
), 1);
933 work
->completion
= completion
;
934 _call_rcu(&work
->head
, _rcu_barrier_complete
, crdp
);
936 call_rcu_unlock(&call_rcu_mutex
);
940 uatomic_dec(&completion
->futex
);
941 /* Decrement futex before reading barrier_count */
943 if (!uatomic_read(&completion
->barrier_count
))
945 call_rcu_completion_wait(completion
);
948 urcu_ref_put(&completion
->ref
, free_completion
);
954 URCU_ATTR_ALIAS(urcu_stringify(rcu_barrier
))
955 void alias_rcu_barrier();
958 * Acquire the call_rcu_mutex in order to ensure that the child sees
959 * all of the call_rcu() data structures in a consistent state. Ensure
960 * that all call_rcu threads are in a quiescent state across fork.
961 * Suitable for pthread_atfork() and friends.
963 void call_rcu_before_fork(void)
965 struct call_rcu_data
*crdp
;
966 struct urcu_atfork
*atfork
;
968 call_rcu_lock(&call_rcu_mutex
);
970 atfork
= registered_rculfhash_atfork
;
972 atfork
->before_fork(atfork
->priv
);
974 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
975 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_PAUSE
);
976 cmm_smp_mb__after_uatomic_or();
977 wake_call_rcu_thread(crdp
);
979 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
980 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSED
) == 0)
981 (void) poll(NULL
, 0, 1);
984 URCU_ATTR_ALIAS(urcu_stringify(call_rcu_before_fork
))
985 void alias_call_rcu_before_fork();
988 * Clean up call_rcu data structures in the parent of a successful fork()
989 * that is not followed by exec() in the child. Suitable for
990 * pthread_atfork() and friends.
992 void call_rcu_after_fork_parent(void)
994 struct call_rcu_data
*crdp
;
995 struct urcu_atfork
*atfork
;
997 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
)
998 uatomic_and(&crdp
->flags
, ~URCU_CALL_RCU_PAUSE
);
999 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
1000 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSED
) != 0)
1001 (void) poll(NULL
, 0, 1);
1003 atfork
= registered_rculfhash_atfork
;
1005 atfork
->after_fork_parent(atfork
->priv
);
1006 call_rcu_unlock(&call_rcu_mutex
);
1008 URCU_ATTR_ALIAS(urcu_stringify(call_rcu_after_fork_parent
))
1009 void alias_call_rcu_after_fork_parent();
1012 * Clean up call_rcu data structures in the child of a successful fork()
1013 * that is not followed by exec(). Suitable for pthread_atfork() and
1016 void call_rcu_after_fork_child(void)
1018 struct call_rcu_data
*crdp
, *next
;
1019 struct urcu_atfork
*atfork
;
1021 /* Release the mutex. */
1022 call_rcu_unlock(&call_rcu_mutex
);
1024 atfork
= registered_rculfhash_atfork
;
1026 atfork
->after_fork_child(atfork
->priv
);
1028 /* Do nothing when call_rcu() has not been used */
1029 if (cds_list_empty(&call_rcu_data_list
))
1033 * Allocate a new default call_rcu_data structure in order
1034 * to get a working call_rcu thread to go with it.
1036 default_call_rcu_data
= NULL
;
1037 (void)get_default_call_rcu_data();
1039 /* Cleanup call_rcu_data pointers before use */
1041 free(per_cpu_call_rcu_data
);
1042 rcu_set_pointer(&per_cpu_call_rcu_data
, NULL
);
1043 URCU_TLS(thread_call_rcu_data
) = NULL
;
1046 * Dispose of all of the rest of the call_rcu_data structures.
1047 * Leftover call_rcu callbacks will be merged into the new
1048 * default call_rcu thread queue.
1050 cds_list_for_each_entry_safe(crdp
, next
, &call_rcu_data_list
, list
) {
1051 if (crdp
== default_call_rcu_data
)
1053 uatomic_set(&crdp
->flags
, URCU_CALL_RCU_STOPPED
);
1054 call_rcu_data_free(crdp
);
1057 URCU_ATTR_ALIAS(urcu_stringify(call_rcu_after_fork_child
))
1058 void alias_call_rcu_after_fork_child();
1060 void urcu_register_rculfhash_atfork(struct urcu_atfork
*atfork
)
1062 call_rcu_lock(&call_rcu_mutex
);
1063 if (registered_rculfhash_atfork_refcount
++)
1065 registered_rculfhash_atfork
= atfork
;
1067 call_rcu_unlock(&call_rcu_mutex
);
1069 URCU_ATTR_ALIAS(urcu_stringify(urcu_register_rculfhash_atfork
))
1070 void alias_urcu_register_rculfhash_atfork();
1072 void urcu_unregister_rculfhash_atfork(struct urcu_atfork
*atfork
__attribute__((unused
)))
1074 call_rcu_lock(&call_rcu_mutex
);
1075 if (--registered_rculfhash_atfork_refcount
)
1077 registered_rculfhash_atfork
= NULL
;
1079 call_rcu_unlock(&call_rcu_mutex
);
1081 URCU_ATTR_ALIAS(urcu_stringify(urcu_unregister_rculfhash_atfork
))
1082 void alias_urcu_unregister_rculfhash_atfork();