Commit | Line | Data |
---|---|---|
d553beef MD |
1 | #ifndef _URCU_RCUJA_H |
2 | #define _URCU_RCUJA_H | |
3 | ||
4 | /* | |
5 | * urcu/rcuja.h | |
6 | * | |
7 | * Userspace RCU library - RCU Judy Array | |
8 | * | |
170e1186 | 9 | * Copyright 2012-2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
d553beef MD |
10 | * |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | * | |
25 | * Include this file _after_ including your URCU flavor. | |
26 | */ | |
27 | ||
28 | #include <stdint.h> | |
29 | #include <urcu/compiler.h> | |
30 | #include <urcu-call-rcu.h> | |
31 | #include <urcu-flavor.h> | |
32 | #include <stdint.h> | |
d553beef MD |
33 | |
34 | #ifdef __cplusplus | |
35 | extern "C" { | |
36 | #endif | |
37 | ||
03ec1aeb MD |
38 | /* |
39 | * Duplicate nodes with the same key are chained into a singly-linked | |
40 | * list. The last item of this list has a NULL next pointer. | |
41 | */ | |
d553beef | 42 | struct cds_ja_node { |
03ec1aeb | 43 | struct cds_ja_node *next; |
d553beef MD |
44 | }; |
45 | ||
46 | struct cds_ja; | |
47 | ||
48 | /* | |
49 | * cds_ja_node_init - initialize a judy array node | |
50 | * @node: the node to initialize. | |
51 | * | |
52 | * This function is kept to be eventually used for debugging purposes | |
53 | * (detection of memory corruption). | |
54 | */ | |
55 | static inline | |
56 | void cds_ja_node_init(struct cds_ja_node *node) | |
57 | { | |
58 | } | |
59 | ||
b41da4d1 MD |
60 | /* |
61 | * cds_ja_lookup - look up by key. | |
62 | * @ja: the Judy array. | |
63 | * @key: key to look up. | |
64 | * | |
65 | * Returns the first node of a duplicate chain if a match is found, else | |
66 | * returns NULL. | |
67 | * A RCU read-side lock should be held across call to this function and | |
68 | * use of its return value. | |
69 | */ | |
03ec1aeb | 70 | struct cds_ja_node *cds_ja_lookup(struct cds_ja *ja, uint64_t key); |
b41da4d1 MD |
71 | |
72 | /* | |
b023ba9f | 73 | * cds_ja_lookup_below_equal - look up first node with key <= @key. |
b41da4d1 MD |
74 | * @ja: the Judy array. |
75 | * @key: key to look up. | |
36305a3d | 76 | * @result_key: key found. |
b41da4d1 MD |
77 | * |
78 | * Returns the first node of a duplicate chain if a node is present in | |
b023ba9f | 79 | * the tree which has a key below or equal to @key, else returns NULL. |
b41da4d1 MD |
80 | * A RCU read-side lock should be held across call to this function and |
81 | * use of its return value. | |
82 | */ | |
b023ba9f | 83 | struct cds_ja_node *cds_ja_lookup_below_equal(struct cds_ja *ja, |
36305a3d | 84 | uint64_t key, uint64_t *result_key); |
b023ba9f MD |
85 | |
86 | /* | |
87 | * cds_ja_lookup_above_equal - look up first node with key >= @key. | |
88 | * @ja: the Judy array. | |
89 | * @key: key to look up. | |
36305a3d | 90 | * @result_key: key found. |
b023ba9f MD |
91 | * |
92 | * Returns the first node of a duplicate chain if a node is present in | |
93 | * the tree which has a key above or equal to @key, else returns NULL. | |
94 | * A RCU read-side lock should be held across call to this function and | |
95 | * use of its return value. | |
96 | */ | |
97 | struct cds_ja_node *cds_ja_lookup_above_equal(struct cds_ja *ja, | |
36305a3d | 98 | uint64_t key, uint64_t *result_key); |
d553beef | 99 | |
d8536f92 MD |
100 | /* |
101 | * cds_ja_add - Add @node at @key, allowing duplicates. | |
102 | * @ja: the Judy array. | |
103 | * @key: key at which @node should be added. | |
104 | * @node: node to add. | |
105 | * | |
106 | * Returns 0 on success, negative error value on error. | |
107 | * A RCU read-side lock should be held across call to this function. | |
108 | */ | |
d553beef | 109 | int cds_ja_add(struct cds_ja *ja, uint64_t key, |
d8536f92 | 110 | struct cds_ja_node *node); |
d553beef | 111 | |
d8536f92 MD |
112 | /* |
113 | * cds_ja_add_unique - Add @node at @key, without duplicates. | |
114 | * @ja: the Judy array. | |
115 | * @key: key at which @node should be added. | |
116 | * @node: node to add. | |
117 | * | |
118 | * Returns @node if successfully added, else returns the already | |
119 | * existing node (acts as a RCU lookup). | |
120 | * A RCU read-side lock should be held across call to this function and | |
121 | * use of its return value. | |
122 | */ | |
75d573aa | 123 | struct cds_ja_node *cds_ja_add_unique(struct cds_ja *ja, uint64_t key, |
d8536f92 | 124 | struct cds_ja_node *node); |
75d573aa | 125 | |
d8536f92 MD |
126 | /* |
127 | * cds_ja_del - Remove @node at @key. | |
128 | * @ja: the Judy array. | |
129 | * @key: key at which @node is expected. | |
130 | * @node: node to remove. | |
131 | * | |
132 | * Returns 0 on success, negative error value on error. | |
133 | * A RCU read-side lock should be held across call to this function. | |
134 | */ | |
d553beef MD |
135 | int cds_ja_del(struct cds_ja *ja, uint64_t key, |
136 | struct cds_ja_node *node); | |
137 | ||
138 | struct cds_ja *_cds_ja_new(unsigned int key_bits, | |
139 | const struct rcu_flavor_struct *flavor); | |
140 | ||
d8536f92 MD |
141 | /* |
142 | * cds_ja_new - Create a Judy array. | |
143 | * @key_bits: Number of bits for key. | |
144 | * | |
145 | * Returns non-NULL pointer on success, else NULL on error. @key_bits | |
146 | * needs to be multiple of 8, either: 8, 16, 24, 32, 40, 48, 56, or 64. | |
147 | */ | |
d553beef MD |
148 | static inline |
149 | struct cds_ja *cds_ja_new(unsigned int key_bits) | |
150 | { | |
151 | return _cds_ja_new(key_bits, &rcu_flavor); | |
152 | } | |
153 | ||
d8536f92 MD |
154 | /* |
155 | * cds_ja_destroy - Destroy a Judy array. | |
156 | * @ja: the Judy array. | |
d8536f92 MD |
157 | * |
158 | * Returns 0 on success, negative error value on error. | |
dc0e9798 MD |
159 | * There should be no more concurrent add, delete, nor look-up performed |
160 | * on the Judy array while it is being destroyed (ensured by the caller). | |
125a6dae MD |
161 | * RCU read-side lock should _not_ be held when calling this function, |
162 | * however, QSBR threads need to be online. | |
d8536f92 | 163 | */ |
99e6e3dc | 164 | int cds_ja_destroy(struct cds_ja *ja); |
d553beef | 165 | |
03ec1aeb | 166 | /* |
c01cfe73 MD |
167 | * cds_ja_for_each_duplicate_rcu: Iterate through duplicates. |
168 | * @pos: struct cds_ja_node *, start of duplicate list and loop cursor. | |
169 | * | |
03ec1aeb MD |
170 | * Iterate through duplicates returned by cds_ja_lookup*() |
171 | * This must be done while rcu_read_lock() is held. | |
172 | * Receives a struct cds_ja_node * as parameter, which is used as start | |
173 | * of duplicate list and loop cursor. | |
c01cfe73 | 174 | * _NOT_ safe against node removal within iteration. |
03ec1aeb | 175 | */ |
99e6e3dc | 176 | #define cds_ja_for_each_duplicate_rcu(pos) \ |
03ec1aeb MD |
177 | for (; (pos) != NULL; (pos) = rcu_dereference((pos)->next)) |
178 | ||
c01cfe73 | 179 | /* |
99e6e3dc | 180 | * cds_ja_for_each_duplicate_safe: Iterate through duplicates. |
c01cfe73 MD |
181 | * @pos: struct cds_ja_node *, start of duplicate list and loop cursor. |
182 | * @p: struct cds_ja_node *, temporary pointer to next. | |
183 | * | |
99e6e3dc | 184 | * Iterate through duplicates returned by cds_ja_lookup*(). |
c01cfe73 | 185 | * Safe against node removal within iteration. |
99e6e3dc | 186 | * This must be done while rcu_read_lock() is held. |
c01cfe73 MD |
187 | */ |
188 | #define cds_ja_for_each_duplicate_safe_rcu(pos, p) \ | |
189 | for (; (pos) != NULL ? \ | |
190 | ((p) = rcu_dereference((pos)->next), 1) : 0; \ | |
191 | (pos) = (p)) | |
192 | ||
193 | /* | |
194 | * cds_ja_for_each_key_rcu: Iterate over all keys in ascending order. | |
195 | * @ja: Judy array on which iteration should be done. | |
196 | * @key: Key cursor, needs to be a uint64_t. | |
197 | * @pos: struct cds_ja_node *, used as loop cursor. | |
198 | * | |
199 | * Iterate over all keys of a RCU Judy array (_not_ duplicates) in | |
200 | * ascending order. | |
201 | * This must be done while rcu_read_lock() is held. | |
202 | * Safe against node removal during iteration. | |
203 | */ | |
204 | #define cds_ja_for_each_key_rcu(ja, key, pos) \ | |
205 | for ((key) = 0; \ | |
206 | ((pos) = cds_ja_lookup_above_equal(ja, key, &(key))); ) | |
207 | ||
208 | /* | |
209 | * cds_ja_for_each_key_prev_rcu: Iterate over all keys in descending order. | |
210 | * @ja: Judy array on which iteration should be done. | |
211 | * @key: Key cursor, needs to be a uint64_t. | |
212 | * @pos: struct cds_ja_node *, used as loop cursor. | |
213 | * | |
214 | * Iterate over all keys of a RCU Judy array (_not_ duplicates) in | |
215 | * descending order. | |
216 | * This must be done while rcu_read_lock() is held. | |
217 | * Safe against node removal during iteration. | |
218 | */ | |
219 | #define cds_ja_for_each_key_prev_rcu(ja, key, pos) \ | |
220 | for ((key) = UINT64_MAX; \ | |
221 | ((pos) = cds_ja_lookup_below_equal(ja, key, &(key))); ) | |
222 | ||
d553beef MD |
223 | #ifdef __cplusplus |
224 | } | |
225 | #endif | |
226 | ||
227 | #endif /* _URCU_RCUJA_H */ |