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 do a non-circular RCU linked list
14 * traversal, safely against concurrent RCU updates.
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 */
31 int main(int argc
, char **argv
)
33 int values
[] = { -5, 42, 36, 24, };
34 CDS_HLIST_HEAD(mylist
); /* Defines an empty hlist head */
40 * Each thread need using RCU read-side need to be explicitly
43 urcu_memb_register_thread();
46 * Adding nodes to the linked-list. Safe against concurrent
47 * RCU traversals, require mutual exclusion with list updates.
49 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
50 node
= malloc(sizeof(*node
));
55 node
->value
= values
[i
];
56 cds_hlist_add_head_rcu(&node
->node
, &mylist
);
60 * RCU-safe iteration on the list.
62 printf("mylist content:");
65 * Surround the RCU read-side critical section with rcu_read_lock()
66 * and rcu_read_unlock().
68 urcu_memb_read_lock();
71 * This traversal can be performed concurrently with RCU
74 cds_hlist_for_each_entry_rcu_2(node
, &mylist
, node
) {
75 printf(" %d", node
->value
);
78 urcu_memb_read_unlock();
82 urcu_memb_unregister_thread();
This page took 0.030646 seconds and 4 git commands to generate.