Fix ust consumer channel memory non-zeroed allocation
[lttng-tools.git] / 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>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Include this file _after_ including your URCU flavor.
26 */
27
28#include <stdint.h>
29#include <urcu-call-rcu.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/*
36 * struct cds_lfht_node and struct _cds_lfht_node should be aligned on
37 * 4-bytes boundaries because the two lower bits are used as flags.
38 */
39
d6b18934
DG
40/*
41 * _cds_lfht_node: Contains the internal pointers and reverse-hash
42 * value required for lookup and traversal of the hash table.
43 */
fa68aa62
MD
44struct _cds_lfht_node {
45 struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
46 unsigned long reverse_hash;
47} __attribute__((aligned(4)));
48
f6a9efaa 49/*
d6b18934
DG
50 * cds_lfht_node: Contains the full key and length required to check for
51 * an actual match, and also contains an rcu_head structure that is used
52 * by RCU to track a node through a given RCU grace period. There is an
53 * instance of _cds_lfht_node enclosed as a field within each
54 * _cds_lfht_node structure.
55 *
f6a9efaa
DG
56 * struct cds_lfht_node can be embedded into a structure (as a field).
57 * caa_container_of() can be used to get the structure from the struct
58 * cds_lfht_node after a lookup.
59 */
fa68aa62
MD
60struct cds_lfht_node {
61 /* cache-hot for iteration */
62 struct _cds_lfht_node p; /* needs to be first field */
63 void *key;
64 unsigned int key_len;
65 /* cache-cold for iteration */
66 struct rcu_head head;
67};
68
d6b18934 69/* cds_lfht_iter: Used to track state while traversing a hash chain. */
fa68aa62
MD
70struct cds_lfht_iter {
71 struct cds_lfht_node *node, *next;
72};
73
74static inline
75struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
76{
77 return iter->node;
78}
79
80struct cds_lfht;
81
82/*
83 * Caution !
84 * Ensure reader and writer threads are registered as urcu readers.
85 */
86
87typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
88 unsigned long seed);
89typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len,
90 void *key2, size_t key2_len);
91
92/*
93 * cds_lfht_node_init - initialize a hash table node
94 */
95static inline
96void cds_lfht_node_init(struct cds_lfht_node *node, void *key,
97 size_t key_len)
98{
99 node->key = key;
100 node->key_len = key_len;
101}
102
103/*
104 * Hash table creation flags.
105 */
106enum {
107 CDS_LFHT_AUTO_RESIZE = (1U << 0),
d6b18934 108 CDS_LFHT_ACCOUNTING = (1U << 1),
fa68aa62
MD
109};
110
111/*
112 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
113 */
114struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct,
115 cds_lfht_compare_fct compare_fct,
116 unsigned long hash_seed,
117 unsigned long init_size,
d6b18934 118 unsigned long min_alloc_size,
fa68aa62
MD
119 int flags,
120 void (*cds_lfht_call_rcu)(struct rcu_head *head,
121 void (*func)(struct rcu_head *head)),
122 void (*cds_lfht_synchronize_rcu)(void),
123 void (*cds_lfht_rcu_read_lock)(void),
124 void (*cds_lfht_rcu_read_unlock)(void),
125 void (*cds_lfht_rcu_thread_offline)(void),
126 void (*cds_lfht_rcu_thread_online)(void),
127 void (*cds_lfht_rcu_register_thread)(void),
128 void (*cds_lfht_rcu_unregister_thread)(void),
129 pthread_attr_t *attr);
130
131/*
132 * cds_lfht_new - allocate a hash table.
133 * @hash_fct: the hashing function.
134 * @compare_fct: the key comparison function.
135 * @hash_seed: the seed for hash function.
136 * @init_size: number of nodes to allocate initially. Must be power of two.
d6b18934 137 * @min_alloc_size: the smallest allocation size to use. 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.
141 * @attr: optional resize worker thread attributes. NULL for default.
142 *
143 * Return NULL on error.
144 * Note: the RCU flavor must be already included before the hash table header.
145 *
146 * The programmer is responsible for ensuring that resize operation has a
147 * priority equal to hash table updater threads. It should be performed by
148 * specifying the appropriate priority in the pthread "attr" argument, and,
149 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
150 * this priority level. Having lower priority for call_rcu and resize threads
151 * does not pose any correctness issue, but the resize operations could be
152 * starved by updates, thus leading to long hash table bucket chains.
d6b18934 153 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62
MD
154 */
155static inline
156struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
157 cds_lfht_compare_fct compare_fct,
158 unsigned long hash_seed,
159 unsigned long init_size,
d6b18934 160 unsigned long min_alloc_size,
fa68aa62
MD
161 int flags,
162 pthread_attr_t *attr)
163{
164 return _cds_lfht_new(hash_fct, compare_fct, hash_seed,
d6b18934 165 init_size, min_alloc_size, flags,
fa68aa62
MD
166 call_rcu, synchronize_rcu, rcu_read_lock,
167 rcu_read_unlock, rcu_thread_offline,
168 rcu_thread_online, rcu_register_thread,
169 rcu_unregister_thread, attr);
170}
171
172/*
173 * cds_lfht_destroy - destroy a hash table.
174 * @ht: the hash table to destroy.
175 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
176 * The caller will typically want to free this pointer if dynamically
d6b18934
DG
177 * allocated. The attr point can be NULL if the caller does not
178 * need to be informed of the value passed to cds_lfht_new().
fa68aa62
MD
179 *
180 * Return 0 on success, negative error value on error.
d6b18934 181 * Threads calling this API need to be registered RCU read-side threads.
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
DG
187 * @ht: the hash table.
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 * @removed: Number of logically removed nodes observed during traversal.
191 * @split_count_after: Sample the node count split-counter after traversal.
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
MD
197 unsigned long *count,
198 unsigned long *removed,
d6b18934 199 long *split_count_after);
fa68aa62
MD
200
201/*
202 * cds_lfht_lookup - lookup a node by key.
203 *
204 * Output in "*iter". *iter->node set to NULL if not found.
205 * Call with rcu_read_lock held.
d6b18934 206 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62
MD
207 */
208void cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len,
209 struct cds_lfht_iter *iter);
210
211/*
f6a9efaa 212 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
fa68aa62
MD
213 *
214 * Uses an iterator initialized by a lookup.
215 * Sets *iter-node to the following node with same key.
216 * Sets *iter->node to NULL if no following node exists with same key.
217 * RCU read-side lock must be held across cds_lfht_lookup and
218 * cds_lfht_next calls, and also between cds_lfht_next calls using the
219 * node returned by a previous cds_lfht_next.
220 * Call with rcu_read_lock held.
d6b18934 221 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62 222 */
f6a9efaa
DG
223void cds_lfht_next_duplicate(struct cds_lfht *ht, struct cds_lfht_iter *iter);
224
225/*
226 * cds_lfht_first - get the first node in the table.
227 *
228 * Output in "*iter". *iter->node set to NULL if table is empty.
229 * Call with rcu_read_lock held.
d6b18934 230 * Threads calling this API need to be registered RCU read-side threads.
f6a9efaa
DG
231 */
232void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter);
233
234/*
235 * cds_lfht_next - get the next node in the table.
236 *
237 * Input/Output in "*iter". *iter->node set to NULL if *iter was
238 * pointing to the last table node.
239 * Call with rcu_read_lock held.
d6b18934 240 * Threads calling this API need to be registered RCU read-side threads.
f6a9efaa 241 */
fa68aa62
MD
242void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
243
244/*
245 * cds_lfht_add - add a node to the hash table.
246 *
fa68aa62 247 * This function supports adding redundant keys into the table.
d6b18934
DG
248 * Call with rcu_read_lock held.
249 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62
MD
250 */
251void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node);
252
253/*
254 * cds_lfht_add_unique - add a node to hash table, if key is not present.
255 *
256 * Return the node added upon success.
257 * Return the unique node already present upon failure. If
258 * cds_lfht_add_unique fails, the node passed as parameter should be
259 * freed by the caller.
260 * Call with rcu_read_lock held.
d6b18934 261 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62
MD
262 *
263 * The semantic of this function is that if only this function is used
264 * to add keys into the table, no duplicated keys should ever be
265 * observable in the table. The same guarantee apply for combination of
266 * add_unique and add_replace (see below).
267 */
268struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
269 struct cds_lfht_node *node);
270
271/*
272 * cds_lfht_add_replace - replace or add a node within hash table.
273 *
274 * Return the node replaced upon success. If no node matching the key
275 * was present, return NULL, which also means the operation succeeded.
276 * This replacement operation should never fail.
277 * Call with rcu_read_lock held.
d6b18934 278 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62
MD
279 * After successful replacement, a grace period must be waited for before
280 * freeing the memory reserved for the returned node.
281 *
282 * The semantic of replacement vs lookups is the following: if lookups
283 * are performed between a key unique insertion and its removal, we
284 * guarantee that the lookups and get next will always find exactly one
285 * instance of the key if it is replaced concurrently with the lookups.
286 *
287 * Providing this semantic allows us to ensure that replacement-only
288 * schemes will never generate duplicated keys. It also allows us to
289 * guarantee that a combination of add_replace and add_unique updates
290 * will never generate duplicated keys.
291 */
292struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
293 struct cds_lfht_node *node);
294
295/*
296 * cds_lfht_replace - replace a node pointer to by iter within hash table.
297 *
298 * Return 0 if replacement is successful, negative value otherwise.
299 * Replacing a NULL old node or an already removed node will fail with a
300 * negative value.
301 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
302 * RCU read-side lock must be held between lookup and replacement.
303 * Call with rcu_read_lock held.
d6b18934 304 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62
MD
305 * After successful replacement, a grace period must be waited for before
306 * freeing the memory reserved for the old node (which can be accessed
307 * with cds_lfht_iter_get_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 */
319int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter,
320 struct cds_lfht_node *new_node);
321
322/*
323 * cds_lfht_del - remove node pointed to by iterator from hash table.
324 *
325 * Return 0 if the node is successfully removed, negative value
326 * otherwise.
327 * Replacing a NULL node or an already removed node will fail with a
328 * negative value.
329 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
330 * cds_lfht_iter_get_node.
331 * RCU read-side lock must be held between lookup and removal.
332 * Call with rcu_read_lock held.
d6b18934 333 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62
MD
334 * After successful removal, a grace period must be waited for before
335 * freeing the memory reserved for old node (which can be accessed with
336 * cds_lfht_iter_get_node).
337 */
338int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter);
339
340/*
341 * cds_lfht_resize - Force a hash table resize
342 * @new_size: update to this hash table size.
d6b18934
DG
343 *
344 * Threads calling this API need to be registered RCU read-side threads.
fa68aa62
MD
345 */
346void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
347
348#ifdef __cplusplus
349}
350#endif
351
352#endif /* _URCU_RCULFHASH_H */
This page took 0.059179 seconds and 4 git commands to generate.