Commit | Line | Data |
---|---|---|
d3d3857f MJ |
1 | // SPDX-FileCopyrightText: 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // SPDX-FileCopyrightText: 2011 Lai Jiangshan <laijs@cn.fujitsu.com> | |
3 | // | |
4 | // SPDX-License-Identifier: LGPL-2.1-or-later | |
5 | ||
a42cc659 MD |
6 | #ifndef _URCU_RCULFHASH_H |
7 | #define _URCU_RCULFHASH_H | |
ab7d5fc6 | 8 | |
f0c29ed7 | 9 | /* |
f0c29ed7 MD |
10 | * Userspace RCU library - Lock-Free RCU Hash Table |
11 | * | |
938465b4 MD |
12 | * For use with URCU_API_MAP (API mapping of liburcu), include this file |
13 | * _after_ including your URCU flavor. | |
f0c29ed7 MD |
14 | */ |
15 | ||
d7c76f85 | 16 | #include <urcu/config.h> |
674f7a69 | 17 | #include <stdint.h> |
6bcce235 | 18 | #include <pthread.h> |
6d320126 | 19 | #include <urcu/compiler.h> |
abc490a1 | 20 | |
01389791 MD |
21 | #ifdef __cplusplus |
22 | extern "C" { | |
23 | #endif | |
24 | ||
d7c76f85 MD |
25 | struct cds_lfht; |
26 | ||
7f52427b | 27 | /* |
04db56f8 | 28 | * cds_lfht_node: Contains the next pointers and reverse-hash |
7f52427b | 29 | * value required for lookup and traversal of the hash table. |
04db56f8 | 30 | * |
db00ccc3 | 31 | * struct cds_lfht_node should be aligned on 8-bytes boundaries because |
13f656f9 MD |
32 | * the three lower bits are used as flags. It is worth noting that the |
33 | * information contained within these three bits could be represented on | |
34 | * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and | |
35 | * BUCKET_FLAG. This can be done if we ensure that no iterator nor | |
36 | * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG | |
37 | * is set. Given the minimum size of struct cds_lfht_node is 8 bytes on | |
38 | * 32-bit architectures, we choose to go for simplicity and reserve | |
39 | * three bits. | |
7f52427b | 40 | * |
ae62aa6a MD |
41 | * struct cds_lfht_node can be embedded into a structure (as a field). |
42 | * caa_container_of() can be used to get the structure from the struct | |
43 | * cds_lfht_node after a lookup. | |
04db56f8 LJ |
44 | * |
45 | * The structure which embeds it typically holds the key (or key-value | |
46 | * pair) of the object. The caller code is responsible for calculation | |
47 | * of the hash value for cds_lfht APIs. | |
ae62aa6a | 48 | */ |
14044b37 | 49 | struct cds_lfht_node { |
3f2f3714 | 50 | struct cds_lfht_node *next; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */ |
04db56f8 | 51 | unsigned long reverse_hash; |
db00ccc3 | 52 | } __attribute__((aligned(8))); |
abc490a1 | 53 | |
7f52427b | 54 | /* cds_lfht_iter: Used to track state while traversing a hash chain. */ |
adc0de68 MD |
55 | struct cds_lfht_iter { |
56 | struct cds_lfht_node *node, *next; | |
d7c76f85 MD |
57 | /* |
58 | * For debugging purposes, build both API users and rculfhash | |
59 | * library with CDS_LFHT_ITER_DEBUG defined. This enables extra | |
60 | * consistency checks for calls to a cds_lfht_next() or | |
61 | * cds_lfht_next_duplicate() after the iterator has been | |
62 | * re-purposed to iterate on a different hash table. This is a | |
63 | * common programming mistake when performing hash table lookup | |
64 | * nested in a hash table traversal. | |
65 | */ | |
66 | #ifdef CONFIG_CDS_LFHT_ITER_DEBUG | |
67 | struct cds_lfht *lfht; | |
68 | #endif | |
adc0de68 MD |
69 | }; |
70 | ||
ac735254 XF |
71 | /* |
72 | * cds_lfht_alloc: Callbacks if we want to use custom memory allocator. | |
73 | */ | |
74 | struct cds_lfht_alloc { | |
75 | void *(*malloc)(void *state, size_t size); | |
76 | void *(*calloc)(void *state, size_t nmemb, size_t size); | |
77 | void *(*realloc)(void *state, void *ptr, size_t size); | |
78 | void *(*aligned_alloc)(void *state, size_t alignment, size_t size); | |
79 | void (*free)(void *state, void *ptr); | |
80 | void *state; | |
81 | }; | |
82 | ||
adc0de68 MD |
83 | static inline |
84 | struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) | |
85 | { | |
86 | return iter->node; | |
87 | } | |
88 | ||
938465b4 | 89 | struct rcu_flavor_struct; |
ab7d5fc6 | 90 | |
5e28c532 MD |
91 | /* |
92 | * Caution ! | |
abc490a1 | 93 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
94 | */ |
95 | ||
996ff57c | 96 | typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key); |
abc490a1 | 97 | |
f0c29ed7 | 98 | /* |
14044b37 | 99 | * cds_lfht_node_init - initialize a hash table node |
0422d92c | 100 | * @node: the node to initialize. |
04db56f8 LJ |
101 | * |
102 | * This function is kept to be eventually used for debugging purposes | |
103 | * (detection of memory corruption). | |
f0c29ed7 | 104 | */ |
abc490a1 | 105 | static inline |
70469b43 | 106 | void cds_lfht_node_init(struct cds_lfht_node *node __attribute__((unused))) |
abc490a1 | 107 | { |
abc490a1 | 108 | } |
674f7a69 | 109 | |
4c10e9af MD |
110 | /* |
111 | * cds_lfht_node_init_deleted - initialize a hash table node to "removed" state | |
112 | * @node: the node to initialize. | |
113 | * | |
114 | * Initialize the node such that cds_lfht_is_node_deleted() can be used | |
115 | * on the node before it is added to a hash table. | |
116 | */ | |
117 | extern | |
118 | void cds_lfht_node_init_deleted(struct cds_lfht_node *node); | |
119 | ||
b8af5011 MD |
120 | /* |
121 | * Hash table creation flags. | |
122 | */ | |
123 | enum { | |
124 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
5afadd12 | 125 | CDS_LFHT_ACCOUNTING = (1U << 1), |
b8af5011 MD |
126 | }; |
127 | ||
0b6aa001 LJ |
128 | struct cds_lfht_mm_type { |
129 | struct cds_lfht *(*alloc_cds_lfht)(unsigned long min_nr_alloc_buckets, | |
ac735254 | 130 | unsigned long max_nr_buckets, const struct cds_lfht_alloc *alloc); |
0b6aa001 LJ |
131 | void (*alloc_bucket_table)(struct cds_lfht *ht, unsigned long order); |
132 | void (*free_bucket_table)(struct cds_lfht *ht, unsigned long order); | |
133 | struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht, | |
134 | unsigned long index); | |
135 | }; | |
136 | ||
137 | extern const struct cds_lfht_mm_type cds_lfht_mm_order; | |
308d1cb3 | 138 | extern const struct cds_lfht_mm_type cds_lfht_mm_chunk; |
b0b55251 | 139 | extern const struct cds_lfht_mm_type cds_lfht_mm_mmap; |
0b6aa001 | 140 | |
674f7a69 | 141 | /* |
7a9dcf9b | 142 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
674f7a69 | 143 | */ |
b9c27904 | 144 | extern |
0422d92c | 145 | struct cds_lfht *_cds_lfht_new(unsigned long init_size, |
0722081a | 146 | unsigned long min_nr_alloc_buckets, |
747d725c | 147 | unsigned long max_nr_buckets, |
b8af5011 | 148 | int flags, |
0b6aa001 | 149 | const struct cds_lfht_mm_type *mm, |
7b17c13e | 150 | const struct rcu_flavor_struct *flavor, |
b7d619b0 | 151 | pthread_attr_t *attr); |
ab7d5fc6 | 152 | |
ac735254 XF |
153 | /* |
154 | * _cds_lfht_new_with_alloc - API used by cds_lfht_new_with_flavor_alloc. | |
155 | */ | |
156 | extern | |
157 | struct cds_lfht *_cds_lfht_new_with_alloc(unsigned long init_size, | |
158 | unsigned long min_nr_alloc_buckets, | |
159 | unsigned long max_nr_buckets, | |
160 | int flags, | |
161 | const struct cds_lfht_mm_type *mm, | |
ac735254 | 162 | const struct rcu_flavor_struct *flavor, |
8c5aef69 | 163 | const struct cds_lfht_alloc *alloc, |
ac735254 XF |
164 | pthread_attr_t *attr); |
165 | ||
938465b4 MD |
166 | /* |
167 | * cds_lfht_new_flavor - allocate a hash table tied to a RCU flavor. | |
168 | * @init_size: number of buckets to allocate initially. Must be power of two. | |
169 | * @min_nr_alloc_buckets: the minimum number of allocated buckets. | |
170 | * (must be power of two) | |
171 | * @max_nr_buckets: the maximum number of hash table buckets allowed. | |
172 | * (must be power of two, 0 is accepted, means | |
173 | * "infinite") | |
174 | * @flavor: flavor of liburcu to use to synchronize the hash table | |
175 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). | |
176 | * 0: no flags. | |
177 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
178 | * CDS_LFHT_ACCOUNTING: count the number of node addition | |
179 | * and removal in the table | |
180 | * @attr: optional resize worker thread attributes. NULL for default. | |
181 | * | |
182 | * Return NULL on error. | |
183 | * Note: the RCU flavor must be already included before the hash table header. | |
184 | * | |
185 | * The programmer is responsible for ensuring that resize operation has a | |
186 | * priority equal to hash table updater threads. It should be performed by | |
187 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
188 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
189 | * this priority level. Having lower priority for call_rcu and resize threads | |
190 | * does not pose any correctness issue, but the resize operations could be | |
191 | * starved by updates, thus leading to long hash table bucket chains. | |
192 | * Threads calling cds_lfht_new are NOT required to be registered RCU | |
193 | * read-side threads. It can be called very early. (e.g. before RCU is | |
194 | * initialized) | |
195 | */ | |
196 | static inline | |
197 | struct cds_lfht *cds_lfht_new_flavor(unsigned long init_size, | |
198 | unsigned long min_nr_alloc_buckets, | |
199 | unsigned long max_nr_buckets, | |
200 | int flags, | |
201 | const struct rcu_flavor_struct *flavor, | |
202 | pthread_attr_t *attr) | |
203 | { | |
204 | return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets, | |
205 | flags, NULL, flavor, attr); | |
206 | } | |
207 | ||
ac735254 XF |
208 | /* |
209 | * cds_lfht_new_with_flavor_alloc - allocate a hash table tied to a RCU flavor. | |
210 | * @init_size: number of buckets to allocate initially. Must be power of two. | |
211 | * @min_nr_alloc_buckets: the minimum number of allocated buckets. | |
212 | * (must be power of two) | |
213 | * @max_nr_buckets: the maximum number of hash table buckets allowed. | |
214 | * (must be power of two, 0 is accepted, means | |
215 | * "infinite") | |
216 | * @flavor: flavor of liburcu to use to synchronize the hash table | |
217 | * @alloc: Custom memory allocator for hash table memory management. | |
218 | * NULL for default. If a custom allocator is used, then | |
219 | * the whole interface of struct cds_lfht_alloc must be implemented. | |
220 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). | |
221 | * 0: no flags. | |
222 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
223 | * CDS_LFHT_ACCOUNTING: count the number of node addition | |
224 | * and removal in the table | |
225 | * @attr: optional resize worker thread attributes. NULL for default. | |
226 | * | |
227 | * Return NULL on error. | |
228 | * Note: the RCU flavor must be already included before the hash table header. | |
229 | * | |
230 | * The programmer is responsible for ensuring that resize operation has a | |
231 | * priority equal to hash table updater threads. It should be performed by | |
232 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
233 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
234 | * this priority level. Having lower priority for call_rcu and resize threads | |
235 | * does not pose any correctness issue, but the resize operations could be | |
236 | * starved by updates, thus leading to long hash table bucket chains. | |
237 | * Threads calling cds_lfht_new are NOT required to be registered RCU | |
238 | * read-side threads. It can be called very early. (e.g. before RCU is | |
239 | * initialized) | |
240 | */ | |
241 | static inline | |
242 | struct cds_lfht *cds_lfht_new_with_flavor_alloc(unsigned long init_size, | |
243 | unsigned long min_nr_alloc_buckets, | |
244 | unsigned long max_nr_buckets, | |
245 | int flags, | |
246 | const struct rcu_flavor_struct *flavor, | |
247 | const struct cds_lfht_alloc *alloc, | |
248 | pthread_attr_t *attr) | |
249 | { | |
250 | return _cds_lfht_new_with_alloc(init_size, min_nr_alloc_buckets, max_nr_buckets, | |
8c5aef69 | 251 | flags, NULL, flavor, alloc, attr); |
ac735254 XF |
252 | } |
253 | ||
938465b4 MD |
254 | |
255 | #ifdef URCU_API_MAP | |
7a9dcf9b MD |
256 | /* |
257 | * cds_lfht_new - allocate a hash table. | |
747d725c LJ |
258 | * @init_size: number of buckets to allocate initially. Must be power of two. |
259 | * @min_nr_alloc_buckets: the minimum number of allocated buckets. | |
260 | * (must be power of two) | |
261 | * @max_nr_buckets: the maximum number of hash table buckets allowed. | |
28d5c1b5 MD |
262 | * (must be power of two, 0 is accepted, means |
263 | * "infinite") | |
f22dd01d MD |
264 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). |
265 | * 0: no flags. | |
266 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
44bbe7fa LJ |
267 | * CDS_LFHT_ACCOUNTING: count the number of node addition |
268 | * and removal in the table | |
b7d619b0 | 269 | * @attr: optional resize worker thread attributes. NULL for default. |
7a9dcf9b | 270 | * |
7a9dcf9b MD |
271 | * Return NULL on error. |
272 | * Note: the RCU flavor must be already included before the hash table header. | |
b7d619b0 MD |
273 | * |
274 | * The programmer is responsible for ensuring that resize operation has a | |
275 | * priority equal to hash table updater threads. It should be performed by | |
276 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
277 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
278 | * this priority level. Having lower priority for call_rcu and resize threads | |
279 | * does not pose any correctness issue, but the resize operations could be | |
280 | * starved by updates, thus leading to long hash table bucket chains. | |
3a9c66db MD |
281 | * Threads calling cds_lfht_new are NOT required to be registered RCU |
282 | * read-side threads. It can be called very early. (e.g. before RCU is | |
283 | * initialized) | |
7a9dcf9b MD |
284 | */ |
285 | static inline | |
0422d92c | 286 | struct cds_lfht *cds_lfht_new(unsigned long init_size, |
0722081a | 287 | unsigned long min_nr_alloc_buckets, |
747d725c | 288 | unsigned long max_nr_buckets, |
b7d619b0 MD |
289 | int flags, |
290 | pthread_attr_t *attr) | |
7a9dcf9b | 291 | { |
7b17c13e | 292 | return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets, |
c1888f3a | 293 | flags, NULL, &rcu_flavor, attr); |
7a9dcf9b | 294 | } |
938465b4 | 295 | #endif /* URCU_API_MAP */ |
7a9dcf9b | 296 | |
f0c29ed7 | 297 | /* |
14044b37 | 298 | * cds_lfht_destroy - destroy a hash table. |
b7d619b0 MD |
299 | * @ht: the hash table to destroy. |
300 | * @attr: (output) resize worker thread attributes, as received by cds_lfht_new. | |
301 | * The caller will typically want to free this pointer if dynamically | |
7f52427b MD |
302 | * allocated. The attr point can be NULL if the caller does not |
303 | * need to be informed of the value passed to cds_lfht_new(). | |
6878c72b MD |
304 | * |
305 | * Return 0 on success, negative error value on error. | |
d0ec0ed2 | 306 | |
b047e7a7 MD |
307 | * Threads calling this API need to be registered RCU read-side threads. |
308 | * | |
d0ec0ed2 | 309 | * Prior to liburcu 0.10: |
d0ec0ed2 MD |
310 | * - cds_lfht_destroy should *not* be called from a RCU read-side |
311 | * critical section. It should *not* be called from a call_rcu thread | |
312 | * context neither. | |
313 | * | |
314 | * Starting from liburcu 0.10, rculfhash implements its own worker | |
b047e7a7 MD |
315 | * thread to handle resize operations, which removes the above RCU |
316 | * read-side critical section requirement on cds_lfht_destroy. | |
f0c29ed7 | 317 | */ |
b9c27904 | 318 | extern |
b7d619b0 | 319 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
f0c29ed7 MD |
320 | |
321 | /* | |
14044b37 | 322 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
7f52427b | 323 | * @ht: the hash table. |
3a9c66db MD |
324 | * @split_count_before: sample the node count split-counter before traversal. |
325 | * @count: traverse the hash table, count the number of nodes observed. | |
326 | * @split_count_after: sample the node count split-counter after traversal. | |
0422d92c | 327 | * |
f0c29ed7 | 328 | * Call with rcu_read_lock held. |
3df0c49c | 329 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 330 | */ |
b9c27904 | 331 | extern |
14044b37 | 332 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
7f52427b | 333 | long *split_count_before, |
adc0de68 | 334 | unsigned long *count, |
7f52427b | 335 | long *split_count_after); |
ab7d5fc6 | 336 | |
f0c29ed7 | 337 | /* |
14044b37 | 338 | * cds_lfht_lookup - lookup a node by key. |
0422d92c | 339 | * @ht: the hash table. |
0422d92c | 340 | * @hash: the key hash. |
6f554439 LJ |
341 | * @match: the key match function. |
342 | * @key: the current node key. | |
3a9c66db | 343 | * @iter: node, if found (output). *iter->node set to NULL if not found. |
f0c29ed7 | 344 | * |
f0c29ed7 | 345 | * Call with rcu_read_lock held. |
3df0c49c | 346 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 347 | * This function acts as a rcu_dereference() to read the node pointer. |
f0c29ed7 | 348 | */ |
b9c27904 | 349 | extern |
6f554439 | 350 | void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash, |
996ff57c | 351 | cds_lfht_match_fct match, const void *key, |
6f554439 | 352 | struct cds_lfht_iter *iter); |
ab7d5fc6 | 353 | |
f0c29ed7 | 354 | /* |
3a9c66db | 355 | * cds_lfht_next_duplicate - get the next item with same key, after iterator. |
0422d92c MD |
356 | * @ht: the hash table. |
357 | * @match: the key match function. | |
04db56f8 | 358 | * @key: the current node key. |
3a9c66db MD |
359 | * @iter: input: current iterator. |
360 | * output: node, if found. *iter->node set to NULL if not found. | |
f0c29ed7 | 361 | * |
3a9c66db MD |
362 | * Uses an iterator initialized by a lookup or traversal. Important: the |
363 | * iterator _needs_ to be initialized before calling | |
364 | * cds_lfht_next_duplicate. | |
adc0de68 MD |
365 | * Sets *iter-node to the following node with same key. |
366 | * Sets *iter->node to NULL if no following node exists with same key. | |
367 | * RCU read-side lock must be held across cds_lfht_lookup and | |
368 | * cds_lfht_next calls, and also between cds_lfht_next calls using the | |
369 | * node returned by a previous cds_lfht_next. | |
370 | * Call with rcu_read_lock held. | |
3df0c49c | 371 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 372 | * This function acts as a rcu_dereference() to read the node pointer. |
f0c29ed7 | 373 | */ |
b9c27904 | 374 | extern |
0422d92c | 375 | void cds_lfht_next_duplicate(struct cds_lfht *ht, |
996ff57c | 376 | cds_lfht_match_fct match, const void *key, |
04db56f8 | 377 | struct cds_lfht_iter *iter); |
4e9b9fbf MD |
378 | |
379 | /* | |
380 | * cds_lfht_first - get the first node in the table. | |
0422d92c MD |
381 | * @ht: the hash table. |
382 | * @iter: First node, if exists (output). *iter->node set to NULL if not found. | |
4e9b9fbf MD |
383 | * |
384 | * Output in "*iter". *iter->node set to NULL if table is empty. | |
385 | * Call with rcu_read_lock held. | |
3df0c49c | 386 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 387 | * This function acts as a rcu_dereference() to read the node pointer. |
4e9b9fbf | 388 | */ |
b9c27904 | 389 | extern |
4e9b9fbf MD |
390 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
391 | ||
392 | /* | |
393 | * cds_lfht_next - get the next node in the table. | |
0422d92c | 394 | * @ht: the hash table. |
3a9c66db MD |
395 | * @iter: input: current iterator. |
396 | * output: next node, if exists. *iter->node set to NULL if not found. | |
4e9b9fbf MD |
397 | * |
398 | * Input/Output in "*iter". *iter->node set to NULL if *iter was | |
399 | * pointing to the last table node. | |
400 | * Call with rcu_read_lock held. | |
3df0c49c | 401 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 402 | * This function acts as a rcu_dereference() to read the node pointer. |
4e9b9fbf | 403 | */ |
b9c27904 | 404 | extern |
4e9b9fbf | 405 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
ab7d5fc6 | 406 | |
18117871 | 407 | /* |
14044b37 | 408 | * cds_lfht_add - add a node to the hash table. |
0422d92c MD |
409 | * @ht: the hash table. |
410 | * @hash: the key hash. | |
411 | * @node: the node to add. | |
f0c29ed7 | 412 | * |
48ed1c18 | 413 | * This function supports adding redundant keys into the table. |
3df0c49c MD |
414 | * Call with rcu_read_lock held. |
415 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 MD |
416 | * This function issues a full memory barrier before and after its |
417 | * atomic commit. | |
f0c29ed7 | 418 | */ |
b9c27904 | 419 | extern |
0422d92c MD |
420 | void cds_lfht_add(struct cds_lfht *ht, unsigned long hash, |
421 | struct cds_lfht_node *node); | |
f0c29ed7 MD |
422 | |
423 | /* | |
14044b37 | 424 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
0422d92c | 425 | * @ht: the hash table. |
6f554439 | 426 | * @hash: the node's hash. |
0422d92c | 427 | * @match: the key match function. |
04db56f8 | 428 | * @key: the node's key. |
0422d92c | 429 | * @node: the node to try adding. |
f0c29ed7 | 430 | * |
6878c72b | 431 | * Return the node added upon success. |
860d07e8 MD |
432 | * Return the unique node already present upon failure. If |
433 | * cds_lfht_add_unique fails, the node passed as parameter should be | |
3a9c66db | 434 | * freed by the caller. In this case, the caller does NOT need to wait |
50024344 | 435 | * for a grace period before freeing or re-using the node. |
f0c29ed7 | 436 | * Call with rcu_read_lock held. |
3df0c49c | 437 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
438 | * |
439 | * The semantic of this function is that if only this function is used | |
440 | * to add keys into the table, no duplicated keys should ever be | |
441 | * observable in the table. The same guarantee apply for combination of | |
9357c415 | 442 | * add_unique and add_replace (see below). |
7b783f81 MD |
443 | * |
444 | * Upon success, this function issues a full memory barrier before and | |
445 | * after its atomic commit. Upon failure, this function acts like a | |
446 | * simple lookup operation: it acts as a rcu_dereference() to read the | |
447 | * node pointer. The failure case does not guarantee any other memory | |
448 | * barrier. | |
18117871 | 449 | */ |
b9c27904 | 450 | extern |
adc0de68 | 451 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
6f554439 | 452 | unsigned long hash, |
0422d92c | 453 | cds_lfht_match_fct match, |
996ff57c | 454 | const void *key, |
adc0de68 | 455 | struct cds_lfht_node *node); |
3eca1b8c | 456 | |
48ed1c18 | 457 | /* |
9357c415 | 458 | * cds_lfht_add_replace - replace or add a node within hash table. |
0422d92c | 459 | * @ht: the hash table. |
6f554439 | 460 | * @hash: the node's hash. |
0422d92c | 461 | * @match: the key match function. |
04db56f8 | 462 | * @key: the node's key. |
0422d92c | 463 | * @node: the node to add. |
48ed1c18 MD |
464 | * |
465 | * Return the node replaced upon success. If no node matching the key | |
466 | * was present, return NULL, which also means the operation succeeded. | |
467 | * This replacement operation should never fail. | |
468 | * Call with rcu_read_lock held. | |
3df0c49c | 469 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 470 | * After successful replacement, a grace period must be waited for before |
50024344 | 471 | * freeing or re-using the memory reserved for the returned node. |
48ed1c18 | 472 | * |
3a9c66db MD |
473 | * The semantic of replacement vs lookups and traversals is the |
474 | * following: if lookups and traversals are performed between a key | |
475 | * unique insertion and its removal, we guarantee that the lookups and | |
476 | * traversals will always find exactly one instance of the key if it is | |
477 | * replaced concurrently with the lookups. | |
48ed1c18 MD |
478 | * |
479 | * Providing this semantic allows us to ensure that replacement-only | |
480 | * schemes will never generate duplicated keys. It also allows us to | |
9357c415 | 481 | * guarantee that a combination of add_replace and add_unique updates |
48ed1c18 | 482 | * will never generate duplicated keys. |
7b783f81 MD |
483 | * |
484 | * This function issues a full memory barrier before and after its | |
485 | * atomic commit. | |
48ed1c18 | 486 | */ |
b9c27904 | 487 | extern |
9357c415 | 488 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
6f554439 | 489 | unsigned long hash, |
0422d92c | 490 | cds_lfht_match_fct match, |
996ff57c | 491 | const void *key, |
adc0de68 | 492 | struct cds_lfht_node *node); |
48ed1c18 | 493 | |
f0c29ed7 | 494 | /* |
929ad508 | 495 | * cds_lfht_replace - replace a node pointed to by iter within hash table. |
0422d92c MD |
496 | * @ht: the hash table. |
497 | * @old_iter: the iterator position of the node to replace. | |
2e79c445 MD |
498 | * @hash: the node's hash. |
499 | * @match: the key match function. | |
500 | * @key: the node's key. | |
501 | * @new_node: the new node to use as replacement. | |
f0c29ed7 | 502 | * |
9357c415 | 503 | * Return 0 if replacement is successful, negative value otherwise. |
2e79c445 MD |
504 | * Replacing a NULL old node or an already removed node will fail with |
505 | * -ENOENT. | |
506 | * If the hash or value of the node to replace and the new node differ, | |
507 | * this function returns -EINVAL without proceeding to the replacement. | |
9357c415 MD |
508 | * Old node can be looked up with cds_lfht_lookup and cds_lfht_next. |
509 | * RCU read-side lock must be held between lookup and replacement. | |
510 | * Call with rcu_read_lock held. | |
3df0c49c | 511 | * Threads calling this API need to be registered RCU read-side threads. |
9357c415 | 512 | * After successful replacement, a grace period must be waited for before |
50024344 MD |
513 | * freeing or re-using the memory reserved for the old node (which can |
514 | * be accessed with cds_lfht_iter_get_node). | |
9357c415 | 515 | * |
3a9c66db MD |
516 | * The semantic of replacement vs lookups is the same as |
517 | * cds_lfht_add_replace(). | |
7b783f81 MD |
518 | * |
519 | * Upon success, this function issues a full memory barrier before and | |
520 | * after its atomic commit. Upon failure, this function does not issue | |
521 | * any memory barrier. | |
9357c415 | 522 | */ |
b9c27904 | 523 | extern |
2e79c445 MD |
524 | int cds_lfht_replace(struct cds_lfht *ht, |
525 | struct cds_lfht_iter *old_iter, | |
526 | unsigned long hash, | |
527 | cds_lfht_match_fct match, | |
528 | const void *key, | |
9357c415 MD |
529 | struct cds_lfht_node *new_node); |
530 | ||
531 | /* | |
532 | * cds_lfht_del - remove node pointed to by iterator from hash table. | |
0422d92c | 533 | * @ht: the hash table. |
bc8c3c74 | 534 | * @node: the node to delete. |
9357c415 MD |
535 | * |
536 | * Return 0 if the node is successfully removed, negative value | |
537 | * otherwise. | |
bc8c3c74 | 538 | * Deleting a NULL node or an already removed node will fail with a |
9357c415 | 539 | * negative value. |
bc8c3c74 MD |
540 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next, |
541 | * followed by use of cds_lfht_iter_get_node. | |
9357c415 | 542 | * RCU read-side lock must be held between lookup and removal. |
f0c29ed7 | 543 | * Call with rcu_read_lock held. |
3df0c49c | 544 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 545 | * After successful removal, a grace period must be waited for before |
50024344 MD |
546 | * freeing or re-using the memory reserved for old node (which can be |
547 | * accessed with cds_lfht_iter_get_node). | |
7b783f81 MD |
548 | * Upon success, this function issues a full memory barrier before and |
549 | * after its atomic commit. Upon failure, this function does not issue | |
550 | * any memory barrier. | |
f0c29ed7 | 551 | */ |
b9c27904 | 552 | extern |
bc8c3c74 | 553 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node); |
ab7d5fc6 | 554 | |
df55172a | 555 | /* |
3a9c66db | 556 | * cds_lfht_is_node_deleted - query whether a node is removed from hash table. |
df55172a MD |
557 | * |
558 | * Return non-zero if the node is deleted from the hash table, 0 | |
559 | * otherwise. | |
560 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next, | |
561 | * followed by use of cds_lfht_iter_get_node. | |
562 | * RCU read-side lock must be held between lookup and call to this | |
563 | * function. | |
564 | * Call with rcu_read_lock held. | |
565 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 | 566 | * This function does not issue any memory barrier. |
df55172a | 567 | */ |
b9c27904 | 568 | extern |
afa5940d | 569 | int cds_lfht_is_node_deleted(const struct cds_lfht_node *node); |
df55172a | 570 | |
f0c29ed7 | 571 | /* |
14044b37 | 572 | * cds_lfht_resize - Force a hash table resize |
0422d92c | 573 | * @ht: the hash table. |
1475579c | 574 | * @new_size: update to this hash table size. |
3df0c49c MD |
575 | * |
576 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 | 577 | * This function does not (necessarily) issue memory barriers. |
43387742 MD |
578 | * cds_lfht_resize should *not* be called from a RCU read-side critical |
579 | * section. | |
f0c29ed7 | 580 | */ |
b9c27904 | 581 | extern |
1475579c | 582 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 583 | |
6d320126 | 584 | /* |
3a9c66db MD |
585 | * Note: it is safe to perform element removal (del), replacement, or |
586 | * any hash table update operation during any of the following hash | |
587 | * table traversals. | |
7b783f81 | 588 | * These functions act as rcu_dereference() to read the node pointers. |
6d320126 MD |
589 | */ |
590 | #define cds_lfht_for_each(ht, iter, node) \ | |
591 | for (cds_lfht_first(ht, iter), \ | |
592 | node = cds_lfht_iter_get_node(iter); \ | |
593 | node != NULL; \ | |
594 | cds_lfht_next(ht, iter), \ | |
595 | node = cds_lfht_iter_get_node(iter)) | |
596 | ||
6f554439 LJ |
597 | #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \ |
598 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ | |
6d320126 MD |
599 | node = cds_lfht_iter_get_node(iter); \ |
600 | node != NULL; \ | |
601 | cds_lfht_next_duplicate(ht, match, key, iter), \ | |
602 | node = cds_lfht_iter_get_node(iter)) | |
603 | ||
e58cdfcf | 604 | #define cds_lfht_entry(ptr, type, member) \ |
0ef4782c | 605 | caa_container_of_check_null(ptr, type, member) |
e58cdfcf | 606 | |
6d320126 MD |
607 | #define cds_lfht_for_each_entry(ht, iter, pos, member) \ |
608 | for (cds_lfht_first(ht, iter), \ | |
e58cdfcf MD |
609 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
610 | __typeof__(*(pos)), member); \ | |
611 | pos != NULL; \ | |
6d320126 | 612 | cds_lfht_next(ht, iter), \ |
e58cdfcf MD |
613 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
614 | __typeof__(*(pos)), member)) | |
6d320126 | 615 | |
6f554439 | 616 | #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \ |
6d320126 | 617 | iter, pos, member) \ |
6f554439 | 618 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ |
e58cdfcf MD |
619 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
620 | __typeof__(*(pos)), member); \ | |
621 | pos != NULL; \ | |
6d320126 | 622 | cds_lfht_next_duplicate(ht, match, key, iter), \ |
e58cdfcf MD |
623 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
624 | __typeof__(*(pos)), member)) | |
6d320126 | 625 | |
01389791 MD |
626 | #ifdef __cplusplus |
627 | } | |
628 | #endif | |
629 | ||
a42cc659 | 630 | #endif /* _URCU_RCULFHASH_H */ |