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