044aa5efeb7b9f3ace93e2677ad4bf273d78efb6
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. cds_list_for_each_rcu() iterates on
8 * struct cds_list_head, and thus, either caa_container_of() or
9 * cds_list_entry() are needed to access the container structure.
14 #include <urcu/urcu-memb.h> /* Userspace RCU flavor */
15 #include <urcu/rculist.h> /* RCU list */
16 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
19 * Nodes populated into the list.
22 int value
; /* Node content */
23 struct cds_list_head node
; /* Linked-list chaining */
28 int values
[] = { -5, 42, 36, 24, };
29 CDS_LIST_HEAD(mylist
); /* Defines an empty list head */
32 struct cds_list_head
*pos
;
35 * Each thread need using RCU read-side need to be explicitly
38 urcu_memb_register_thread();
41 * Adding nodes to the linked-list. Safe against concurrent
42 * RCU traversals, require mutual exclusion with list updates.
44 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
47 node
= malloc(sizeof(*node
));
52 node
->value
= values
[i
];
53 cds_list_add_tail_rcu(&node
->node
, &mylist
);
57 * RCU-safe iteration on the list.
59 printf("mylist content:");
62 * Surround the RCU read-side critical section with urcu_memb_read_lock()
63 * and urcu_memb_read_unlock().
65 urcu_memb_read_lock();
68 * This traversal can be performed concurrently with RCU
71 cds_list_for_each_rcu(pos
, &mylist
) {
72 struct mynode
*node
= cds_list_entry(pos
, struct mynode
, node
);
74 printf(" %d", node
->value
);
77 urcu_memb_read_unlock();
81 urcu_memb_unregister_thread();
This page took 0.033847 seconds and 5 git commands to generate.