Commit | Line | Data |
---|---|---|
17fb3188 MD |
1 | /* |
2 | * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
3 | * | |
4 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED | |
5 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. | |
6 | * | |
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. | |
12 | * | |
8fd9af4a | 13 | * This example shows how to do a RCU linked list traversal, safely |
17fb3188 MD |
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. | |
17 | */ | |
18 | ||
19 | #include <stdio.h> | |
20 | ||
b9050d91 | 21 | #include <urcu/urcu-memb.h> /* Userspace RCU flavor */ |
17fb3188 MD |
22 | #include <urcu/rculist.h> /* RCU list */ |
23 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ | |
24 | ||
25 | /* | |
26 | * Nodes populated into the list. | |
27 | */ | |
28 | struct mynode { | |
29 | int value; /* Node content */ | |
30 | struct cds_list_head node; /* Linked-list chaining */ | |
31 | }; | |
32 | ||
33 | int main(int argc, char **argv) | |
34 | { | |
35 | int values[] = { -5, 42, 36, 24, }; | |
36 | CDS_LIST_HEAD(mylist); /* Defines an empty list head */ | |
37 | unsigned int i; | |
38 | int ret = 0; | |
39 | struct cds_list_head *pos; | |
40 | ||
41 | /* | |
8fd9af4a | 42 | * Each thread need using RCU read-side need to be explicitly |
17fb3188 MD |
43 | * registered. |
44 | */ | |
b9050d91 | 45 | urcu_memb_register_thread(); |
17fb3188 MD |
46 | |
47 | /* | |
48 | * Adding nodes to the linked-list. Safe against concurrent | |
49 | * RCU traversals, require mutual exclusion with list updates. | |
50 | */ | |
51 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { | |
52 | struct mynode *node; | |
53 | ||
54 | node = malloc(sizeof(*node)); | |
55 | if (!node) { | |
56 | ret = -1; | |
57 | goto end; | |
58 | } | |
59 | node->value = values[i]; | |
60 | cds_list_add_tail_rcu(&node->node, &mylist); | |
61 | } | |
62 | ||
63 | /* | |
64 | * RCU-safe iteration on the list. | |
65 | */ | |
66 | printf("mylist content:"); | |
67 | ||
68 | /* | |
b9050d91 MD |
69 | * Surround the RCU read-side critical section with urcu_memb_read_lock() |
70 | * and urcu_memb_read_unlock(). | |
17fb3188 | 71 | */ |
b9050d91 | 72 | urcu_memb_read_lock(); |
17fb3188 MD |
73 | |
74 | /* | |
75 | * This traversal can be performed concurrently with RCU | |
76 | * updates. | |
77 | */ | |
78 | cds_list_for_each_rcu(pos, &mylist) { | |
79 | struct mynode *node = cds_list_entry(pos, struct mynode, node); | |
80 | ||
81 | printf(" %d", node->value); | |
82 | } | |
83 | ||
b9050d91 | 84 | urcu_memb_read_unlock(); |
17fb3188 MD |
85 | |
86 | printf("\n"); | |
87 | end: | |
b9050d91 | 88 | urcu_memb_unregister_thread(); |
17fb3188 MD |
89 | return ret; |
90 | } |