1ef229f61a9d6e2c2a621b80ab6111f3bd1732c5
1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to remove from a non-circular linked-list
7 * safely against concurrent RCU traversals.
12 #include <urcu/urcu-memb.h> /* Userspace RCU flavor */
13 #include <urcu/rcuhlist.h> /* RCU hlist */
14 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
17 * Nodes populated into the list.
20 int value
; /* Node content */
21 struct cds_hlist_node node
; /* Linked-list chaining */
22 struct rcu_head rcu_head
; /* For call_rcu() */
26 void free_node_rcu(struct rcu_head
*head
)
28 struct mynode
*node
= caa_container_of(head
, struct mynode
, rcu_head
);
35 int values
[] = { -5, 42, 36, 24, };
36 CDS_HLIST_HEAD(mylist
); /* Defines an empty hlist head */
39 struct mynode
*node
, *n
;
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
++) {
46 node
= malloc(sizeof(*node
));
51 node
->value
= values
[i
];
52 cds_hlist_add_head_rcu(&node
->node
, &mylist
);
56 * Removing all positive values. Safe against concurrent RCU
57 * traversals, require mutual exclusion with list updates.
58 * Notice the "safe" iteration: it is safe against removal of
59 * nodes as we iterate on the list.
61 cds_hlist_for_each_entry_safe_2(node
, n
, &mylist
, node
) {
62 if (node
->value
> 0) {
63 cds_hlist_del_rcu(&node
->node
);
65 * We can only reclaim memory after a grace
66 * period has passed after cds_hlist_del_rcu().
68 urcu_memb_call_rcu(&node
->rcu_head
, free_node_rcu
);
73 * Just show the list content. This is _not_ an RCU-safe
74 * iteration on the list.
76 printf("mylist content:");
77 cds_hlist_for_each_entry_2(node
, &mylist
, node
) {
78 printf(" %d", node
->value
);
This page took 0.043432 seconds and 5 git commands to generate.