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