136c7259bc45ff7d473f2b0f61f072b0b969875e
[lttng-tools.git] / liblttng-ht / 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 <stdint.h>
30 #include <urcu/compiler.h>
31 #include <urcu-call-rcu.h>
32
33 #include "urcu-flavor.h"
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /*
40 * cds_lfht_node: Contains the next pointers and reverse-hash
41 * value required for lookup and traversal of the hash table.
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.
52 *
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.
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.
60 */
61 struct cds_lfht_node {
62 struct cds_lfht_node *next; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
63 unsigned long reverse_hash;
64 } __attribute__((aligned(8)));
65
66 /* cds_lfht_iter: Used to track state while traversing a hash chain. */
67 struct cds_lfht_iter {
68 struct cds_lfht_node *node, *next;
69 };
70
71 static inline
72 struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
73 {
74 return iter->node;
75 }
76
77 struct cds_lfht;
78
79 /*
80 * Caution !
81 * Ensure reader and writer threads are registered as urcu readers.
82 */
83
84 typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key);
85
86 /*
87 * cds_lfht_node_init - initialize a hash table node
88 * @node: the node to initialize.
89 *
90 * This function is kept to be eventually used for debugging purposes
91 * (detection of memory corruption).
92 */
93 static inline
94 void cds_lfht_node_init(struct cds_lfht_node *node)
95 {
96 }
97
98 /*
99 * Hash table creation flags.
100 */
101 enum {
102 CDS_LFHT_AUTO_RESIZE = (1U << 0),
103 CDS_LFHT_ACCOUNTING = (1U << 1),
104 };
105
106 struct 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
115 extern const struct cds_lfht_mm_type cds_lfht_mm_order;
116 extern const struct cds_lfht_mm_type cds_lfht_mm_chunk;
117 extern const struct cds_lfht_mm_type cds_lfht_mm_mmap;
118
119 /*
120 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
121 */
122 struct cds_lfht *_cds_lfht_new(unsigned long init_size,
123 unsigned long min_nr_alloc_buckets,
124 unsigned long max_nr_buckets,
125 int flags,
126 const struct cds_lfht_mm_type *mm,
127 const struct rcu_flavor_struct *flavor,
128 pthread_attr_t *attr);
129
130 /*
131 * cds_lfht_new - allocate a hash table.
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)
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.
140 * CDS_LFHT_ACCOUNTING: count the number of node addition
141 * and removal in the table
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.
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.)
156 */
157 static inline
158 struct cds_lfht *cds_lfht_new(unsigned long init_size,
159 unsigned long min_nr_alloc_buckets,
160 unsigned long max_nr_buckets,
161 int flags,
162 pthread_attr_t *attr)
163 {
164 return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets,
165 flags, NULL, &rcu_flavor, attr);
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
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().
175 *
176 * Return 0 on success, negative error value on error.
177 * Threads calling this API need to be registered RCU read-side threads.
178 */
179 int 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.
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.
186 * @split_count_after: Sample the node count split-counter after traversal.
187 *
188 * Call with rcu_read_lock held.
189 * Threads calling this API need to be registered RCU read-side threads.
190 */
191 void cds_lfht_count_nodes(struct cds_lfht *ht,
192 long *split_count_before,
193 unsigned long *count,
194 long *split_count_after);
195
196 /*
197 * cds_lfht_lookup - lookup a node by key.
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.
203 *
204 * Call with rcu_read_lock held.
205 * Threads calling this API need to be registered RCU read-side threads.
206 */
207 void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
208 cds_lfht_match_fct match, const void *key,
209 struct cds_lfht_iter *iter);
210
211 /*
212 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
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.
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.
225 * Threads calling this API need to be registered RCU read-side threads.
226 */
227 void cds_lfht_next_duplicate(struct cds_lfht *ht,
228 cds_lfht_match_fct match, const void *key,
229 struct cds_lfht_iter *iter);
230
231 /*
232 * cds_lfht_first - get the first node in the table.
233 * @ht: the hash table.
234 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
235 *
236 * Output in "*iter". *iter->node set to NULL if table is empty.
237 * Call with rcu_read_lock held.
238 * Threads calling this API need to be registered RCU read-side threads.
239 */
240 void 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.
244 * @ht: the hash table.
245 * @iter: Next node, if exists (output). *iter->node set to NULL if not found.
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.
250 * Threads calling this API need to be registered RCU read-side threads.
251 */
252 void 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.
256 * @ht: the hash table.
257 * @hash: the key hash.
258 * @node: the node to add.
259 *
260 * This function supports adding redundant keys into the table.
261 * Call with rcu_read_lock held.
262 * Threads calling this API need to be registered RCU read-side threads.
263 */
264 void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
265 struct cds_lfht_node *node);
266
267 /*
268 * cds_lfht_add_unique - add a node to hash table, if key is not present.
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.
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.
280 * Threads calling this API need to be registered RCU read-side threads.
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 */
287 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
288 unsigned long hash,
289 cds_lfht_match_fct match,
290 const void *key,
291 struct cds_lfht_node *node);
292
293 /*
294 * cds_lfht_add_replace - replace or add a node within hash table.
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.
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.
305 * Threads calling this API need to be registered RCU read-side threads.
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 */
319 struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
320 unsigned long hash,
321 cds_lfht_match_fct match,
322 const void *key,
323 struct cds_lfht_node *node);
324
325 /*
326 * cds_lfht_replace - replace a node pointer 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 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.
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 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 */
357 int 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,
362 struct cds_lfht_node *new_node);
363
364 /*
365 * cds_lfht_del - remove node pointed to by iterator from hash table.
366 * @ht: the hash table.
367 * @node: the node to delete.
368 *
369 * Return 0 if the node is successfully removed, negative value
370 * otherwise.
371 * Deleting a NULL node or an already removed node will fail with a
372 * negative value.
373 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
374 * followed by use of cds_lfht_iter_get_node.
375 * RCU read-side lock must be held between lookup and removal.
376 * Call with rcu_read_lock held.
377 * Threads calling this API need to be registered RCU read-side threads.
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 */
382 int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node);
383
384 /*
385 * cds_lfht_resize - Force a hash table resize
386 * @ht: the hash table.
387 * @new_size: update to this hash table size.
388 *
389 * Threads calling this API need to be registered RCU read-side threads.
390 */
391 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
392
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); \
400 node != NULL; \
401 cds_lfht_next(ht, iter), \
402 node = cds_lfht_iter_get_node(iter))
403
404 #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
405 for (cds_lfht_lookup(ht, hash, match, key, iter), \
406 node = cds_lfht_iter_get_node(iter); \
407 node != NULL; \
408 cds_lfht_next_duplicate(ht, match, key, iter), \
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), \
414 typeof(*(pos)), member); \
415 &(pos)->member != NULL; \
416 cds_lfht_next(ht, iter), \
417 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
418 typeof(*(pos)), member))
419
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); \
425 &(pos)->member != NULL; \
426 cds_lfht_next_duplicate(ht, match, key, iter), \
427 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
428 typeof(*(pos)), member))
429
430 #ifdef __cplusplus
431 }
432 #endif
433
434 #endif /* _URCU_RCULFHASH_H */
This page took 0.041573 seconds and 3 git commands to generate.