d17084b661993f86d66af6b50b262ddba40416b9
1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to replace a node within a linked-list safely
7 * against concurrent RCU traversals.
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 */
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_LIST_HEAD(mylist
); /* Defines an empty list 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_list_add_tail_rcu(&node
->node
, &mylist
);
56 * Replacing all values by their negated value. Safe against
57 * concurrent RCU traversals, require mutual exclusion with list
58 * updates. Notice the "safe" iteration: it is safe against
59 * removal (and thus replacement) of nodes as we iterate on the
62 cds_list_for_each_entry_safe(node
, n
, &mylist
, node
) {
63 struct mynode
*new_node
;
65 new_node
= malloc(sizeof(*node
));
70 /* Replacement node value is negated original value. */
71 new_node
->value
= -node
->value
;
72 cds_list_replace_rcu(&node
->node
, &new_node
->node
);
73 urcu_memb_call_rcu(&node
->rcu_head
, free_node_rcu
);
77 * Just show the list content. This is _not_ an RCU-safe
78 * iteration on the list.
80 printf("mylist content:");
81 cds_list_for_each_entry(node
, &mylist
, node
) {
82 printf(" %d", node
->value
);
This page took 0.041956 seconds and 5 git commands to generate.