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.
15 * cds_hlist_for_each_rcu() iterates on struct cds_hlist_node, and thus,
16 * either caa_container_of() or cds_hlist_entry() are needed to access
17 * the container structure.
22 #include <urcu/urcu-memb.h> /* Userspace RCU flavor */
23 #include <urcu/rcuhlist.h> /* RCU hlist */
24 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
27 * Nodes populated into the list.
30 int value
; /* Node content */
31 struct cds_hlist_node node
; /* Linked-list chaining */
36 int values
[] = { -5, 42, 36, 24, };
37 CDS_HLIST_HEAD(mylist
); /* Defines an empty hlist head */
40 struct cds_hlist_node
*pos
;
43 * Each thread need using RCU read-side need to be explicitly
46 urcu_memb_register_thread();
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
++) {
55 node
= malloc(sizeof(*node
));
60 node
->value
= values
[i
];
61 cds_hlist_add_head_rcu(&node
->node
, &mylist
);
65 * RCU-safe iteration on the list.
67 printf("mylist content:");
70 * Surround the RCU read-side critical section with rcu_read_lock()
71 * and rcu_read_unlock().
73 urcu_memb_read_lock();
76 * This traversal can be performed concurrently with RCU
79 cds_hlist_for_each_rcu(pos
, &mylist
) {
80 struct mynode
*node
= cds_hlist_entry(pos
, struct mynode
, node
);
82 printf(" %d", node
->value
);
85 urcu_memb_read_unlock();
89 urcu_memb_unregister_thread();
This page took 0.02981 seconds and 4 git commands to generate.