Cleanup: enable signed/unsigned compare compiler warning
[urcu.git] / doc / examples / urcu-flavors / bp.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 #include <urcu/urcu-bp.h> /* Bulletproof RCU flavor */
26 #include <urcu/rculist.h> /* List example */
27 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
28
29 /*
30 * Example showing how to use the Bulletproof Userspace RCU flavor.
31 *
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 int main(int argc, char **argv)
58 {
59 uint64_t values[] = { 42, 36, 24, };
60 unsigned int i;
61 int ret;
62 struct mynode *node, *n;
63
64 /*
65 * Notice that with the bulletproof flavor, there is no need to
66 * register/unregister RCU reader threads.
67 */
68
69 /*
70 * Adding nodes to the linked-list. Safe against concurrent
71 * RCU traversals, require mutual exclusion with list updates.
72 */
73 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
74 ret = add_node(values[i]);
75 if (ret)
76 goto end;
77 }
78
79 /*
80 * We need to explicitly mark RCU read-side critical sections
81 * with rcu_read_lock() and rcu_read_unlock(). They can be
82 * nested. Those are no-ops for the QSBR flavor.
83 */
84 urcu_bp_read_lock();
85
86 /*
87 * RCU traversal of the linked list.
88 */
89 cds_list_for_each_entry_rcu(node, &mylist, node) {
90 printf("Value: %" PRIu64 "\n", node->value);
91 }
92 urcu_bp_read_unlock();
93
94 /*
95 * Removing nodes from linked list. Safe against concurrent RCU
96 * traversals, require mutual exclusion with list updates.
97 */
98 cds_list_for_each_entry_safe(node, n, &mylist, node) {
99 cds_list_del_rcu(&node->node);
100
101 /*
102 * Using synchronize_rcu() directly for synchronization
103 * so we keep a minimal impact on the system by not
104 * spawning any call_rcu() thread. It is slower though,
105 * since there is no batching.
106 */
107 urcu_bp_synchronize_rcu();
108 free(node);
109 }
110
111 sleep(1);
112
113 end:
114 return ret;
115 }
This page took 0.031575 seconds and 4 git commands to generate.