1 #ifndef _URCU_RCULFHASH_H
2 #define _URCU_RCULFHASH_H
7 * Userspace RCU library - Lock-Free RCU Hash Table
9 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
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.
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.
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
26 * For use with URCU_API_MAP (API mapping of liburcu), include this file
27 * _after_ including your URCU flavor.
30 #include <urcu/config.h>
33 #include <urcu/compiler.h>
42 * cds_lfht_node: Contains the next pointers and reverse-hash
43 * value required for lookup and traversal of the hash table.
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
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.
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.
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)));
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
;
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.
80 #ifdef CONFIG_CDS_LFHT_ITER_DEBUG
81 struct cds_lfht
*lfht
;
86 struct cds_lfht_node
*cds_lfht_iter_get_node(struct cds_lfht_iter
*iter
)
91 struct rcu_flavor_struct
;
95 * Ensure reader and writer threads are registered as urcu readers.
98 typedef int (*cds_lfht_match_fct
)(struct cds_lfht_node
*node
, const void *key
);
101 * cds_lfht_node_init - initialize a hash table node
102 * @node: the node to initialize.
104 * This function is kept to be eventually used for debugging purposes
105 * (detection of memory corruption).
108 void cds_lfht_node_init(struct cds_lfht_node
*node
__attribute__((unused
)))
113 * cds_lfht_node_init_deleted - initialize a hash table node to "removed" state
114 * @node: the node to initialize.
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.
120 void cds_lfht_node_init_deleted(struct cds_lfht_node
*node
);
123 * Hash table creation flags.
126 CDS_LFHT_AUTO_RESIZE
= (1U << 0),
127 CDS_LFHT_ACCOUNTING
= (1U << 1),
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
);
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
;
144 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
147 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
148 unsigned long min_nr_alloc_buckets
,
149 unsigned long max_nr_buckets
,
151 const struct cds_lfht_mm_type
*mm
,
152 const struct rcu_flavor_struct
*flavor
,
153 pthread_attr_t
*attr
);
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
163 * @flavor: flavor of liburcu to use to synchronize the hash table
164 * @flags: hash table creation flags (can be combined with bitwise or: '|').
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.
171 * Return NULL on error.
172 * Note: the RCU flavor must be already included before the hash table header.
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
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
,
190 const struct rcu_flavor_struct
*flavor
,
191 pthread_attr_t
*attr
)
193 return _cds_lfht_new(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
194 flags
, NULL
, flavor
, attr
);
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
207 * @flags: hash table creation flags (can be combined with bitwise or: '|').
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.
214 * Return NULL on error.
215 * Note: the RCU flavor must be already included before the hash table header.
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
229 struct cds_lfht
*cds_lfht_new(unsigned long init_size
,
230 unsigned long min_nr_alloc_buckets
,
231 unsigned long max_nr_buckets
,
233 pthread_attr_t
*attr
)
235 return _cds_lfht_new(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
236 flags
, NULL
, &rcu_flavor
, attr
);
238 #endif /* URCU_API_MAP */
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().
248 * Return 0 on success, negative error value on error.
250 * Threads calling this API need to be registered RCU read-side threads.
252 * Prior to liburcu 0.10:
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
257 * Starting from liburcu 0.10, rculfhash implements its own worker
258 * thread to handle resize operations, which removes the above RCU
259 * read-side critical section requirement on cds_lfht_destroy.
262 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
);
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.
271 * Call with rcu_read_lock held.
272 * Threads calling this API need to be registered RCU read-side threads.
275 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
276 long *split_count_before
,
277 unsigned long *count
,
278 long *split_count_after
);
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.
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.
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
);
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.
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.
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
);
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.
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.
333 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
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.
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.
348 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
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.
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
363 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
364 struct cds_lfht_node
*node
);
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.
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.
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).
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
394 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
396 cds_lfht_match_fct match
,
398 struct cds_lfht_node
*node
);
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.
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.
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.
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.
427 * This function issues a full memory barrier before and after its
431 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
433 cds_lfht_match_fct match
,
435 struct cds_lfht_node
*node
);
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.
446 * Return 0 if replacement is successful, negative value otherwise.
447 * Replacing a NULL old node or an already removed node will fail with
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).
459 * The semantic of replacement vs lookups is the same as
460 * cds_lfht_add_replace().
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.
467 int cds_lfht_replace(struct cds_lfht
*ht
,
468 struct cds_lfht_iter
*old_iter
,
470 cds_lfht_match_fct match
,
472 struct cds_lfht_node
*new_node
);
475 * cds_lfht_del - remove node pointed to by iterator from hash table.
476 * @ht: the hash table.
477 * @node: the node to delete.
479 * Return 0 if the node is successfully removed, negative value
481 * Deleting a NULL node or an already removed node will fail with a
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.
496 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
499 * cds_lfht_is_node_deleted - query whether a node is removed from hash table.
501 * Return non-zero if the node is deleted from the hash table, 0
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
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.
512 int cds_lfht_is_node_deleted(const struct cds_lfht_node
*node
);
515 * cds_lfht_resize - Force a hash table resize
516 * @ht: the hash table.
517 * @new_size: update to this hash table size.
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
525 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
);
528 * Note: it is safe to perform element removal (del), replacement, or
529 * any hash table update operation during any of the following hash
531 * These functions act as rcu_dereference() to read the node pointers.
533 #define cds_lfht_for_each(ht, iter, node) \
534 for (cds_lfht_first(ht, iter), \
535 node = cds_lfht_iter_get_node(iter); \
537 cds_lfht_next(ht, iter), \
538 node = cds_lfht_iter_get_node(iter))
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); \
544 cds_lfht_next_duplicate(ht, match, key, iter), \
545 node = cds_lfht_iter_get_node(iter))
547 #define cds_lfht_entry(ptr, type, member) \
548 caa_container_of_check_null(ptr, type, member)
550 #define cds_lfht_for_each_entry(ht, iter, pos, member) \
551 for (cds_lfht_first(ht, iter), \
552 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
553 __typeof__(*(pos)), member); \
555 cds_lfht_next(ht, iter), \
556 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
557 __typeof__(*(pos)), member))
559 #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
561 for (cds_lfht_lookup(ht, hash, match, key, iter), \
562 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
563 __typeof__(*(pos)), member); \
565 cds_lfht_next_duplicate(ht, match, key, iter), \
566 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
567 __typeof__(*(pos)), member))
573 #endif /* _URCU_RCULFHASH_H */