Fix: pass private data to context callbacks
[lttng-ust.git] / liblttng-ust / rculfhash.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * Copyright 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
6 *
7 * Userspace RCU library - Lock-Free RCU Hash Table
8 */
9
10 #ifndef _LTTNG_UST_RCULFHASH_H
11 #define _LTTNG_UST_RCULFHASH_H
12
13 #include <stdint.h>
14 #include <pthread.h>
15 #include <urcu/compiler.h>
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 struct lttng_ust_lfht;
22
23 /*
24 * lttng_ust_lfht_node: Contains the next pointers and reverse-hash
25 * value required for lookup and traversal of the hash table.
26 *
27 * struct lttng_ust_lfht_node should be aligned on 8-bytes boundaries because
28 * the three lower bits are used as flags. It is worth noting that the
29 * information contained within these three bits could be represented on
30 * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and
31 * BUCKET_FLAG. This can be done if we ensure that no iterator nor
32 * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG
33 * is set. Given the minimum size of struct lttng_ust_lfht_node is 8 bytes on
34 * 32-bit architectures, we choose to go for simplicity and reserve
35 * three bits.
36 *
37 * struct lttng_ust_lfht_node can be embedded into a structure (as a field).
38 * caa_container_of() can be used to get the structure from the struct
39 * lttng_ust_lfht_node after a lookup.
40 *
41 * The structure which embeds it typically holds the key (or key-value
42 * pair) of the object. The caller code is responsible for calculation
43 * of the hash value for lttng_ust_lfht APIs.
44 */
45 struct lttng_ust_lfht_node {
46 struct lttng_ust_lfht_node *next; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
47 unsigned long reverse_hash;
48 } __attribute__((aligned(8)));
49
50 /* lttng_ust_lfht_iter: Used to track state while traversing a hash chain. */
51 struct lttng_ust_lfht_iter {
52 struct lttng_ust_lfht_node *node, *next;
53 };
54
55 static inline
56 struct lttng_ust_lfht_node *lttng_ust_lfht_iter_get_node(struct lttng_ust_lfht_iter *iter)
57 {
58 return iter->node;
59 }
60
61 struct rcu_flavor_struct;
62
63 /*
64 * Caution !
65 * Ensure reader and writer threads are registered as urcu readers.
66 */
67
68 typedef int (*lttng_ust_lfht_match_fct)(struct lttng_ust_lfht_node *node, const void *key);
69
70 /*
71 * lttng_ust_lfht_node_init - initialize a hash table node
72 * @node: the node to initialize.
73 *
74 * This function is kept to be eventually used for debugging purposes
75 * (detection of memory corruption).
76 */
77 static inline
78 void lttng_ust_lfht_node_init(struct lttng_ust_lfht_node *node __attribute__((unused)))
79 {
80 }
81
82 /*
83 * Hash table creation flags.
84 */
85 enum {
86 LTTNG_UST_LFHT_AUTO_RESIZE = (1U << 0),
87 LTTNG_UST_LFHT_ACCOUNTING = (1U << 1),
88 };
89
90 struct lttng_ust_lfht_mm_type {
91 struct lttng_ust_lfht *(*alloc_lttng_ust_lfht)(unsigned long min_nr_alloc_buckets,
92 unsigned long max_nr_buckets);
93 void (*alloc_bucket_table)(struct lttng_ust_lfht *ht, unsigned long order);
94 void (*free_bucket_table)(struct lttng_ust_lfht *ht, unsigned long order);
95 struct lttng_ust_lfht_node *(*bucket_at)(struct lttng_ust_lfht *ht,
96 unsigned long index);
97 };
98
99 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_order
100 __attribute__((visibility("hidden")));
101
102 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_chunk
103 __attribute__((visibility("hidden")));
104
105 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_mmap
106 __attribute__((visibility("hidden")));
107
108 /*
109 * lttng_ust_lfht_new - allocate a hash table.
110 * @init_size: number of buckets to allocate initially. Must be power of two.
111 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
112 * (must be power of two)
113 * @max_nr_buckets: the maximum number of hash table buckets allowed.
114 * (must be power of two, 0 is accepted, means
115 * "infinite")
116 * @flags: hash table creation flags (can be combined with bitwise or: '|').
117 * 0: no flags.
118 * LTTNG_UST_LFHT_AUTO_RESIZE: automatically resize hash table.
119 * LTTNG_UST_LFHT_ACCOUNTING: count the number of node addition
120 * and removal in the table
121 *
122 * Return NULL on error.
123 * Note: the RCU flavor must be already included before the hash table header.
124 */
125 extern struct lttng_ust_lfht *lttng_ust_lfht_new(unsigned long init_size,
126 unsigned long min_nr_alloc_buckets,
127 unsigned long max_nr_buckets,
128 int flags,
129 const struct lttng_ust_lfht_mm_type *mm)
130 __attribute__((visibility("hidden")));
131
132 /*
133 * lttng_ust_lfht_destroy - destroy a hash table.
134 * @ht: the hash table to destroy.
135 *
136 * Return 0 on success, negative error value on error.
137
138 * Prior to liburcu 0.10:
139 * - Threads calling this API need to be registered RCU read-side
140 * threads.
141 * - lttng_ust_lfht_destroy should *not* be called from a RCU read-side
142 * critical section. It should *not* be called from a call_rcu thread
143 * context neither.
144 *
145 * Starting from liburcu 0.10, rculfhash implements its own worker
146 * thread to handle resize operations, which removes RCU requirements on
147 * lttng_ust_lfht_destroy.
148 */
149 extern int lttng_ust_lfht_destroy(struct lttng_ust_lfht *ht)
150 __attribute__((visibility("hidden")));
151
152 /*
153 * lttng_ust_lfht_count_nodes - count the number of nodes in the hash table.
154 * @ht: the hash table.
155 * @split_count_before: sample the node count split-counter before traversal.
156 * @count: traverse the hash table, count the number of nodes observed.
157 * @split_count_after: sample the node count split-counter after traversal.
158 *
159 * Call with rcu_read_lock held.
160 * Threads calling this API need to be registered RCU read-side threads.
161 */
162 extern void lttng_ust_lfht_count_nodes(struct lttng_ust_lfht *ht,
163 long *split_count_before,
164 unsigned long *count,
165 long *split_count_after)
166 __attribute__((visibility("hidden")));
167
168 /*
169 * lttng_ust_lfht_lookup - lookup a node by key.
170 * @ht: the hash table.
171 * @hash: the key hash.
172 * @match: the key match function.
173 * @key: the current node key.
174 * @iter: node, if found (output). *iter->node set to NULL if not found.
175 *
176 * Call with rcu_read_lock held.
177 * Threads calling this API need to be registered RCU read-side threads.
178 * This function acts as a rcu_dereference() to read the node pointer.
179 */
180 extern void lttng_ust_lfht_lookup(struct lttng_ust_lfht *ht, unsigned long hash,
181 lttng_ust_lfht_match_fct match, const void *key,
182 struct lttng_ust_lfht_iter *iter)
183 __attribute__((visibility("hidden")));
184
185 /*
186 * lttng_ust_lfht_next_duplicate - get the next item with same key, after iterator.
187 * @ht: the hash table.
188 * @match: the key match function.
189 * @key: the current node key.
190 * @iter: input: current iterator.
191 * output: node, if found. *iter->node set to NULL if not found.
192 *
193 * Uses an iterator initialized by a lookup or traversal. Important: the
194 * iterator _needs_ to be initialized before calling
195 * lttng_ust_lfht_next_duplicate.
196 * Sets *iter-node to the following node with same key.
197 * Sets *iter->node to NULL if no following node exists with same key.
198 * RCU read-side lock must be held across lttng_ust_lfht_lookup and
199 * lttng_ust_lfht_next calls, and also between lttng_ust_lfht_next calls using the
200 * node returned by a previous lttng_ust_lfht_next.
201 * Call with rcu_read_lock held.
202 * Threads calling this API need to be registered RCU read-side threads.
203 * This function acts as a rcu_dereference() to read the node pointer.
204 */
205 extern void lttng_ust_lfht_next_duplicate(struct lttng_ust_lfht *ht,
206 lttng_ust_lfht_match_fct match, const void *key,
207 struct lttng_ust_lfht_iter *iter)
208 __attribute__((visibility("hidden")));
209
210 /*
211 * lttng_ust_lfht_first - get the first node in the table.
212 * @ht: the hash table.
213 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
214 *
215 * Output in "*iter". *iter->node set to NULL if table is empty.
216 * Call with rcu_read_lock held.
217 * Threads calling this API need to be registered RCU read-side threads.
218 * This function acts as a rcu_dereference() to read the node pointer.
219 */
220 extern void lttng_ust_lfht_first(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_iter *iter)
221 __attribute__((visibility("hidden")));
222
223 /*
224 * lttng_ust_lfht_next - get the next node in the table.
225 * @ht: the hash table.
226 * @iter: input: current iterator.
227 * output: next node, if exists. *iter->node set to NULL if not found.
228 *
229 * Input/Output in "*iter". *iter->node set to NULL if *iter was
230 * pointing to the last table node.
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 extern void lttng_ust_lfht_next(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_iter *iter)
236 __attribute__((visibility("hidden")));
237
238 /*
239 * lttng_ust_lfht_add - add a node to the hash table.
240 * @ht: the hash table.
241 * @hash: the key hash.
242 * @node: the node to add.
243 *
244 * This function supports adding redundant keys into the table.
245 * Call with rcu_read_lock held.
246 * Threads calling this API need to be registered RCU read-side threads.
247 * This function issues a full memory barrier before and after its
248 * atomic commit.
249 */
250 extern void lttng_ust_lfht_add(struct lttng_ust_lfht *ht, unsigned long hash,
251 struct lttng_ust_lfht_node *node)
252 __attribute__((visibility("hidden")));
253
254 /*
255 * lttng_ust_lfht_add_unique - add a node to hash table, if key is not present.
256 * @ht: the hash table.
257 * @hash: the node's hash.
258 * @match: the key match function.
259 * @key: the node's key.
260 * @node: the node to try adding.
261 *
262 * Return the node added upon success.
263 * Return the unique node already present upon failure. If
264 * lttng_ust_lfht_add_unique fails, the node passed as parameter should be
265 * freed by the caller. In this case, the caller does NOT need to wait
266 * for a grace period before freeing or re-using the node.
267 * Call with rcu_read_lock held.
268 * Threads calling this API need to be registered RCU read-side threads.
269 *
270 * The semantic of this function is that if only this function is used
271 * to add keys into the table, no duplicated keys should ever be
272 * observable in the table. The same guarantee apply for combination of
273 * add_unique and add_replace (see below).
274 *
275 * Upon success, this function issues a full memory barrier before and
276 * after its atomic commit. Upon failure, this function acts like a
277 * simple lookup operation: it acts as a rcu_dereference() to read the
278 * node pointer. The failure case does not guarantee any other memory
279 * barrier.
280 */
281 extern struct lttng_ust_lfht_node *lttng_ust_lfht_add_unique(struct lttng_ust_lfht *ht,
282 unsigned long hash,
283 lttng_ust_lfht_match_fct match,
284 const void *key,
285 struct lttng_ust_lfht_node *node)
286 __attribute__((visibility("hidden")));
287
288 /*
289 * lttng_ust_lfht_add_replace - replace or add a node within hash table.
290 * @ht: the hash table.
291 * @hash: the node's hash.
292 * @match: the key match function.
293 * @key: the node's key.
294 * @node: the node to add.
295 *
296 * Return the node replaced upon success. If no node matching the key
297 * was present, return NULL, which also means the operation succeeded.
298 * This replacement operation should never fail.
299 * Call with rcu_read_lock held.
300 * Threads calling this API need to be registered RCU read-side threads.
301 * After successful replacement, a grace period must be waited for before
302 * freeing or re-using the memory reserved for the returned node.
303 *
304 * The semantic of replacement vs lookups and traversals is the
305 * following: if lookups and traversals are performed between a key
306 * unique insertion and its removal, we guarantee that the lookups and
307 * traversals will always find exactly one instance of the key if it is
308 * replaced concurrently with the lookups.
309 *
310 * Providing this semantic allows us to ensure that replacement-only
311 * schemes will never generate duplicated keys. It also allows us to
312 * guarantee that a combination of add_replace and add_unique updates
313 * will never generate duplicated keys.
314 *
315 * This function issues a full memory barrier before and after its
316 * atomic commit.
317 */
318 extern struct lttng_ust_lfht_node *lttng_ust_lfht_add_replace(struct lttng_ust_lfht *ht,
319 unsigned long hash,
320 lttng_ust_lfht_match_fct match,
321 const void *key,
322 struct lttng_ust_lfht_node *node)
323 __attribute__((visibility("hidden")));
324
325 /*
326 * lttng_ust_lfht_replace - replace a node pointed to by iter within hash table.
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.
333 *
334 * Return 0 if replacement is successful, negative value otherwise.
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.
339 * Old node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next.
340 * RCU read-side lock must be held between lookup and replacement.
341 * Call with rcu_read_lock held.
342 * Threads calling this API need to be registered RCU read-side threads.
343 * After successful replacement, a grace period must be waited for before
344 * freeing or re-using the memory reserved for the old node (which can
345 * be accessed with lttng_ust_lfht_iter_get_node).
346 *
347 * The semantic of replacement vs lookups is the same as
348 * lttng_ust_lfht_add_replace().
349 *
350 * Upon success, this function issues a full memory barrier before and
351 * after its atomic commit. Upon failure, this function does not issue
352 * any memory barrier.
353 */
354 extern int lttng_ust_lfht_replace(struct lttng_ust_lfht *ht,
355 struct lttng_ust_lfht_iter *old_iter,
356 unsigned long hash,
357 lttng_ust_lfht_match_fct match,
358 const void *key,
359 struct lttng_ust_lfht_node *new_node)
360 __attribute__((visibility("hidden")));
361
362 /*
363 * lttng_ust_lfht_del - remove node pointed to by iterator from hash table.
364 * @ht: the hash table.
365 * @node: the node to delete.
366 *
367 * Return 0 if the node is successfully removed, negative value
368 * otherwise.
369 * Deleting a NULL node or an already removed node will fail with a
370 * negative value.
371 * Node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next,
372 * followed by use of lttng_ust_lfht_iter_get_node.
373 * RCU read-side lock must be held between lookup and removal.
374 * Call with rcu_read_lock held.
375 * Threads calling this API need to be registered RCU read-side threads.
376 * After successful removal, a grace period must be waited for before
377 * freeing or re-using the memory reserved for old node (which can be
378 * accessed with lttng_ust_lfht_iter_get_node).
379 * Upon success, this function issues a full memory barrier before and
380 * after its atomic commit. Upon failure, this function does not issue
381 * any memory barrier.
382 */
383 extern int lttng_ust_lfht_del(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_node *node)
384 __attribute__((visibility("hidden")));
385
386 /*
387 * lttng_ust_lfht_is_node_deleted - query whether a node is removed from hash table.
388 *
389 * Return non-zero if the node is deleted from the hash table, 0
390 * otherwise.
391 * Node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next,
392 * followed by use of lttng_ust_lfht_iter_get_node.
393 * RCU read-side lock must be held between lookup and call to this
394 * function.
395 * Call with rcu_read_lock held.
396 * Threads calling this API need to be registered RCU read-side threads.
397 * This function does not issue any memory barrier.
398 */
399 extern int lttng_ust_lfht_is_node_deleted(const struct lttng_ust_lfht_node *node)
400 __attribute__((visibility("hidden")));
401
402 /*
403 * lttng_ust_lfht_resize - Force a hash table resize
404 * @ht: the hash table.
405 * @new_size: update to this hash table size.
406 *
407 * Threads calling this API need to be registered RCU read-side threads.
408 * This function does not (necessarily) issue memory barriers.
409 * lttng_ust_lfht_resize should *not* be called from a RCU read-side critical
410 * section.
411 */
412 extern void lttng_ust_lfht_resize(struct lttng_ust_lfht *ht, unsigned long new_size)
413 __attribute__((visibility("hidden")));
414
415 /*
416 * Note: it is safe to perform element removal (del), replacement, or
417 * any hash table update operation during any of the following hash
418 * table traversals.
419 * These functions act as rcu_dereference() to read the node pointers.
420 */
421 #define lttng_ust_lfht_for_each(ht, iter, node) \
422 for (lttng_ust_lfht_first(ht, iter), \
423 node = lttng_ust_lfht_iter_get_node(iter); \
424 node != NULL; \
425 lttng_ust_lfht_next(ht, iter), \
426 node = lttng_ust_lfht_iter_get_node(iter))
427
428 #define lttng_ust_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
429 for (lttng_ust_lfht_lookup(ht, hash, match, key, iter), \
430 node = lttng_ust_lfht_iter_get_node(iter); \
431 node != NULL; \
432 lttng_ust_lfht_next_duplicate(ht, match, key, iter), \
433 node = lttng_ust_lfht_iter_get_node(iter))
434
435 #define lttng_ust_lfht_for_each_entry(ht, iter, pos, member) \
436 for (lttng_ust_lfht_first(ht, iter), \
437 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
438 __typeof__(*(pos)), member); \
439 lttng_ust_lfht_iter_get_node(iter) != NULL; \
440 lttng_ust_lfht_next(ht, iter), \
441 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
442 __typeof__(*(pos)), member))
443
444 #define lttng_ust_lfht_for_each_entry_duplicate(ht, hash, match, key, \
445 iter, pos, member) \
446 for (lttng_ust_lfht_lookup(ht, hash, match, key, iter), \
447 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
448 __typeof__(*(pos)), member); \
449 lttng_ust_lfht_iter_get_node(iter) != NULL; \
450 lttng_ust_lfht_next_duplicate(ht, match, key, iter), \
451 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
452 __typeof__(*(pos)), member))
453
454 #ifdef __cplusplus
455 }
456 #endif
457
458 #endif /* _LTTNG_UST_RCULFHASH_H */
This page took 0.042506 seconds and 4 git commands to generate.