2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <urcu-qsbr.h> /* QSBR RCU flavor */
25 #include <urcu/rculist.h> /* List example */
26 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
29 * Example showing how to use the QSBR Userspace RCU flavor.
31 * This is a mock-up example where updates and RCU traversals are
32 * performed by the same thread to keep things simple on purpose.
35 static CDS_LIST_HEAD(mylist
);
39 struct cds_list_head node
; /* linked-list chaining */
40 struct rcu_head rcu_head
; /* for call_rcu() */
44 int add_node(uint64_t v
)
48 node
= calloc(sizeof(*node
), 1);
52 cds_list_add_rcu(&node
->node
, &mylist
);
57 void rcu_free_node(struct rcu_head
*rh
)
59 struct mynode
*node
= caa_container_of(rh
, struct mynode
, rcu_head
);
64 int main(int argc
, char **argv
)
66 uint64_t values
[] = { 42, 36, 24, };
69 struct mynode
*node
, *n
;
72 * Each thread need using RCU read-side need to be explicitly
75 rcu_register_thread();
78 * Adding nodes to the linked-list. Safe against concurrent
79 * RCU traversals, require mutual exclusion with list updates.
81 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
82 ret
= add_node(values
[i
]);
88 * For all RCU flavors except QSBR, we need to explicitly mark
89 * RCU read-side critical sections with rcu_read_lock() and
90 * rcu_read_unlock(). They can be nested. Those are no-ops for
96 * RCU traversal of the linked list.
98 cds_list_for_each_entry_rcu(node
, &mylist
, node
) {
99 printf("Value: %" PRIu64
"\n", node
->value
);
104 * Removing nodes from linked list. Safe against concurrent RCU
105 * traversals, require mutual exclusion with list updates.
107 cds_list_for_each_entry_safe(node
, n
, &mylist
, node
) {
108 cds_list_del_rcu(&node
->node
);
109 call_rcu(&node
->rcu_head
, rcu_free_node
);
113 * For QSBR flavor, we need to explicitly announce quiescent
114 * states. Here is how it is done. This should be performed by
115 * every online registered RCU threads in the program
118 rcu_quiescent_state();
121 * For QSBR flavor, when a thread needs to be in a quiescent
122 * state for a long period of time, we use rcu_thread_offline()
123 * and rcu_thread_online().
125 rcu_thread_offline();
132 * Waiting for previously called call_rcu handlers to complete
133 * before program exits, or in library destructors, is a good
139 rcu_unregister_thread();
This page took 0.040202 seconds and 4 git commands to generate.