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 | ||
88c3c5ba | 19 | #include <unistd.h> |
8bad63a0 MD |
20 | #include <stdlib.h> |
21 | #include <stdio.h> | |
22 | #include <stdint.h> | |
23 | #include <inttypes.h> | |
24 | ||
b9050d91 | 25 | #include <urcu/urcu-qsbr.h> /* QSBR RCU flavor */ |
71ea53b1 MD |
26 | #include <urcu/rculist.h> /* List example */ |
27 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ | |
28 | ||
8bad63a0 | 29 | /* |
6df9f02d MD |
30 | * Example showing how to use the QSBR Userspace RCU flavor. |
31 | * | |
8bad63a0 MD |
32 | * This is a mock-up example where updates and RCU traversals are |
33 | * performed by the same thread to keep things simple on purpose. | |
34 | */ | |
35 | ||
36 | static CDS_LIST_HEAD(mylist); | |
37 | ||
38 | struct mynode { | |
39 | uint64_t value; | |
40 | struct cds_list_head node; /* linked-list chaining */ | |
41 | struct rcu_head rcu_head; /* for call_rcu() */ | |
42 | }; | |
43 | ||
44 | static | |
45 | int add_node(uint64_t v) | |
46 | { | |
47 | struct mynode *node; | |
48 | ||
49 | node = calloc(sizeof(*node), 1); | |
50 | if (!node) | |
51 | return -1; | |
52 | node->value = v; | |
53 | cds_list_add_rcu(&node->node, &mylist); | |
54 | return 0; | |
55 | } | |
56 | ||
57 | static | |
58 | void rcu_free_node(struct rcu_head *rh) | |
59 | { | |
60 | struct mynode *node = caa_container_of(rh, struct mynode, rcu_head); | |
61 | ||
62 | free(node); | |
63 | } | |
64 | ||
65 | int main(int argc, char **argv) | |
66 | { | |
67 | uint64_t values[] = { 42, 36, 24, }; | |
68 | unsigned int i; | |
69 | int ret; | |
70 | struct mynode *node, *n; | |
71 | ||
72 | /* | |
8fd9af4a | 73 | * Each thread need using RCU read-side need to be explicitly |
8bad63a0 MD |
74 | * registered. |
75 | */ | |
b9050d91 | 76 | urcu_qsbr_register_thread(); |
8bad63a0 MD |
77 | |
78 | /* | |
79 | * Adding nodes to the linked-list. Safe against concurrent | |
80 | * RCU traversals, require mutual exclusion with list updates. | |
81 | */ | |
82 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { | |
83 | ret = add_node(values[i]); | |
84 | if (ret) | |
85 | goto end; | |
86 | } | |
87 | ||
8bad63a0 MD |
88 | /* |
89 | * RCU traversal of the linked list. | |
90 | */ | |
91 | cds_list_for_each_entry_rcu(node, &mylist, node) { | |
92 | printf("Value: %" PRIu64 "\n", node->value); | |
93 | } | |
8bad63a0 MD |
94 | |
95 | /* | |
96 | * Removing nodes from linked list. Safe against concurrent RCU | |
97 | * traversals, require mutual exclusion with list updates. | |
98 | */ | |
99 | cds_list_for_each_entry_safe(node, n, &mylist, node) { | |
100 | cds_list_del_rcu(&node->node); | |
238eb13d MD |
101 | /* |
102 | * call_rcu() will ensure that the handler | |
103 | * "rcu_free_node" is executed after a grace period. | |
104 | * call_rcu() can be called from RCU read-side critical | |
105 | * sections. | |
106 | */ | |
b9050d91 | 107 | urcu_qsbr_call_rcu(&node->rcu_head, rcu_free_node); |
8bad63a0 MD |
108 | } |
109 | ||
110 | /* | |
111 | * For QSBR flavor, we need to explicitly announce quiescent | |
71ea53b1 MD |
112 | * states. Here is how it is done. This should be performed by |
113 | * every online registered RCU threads in the program | |
114 | * periodically. | |
8bad63a0 | 115 | */ |
b9050d91 | 116 | urcu_qsbr_quiescent_state(); |
8bad63a0 MD |
117 | |
118 | /* | |
119 | * For QSBR flavor, when a thread needs to be in a quiescent | |
120 | * state for a long period of time, we use rcu_thread_offline() | |
121 | * and rcu_thread_online(). | |
122 | */ | |
b9050d91 | 123 | urcu_qsbr_thread_offline(); |
8bad63a0 MD |
124 | |
125 | sleep(1); | |
126 | ||
b9050d91 | 127 | urcu_qsbr_thread_online(); |
8bad63a0 | 128 | |
d7818a6f MD |
129 | /* |
130 | * We can also wait for a quiescent state by calling | |
131 | * synchronize_rcu() rather than using call_rcu(). It is usually | |
132 | * a slower approach than call_rcu(), because the latter can | |
133 | * batch work. Moreover, call_rcu() can be called from a RCU | |
134 | * read-side critical section, but synchronize_rcu() ensures the | |
135 | * caller thread is offline, thus acting as a quiescent state. | |
136 | */ | |
b9050d91 | 137 | urcu_qsbr_synchronize_rcu(); |
d7818a6f | 138 | |
8bad63a0 MD |
139 | /* |
140 | * Waiting for previously called call_rcu handlers to complete | |
71ea53b1 MD |
141 | * before program exits, or in library destructors, is a good |
142 | * practice. | |
8bad63a0 | 143 | */ |
b9050d91 | 144 | urcu_qsbr_barrier(); |
8bad63a0 MD |
145 | |
146 | end: | |
b9050d91 | 147 | urcu_qsbr_unregister_thread(); |
8bad63a0 MD |
148 | return ret; |
149 | } |