Commit | Line | Data |
---|---|---|
8bad63a0 MD |
1 | /* |
2 | * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; either | |
7 | * version 2.1 of the License, or (at your option) any later version. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public | |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
8bad63a0 MD |
19 | #include <stdlib.h> |
20 | #include <stdio.h> | |
21 | #include <stdint.h> | |
22 | #include <inttypes.h> | |
23 | ||
71ea53b1 MD |
24 | #include <urcu-qsbr.h> /* QSBR RCU flavor */ |
25 | #include <urcu/rculist.h> /* List example */ | |
26 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ | |
27 | ||
8bad63a0 | 28 | /* |
6df9f02d MD |
29 | * Example showing how to use the QSBR Userspace RCU flavor. |
30 | * | |
8bad63a0 MD |
31 | * This is a mock-up example where updates and RCU traversals are |
32 | * performed by the same thread to keep things simple on purpose. | |
33 | */ | |
34 | ||
35 | static CDS_LIST_HEAD(mylist); | |
36 | ||
37 | struct mynode { | |
38 | uint64_t value; | |
39 | struct cds_list_head node; /* linked-list chaining */ | |
40 | struct rcu_head rcu_head; /* for call_rcu() */ | |
41 | }; | |
42 | ||
43 | static | |
44 | int add_node(uint64_t v) | |
45 | { | |
46 | struct mynode *node; | |
47 | ||
48 | node = calloc(sizeof(*node), 1); | |
49 | if (!node) | |
50 | return -1; | |
51 | node->value = v; | |
52 | cds_list_add_rcu(&node->node, &mylist); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static | |
57 | void rcu_free_node(struct rcu_head *rh) | |
58 | { | |
59 | struct mynode *node = caa_container_of(rh, struct mynode, rcu_head); | |
60 | ||
61 | free(node); | |
62 | } | |
63 | ||
64 | int main(int argc, char **argv) | |
65 | { | |
66 | uint64_t values[] = { 42, 36, 24, }; | |
67 | unsigned int i; | |
68 | int ret; | |
69 | struct mynode *node, *n; | |
70 | ||
71 | /* | |
8fd9af4a | 72 | * Each thread need using RCU read-side need to be explicitly |
8bad63a0 MD |
73 | * registered. |
74 | */ | |
75 | rcu_register_thread(); | |
76 | ||
77 | /* | |
78 | * Adding nodes to the linked-list. Safe against concurrent | |
79 | * RCU traversals, require mutual exclusion with list updates. | |
80 | */ | |
81 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { | |
82 | ret = add_node(values[i]); | |
83 | if (ret) | |
84 | goto end; | |
85 | } | |
86 | ||
87 | /* | |
88 | * For all RCU flavors except QSBR, we need to explicitly mark | |
89 | * RCU read-side critical sections with rcu_read_lock() and | |
90 | * rcu_read_unlock(). They can be nested. Those are no-ops for | |
91 | * the QSBR flavor. | |
92 | */ | |
93 | rcu_read_lock(); | |
94 | ||
95 | /* | |
96 | * RCU traversal of the linked list. | |
97 | */ | |
98 | cds_list_for_each_entry_rcu(node, &mylist, node) { | |
99 | printf("Value: %" PRIu64 "\n", node->value); | |
100 | } | |
101 | rcu_read_unlock(); | |
102 | ||
103 | /* | |
104 | * Removing nodes from linked list. Safe against concurrent RCU | |
105 | * traversals, require mutual exclusion with list updates. | |
106 | */ | |
107 | cds_list_for_each_entry_safe(node, n, &mylist, node) { | |
108 | cds_list_del_rcu(&node->node); | |
109 | call_rcu(&node->rcu_head, rcu_free_node); | |
110 | } | |
111 | ||
112 | /* | |
113 | * For QSBR flavor, we need to explicitly announce quiescent | |
71ea53b1 MD |
114 | * states. Here is how it is done. This should be performed by |
115 | * every online registered RCU threads in the program | |
116 | * periodically. | |
8bad63a0 MD |
117 | */ |
118 | rcu_quiescent_state(); | |
119 | ||
120 | /* | |
121 | * For QSBR flavor, when a thread needs to be in a quiescent | |
122 | * state for a long period of time, we use rcu_thread_offline() | |
123 | * and rcu_thread_online(). | |
124 | */ | |
125 | rcu_thread_offline(); | |
126 | ||
127 | sleep(1); | |
128 | ||
129 | rcu_thread_online(); | |
130 | ||
131 | /* | |
132 | * Waiting for previously called call_rcu handlers to complete | |
71ea53b1 MD |
133 | * before program exits, or in library destructors, is a good |
134 | * practice. | |
8bad63a0 MD |
135 | */ |
136 | rcu_barrier(); | |
137 | ||
138 | end: | |
139 | rcu_unregister_thread(); | |
140 | return ret; | |
141 | } |