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