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