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 * Include this file _after_ including your URCU flavor.
30 #include <urcu/compiler.h>
31 #include <urcu-call-rcu.h>
32 #include <urcu-flavor.h>
39 * cds_lfht_node: Contains the next pointers and reverse-hash
40 * value required for lookup and traversal of the hash table.
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
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.
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.
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)));
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
;
71 struct cds_lfht_node
*cds_lfht_iter_get_node(struct cds_lfht_iter
*iter
)
80 * Ensure reader and writer threads are registered as urcu readers.
83 typedef int (*cds_lfht_match_fct
)(struct cds_lfht_node
*node
, const void *key
);
86 * cds_lfht_node_init - initialize a hash table node
87 * @node: the node to initialize.
89 * This function is kept to be eventually used for debugging purposes
90 * (detection of memory corruption).
93 void cds_lfht_node_init(struct cds_lfht_node
*node
)
98 * Hash table creation flags.
101 CDS_LFHT_AUTO_RESIZE
= (1U << 0),
102 CDS_LFHT_ACCOUNTING
= (1U << 1),
105 struct cds_lfht_mm_type
{
106 struct cds_lfht
*(*alloc_cds_lfht
)(unsigned long min_nr_alloc_buckets
,
107 unsigned long max_nr_buckets
);
108 void (*alloc_bucket_table
)(struct cds_lfht
*ht
, unsigned long order
);
109 void (*free_bucket_table
)(struct cds_lfht
*ht
, unsigned long order
);
110 struct cds_lfht_node
*(*bucket_at
)(struct cds_lfht
*ht
,
111 unsigned long index
);
114 extern const struct cds_lfht_mm_type cds_lfht_mm_order
;
115 extern const struct cds_lfht_mm_type cds_lfht_mm_chunk
;
116 extern const struct cds_lfht_mm_type cds_lfht_mm_mmap
;
119 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
122 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
123 unsigned long min_nr_alloc_buckets
,
124 unsigned long max_nr_buckets
,
126 const struct cds_lfht_mm_type
*mm
,
127 const struct rcu_flavor_struct
*flavor
,
128 pthread_attr_t
*attr
);
131 * cds_lfht_new - allocate a hash table.
132 * @init_size: number of buckets to allocate initially. Must be power of two.
133 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
134 * (must be power of two)
135 * @max_nr_buckets: the maximum number of hash table buckets allowed.
136 * (must be power of two, 0 is accepted, means
138 * @flags: hash table creation flags (can be combined with bitwise or: '|').
140 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
141 * CDS_LFHT_ACCOUNTING: count the number of node addition
142 * and removal in the table
143 * @attr: optional resize worker thread attributes. NULL for default.
145 * Return NULL on error.
146 * Note: the RCU flavor must be already included before the hash table header.
148 * The programmer is responsible for ensuring that resize operation has a
149 * priority equal to hash table updater threads. It should be performed by
150 * specifying the appropriate priority in the pthread "attr" argument, and,
151 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
152 * this priority level. Having lower priority for call_rcu and resize threads
153 * does not pose any correctness issue, but the resize operations could be
154 * starved by updates, thus leading to long hash table bucket chains.
155 * Threads calling cds_lfht_new are NOT required to be registered RCU
156 * read-side threads. It can be called very early. (e.g. before RCU is
160 struct cds_lfht
*cds_lfht_new(unsigned long init_size
,
161 unsigned long min_nr_alloc_buckets
,
162 unsigned long max_nr_buckets
,
164 pthread_attr_t
*attr
)
166 return _cds_lfht_new(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
167 flags
, NULL
, &rcu_flavor
, attr
);
171 * cds_lfht_destroy - destroy a hash table.
172 * @ht: the hash table to destroy.
173 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
174 * The caller will typically want to free this pointer if dynamically
175 * allocated. The attr point can be NULL if the caller does not
176 * need to be informed of the value passed to cds_lfht_new().
178 * Return 0 on success, negative error value on error.
180 * Prior to liburcu 0.10:
181 * - Threads calling this API need to be registered RCU read-side
183 * - cds_lfht_destroy should *not* be called from a RCU read-side
184 * critical section. It should *not* be called from a call_rcu thread
187 * Starting from liburcu 0.10, rculfhash implements its own worker
188 * thread to handle resize operations, which removes RCU requirements on
192 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
);
195 * cds_lfht_count_nodes - count the number of nodes in the hash table.
196 * @ht: the hash table.
197 * @split_count_before: sample the node count split-counter before traversal.
198 * @count: traverse the hash table, count the number of nodes observed.
199 * @split_count_after: sample the node count split-counter after traversal.
201 * Call with rcu_read_lock held.
202 * Threads calling this API need to be registered RCU read-side threads.
205 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
206 long *split_count_before
,
207 unsigned long *count
,
208 long *split_count_after
);
211 * cds_lfht_lookup - lookup a node by key.
212 * @ht: the hash table.
213 * @hash: the key hash.
214 * @match: the key match function.
215 * @key: the current node key.
216 * @iter: node, if found (output). *iter->node set to NULL if not found.
218 * Call with rcu_read_lock held.
219 * Threads calling this API need to be registered RCU read-side threads.
220 * This function acts as a rcu_dereference() to read the node pointer.
223 void cds_lfht_lookup(struct cds_lfht
*ht
, unsigned long hash
,
224 cds_lfht_match_fct match
, const void *key
,
225 struct cds_lfht_iter
*iter
);
228 * cds_lfht_next_duplicate - get the next item with same key, after iterator.
229 * @ht: the hash table.
230 * @match: the key match function.
231 * @key: the current node key.
232 * @iter: input: current iterator.
233 * output: node, if found. *iter->node set to NULL if not found.
235 * Uses an iterator initialized by a lookup or traversal. Important: the
236 * iterator _needs_ to be initialized before calling
237 * cds_lfht_next_duplicate.
238 * Sets *iter-node to the following node with same key.
239 * Sets *iter->node to NULL if no following node exists with same key.
240 * RCU read-side lock must be held across cds_lfht_lookup and
241 * cds_lfht_next calls, and also between cds_lfht_next calls using the
242 * node returned by a previous cds_lfht_next.
243 * Call with rcu_read_lock held.
244 * Threads calling this API need to be registered RCU read-side threads.
245 * This function acts as a rcu_dereference() to read the node pointer.
248 void cds_lfht_next_duplicate(struct cds_lfht
*ht
,
249 cds_lfht_match_fct match
, const void *key
,
250 struct cds_lfht_iter
*iter
);
253 * cds_lfht_first - get the first node in the table.
254 * @ht: the hash table.
255 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
257 * Output in "*iter". *iter->node set to NULL if table is empty.
258 * Call with rcu_read_lock held.
259 * Threads calling this API need to be registered RCU read-side threads.
260 * This function acts as a rcu_dereference() to read the node pointer.
263 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
266 * cds_lfht_next - get the next node in the table.
267 * @ht: the hash table.
268 * @iter: input: current iterator.
269 * output: next node, if exists. *iter->node set to NULL if not found.
271 * Input/Output in "*iter". *iter->node set to NULL if *iter was
272 * pointing to the last table node.
273 * Call with rcu_read_lock held.
274 * Threads calling this API need to be registered RCU read-side threads.
275 * This function acts as a rcu_dereference() to read the node pointer.
278 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
281 * cds_lfht_add - add a node to the hash table.
282 * @ht: the hash table.
283 * @hash: the key hash.
284 * @node: the node to add.
286 * This function supports adding redundant keys into the table.
287 * Call with rcu_read_lock held.
288 * Threads calling this API need to be registered RCU read-side threads.
289 * This function issues a full memory barrier before and after its
293 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
294 struct cds_lfht_node
*node
);
297 * cds_lfht_add_unique - add a node to hash table, if key is not present.
298 * @ht: the hash table.
299 * @hash: the node's hash.
300 * @match: the key match function.
301 * @key: the node's key.
302 * @node: the node to try adding.
304 * Return the node added upon success.
305 * Return the unique node already present upon failure. If
306 * cds_lfht_add_unique fails, the node passed as parameter should be
307 * freed by the caller. In this case, the caller does NOT need to wait
308 * for a grace period before freeing or re-using the node.
309 * Call with rcu_read_lock held.
310 * Threads calling this API need to be registered RCU read-side threads.
312 * The semantic of this function is that if only this function is used
313 * to add keys into the table, no duplicated keys should ever be
314 * observable in the table. The same guarantee apply for combination of
315 * add_unique and add_replace (see below).
317 * Upon success, this function issues a full memory barrier before and
318 * after its atomic commit. Upon failure, this function acts like a
319 * simple lookup operation: it acts as a rcu_dereference() to read the
320 * node pointer. The failure case does not guarantee any other memory
324 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
326 cds_lfht_match_fct match
,
328 struct cds_lfht_node
*node
);
331 * cds_lfht_add_replace - replace or add a node within hash table.
332 * @ht: the hash table.
333 * @hash: the node's hash.
334 * @match: the key match function.
335 * @key: the node's key.
336 * @node: the node to add.
338 * Return the node replaced upon success. If no node matching the key
339 * was present, return NULL, which also means the operation succeeded.
340 * This replacement operation should never fail.
341 * Call with rcu_read_lock held.
342 * Threads calling this API need to be registered RCU read-side threads.
343 * After successful replacement, a grace period must be waited for before
344 * freeing or re-using the memory reserved for the returned node.
346 * The semantic of replacement vs lookups and traversals is the
347 * following: if lookups and traversals are performed between a key
348 * unique insertion and its removal, we guarantee that the lookups and
349 * traversals will always find exactly one instance of the key if it is
350 * replaced concurrently with the lookups.
352 * Providing this semantic allows us to ensure that replacement-only
353 * schemes will never generate duplicated keys. It also allows us to
354 * guarantee that a combination of add_replace and add_unique updates
355 * will never generate duplicated keys.
357 * This function issues a full memory barrier before and after its
361 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
363 cds_lfht_match_fct match
,
365 struct cds_lfht_node
*node
);
368 * cds_lfht_replace - replace a node pointed to by iter within hash table.
369 * @ht: the hash table.
370 * @old_iter: the iterator position of the node to replace.
371 * @hash: the node's hash.
372 * @match: the key match function.
373 * @key: the node's key.
374 * @new_node: the new node to use as replacement.
376 * Return 0 if replacement is successful, negative value otherwise.
377 * Replacing a NULL old node or an already removed node will fail with
379 * If the hash or value of the node to replace and the new node differ,
380 * this function returns -EINVAL without proceeding to the replacement.
381 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
382 * RCU read-side lock must be held between lookup and replacement.
383 * Call with rcu_read_lock held.
384 * Threads calling this API need to be registered RCU read-side threads.
385 * After successful replacement, a grace period must be waited for before
386 * freeing or re-using the memory reserved for the old node (which can
387 * be accessed with cds_lfht_iter_get_node).
389 * The semantic of replacement vs lookups is the same as
390 * cds_lfht_add_replace().
392 * Upon success, this function issues a full memory barrier before and
393 * after its atomic commit. Upon failure, this function does not issue
394 * any memory barrier.
397 int cds_lfht_replace(struct cds_lfht
*ht
,
398 struct cds_lfht_iter
*old_iter
,
400 cds_lfht_match_fct match
,
402 struct cds_lfht_node
*new_node
);
405 * cds_lfht_del - remove node pointed to by iterator from hash table.
406 * @ht: the hash table.
407 * @node: the node to delete.
409 * Return 0 if the node is successfully removed, negative value
411 * Deleting a NULL node or an already removed node will fail with a
413 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
414 * followed by use of cds_lfht_iter_get_node.
415 * RCU read-side lock must be held between lookup and removal.
416 * Call with rcu_read_lock held.
417 * Threads calling this API need to be registered RCU read-side threads.
418 * After successful removal, a grace period must be waited for before
419 * freeing or re-using the memory reserved for old node (which can be
420 * accessed with cds_lfht_iter_get_node).
421 * Upon success, this function issues a full memory barrier before and
422 * after its atomic commit. Upon failure, this function does not issue
423 * any memory barrier.
426 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
429 * cds_lfht_is_node_deleted - query whether a node is removed from hash table.
431 * Return non-zero if the node is deleted from the hash table, 0
433 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
434 * followed by use of cds_lfht_iter_get_node.
435 * RCU read-side lock must be held between lookup and call to this
437 * Call with rcu_read_lock held.
438 * Threads calling this API need to be registered RCU read-side threads.
439 * This function does not issue any memory barrier.
442 int cds_lfht_is_node_deleted(struct cds_lfht_node
*node
);
445 * cds_lfht_resize - Force a hash table resize
446 * @ht: the hash table.
447 * @new_size: update to this hash table size.
449 * Threads calling this API need to be registered RCU read-side threads.
450 * This function does not (necessarily) issue memory barriers.
451 * cds_lfht_resize should *not* be called from a RCU read-side critical
455 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
);
458 * Note: it is safe to perform element removal (del), replacement, or
459 * any hash table update operation during any of the following hash
461 * These functions act as rcu_dereference() to read the node pointers.
463 #define cds_lfht_for_each(ht, iter, node) \
464 for (cds_lfht_first(ht, iter), \
465 node = cds_lfht_iter_get_node(iter); \
467 cds_lfht_next(ht, iter), \
468 node = cds_lfht_iter_get_node(iter))
470 #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
471 for (cds_lfht_lookup(ht, hash, match, key, iter), \
472 node = cds_lfht_iter_get_node(iter); \
474 cds_lfht_next_duplicate(ht, match, key, iter), \
475 node = cds_lfht_iter_get_node(iter))
477 #define cds_lfht_for_each_entry(ht, iter, pos, member) \
478 for (cds_lfht_first(ht, iter), \
479 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
480 __typeof__(*(pos)), member); \
481 cds_lfht_iter_get_node(iter) != NULL; \
482 cds_lfht_next(ht, iter), \
483 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
484 __typeof__(*(pos)), member))
486 #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
488 for (cds_lfht_lookup(ht, hash, match, key, iter), \
489 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
490 __typeof__(*(pos)), member); \
491 cds_lfht_iter_get_node(iter) != NULL; \
492 cds_lfht_next_duplicate(ht, match, key, iter), \
493 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
494 __typeof__(*(pos)), member))
500 #endif /* _URCU_RCULFHASH_H */