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