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