doc/examples: rename qsbr-minimal to rcu-flavor-qsbr
[urcu.git] / doc / examples / rcu-flavor-qsbr / rcu-flavor-qsbr.c
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
19 #define _LGPL_SOURCE
20 #include <urcu-qsbr.h>
21 #include <urcu/rculist.h>
22 #include <urcu/compiler.h>
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <inttypes.h>
28
29 /*
30 * This is a mock-up example where updates and RCU traversals are
31 * performed by the same thread to keep things simple on purpose.
32 */
33
34 static CDS_LIST_HEAD(mylist);
35
36 struct mynode {
37 uint64_t value;
38 struct cds_list_head node; /* linked-list chaining */
39 struct rcu_head rcu_head; /* for call_rcu() */
40 };
41
42 static
43 int add_node(uint64_t v)
44 {
45 struct mynode *node;
46
47 node = calloc(sizeof(*node), 1);
48 if (!node)
49 return -1;
50 node->value = v;
51 cds_list_add_rcu(&node->node, &mylist);
52 return 0;
53 }
54
55 static
56 void rcu_free_node(struct rcu_head *rh)
57 {
58 struct mynode *node = caa_container_of(rh, struct mynode, rcu_head);
59
60 free(node);
61 }
62
63 int main(int argc, char **argv)
64 {
65 uint64_t values[] = { 42, 36, 24, };
66 unsigned int i;
67 int ret;
68 struct mynode *node, *n;
69
70 /*
71 * Each thread need using RCU read-side need to be explicitly
72 * registered.
73 */
74 rcu_register_thread();
75
76 /*
77 * Adding nodes to the linked-list. Safe against concurrent
78 * RCU traversals, require mutual exclusion with list updates.
79 */
80 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
81 ret = add_node(values[i]);
82 if (ret)
83 goto end;
84 }
85
86 /*
87 * For all RCU flavors except QSBR, we need to explicitly mark
88 * RCU read-side critical sections with rcu_read_lock() and
89 * rcu_read_unlock(). They can be nested. Those are no-ops for
90 * the QSBR flavor.
91 */
92 rcu_read_lock();
93
94 /*
95 * RCU traversal of the linked list.
96 */
97 cds_list_for_each_entry_rcu(node, &mylist, node) {
98 printf("Value: %" PRIu64 "\n", node->value);
99 }
100 rcu_read_unlock();
101
102 /*
103 * Removing nodes from linked list. Safe against concurrent RCU
104 * traversals, require mutual exclusion with list updates.
105 */
106 cds_list_for_each_entry_safe(node, n, &mylist, node) {
107 cds_list_del_rcu(&node->node);
108 call_rcu(&node->rcu_head, rcu_free_node);
109 }
110
111 /*
112 * For QSBR flavor, we need to explicitly announce quiescent
113 * states.
114 */
115 rcu_quiescent_state();
116
117 /*
118 * For QSBR flavor, when a thread needs to be in a quiescent
119 * state for a long period of time, we use rcu_thread_offline()
120 * and rcu_thread_online().
121 */
122 rcu_thread_offline();
123
124 sleep(1);
125
126 rcu_thread_online();
127
128 /*
129 * Waiting for previously called call_rcu handlers to complete
130 * before program exits is a good practice.
131 */
132 rcu_barrier();
133
134 end:
135 rcu_unregister_thread();
136 return ret;
137 }
This page took 0.03096 seconds and 4 git commands to generate.