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 | ||
71 | static inline | |
72 | struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) | |
73 | { | |
74 | return iter->node; | |
75 | } | |
76 | ||
938465b4 | 77 | struct rcu_flavor_struct; |
ab7d5fc6 | 78 | |
5e28c532 MD |
79 | /* |
80 | * Caution ! | |
abc490a1 | 81 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
82 | */ |
83 | ||
996ff57c | 84 | typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key); |
abc490a1 | 85 | |
f0c29ed7 | 86 | /* |
14044b37 | 87 | * cds_lfht_node_init - initialize a hash table node |
0422d92c | 88 | * @node: the node to initialize. |
04db56f8 LJ |
89 | * |
90 | * This function is kept to be eventually used for debugging purposes | |
91 | * (detection of memory corruption). | |
f0c29ed7 | 92 | */ |
abc490a1 | 93 | static inline |
70469b43 | 94 | void cds_lfht_node_init(struct cds_lfht_node *node __attribute__((unused))) |
abc490a1 | 95 | { |
abc490a1 | 96 | } |
674f7a69 | 97 | |
4c10e9af MD |
98 | /* |
99 | * cds_lfht_node_init_deleted - initialize a hash table node to "removed" state | |
100 | * @node: the node to initialize. | |
101 | * | |
102 | * Initialize the node such that cds_lfht_is_node_deleted() can be used | |
103 | * on the node before it is added to a hash table. | |
104 | */ | |
105 | extern | |
106 | void cds_lfht_node_init_deleted(struct cds_lfht_node *node); | |
107 | ||
b8af5011 MD |
108 | /* |
109 | * Hash table creation flags. | |
110 | */ | |
111 | enum { | |
112 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
5afadd12 | 113 | CDS_LFHT_ACCOUNTING = (1U << 1), |
b8af5011 MD |
114 | }; |
115 | ||
0b6aa001 LJ |
116 | struct cds_lfht_mm_type { |
117 | struct cds_lfht *(*alloc_cds_lfht)(unsigned long min_nr_alloc_buckets, | |
118 | unsigned long max_nr_buckets); | |
119 | void (*alloc_bucket_table)(struct cds_lfht *ht, unsigned long order); | |
120 | void (*free_bucket_table)(struct cds_lfht *ht, unsigned long order); | |
121 | struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht, | |
122 | unsigned long index); | |
123 | }; | |
124 | ||
125 | extern const struct cds_lfht_mm_type cds_lfht_mm_order; | |
308d1cb3 | 126 | extern const struct cds_lfht_mm_type cds_lfht_mm_chunk; |
b0b55251 | 127 | extern const struct cds_lfht_mm_type cds_lfht_mm_mmap; |
0b6aa001 | 128 | |
674f7a69 | 129 | /* |
7a9dcf9b | 130 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
674f7a69 | 131 | */ |
b9c27904 | 132 | extern |
0422d92c | 133 | struct cds_lfht *_cds_lfht_new(unsigned long init_size, |
0722081a | 134 | unsigned long min_nr_alloc_buckets, |
747d725c | 135 | unsigned long max_nr_buckets, |
b8af5011 | 136 | int flags, |
0b6aa001 | 137 | const struct cds_lfht_mm_type *mm, |
7b17c13e | 138 | const struct rcu_flavor_struct *flavor, |
b7d619b0 | 139 | pthread_attr_t *attr); |
ab7d5fc6 | 140 | |
938465b4 MD |
141 | /* |
142 | * cds_lfht_new_flavor - allocate a hash table tied to a RCU flavor. | |
143 | * @init_size: number of buckets to allocate initially. Must be power of two. | |
144 | * @min_nr_alloc_buckets: the minimum number of allocated buckets. | |
145 | * (must be power of two) | |
146 | * @max_nr_buckets: the maximum number of hash table buckets allowed. | |
147 | * (must be power of two, 0 is accepted, means | |
148 | * "infinite") | |
149 | * @flavor: flavor of liburcu to use to synchronize the hash table | |
150 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). | |
151 | * 0: no flags. | |
152 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
153 | * CDS_LFHT_ACCOUNTING: count the number of node addition | |
154 | * and removal in the table | |
155 | * @attr: optional resize worker thread attributes. NULL for default. | |
156 | * | |
157 | * Return NULL on error. | |
158 | * Note: the RCU flavor must be already included before the hash table header. | |
159 | * | |
160 | * The programmer is responsible for ensuring that resize operation has a | |
161 | * priority equal to hash table updater threads. It should be performed by | |
162 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
163 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
164 | * this priority level. Having lower priority for call_rcu and resize threads | |
165 | * does not pose any correctness issue, but the resize operations could be | |
166 | * starved by updates, thus leading to long hash table bucket chains. | |
167 | * Threads calling cds_lfht_new are NOT required to be registered RCU | |
168 | * read-side threads. It can be called very early. (e.g. before RCU is | |
169 | * initialized) | |
170 | */ | |
171 | static inline | |
172 | struct cds_lfht *cds_lfht_new_flavor(unsigned long init_size, | |
173 | unsigned long min_nr_alloc_buckets, | |
174 | unsigned long max_nr_buckets, | |
175 | int flags, | |
176 | const struct rcu_flavor_struct *flavor, | |
177 | pthread_attr_t *attr) | |
178 | { | |
179 | return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets, | |
180 | flags, NULL, flavor, attr); | |
181 | } | |
182 | ||
183 | ||
184 | #ifdef URCU_API_MAP | |
7a9dcf9b MD |
185 | /* |
186 | * cds_lfht_new - allocate a hash table. | |
747d725c LJ |
187 | * @init_size: number of buckets to allocate initially. Must be power of two. |
188 | * @min_nr_alloc_buckets: the minimum number of allocated buckets. | |
189 | * (must be power of two) | |
190 | * @max_nr_buckets: the maximum number of hash table buckets allowed. | |
28d5c1b5 MD |
191 | * (must be power of two, 0 is accepted, means |
192 | * "infinite") | |
f22dd01d MD |
193 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). |
194 | * 0: no flags. | |
195 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
44bbe7fa LJ |
196 | * CDS_LFHT_ACCOUNTING: count the number of node addition |
197 | * and removal in the table | |
b7d619b0 | 198 | * @attr: optional resize worker thread attributes. NULL for default. |
7a9dcf9b | 199 | * |
7a9dcf9b MD |
200 | * Return NULL on error. |
201 | * Note: the RCU flavor must be already included before the hash table header. | |
b7d619b0 MD |
202 | * |
203 | * The programmer is responsible for ensuring that resize operation has a | |
204 | * priority equal to hash table updater threads. It should be performed by | |
205 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
206 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
207 | * this priority level. Having lower priority for call_rcu and resize threads | |
208 | * does not pose any correctness issue, but the resize operations could be | |
209 | * starved by updates, thus leading to long hash table bucket chains. | |
3a9c66db MD |
210 | * Threads calling cds_lfht_new are NOT required to be registered RCU |
211 | * read-side threads. It can be called very early. (e.g. before RCU is | |
212 | * initialized) | |
7a9dcf9b MD |
213 | */ |
214 | static inline | |
0422d92c | 215 | struct cds_lfht *cds_lfht_new(unsigned long init_size, |
0722081a | 216 | unsigned long min_nr_alloc_buckets, |
747d725c | 217 | unsigned long max_nr_buckets, |
b7d619b0 MD |
218 | int flags, |
219 | pthread_attr_t *attr) | |
7a9dcf9b | 220 | { |
7b17c13e | 221 | return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets, |
c1888f3a | 222 | flags, NULL, &rcu_flavor, attr); |
7a9dcf9b | 223 | } |
938465b4 | 224 | #endif /* URCU_API_MAP */ |
7a9dcf9b | 225 | |
f0c29ed7 | 226 | /* |
14044b37 | 227 | * cds_lfht_destroy - destroy a hash table. |
b7d619b0 MD |
228 | * @ht: the hash table to destroy. |
229 | * @attr: (output) resize worker thread attributes, as received by cds_lfht_new. | |
230 | * The caller will typically want to free this pointer if dynamically | |
7f52427b MD |
231 | * allocated. The attr point can be NULL if the caller does not |
232 | * need to be informed of the value passed to cds_lfht_new(). | |
6878c72b MD |
233 | * |
234 | * Return 0 on success, negative error value on error. | |
d0ec0ed2 | 235 | |
b047e7a7 MD |
236 | * Threads calling this API need to be registered RCU read-side threads. |
237 | * | |
d0ec0ed2 | 238 | * Prior to liburcu 0.10: |
d0ec0ed2 MD |
239 | * - cds_lfht_destroy should *not* be called from a RCU read-side |
240 | * critical section. It should *not* be called from a call_rcu thread | |
241 | * context neither. | |
242 | * | |
243 | * Starting from liburcu 0.10, rculfhash implements its own worker | |
b047e7a7 MD |
244 | * thread to handle resize operations, which removes the above RCU |
245 | * read-side critical section requirement on cds_lfht_destroy. | |
f0c29ed7 | 246 | */ |
b9c27904 | 247 | extern |
b7d619b0 | 248 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
f0c29ed7 MD |
249 | |
250 | /* | |
14044b37 | 251 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
7f52427b | 252 | * @ht: the hash table. |
3a9c66db MD |
253 | * @split_count_before: sample the node count split-counter before traversal. |
254 | * @count: traverse the hash table, count the number of nodes observed. | |
255 | * @split_count_after: sample the node count split-counter after traversal. | |
0422d92c | 256 | * |
f0c29ed7 | 257 | * Call with rcu_read_lock held. |
3df0c49c | 258 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 259 | */ |
b9c27904 | 260 | extern |
14044b37 | 261 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
7f52427b | 262 | long *split_count_before, |
adc0de68 | 263 | unsigned long *count, |
7f52427b | 264 | long *split_count_after); |
ab7d5fc6 | 265 | |
f0c29ed7 | 266 | /* |
14044b37 | 267 | * cds_lfht_lookup - lookup a node by key. |
0422d92c | 268 | * @ht: the hash table. |
0422d92c | 269 | * @hash: the key hash. |
6f554439 LJ |
270 | * @match: the key match function. |
271 | * @key: the current node key. | |
3a9c66db | 272 | * @iter: node, if found (output). *iter->node set to NULL if not found. |
f0c29ed7 | 273 | * |
f0c29ed7 | 274 | * Call with rcu_read_lock held. |
3df0c49c | 275 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 276 | * This function acts as a rcu_dereference() to read the node pointer. |
f0c29ed7 | 277 | */ |
b9c27904 | 278 | extern |
6f554439 | 279 | void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash, |
996ff57c | 280 | cds_lfht_match_fct match, const void *key, |
6f554439 | 281 | struct cds_lfht_iter *iter); |
ab7d5fc6 | 282 | |
f0c29ed7 | 283 | /* |
3a9c66db | 284 | * cds_lfht_next_duplicate - get the next item with same key, after iterator. |
0422d92c MD |
285 | * @ht: the hash table. |
286 | * @match: the key match function. | |
04db56f8 | 287 | * @key: the current node key. |
3a9c66db MD |
288 | * @iter: input: current iterator. |
289 | * output: node, if found. *iter->node set to NULL if not found. | |
f0c29ed7 | 290 | * |
3a9c66db MD |
291 | * Uses an iterator initialized by a lookup or traversal. Important: the |
292 | * iterator _needs_ to be initialized before calling | |
293 | * cds_lfht_next_duplicate. | |
adc0de68 MD |
294 | * Sets *iter-node to the following node with same key. |
295 | * Sets *iter->node to NULL if no following node exists with same key. | |
296 | * RCU read-side lock must be held across cds_lfht_lookup and | |
297 | * cds_lfht_next calls, and also between cds_lfht_next calls using the | |
298 | * node returned by a previous cds_lfht_next. | |
299 | * Call with rcu_read_lock held. | |
3df0c49c | 300 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 301 | * This function acts as a rcu_dereference() to read the node pointer. |
f0c29ed7 | 302 | */ |
b9c27904 | 303 | extern |
0422d92c | 304 | void cds_lfht_next_duplicate(struct cds_lfht *ht, |
996ff57c | 305 | cds_lfht_match_fct match, const void *key, |
04db56f8 | 306 | struct cds_lfht_iter *iter); |
4e9b9fbf MD |
307 | |
308 | /* | |
309 | * cds_lfht_first - get the first node in the table. | |
0422d92c MD |
310 | * @ht: the hash table. |
311 | * @iter: First node, if exists (output). *iter->node set to NULL if not found. | |
4e9b9fbf MD |
312 | * |
313 | * Output in "*iter". *iter->node set to NULL if table is empty. | |
314 | * Call with rcu_read_lock held. | |
3df0c49c | 315 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 316 | * This function acts as a rcu_dereference() to read the node pointer. |
4e9b9fbf | 317 | */ |
b9c27904 | 318 | extern |
4e9b9fbf MD |
319 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
320 | ||
321 | /* | |
322 | * cds_lfht_next - get the next node in the table. | |
0422d92c | 323 | * @ht: the hash table. |
3a9c66db MD |
324 | * @iter: input: current iterator. |
325 | * output: next node, if exists. *iter->node set to NULL if not found. | |
4e9b9fbf MD |
326 | * |
327 | * Input/Output in "*iter". *iter->node set to NULL if *iter was | |
328 | * pointing to the last table node. | |
329 | * Call with rcu_read_lock held. | |
3df0c49c | 330 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 331 | * This function acts as a rcu_dereference() to read the node pointer. |
4e9b9fbf | 332 | */ |
b9c27904 | 333 | extern |
4e9b9fbf | 334 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
ab7d5fc6 | 335 | |
18117871 | 336 | /* |
14044b37 | 337 | * cds_lfht_add - add a node to the hash table. |
0422d92c MD |
338 | * @ht: the hash table. |
339 | * @hash: the key hash. | |
340 | * @node: the node to add. | |
f0c29ed7 | 341 | * |
48ed1c18 | 342 | * This function supports adding redundant keys into the table. |
3df0c49c MD |
343 | * Call with rcu_read_lock held. |
344 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 MD |
345 | * This function issues a full memory barrier before and after its |
346 | * atomic commit. | |
f0c29ed7 | 347 | */ |
b9c27904 | 348 | extern |
0422d92c MD |
349 | void cds_lfht_add(struct cds_lfht *ht, unsigned long hash, |
350 | struct cds_lfht_node *node); | |
f0c29ed7 MD |
351 | |
352 | /* | |
14044b37 | 353 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
0422d92c | 354 | * @ht: the hash table. |
6f554439 | 355 | * @hash: the node's hash. |
0422d92c | 356 | * @match: the key match function. |
04db56f8 | 357 | * @key: the node's key. |
0422d92c | 358 | * @node: the node to try adding. |
f0c29ed7 | 359 | * |
6878c72b | 360 | * Return the node added upon success. |
860d07e8 MD |
361 | * Return the unique node already present upon failure. If |
362 | * cds_lfht_add_unique fails, the node passed as parameter should be | |
3a9c66db | 363 | * freed by the caller. In this case, the caller does NOT need to wait |
50024344 | 364 | * for a grace period before freeing or re-using the node. |
f0c29ed7 | 365 | * Call with rcu_read_lock held. |
3df0c49c | 366 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
367 | * |
368 | * The semantic of this function is that if only this function is used | |
369 | * to add keys into the table, no duplicated keys should ever be | |
370 | * observable in the table. The same guarantee apply for combination of | |
9357c415 | 371 | * add_unique and add_replace (see below). |
7b783f81 MD |
372 | * |
373 | * Upon success, this function issues a full memory barrier before and | |
374 | * after its atomic commit. Upon failure, this function acts like a | |
375 | * simple lookup operation: it acts as a rcu_dereference() to read the | |
376 | * node pointer. The failure case does not guarantee any other memory | |
377 | * barrier. | |
18117871 | 378 | */ |
b9c27904 | 379 | extern |
adc0de68 | 380 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
6f554439 | 381 | unsigned long hash, |
0422d92c | 382 | cds_lfht_match_fct match, |
996ff57c | 383 | const void *key, |
adc0de68 | 384 | struct cds_lfht_node *node); |
3eca1b8c | 385 | |
48ed1c18 | 386 | /* |
9357c415 | 387 | * cds_lfht_add_replace - replace or add a node within hash table. |
0422d92c | 388 | * @ht: the hash table. |
6f554439 | 389 | * @hash: the node's hash. |
0422d92c | 390 | * @match: the key match function. |
04db56f8 | 391 | * @key: the node's key. |
0422d92c | 392 | * @node: the node to add. |
48ed1c18 MD |
393 | * |
394 | * Return the node replaced upon success. If no node matching the key | |
395 | * was present, return NULL, which also means the operation succeeded. | |
396 | * This replacement operation should never fail. | |
397 | * Call with rcu_read_lock held. | |
3df0c49c | 398 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 399 | * After successful replacement, a grace period must be waited for before |
50024344 | 400 | * freeing or re-using the memory reserved for the returned node. |
48ed1c18 | 401 | * |
3a9c66db MD |
402 | * The semantic of replacement vs lookups and traversals is the |
403 | * following: if lookups and traversals are performed between a key | |
404 | * unique insertion and its removal, we guarantee that the lookups and | |
405 | * traversals will always find exactly one instance of the key if it is | |
406 | * replaced concurrently with the lookups. | |
48ed1c18 MD |
407 | * |
408 | * Providing this semantic allows us to ensure that replacement-only | |
409 | * schemes will never generate duplicated keys. It also allows us to | |
9357c415 | 410 | * guarantee that a combination of add_replace and add_unique updates |
48ed1c18 | 411 | * will never generate duplicated keys. |
7b783f81 MD |
412 | * |
413 | * This function issues a full memory barrier before and after its | |
414 | * atomic commit. | |
48ed1c18 | 415 | */ |
b9c27904 | 416 | extern |
9357c415 | 417 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
6f554439 | 418 | unsigned long hash, |
0422d92c | 419 | cds_lfht_match_fct match, |
996ff57c | 420 | const void *key, |
adc0de68 | 421 | struct cds_lfht_node *node); |
48ed1c18 | 422 | |
f0c29ed7 | 423 | /* |
929ad508 | 424 | * cds_lfht_replace - replace a node pointed to by iter within hash table. |
0422d92c MD |
425 | * @ht: the hash table. |
426 | * @old_iter: the iterator position of the node to replace. | |
2e79c445 MD |
427 | * @hash: the node's hash. |
428 | * @match: the key match function. | |
429 | * @key: the node's key. | |
430 | * @new_node: the new node to use as replacement. | |
f0c29ed7 | 431 | * |
9357c415 | 432 | * Return 0 if replacement is successful, negative value otherwise. |
2e79c445 MD |
433 | * Replacing a NULL old node or an already removed node will fail with |
434 | * -ENOENT. | |
435 | * If the hash or value of the node to replace and the new node differ, | |
436 | * this function returns -EINVAL without proceeding to the replacement. | |
9357c415 MD |
437 | * Old node can be looked up with cds_lfht_lookup and cds_lfht_next. |
438 | * RCU read-side lock must be held between lookup and replacement. | |
439 | * Call with rcu_read_lock held. | |
3df0c49c | 440 | * Threads calling this API need to be registered RCU read-side threads. |
9357c415 | 441 | * After successful replacement, a grace period must be waited for before |
50024344 MD |
442 | * freeing or re-using the memory reserved for the old node (which can |
443 | * be accessed with cds_lfht_iter_get_node). | |
9357c415 | 444 | * |
3a9c66db MD |
445 | * The semantic of replacement vs lookups is the same as |
446 | * cds_lfht_add_replace(). | |
7b783f81 MD |
447 | * |
448 | * Upon success, this function issues a full memory barrier before and | |
449 | * after its atomic commit. Upon failure, this function does not issue | |
450 | * any memory barrier. | |
9357c415 | 451 | */ |
b9c27904 | 452 | extern |
2e79c445 MD |
453 | int cds_lfht_replace(struct cds_lfht *ht, |
454 | struct cds_lfht_iter *old_iter, | |
455 | unsigned long hash, | |
456 | cds_lfht_match_fct match, | |
457 | const void *key, | |
9357c415 MD |
458 | struct cds_lfht_node *new_node); |
459 | ||
460 | /* | |
461 | * cds_lfht_del - remove node pointed to by iterator from hash table. | |
0422d92c | 462 | * @ht: the hash table. |
bc8c3c74 | 463 | * @node: the node to delete. |
9357c415 MD |
464 | * |
465 | * Return 0 if the node is successfully removed, negative value | |
466 | * otherwise. | |
bc8c3c74 | 467 | * Deleting a NULL node or an already removed node will fail with a |
9357c415 | 468 | * negative value. |
bc8c3c74 MD |
469 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next, |
470 | * followed by use of cds_lfht_iter_get_node. | |
9357c415 | 471 | * RCU read-side lock must be held between lookup and removal. |
f0c29ed7 | 472 | * Call with rcu_read_lock held. |
3df0c49c | 473 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 474 | * After successful removal, a grace period must be waited for before |
50024344 MD |
475 | * freeing or re-using the memory reserved for old node (which can be |
476 | * accessed with cds_lfht_iter_get_node). | |
7b783f81 MD |
477 | * Upon success, this function issues a full memory barrier before and |
478 | * after its atomic commit. Upon failure, this function does not issue | |
479 | * any memory barrier. | |
f0c29ed7 | 480 | */ |
b9c27904 | 481 | extern |
bc8c3c74 | 482 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node); |
ab7d5fc6 | 483 | |
df55172a | 484 | /* |
3a9c66db | 485 | * cds_lfht_is_node_deleted - query whether a node is removed from hash table. |
df55172a MD |
486 | * |
487 | * Return non-zero if the node is deleted from the hash table, 0 | |
488 | * otherwise. | |
489 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next, | |
490 | * followed by use of cds_lfht_iter_get_node. | |
491 | * RCU read-side lock must be held between lookup and call to this | |
492 | * function. | |
493 | * Call with rcu_read_lock held. | |
494 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 | 495 | * This function does not issue any memory barrier. |
df55172a | 496 | */ |
b9c27904 | 497 | extern |
afa5940d | 498 | int cds_lfht_is_node_deleted(const struct cds_lfht_node *node); |
df55172a | 499 | |
f0c29ed7 | 500 | /* |
14044b37 | 501 | * cds_lfht_resize - Force a hash table resize |
0422d92c | 502 | * @ht: the hash table. |
1475579c | 503 | * @new_size: update to this hash table size. |
3df0c49c MD |
504 | * |
505 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 | 506 | * This function does not (necessarily) issue memory barriers. |
43387742 MD |
507 | * cds_lfht_resize should *not* be called from a RCU read-side critical |
508 | * section. | |
f0c29ed7 | 509 | */ |
b9c27904 | 510 | extern |
1475579c | 511 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 512 | |
6d320126 | 513 | /* |
3a9c66db MD |
514 | * Note: it is safe to perform element removal (del), replacement, or |
515 | * any hash table update operation during any of the following hash | |
516 | * table traversals. | |
7b783f81 | 517 | * These functions act as rcu_dereference() to read the node pointers. |
6d320126 MD |
518 | */ |
519 | #define cds_lfht_for_each(ht, iter, node) \ | |
520 | for (cds_lfht_first(ht, iter), \ | |
521 | node = cds_lfht_iter_get_node(iter); \ | |
522 | node != NULL; \ | |
523 | cds_lfht_next(ht, iter), \ | |
524 | node = cds_lfht_iter_get_node(iter)) | |
525 | ||
6f554439 LJ |
526 | #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \ |
527 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ | |
6d320126 MD |
528 | node = cds_lfht_iter_get_node(iter); \ |
529 | node != NULL; \ | |
530 | cds_lfht_next_duplicate(ht, match, key, iter), \ | |
531 | node = cds_lfht_iter_get_node(iter)) | |
532 | ||
e58cdfcf | 533 | #define cds_lfht_entry(ptr, type, member) \ |
0ef4782c | 534 | caa_container_of_check_null(ptr, type, member) |
e58cdfcf | 535 | |
6d320126 MD |
536 | #define cds_lfht_for_each_entry(ht, iter, pos, member) \ |
537 | for (cds_lfht_first(ht, iter), \ | |
e58cdfcf MD |
538 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
539 | __typeof__(*(pos)), member); \ | |
540 | pos != NULL; \ | |
6d320126 | 541 | cds_lfht_next(ht, iter), \ |
e58cdfcf MD |
542 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
543 | __typeof__(*(pos)), member)) | |
6d320126 | 544 | |
6f554439 | 545 | #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \ |
6d320126 | 546 | iter, pos, member) \ |
6f554439 | 547 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ |
e58cdfcf MD |
548 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
549 | __typeof__(*(pos)), member); \ | |
550 | pos != NULL; \ | |
6d320126 | 551 | cds_lfht_next_duplicate(ht, match, key, iter), \ |
e58cdfcf MD |
552 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
553 | __typeof__(*(pos)), member)) | |
6d320126 | 554 | |
01389791 MD |
555 | #ifdef __cplusplus |
556 | } | |
557 | #endif | |
558 | ||
a42cc659 | 559 | #endif /* _URCU_RCULFHASH_H */ |