2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to use or copy this program for any
8 * purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is
10 * granted, provided the above notices are retained, and a notice that
11 * the code was modified is included with the above copyright notice.
13 * This example shows how to remove from a non-circular linked-list
14 * safely against concurrent RCU traversals.
19 #include <urcu/urcu-memb.h> /* Userspace RCU flavor */
20 #include <urcu/rcuhlist.h> /* RCU hlist */
21 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
24 * Nodes populated into the list.
27 int value
; /* Node content */
28 struct cds_hlist_node node
; /* Linked-list chaining */
29 struct rcu_head rcu_head
; /* For call_rcu() */
33 void free_node_rcu(struct rcu_head
*head
)
35 struct mynode
*node
= caa_container_of(head
, struct mynode
, rcu_head
);
42 int values
[] = { -5, 42, 36, 24, };
43 CDS_HLIST_HEAD(mylist
); /* Defines an empty hlist head */
46 struct mynode
*node
, *n
;
49 * Adding nodes to the linked-list. Safe against concurrent
50 * RCU traversals, require mutual exclusion with list updates.
52 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
53 node
= malloc(sizeof(*node
));
58 node
->value
= values
[i
];
59 cds_hlist_add_head_rcu(&node
->node
, &mylist
);
63 * Removing all positive values. Safe against concurrent RCU
64 * traversals, require mutual exclusion with list updates.
65 * Notice the "safe" iteration: it is safe against removal of
66 * nodes as we iterate on the list.
68 cds_hlist_for_each_entry_safe_2(node
, n
, &mylist
, node
) {
69 if (node
->value
> 0) {
70 cds_hlist_del_rcu(&node
->node
);
72 * We can only reclaim memory after a grace
73 * period has passed after cds_hlist_del_rcu().
75 urcu_memb_call_rcu(&node
->rcu_head
, free_node_rcu
);
80 * Just show the list content. This is _not_ an RCU-safe
81 * iteration on the list.
83 printf("mylist content:");
84 cds_hlist_for_each_entry_2(node
, &mylist
, node
) {
85 printf(" %d", node
->value
);
This page took 0.041556 seconds and 5 git commands to generate.