1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: LGPL-2.1-or-later
11 #include <urcu/urcu-bp.h> /* Bulletproof RCU flavor */
12 #include <urcu/rculist.h> /* List example */
13 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
16 * Example showing how to use the Bulletproof Userspace RCU flavor.
18 * This is a mock-up example where updates and RCU traversals are
19 * performed by the same thread to keep things simple on purpose.
22 static CDS_LIST_HEAD(mylist
);
26 struct cds_list_head node
; /* linked-list chaining */
27 struct rcu_head rcu_head
; /* for call_rcu() */
31 int add_node(uint64_t v
)
35 node
= calloc(sizeof(*node
), 1);
39 cds_list_add_rcu(&node
->node
, &mylist
);
45 uint64_t values
[] = { 42, 36, 24, };
48 struct mynode
*node
, *n
;
51 * Notice that with the bulletproof flavor, there is no need to
52 * register/unregister RCU reader threads.
56 * Adding nodes to the linked-list. Safe against concurrent
57 * RCU traversals, require mutual exclusion with list updates.
59 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
60 ret
= add_node(values
[i
]);
66 * We need to explicitly mark RCU read-side critical sections
67 * with rcu_read_lock() and rcu_read_unlock(). They can be
68 * nested. Those are no-ops for the QSBR flavor.
73 * RCU traversal of the linked list.
75 cds_list_for_each_entry_rcu(node
, &mylist
, node
) {
76 printf("Value: %" PRIu64
"\n", node
->value
);
78 urcu_bp_read_unlock();
81 * Removing nodes from linked list. Safe against concurrent RCU
82 * traversals, require mutual exclusion with list updates.
84 cds_list_for_each_entry_safe(node
, n
, &mylist
, node
) {
85 cds_list_del_rcu(&node
->node
);
88 * Using synchronize_rcu() directly for synchronization
89 * so we keep a minimal impact on the system by not
90 * spawning any call_rcu() thread. It is slower though,
91 * since there is no batching.
93 urcu_bp_synchronize_rcu();
This page took 0.03427 seconds and 5 git commands to generate.