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