464205400c7938edcfda2171cb962d66afaabbe7
1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to do a non-circular RCU linked list
7 * traversal, safely against concurrent RCU updates.
8 * cds_hlist_for_each_rcu() iterates on struct cds_hlist_node, and thus,
9 * either caa_container_of() or cds_hlist_entry() are needed to access
10 * the container structure.
15 #include <urcu/urcu-memb.h> /* Userspace RCU flavor */
16 #include <urcu/rcuhlist.h> /* RCU hlist */
17 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
20 * Nodes populated into the list.
23 int value
; /* Node content */
24 struct cds_hlist_node node
; /* Linked-list chaining */
29 int values
[] = { -5, 42, 36, 24, };
30 CDS_HLIST_HEAD(mylist
); /* Defines an empty hlist head */
33 struct cds_hlist_node
*pos
;
36 * Each thread need using RCU read-side need to be explicitly
39 urcu_memb_register_thread();
42 * Adding nodes to the linked-list. Safe against concurrent
43 * RCU traversals, require mutual exclusion with list updates.
45 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
48 node
= malloc(sizeof(*node
));
53 node
->value
= values
[i
];
54 cds_hlist_add_head_rcu(&node
->node
, &mylist
);
58 * RCU-safe iteration on the list.
60 printf("mylist content:");
63 * Surround the RCU read-side critical section with rcu_read_lock()
64 * and rcu_read_unlock().
66 urcu_memb_read_lock();
69 * This traversal can be performed concurrently with RCU
72 cds_hlist_for_each_rcu(pos
, &mylist
) {
73 struct mynode
*node
= cds_hlist_entry(pos
, struct mynode
, node
);
75 printf(" %d", node
->value
);
78 urcu_memb_read_unlock();
82 urcu_memb_unregister_thread();
This page took 0.032579 seconds and 5 git commands to generate.