Commit | Line | Data |
---|---|---|
1c87adb3 MJ |
1 | // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // | |
3 | // SPDX-License-Identifier: LGPL-2.1-or-later | |
9b4bf32b MD |
4 | |
5 | #include <unistd.h> | |
6 | #include <stdlib.h> | |
7 | #include <stdio.h> | |
8 | #include <stdint.h> | |
9 | #include <inttypes.h> | |
10 | ||
b9050d91 | 11 | #include <urcu/urcu-bp.h> /* Bulletproof RCU flavor */ |
9b4bf32b MD |
12 | #include <urcu/rculist.h> /* List example */ |
13 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ | |
14 | ||
15 | /* | |
16 | * Example showing how to use the Bulletproof Userspace RCU flavor. | |
17 | * | |
18 | * This is a mock-up example where updates and RCU traversals are | |
19 | * performed by the same thread to keep things simple on purpose. | |
20 | */ | |
21 | ||
22 | static CDS_LIST_HEAD(mylist); | |
23 | ||
24 | struct mynode { | |
25 | uint64_t value; | |
26 | struct cds_list_head node; /* linked-list chaining */ | |
27 | struct rcu_head rcu_head; /* for call_rcu() */ | |
28 | }; | |
29 | ||
30 | static | |
31 | int add_node(uint64_t v) | |
32 | { | |
33 | struct mynode *node; | |
34 | ||
81270292 | 35 | node = calloc(1, sizeof(*node)); |
9b4bf32b MD |
36 | if (!node) |
37 | return -1; | |
38 | node->value = v; | |
39 | cds_list_add_rcu(&node->node, &mylist); | |
40 | return 0; | |
41 | } | |
42 | ||
70469b43 | 43 | int main(void) |
9b4bf32b MD |
44 | { |
45 | uint64_t values[] = { 42, 36, 24, }; | |
46 | unsigned int i; | |
47 | int ret; | |
48 | struct mynode *node, *n; | |
49 | ||
50 | /* | |
51 | * Notice that with the bulletproof flavor, there is no need to | |
52 | * register/unregister RCU reader threads. | |
53 | */ | |
54 | ||
55 | /* | |
56 | * Adding nodes to the linked-list. Safe against concurrent | |
57 | * RCU traversals, require mutual exclusion with list updates. | |
58 | */ | |
59 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { | |
60 | ret = add_node(values[i]); | |
61 | if (ret) | |
62 | goto end; | |
63 | } | |
64 | ||
65 | /* | |
66 | * We need to explicitly mark RCU read-side critical sections | |
67 | * with rcu_read_lock() and rcu_read_unlock(). They can be | |
68 | * nested. Those are no-ops for the QSBR flavor. | |
69 | */ | |
b9050d91 | 70 | urcu_bp_read_lock(); |
9b4bf32b MD |
71 | |
72 | /* | |
73 | * RCU traversal of the linked list. | |
74 | */ | |
75 | cds_list_for_each_entry_rcu(node, &mylist, node) { | |
76 | printf("Value: %" PRIu64 "\n", node->value); | |
77 | } | |
b9050d91 | 78 | urcu_bp_read_unlock(); |
9b4bf32b MD |
79 | |
80 | /* | |
81 | * Removing nodes from linked list. Safe against concurrent RCU | |
82 | * traversals, require mutual exclusion with list updates. | |
83 | */ | |
84 | cds_list_for_each_entry_safe(node, n, &mylist, node) { | |
85 | cds_list_del_rcu(&node->node); | |
86 | ||
87 | /* | |
88 | * Using synchronize_rcu() directly for synchronization | |
89 | * so we keep a minimal impact on the system by not | |
90 | * spawning any call_rcu() thread. It is slower though, | |
91 | * since there is no batching. | |
92 | */ | |
b9050d91 | 93 | urcu_bp_synchronize_rcu(); |
9b4bf32b MD |
94 | free(node); |
95 | } | |
96 | ||
97 | sleep(1); | |
98 | ||
99 | end: | |
100 | return ret; | |
101 | } |