1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to do a RCU linked list traversal, safely
7 * against concurrent RCU updates.
12 #include <urcu/urcu-memb.h> /* Userspace RCU flavor */
13 #include <urcu/rculist.h> /* RCU list */
14 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
17 * Nodes populated into the list.
20 int value
; /* Node content */
21 struct cds_list_head node
; /* Linked-list chaining */
26 int values
[] = { -5, 42, 36, 24, };
27 CDS_LIST_HEAD(mylist
); /* Defines an empty list head */
33 * Each thread need using RCU read-side need to be explicitly
36 urcu_memb_register_thread();
39 * Adding nodes to the linked-list. Safe against concurrent
40 * RCU traversals, require mutual exclusion with list updates.
42 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
43 node
= malloc(sizeof(*node
));
48 node
->value
= values
[i
];
49 cds_list_add_tail_rcu(&node
->node
, &mylist
);
53 * RCU-safe iteration on the list.
55 printf("mylist content:");
58 * Surround the RCU read-side critical section with urcu_memb_read_lock()
59 * and urcu_memb_read_unlock().
61 urcu_memb_read_lock();
64 * This traversal can be performed concurrently with RCU
67 cds_list_for_each_entry_rcu(node
, &mylist
, node
) {
68 printf(" %d", node
->value
);
71 urcu_memb_read_unlock();
75 urcu_memb_unregister_thread();
This page took 0.032666 seconds and 5 git commands to generate.