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"
47 #include "compat-smp.h"
49 #define SET_AFFINITY_CHECK_PERIOD (1U << 8) /* 256 */
50 #define SET_AFFINITY_CHECK_PERIOD_MASK (SET_AFFINITY_CHECK_PERIOD - 1)
52 /* Data structure that identifies a call_rcu thread. */
54 struct call_rcu_data
{
56 * We do not align head on a different cache-line than tail
57 * mainly because call_rcu callback-invocation threads use
58 * batching ("splice") to get an entire list of callbacks, which
59 * effectively empties the queue, and requires to touch the tail
62 struct cds_wfcq_tail cbs_tail
;
63 struct cds_wfcq_head cbs_head
;
66 unsigned long qlen
; /* maintained for debugging. */
69 unsigned long gp_count
;
70 struct cds_list_head list
;
71 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
73 struct call_rcu_completion
{
79 struct call_rcu_completion_work
{
81 struct call_rcu_completion
*completion
;
85 CRDF_FLAG_JOIN_THREAD
= (1 << 0),
89 * List of all call_rcu_data structures to keep valgrind happy.
90 * Protected by call_rcu_mutex.
93 static CDS_LIST_HEAD(call_rcu_data_list
);
95 /* Link a thread using call_rcu() to its call_rcu thread. */
97 static DEFINE_URCU_TLS(struct call_rcu_data
*, thread_call_rcu_data
);
100 * Guard call_rcu thread creation and atfork handlers.
102 static pthread_mutex_t call_rcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
104 /* If a given thread does not have its own call_rcu thread, this is default. */
106 static struct call_rcu_data
*default_call_rcu_data
;
108 static struct urcu_atfork
*registered_rculfhash_atfork
;
111 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
112 * available, then we can have call_rcu threads assigned to individual
113 * CPUs rather than only to specific threads.
116 #if defined(HAVE_SYSCONF) && (defined(HAVE_SCHED_GETCPU) || defined(HAVE_GETCPUID))
119 * Pointer to array of pointers to per-CPU call_rcu_data structures
120 * and # CPUs. per_cpu_call_rcu_data is a RCU-protected pointer to an
121 * array of RCU-protected pointers to call_rcu_data. call_rcu acts as a
122 * RCU read-side and reads per_cpu_call_rcu_data and the per-cpu pointer
123 * without mutex. The call_rcu_mutex protects updates.
126 static struct call_rcu_data
**per_cpu_call_rcu_data
;
127 static long cpus_array_len
;
129 static void cpus_array_len_reset(void)
134 /* Allocate the array if it has not already been allocated. */
136 static void alloc_cpu_call_rcu_data(void)
138 struct call_rcu_data
**p
;
139 static int warned
= 0;
141 if (cpus_array_len
!= 0)
143 cpus_array_len
= get_possible_cpus_array_len();
144 if (cpus_array_len
<= 0) {
147 p
= malloc(cpus_array_len
* sizeof(*per_cpu_call_rcu_data
));
149 memset(p
, '\0', cpus_array_len
* sizeof(*per_cpu_call_rcu_data
));
150 rcu_set_pointer(&per_cpu_call_rcu_data
, p
);
153 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
159 #else /* #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
162 * per_cpu_call_rcu_data should be constant, but some functions below, used both
163 * for cases where cpu number is available and not available, assume it it not
166 static struct call_rcu_data
**per_cpu_call_rcu_data
= NULL
;
167 static const long cpus_array_len
= -1;
169 static void cpus_array_len_reset(void)
173 static void alloc_cpu_call_rcu_data(void)
177 #endif /* #else #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
179 /* Acquire the specified pthread mutex. */
181 static void call_rcu_lock(pthread_mutex_t
*pmp
)
185 ret
= pthread_mutex_lock(pmp
);
190 /* Release the specified pthread mutex. */
192 static void call_rcu_unlock(pthread_mutex_t
*pmp
)
196 ret
= pthread_mutex_unlock(pmp
);
202 * Periodically retry setting CPU affinity if we migrate.
203 * Losing affinity can be caused by CPU hotunplug/hotplug, or by
206 #ifdef HAVE_SCHED_SETAFFINITY
208 int set_thread_cpu_affinity(struct call_rcu_data
*crdp
)
213 if (crdp
->cpu_affinity
< 0)
215 if (++crdp
->gp_count
& SET_AFFINITY_CHECK_PERIOD_MASK
)
217 if (urcu_sched_getcpu() == crdp
->cpu_affinity
)
221 CPU_SET(crdp
->cpu_affinity
, &mask
);
222 ret
= sched_setaffinity(0, sizeof(mask
), &mask
);
225 * EINVAL is fine: can be caused by hotunplugged CPUs, or by
226 * cpuset(7). This is why we should always retry if we detect
229 if (ret
&& errno
== EINVAL
) {
237 int set_thread_cpu_affinity(struct call_rcu_data
*crdp
__attribute__((unused
)))
243 static void call_rcu_wait(struct call_rcu_data
*crdp
)
245 /* Read call_rcu list before read futex */
247 while (uatomic_read(&crdp
->futex
) == -1) {
248 if (!futex_async(&crdp
->futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
250 * Prior queued wakeups queued by unrelated code
251 * using the same address can cause futex wait to
252 * return 0 even through the futex value is still
253 * -1 (spurious wakeups). Check the value again
254 * in user-space to validate whether it really
261 /* Value already changed. */
264 /* Retry if interrupted by signal. */
265 break; /* Get out of switch. Check again. */
267 /* Unexpected error. */
273 static void call_rcu_wake_up(struct call_rcu_data
*crdp
)
275 /* Write to call_rcu list before reading/writing futex */
277 if (caa_unlikely(uatomic_read(&crdp
->futex
) == -1)) {
278 uatomic_set(&crdp
->futex
, 0);
279 if (futex_async(&crdp
->futex
, FUTEX_WAKE
, 1,
285 static void call_rcu_completion_wait(struct call_rcu_completion
*completion
)
287 /* Read completion barrier count before read futex */
289 while (uatomic_read(&completion
->futex
) == -1) {
290 if (!futex_async(&completion
->futex
, FUTEX_WAIT
, -1, NULL
, NULL
, 0)) {
292 * Prior queued wakeups queued by unrelated code
293 * using the same address can cause futex wait to
294 * return 0 even through the futex value is still
295 * -1 (spurious wakeups). Check the value again
296 * in user-space to validate whether it really
303 /* Value already changed. */
306 /* Retry if interrupted by signal. */
307 break; /* Get out of switch. Check again. */
309 /* Unexpected error. */
315 static void call_rcu_completion_wake_up(struct call_rcu_completion
*completion
)
317 /* Write to completion barrier count before reading/writing futex */
319 if (caa_unlikely(uatomic_read(&completion
->futex
) == -1)) {
320 uatomic_set(&completion
->futex
, 0);
321 if (futex_async(&completion
->futex
, FUTEX_WAKE
, 1,
327 /* This is the code run by each call_rcu thread. */
329 static void *call_rcu_thread(void *arg
)
331 unsigned long cbcount
;
332 struct call_rcu_data
*crdp
= (struct call_rcu_data
*) arg
;
333 int rt
= !!(uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_RT
);
335 if (set_thread_cpu_affinity(crdp
))
339 * If callbacks take a read-side lock, we need to be registered.
341 rcu_register_thread();
343 URCU_TLS(thread_call_rcu_data
) = crdp
;
345 uatomic_dec(&crdp
->futex
);
346 /* Decrement futex before reading call_rcu list */
350 struct cds_wfcq_head cbs_tmp_head
;
351 struct cds_wfcq_tail cbs_tmp_tail
;
352 struct cds_wfcq_node
*cbs
, *cbs_tmp_n
;
353 enum cds_wfcq_ret splice_ret
;
355 if (set_thread_cpu_affinity(crdp
))
358 if (uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSE
) {
360 * Pause requested. Become quiescent: remove
361 * ourself from all global lists, and don't
362 * process any callback. The callback lists may
363 * still be non-empty though.
365 rcu_unregister_thread();
366 cmm_smp_mb__before_uatomic_or();
367 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_PAUSED
);
368 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSE
) != 0)
369 (void) poll(NULL
, 0, 1);
370 uatomic_and(&crdp
->flags
, ~URCU_CALL_RCU_PAUSED
);
371 cmm_smp_mb__after_uatomic_and();
372 rcu_register_thread();
375 cds_wfcq_init(&cbs_tmp_head
, &cbs_tmp_tail
);
376 splice_ret
= __cds_wfcq_splice_blocking(&cbs_tmp_head
,
377 &cbs_tmp_tail
, &crdp
->cbs_head
, &crdp
->cbs_tail
);
378 assert(splice_ret
!= CDS_WFCQ_RET_WOULDBLOCK
);
379 assert(splice_ret
!= CDS_WFCQ_RET_DEST_NON_EMPTY
);
380 if (splice_ret
!= CDS_WFCQ_RET_SRC_EMPTY
) {
383 __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head
,
384 &cbs_tmp_tail
, cbs
, cbs_tmp_n
) {
385 struct rcu_head
*rhp
;
387 rhp
= caa_container_of(cbs
,
388 struct rcu_head
, next
);
392 uatomic_sub(&crdp
->qlen
, cbcount
);
394 if (uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOP
)
396 rcu_thread_offline();
398 if (cds_wfcq_empty(&crdp
->cbs_head
,
401 (void) poll(NULL
, 0, 10);
402 uatomic_dec(&crdp
->futex
);
404 * Decrement futex before reading
409 (void) poll(NULL
, 0, 10);
412 (void) poll(NULL
, 0, 10);
418 * Read call_rcu list before write futex.
421 uatomic_set(&crdp
->futex
, 0);
423 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_STOPPED
);
424 rcu_unregister_thread();
429 * Create both a call_rcu thread and the corresponding call_rcu_data
430 * structure, linking the structure in as specified. Caller must hold
434 static void call_rcu_data_init(struct call_rcu_data
**crdpp
,
438 struct call_rcu_data
*crdp
;
441 crdp
= malloc(sizeof(*crdp
));
444 memset(crdp
, '\0', sizeof(*crdp
));
445 cds_wfcq_init(&crdp
->cbs_head
, &crdp
->cbs_tail
);
449 cds_list_add(&crdp
->list
, &call_rcu_data_list
);
450 crdp
->cpu_affinity
= cpu_affinity
;
452 rcu_set_pointer(crdpp
, crdp
);
454 ret
= pthread_create(&crdp
->tid
, NULL
, call_rcu_thread
, crdp
);
460 * Return a pointer to the call_rcu_data structure for the specified
461 * CPU, returning NULL if there is none. We cannot automatically
462 * created it because the platform we are running on might not define
463 * urcu_sched_getcpu().
465 * The call to this function and use of the returned call_rcu_data
466 * should be protected by RCU read-side lock.
469 struct call_rcu_data
*get_cpu_call_rcu_data(int cpu
)
471 static int warned
= 0;
472 struct call_rcu_data
**pcpu_crdp
;
474 pcpu_crdp
= rcu_dereference(per_cpu_call_rcu_data
);
475 if (pcpu_crdp
== NULL
)
477 if (!warned
&& cpus_array_len
> 0 && (cpu
< 0 || cpus_array_len
<= cpu
)) {
478 fprintf(stderr
, "[error] liburcu: get CPU # out of range\n");
481 if (cpu
< 0 || cpus_array_len
<= cpu
)
483 return rcu_dereference(pcpu_crdp
[cpu
]);
487 * Return the tid corresponding to the call_rcu thread whose
488 * call_rcu_data structure is specified.
491 pthread_t
get_call_rcu_thread(struct call_rcu_data
*crdp
)
497 * Create a call_rcu_data structure (with thread) and return a pointer.
500 static struct call_rcu_data
*__create_call_rcu_data(unsigned long flags
,
503 struct call_rcu_data
*crdp
;
505 call_rcu_data_init(&crdp
, flags
, cpu_affinity
);
509 struct call_rcu_data
*create_call_rcu_data(unsigned long flags
,
512 struct call_rcu_data
*crdp
;
514 call_rcu_lock(&call_rcu_mutex
);
515 crdp
= __create_call_rcu_data(flags
, cpu_affinity
);
516 call_rcu_unlock(&call_rcu_mutex
);
521 * Set the specified CPU to use the specified call_rcu_data structure.
523 * Use NULL to remove a CPU's call_rcu_data structure, but it is
524 * the caller's responsibility to dispose of the removed structure.
525 * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure
526 * (prior to NULLing it out, of course).
528 * The caller must wait for a grace-period to pass between return from
529 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
530 * previous call rcu data as argument.
533 int set_cpu_call_rcu_data(int cpu
, struct call_rcu_data
*crdp
)
535 static int warned
= 0;
537 call_rcu_lock(&call_rcu_mutex
);
538 alloc_cpu_call_rcu_data();
539 if (cpu
< 0 || cpus_array_len
<= cpu
) {
541 fprintf(stderr
, "[error] liburcu: set CPU # out of range\n");
544 call_rcu_unlock(&call_rcu_mutex
);
549 if (per_cpu_call_rcu_data
== NULL
) {
550 call_rcu_unlock(&call_rcu_mutex
);
555 if (per_cpu_call_rcu_data
[cpu
] != NULL
&& crdp
!= NULL
) {
556 call_rcu_unlock(&call_rcu_mutex
);
561 rcu_set_pointer(&per_cpu_call_rcu_data
[cpu
], crdp
);
562 call_rcu_unlock(&call_rcu_mutex
);
567 * Return a pointer to the default call_rcu_data structure, creating
570 * The call to this function with intent to use the returned
571 * call_rcu_data should be protected by RCU read-side lock.
574 struct call_rcu_data
*get_default_call_rcu_data(void)
576 struct call_rcu_data
*crdp
;
578 crdp
= rcu_dereference(default_call_rcu_data
);
582 call_rcu_lock(&call_rcu_mutex
);
583 if (default_call_rcu_data
== NULL
)
584 call_rcu_data_init(&default_call_rcu_data
, 0, -1);
585 crdp
= default_call_rcu_data
;
586 call_rcu_unlock(&call_rcu_mutex
);
592 * Return the call_rcu_data structure that applies to the currently
593 * running thread. Any call_rcu_data structure assigned specifically
594 * to this thread has first priority, followed by any call_rcu_data
595 * structure assigned to the CPU on which the thread is running,
596 * followed by the default call_rcu_data structure. If there is not
597 * yet a default call_rcu_data structure, one will be created.
599 * Calls to this function and use of the returned call_rcu_data should
600 * be protected by RCU read-side lock.
602 struct call_rcu_data
*get_call_rcu_data(void)
604 struct call_rcu_data
*crd
;
606 if (URCU_TLS(thread_call_rcu_data
) != NULL
)
607 return URCU_TLS(thread_call_rcu_data
);
609 if (cpus_array_len
> 0) {
610 crd
= get_cpu_call_rcu_data(urcu_sched_getcpu());
615 return get_default_call_rcu_data();
619 * Return a pointer to this task's call_rcu_data if there is one.
622 struct call_rcu_data
*get_thread_call_rcu_data(void)
624 return URCU_TLS(thread_call_rcu_data
);
628 * Set this task's call_rcu_data structure as specified, regardless
629 * of whether or not this task already had one. (This allows switching
630 * to and from real-time call_rcu threads, for example.)
632 * Use NULL to remove a thread's call_rcu_data structure, but it is
633 * the caller's responsibility to dispose of the removed structure.
634 * Use get_thread_call_rcu_data() to obtain a pointer to the old structure
635 * (prior to NULLing it out, of course).
638 void set_thread_call_rcu_data(struct call_rcu_data
*crdp
)
640 URCU_TLS(thread_call_rcu_data
) = crdp
;
644 * Create a separate call_rcu thread for each CPU. This does not
645 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
646 * function if you want that behavior. Should be paired with
647 * free_all_cpu_call_rcu_data() to teardown these call_rcu worker
651 int create_all_cpu_call_rcu_data(unsigned long flags
)
654 struct call_rcu_data
*crdp
;
657 call_rcu_lock(&call_rcu_mutex
);
658 alloc_cpu_call_rcu_data();
659 call_rcu_unlock(&call_rcu_mutex
);
660 if (cpus_array_len
<= 0) {
664 if (per_cpu_call_rcu_data
== NULL
) {
668 for (i
= 0; i
< cpus_array_len
; i
++) {
669 call_rcu_lock(&call_rcu_mutex
);
670 if (get_cpu_call_rcu_data(i
)) {
671 call_rcu_unlock(&call_rcu_mutex
);
674 crdp
= __create_call_rcu_data(flags
, i
);
676 call_rcu_unlock(&call_rcu_mutex
);
680 call_rcu_unlock(&call_rcu_mutex
);
681 if ((ret
= set_cpu_call_rcu_data(i
, crdp
)) != 0) {
682 call_rcu_data_free(crdp
);
684 /* it has been created by other thread */
695 * Wake up the call_rcu thread corresponding to the specified
696 * call_rcu_data structure.
698 static void wake_call_rcu_thread(struct call_rcu_data
*crdp
)
700 if (!(_CMM_LOAD_SHARED(crdp
->flags
) & URCU_CALL_RCU_RT
))
701 call_rcu_wake_up(crdp
);
704 static void _call_rcu(struct rcu_head
*head
,
705 void (*func
)(struct rcu_head
*head
),
706 struct call_rcu_data
*crdp
)
708 cds_wfcq_node_init(&head
->next
);
710 cds_wfcq_enqueue(&crdp
->cbs_head
, &crdp
->cbs_tail
, &head
->next
);
711 uatomic_inc(&crdp
->qlen
);
712 wake_call_rcu_thread(crdp
);
716 * Schedule a function to be invoked after a following grace period.
717 * This is the only function that must be called -- the others are
718 * only present to allow applications to tune their use of RCU for
719 * maximum performance.
721 * Note that unless a call_rcu thread has not already been created,
722 * the first invocation of call_rcu() will create one. So, if you
723 * need the first invocation of call_rcu() to be fast, make sure
724 * to create a call_rcu thread first. One way to accomplish this is
725 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
727 * call_rcu must be called by registered RCU read-side threads.
729 void call_rcu(struct rcu_head
*head
,
730 void (*func
)(struct rcu_head
*head
))
732 struct call_rcu_data
*crdp
;
734 /* Holding rcu read-side lock across use of per-cpu crdp */
736 crdp
= get_call_rcu_data();
737 _call_rcu(head
, func
, crdp
);
742 * Free up the specified call_rcu_data structure, terminating the
743 * associated call_rcu thread. The caller must have previously
744 * removed the call_rcu_data structure from per-thread or per-CPU
745 * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU
746 * call_rcu_data structures or set_thread_call_rcu_data(NULL) for
747 * per-thread call_rcu_data structures.
749 * We silently refuse to free up the default call_rcu_data structure
750 * because that is where we put any leftover callbacks. Note that
751 * the possibility of self-spawning callbacks makes it impossible
752 * to execute all the callbacks in finite time without putting any
753 * newly spawned callbacks somewhere else. The "somewhere else" of
754 * last resort is the default call_rcu_data structure.
756 * We also silently refuse to free NULL pointers. This simplifies
759 * The caller must wait for a grace-period to pass between return from
760 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
761 * previous call rcu data as argument.
763 * Note: introducing __cds_wfcq_splice_blocking() in this function fixed
764 * a list corruption bug in the 0.7.x series. The equivalent fix
765 * appeared in 0.6.8 for the stable-0.6 branch.
768 void _call_rcu_data_free(struct call_rcu_data
*crdp
, unsigned int flags
)
770 if (crdp
== NULL
|| crdp
== default_call_rcu_data
) {
773 if ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOPPED
) == 0) {
774 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_STOP
);
775 wake_call_rcu_thread(crdp
);
776 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOPPED
) == 0)
777 (void) poll(NULL
, 0, 1);
779 call_rcu_lock(&call_rcu_mutex
);
780 if (!cds_wfcq_empty(&crdp
->cbs_head
, &crdp
->cbs_tail
)) {
781 call_rcu_unlock(&call_rcu_mutex
);
782 /* Create default call rcu data if need be. */
783 /* CBs queued here will be handed to the default list. */
784 (void) get_default_call_rcu_data();
785 call_rcu_lock(&call_rcu_mutex
);
786 __cds_wfcq_splice_blocking(&default_call_rcu_data
->cbs_head
,
787 &default_call_rcu_data
->cbs_tail
,
788 &crdp
->cbs_head
, &crdp
->cbs_tail
);
789 uatomic_add(&default_call_rcu_data
->qlen
,
790 uatomic_read(&crdp
->qlen
));
791 wake_call_rcu_thread(default_call_rcu_data
);
794 cds_list_del(&crdp
->list
);
795 call_rcu_unlock(&call_rcu_mutex
);
797 if (flags
& CRDF_FLAG_JOIN_THREAD
) {
800 ret
= pthread_join(get_call_rcu_thread(crdp
), NULL
);
807 void call_rcu_data_free(struct call_rcu_data
*crdp
)
809 _call_rcu_data_free(crdp
, CRDF_FLAG_JOIN_THREAD
);
813 * Clean up all the per-CPU call_rcu threads.
815 void free_all_cpu_call_rcu_data(void)
818 struct call_rcu_data
**crdp
;
819 static int warned
= 0;
821 if (cpus_array_len
<= 0)
824 crdp
= malloc(sizeof(*crdp
) * cpus_array_len
);
827 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
833 for (cpu
= 0; cpu
< cpus_array_len
; cpu
++) {
834 crdp
[cpu
] = get_cpu_call_rcu_data(cpu
);
835 if (crdp
[cpu
] == NULL
)
837 set_cpu_call_rcu_data(cpu
, NULL
);
840 * Wait for call_rcu sites acting as RCU readers of the
841 * call_rcu_data to become quiescent.
844 for (cpu
= 0; cpu
< cpus_array_len
; cpu
++) {
845 if (crdp
[cpu
] == NULL
)
847 call_rcu_data_free(crdp
[cpu
]);
853 void free_completion(struct urcu_ref
*ref
)
855 struct call_rcu_completion
*completion
;
857 completion
= caa_container_of(ref
, struct call_rcu_completion
, ref
);
862 void _rcu_barrier_complete(struct rcu_head
*head
)
864 struct call_rcu_completion_work
*work
;
865 struct call_rcu_completion
*completion
;
867 work
= caa_container_of(head
, struct call_rcu_completion_work
, head
);
868 completion
= work
->completion
;
869 if (!uatomic_sub_return(&completion
->barrier_count
, 1))
870 call_rcu_completion_wake_up(completion
);
871 urcu_ref_put(&completion
->ref
, free_completion
);
876 * Wait for all in-flight call_rcu callbacks to complete execution.
878 void rcu_barrier(void)
880 struct call_rcu_data
*crdp
;
881 struct call_rcu_completion
*completion
;
885 /* Put in offline state in QSBR. */
886 was_online
= _rcu_read_ongoing();
888 rcu_thread_offline();
890 * Calling a rcu_barrier() within a RCU read-side critical
891 * section is an error.
893 if (_rcu_read_ongoing()) {
894 static int warned
= 0;
897 fprintf(stderr
, "[error] liburcu: rcu_barrier() called from within RCU read-side critical section.\n");
903 completion
= calloc(sizeof(*completion
), 1);
907 call_rcu_lock(&call_rcu_mutex
);
908 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
)
911 /* Referenced by rcu_barrier() and each call_rcu thread. */
912 urcu_ref_set(&completion
->ref
, count
+ 1);
913 completion
->barrier_count
= count
;
915 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
916 struct call_rcu_completion_work
*work
;
918 work
= calloc(sizeof(*work
), 1);
921 work
->completion
= completion
;
922 _call_rcu(&work
->head
, _rcu_barrier_complete
, crdp
);
924 call_rcu_unlock(&call_rcu_mutex
);
928 uatomic_dec(&completion
->futex
);
929 /* Decrement futex before reading barrier_count */
931 if (!uatomic_read(&completion
->barrier_count
))
933 call_rcu_completion_wait(completion
);
936 urcu_ref_put(&completion
->ref
, free_completion
);
944 * Acquire the call_rcu_mutex in order to ensure that the child sees
945 * all of the call_rcu() data structures in a consistent state. Ensure
946 * that all call_rcu threads are in a quiescent state across fork.
947 * Suitable for pthread_atfork() and friends.
949 void call_rcu_before_fork(void)
951 struct call_rcu_data
*crdp
;
952 struct urcu_atfork
*atfork
;
954 call_rcu_lock(&call_rcu_mutex
);
956 atfork
= registered_rculfhash_atfork
;
958 atfork
->before_fork(atfork
->priv
);
960 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
961 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_PAUSE
);
962 cmm_smp_mb__after_uatomic_or();
963 wake_call_rcu_thread(crdp
);
965 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
966 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSED
) == 0)
967 (void) poll(NULL
, 0, 1);
972 * Clean up call_rcu data structures in the parent of a successful fork()
973 * that is not followed by exec() in the child. Suitable for
974 * pthread_atfork() and friends.
976 void call_rcu_after_fork_parent(void)
978 struct call_rcu_data
*crdp
;
979 struct urcu_atfork
*atfork
;
981 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
)
982 uatomic_and(&crdp
->flags
, ~URCU_CALL_RCU_PAUSE
);
983 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
984 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSED
) != 0)
985 (void) poll(NULL
, 0, 1);
987 atfork
= registered_rculfhash_atfork
;
989 atfork
->after_fork_parent(atfork
->priv
);
990 call_rcu_unlock(&call_rcu_mutex
);
994 * Clean up call_rcu data structures in the child of a successful fork()
995 * that is not followed by exec(). Suitable for pthread_atfork() and
998 void call_rcu_after_fork_child(void)
1000 struct call_rcu_data
*crdp
, *next
;
1001 struct urcu_atfork
*atfork
;
1003 /* Release the mutex. */
1004 call_rcu_unlock(&call_rcu_mutex
);
1006 atfork
= registered_rculfhash_atfork
;
1008 atfork
->after_fork_child(atfork
->priv
);
1010 /* Do nothing when call_rcu() has not been used */
1011 if (cds_list_empty(&call_rcu_data_list
))
1015 * Allocate a new default call_rcu_data structure in order
1016 * to get a working call_rcu thread to go with it.
1018 default_call_rcu_data
= NULL
;
1019 (void)get_default_call_rcu_data();
1021 /* Cleanup call_rcu_data pointers before use */
1022 cpus_array_len_reset();
1023 free(per_cpu_call_rcu_data
);
1024 rcu_set_pointer(&per_cpu_call_rcu_data
, NULL
);
1025 URCU_TLS(thread_call_rcu_data
) = NULL
;
1028 * Dispose of all of the rest of the call_rcu_data structures.
1029 * Leftover call_rcu callbacks will be merged into the new
1030 * default call_rcu thread queue.
1032 cds_list_for_each_entry_safe(crdp
, next
, &call_rcu_data_list
, list
) {
1033 if (crdp
== default_call_rcu_data
)
1035 uatomic_set(&crdp
->flags
, URCU_CALL_RCU_STOPPED
);
1037 * Do not join the thread because it does not exist in
1040 _call_rcu_data_free(crdp
, 0);
1044 void urcu_register_rculfhash_atfork(struct urcu_atfork
*atfork
)
1046 if (CMM_LOAD_SHARED(registered_rculfhash_atfork
))
1048 call_rcu_lock(&call_rcu_mutex
);
1049 if (!registered_rculfhash_atfork
)
1050 registered_rculfhash_atfork
= atfork
;
1051 call_rcu_unlock(&call_rcu_mutex
);
1055 * This unregistration function is deprecated, meant only for internal
1058 __attribute__((__noreturn__
))
1059 void urcu_unregister_rculfhash_atfork(struct urcu_atfork
*atfork
__attribute__((unused
)))
1065 * Teardown the default call_rcu worker thread if there are no queued
1066 * callbacks on process exit. This prevents leaking memory.
1068 * Here is how an application can ensure graceful teardown of this
1071 * - An application queuing call_rcu callbacks should invoke
1072 * rcu_barrier() before it exits.
1073 * - When chaining call_rcu callbacks, the number of calls to
1074 * rcu_barrier() on application exit must match at least the maximum
1075 * number of chained callbacks.
1076 * - If an application chains callbacks endlessly, it would have to be
1077 * modified to stop chaining callbacks when it detects an application
1078 * exit (e.g. with a flag), and wait for quiescence with rcu_barrier()
1079 * after setting that flag.
1080 * - The statements above apply to a library which queues call_rcu
1081 * callbacks, only it needs to invoke rcu_barrier in its library
1084 * Note that this function does not presume it is being called when the
1085 * application is single-threaded even though this is invoked from a
1086 * destructor: this function synchronizes against concurrent calls to
1087 * get_default_call_rcu_data().
1089 static void urcu_call_rcu_exit(void)
1091 struct call_rcu_data
*crdp
;
1092 bool teardown
= true;
1094 if (default_call_rcu_data
== NULL
)
1096 call_rcu_lock(&call_rcu_mutex
);
1098 * If the application leaves callbacks in the default call_rcu
1099 * worker queue, keep the default worker in place.
1101 crdp
= default_call_rcu_data
;
1106 if (!cds_wfcq_empty(&crdp
->cbs_head
, &crdp
->cbs_tail
)) {
1110 rcu_set_pointer(&default_call_rcu_data
, NULL
);
1112 call_rcu_unlock(&call_rcu_mutex
);
1115 call_rcu_data_free(crdp
);