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