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