Add cleanup function for UST app
[lttng-tools.git] / 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 *
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
32 extern "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
40 /*
41 * _cds_lfht_node: Contains the internal pointers and reverse-hash
42 * value required for lookup and traversal of the hash table.
43 */
44 struct _cds_lfht_node {
45 struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
46 unsigned long reverse_hash;
47 } __attribute__((aligned(4)));
48
49 /*
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 *
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 */
60 struct 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
69 /* cds_lfht_iter: Used to track state while traversing a hash chain. */
70 struct cds_lfht_iter {
71 struct cds_lfht_node *node, *next;
72 };
73
74 static inline
75 struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
76 {
77 return iter->node;
78 }
79
80 struct cds_lfht;
81
82 /*
83 * Caution !
84 * Ensure reader and writer threads are registered as urcu readers.
85 */
86
87 typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
88 unsigned long seed);
89 typedef 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 */
95 static inline
96 void 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 */
106 enum {
107 CDS_LFHT_AUTO_RESIZE = (1U << 0),
108 CDS_LFHT_ACCOUNTING = (1U << 1),
109 };
110
111 /*
112 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
113 */
114 struct 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,
118 unsigned long min_alloc_size,
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.
137 * @min_alloc_size: the smallest allocation size to use. Must be power of two.
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.
153 * Threads calling this API need to be registered RCU read-side threads.
154 */
155 static inline
156 struct 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,
160 unsigned long min_alloc_size,
161 int flags,
162 pthread_attr_t *attr)
163 {
164 return _cds_lfht_new(hash_fct, compare_fct, hash_seed,
165 init_size, min_alloc_size, flags,
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
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().
179 *
180 * Return 0 on success, negative error value on error.
181 * Threads calling this API need to be registered RCU read-side threads.
182 */
183 int 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.
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.
192 * Call with rcu_read_lock held.
193 * Threads calling this API need to be registered RCU read-side threads.
194 */
195 void cds_lfht_count_nodes(struct cds_lfht *ht,
196 long *split_count_before,
197 unsigned long *count,
198 unsigned long *removed,
199 long *split_count_after);
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.
206 * Threads calling this API need to be registered RCU read-side threads.
207 */
208 void cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len,
209 struct cds_lfht_iter *iter);
210
211 /*
212 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
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.
221 * Threads calling this API need to be registered RCU read-side threads.
222 */
223 void 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.
230 * Threads calling this API need to be registered RCU read-side threads.
231 */
232 void 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.
240 * Threads calling this API need to be registered RCU read-side threads.
241 */
242 void 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 *
247 * This function supports adding redundant keys into the table.
248 * Call with rcu_read_lock held.
249 * Threads calling this API need to be registered RCU read-side threads.
250 */
251 void 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.
261 * Threads calling this API need to be registered RCU read-side threads.
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 */
268 struct 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.
278 * Threads calling this API need to be registered RCU read-side threads.
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 */
292 struct 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.
304 * Threads calling this API need to be registered RCU read-side threads.
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 */
319 int 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.
333 * Threads calling this API need to be registered RCU read-side threads.
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 */
338 int 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.
343 *
344 * Threads calling this API need to be registered RCU read-side threads.
345 */
346 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
347
348 /*
349 * Note: cds_lfht_for_each are safe for element removal during
350 * iteration.
351 */
352 #define cds_lfht_for_each(ht, iter, node) \
353 for (cds_lfht_first(ht, iter), \
354 node = cds_lfht_iter_get_node(iter); \
355 node != NULL; \
356 cds_lfht_next(ht, iter), \
357 node = cds_lfht_iter_get_node(iter))
358
359 #define cds_lfht_for_each_duplicate(ht, match, hash, key, iter, node) \
360 for (cds_lfht_lookup(ht, match, hash, key, iter), \
361 node = cds_lfht_iter_get_node(iter); \
362 node != NULL; \
363 cds_lfht_next_duplicate(ht, match, key, iter), \
364 node = cds_lfht_iter_get_node(iter))
365
366 #define cds_lfht_for_each_entry(ht, iter, pos, member) \
367 for (cds_lfht_first(ht, iter), \
368 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
369 typeof(*(pos)), member); \
370 &pos->member != NULL; \
371 cds_lfht_next(ht, iter), \
372 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
373 typeof(*(pos)), member))
374
375 #define cds_lfht_for_each_entry_duplicate(ht, match, hash, key, \
376 iter, pos, member) \
377 for (cds_lfht_lookup(ht, match, hash, key, iter), \
378 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
379 typeof(*(pos)), member); \
380 &pos->member != NULL; \
381 cds_lfht_next_duplicate(ht, match, key, iter), \
382 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
383 typeof(*(pos)), member))
384
385 #ifdef __cplusplus
386 }
387 #endif
388
389 #endif /* _URCU_RCULFHASH_H */
This page took 0.057673 seconds and 4 git commands to generate.