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 RCU linked list traversal, safely
14 * against concurrent RCU updates. cds_list_for_each_rcu() iterates on
15 * struct cds_list_head, and thus, either caa_container_of() or
16 * cds_list_entry() are needed to access the container structure.
21 #include <urcu.h> /* Userspace RCU flavor */
22 #include <urcu/rculist.h> /* RCU list */
23 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
26 * Nodes populated into the list.
29 int value
; /* Node content */
30 struct cds_list_head node
; /* Linked-list chaining */
33 int main(int argc
, char **argv
)
35 int values
[] = { -5, 42, 36, 24, };
36 CDS_LIST_HEAD(mylist
); /* Defines an empty list head */
39 struct cds_list_head
*pos
;
42 * Each thread need using RCU read-side need to be explicitly
45 rcu_register_thread();
48 * Adding nodes to the linked-list. Safe against concurrent
49 * RCU traversals, require mutual exclusion with list updates.
51 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
54 node
= malloc(sizeof(*node
));
59 node
->value
= values
[i
];
60 cds_list_add_tail_rcu(&node
->node
, &mylist
);
64 * RCU-safe iteration on the list.
66 printf("mylist content:");
69 * Surround the RCU read-side critical section with rcu_read_lock()
70 * and rcu_read_unlock().
75 * This traversal can be performed concurrently with RCU
78 cds_list_for_each_rcu(pos
, &mylist
) {
79 struct mynode
*node
= cds_list_entry(pos
, struct mynode
, node
);
81 printf(" %d", node
->value
);
88 rcu_unregister_thread();
This page took 0.030989 seconds and 4 git commands to generate.