23bd1ce3bded4fd4e83f51e0c63c34b48f788acb
[userspace-rcu.git] / urcu / 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 #include <urcu-flavor.h>
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /*
39 * cds_lfht_node: Contains the next pointers and reverse-hash
40 * value required for lookup and traversal of the hash table.
41 *
42 * struct cds_lfht_node should be aligned on 8-bytes boundaries because
43 * the three lower bits are used as flags. It is worth noting that the
44 * information contained within these three bits could be represented on
45 * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and
46 * BUCKET_FLAG. This can be done if we ensure that no iterator nor
47 * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG
48 * is set. Given the minimum size of struct cds_lfht_node is 8 bytes on
49 * 32-bit architectures, we choose to go for simplicity and reserve
50 * three bits.
51 *
52 * struct cds_lfht_node can be embedded into a structure (as a field).
53 * caa_container_of() can be used to get the structure from the struct
54 * cds_lfht_node after a lookup.
55 *
56 * The structure which embeds it typically holds the key (or key-value
57 * pair) of the object. The caller code is responsible for calculation
58 * of the hash value for cds_lfht APIs.
59 */
60 struct cds_lfht_node {
61 struct cds_lfht_node *next; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
62 unsigned long reverse_hash;
63 } __attribute__((aligned(8)));
64
65 /* cds_lfht_iter: Used to track state while traversing a hash chain. */
66 struct cds_lfht_iter {
67 struct cds_lfht_node *node, *next;
68 };
69
70 static inline
71 struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
72 {
73 return iter->node;
74 }
75
76 struct cds_lfht;
77
78 /*
79 * Caution !
80 * Ensure reader and writer threads are registered as urcu readers.
81 */
82
83 typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key);
84
85 /*
86 * cds_lfht_node_init - initialize a hash table node
87 * @node: the node to initialize.
88 *
89 * This function is kept to be eventually used for debugging purposes
90 * (detection of memory corruption).
91 */
92 static inline
93 void cds_lfht_node_init(struct cds_lfht_node *node)
94 {
95 }
96
97 /*
98 * Hash table creation flags.
99 */
100 enum {
101 CDS_LFHT_AUTO_RESIZE = (1U << 0),
102 CDS_LFHT_ACCOUNTING = (1U << 1),
103 };
104
105 struct cds_lfht_mm_type {
106 struct cds_lfht *(*alloc_cds_lfht)(unsigned long min_nr_alloc_buckets,
107 unsigned long max_nr_buckets);
108 void (*alloc_bucket_table)(struct cds_lfht *ht, unsigned long order);
109 void (*free_bucket_table)(struct cds_lfht *ht, unsigned long order);
110 struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht,
111 unsigned long index);
112 };
113
114 extern const struct cds_lfht_mm_type cds_lfht_mm_order;
115 extern const struct cds_lfht_mm_type cds_lfht_mm_chunk;
116 extern const struct cds_lfht_mm_type cds_lfht_mm_mmap;
117
118 /*
119 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
120 */
121 struct cds_lfht *_cds_lfht_new(unsigned long init_size,
122 unsigned long min_nr_alloc_buckets,
123 unsigned long max_nr_buckets,
124 int flags,
125 const struct cds_lfht_mm_type *mm,
126 const struct rcu_flavor_struct *flavor,
127 pthread_attr_t *attr);
128
129 /*
130 * cds_lfht_new - allocate a hash table.
131 * @init_size: number of buckets to allocate initially. Must be power of two.
132 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
133 * (must be power of two)
134 * @max_nr_buckets: the maximum number of hash table buckets allowed.
135 * (must be power of two)
136 * @flags: hash table creation flags (can be combined with bitwise or: '|').
137 * 0: no flags.
138 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
139 * CDS_LFHT_ACCOUNTING: count the number of node addition
140 * and removal in the 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 cds_lfht_new are NOT required to be registered RCU
154 * read-side threads. It can be called very early. (e.g. before RCU is
155 * initialized)
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 * cds_lfht_destroy should *not* be called from a RCU read-side critical
179 * section. It should *not* be called from call_rcu thread context
180 * neither.
181 * In userspace RCU 0.7.x, for QSBR RCU flavor, cds_lfht_destroy() has a
182 * side-effect: it puts the caller thread in "online" state. This will
183 * be fixed in userspace RCU 0.8.x.
184 */
185 int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
186
187 /*
188 * cds_lfht_count_nodes - count the number of nodes in the hash table.
189 * @ht: the hash table.
190 * @split_count_before: sample the node count split-counter before traversal.
191 * @count: traverse the hash table, count the number of nodes observed.
192 * @split_count_after: sample the node count split-counter after traversal.
193 *
194 * Call with rcu_read_lock held.
195 * Threads calling this API need to be registered RCU read-side threads.
196 */
197 void cds_lfht_count_nodes(struct cds_lfht *ht,
198 long *split_count_before,
199 unsigned long *count,
200 long *split_count_after);
201
202 /*
203 * cds_lfht_lookup - lookup a node by key.
204 * @ht: the hash table.
205 * @hash: the key hash.
206 * @match: the key match function.
207 * @key: the current node key.
208 * @iter: node, if found (output). *iter->node set to NULL if not found.
209 *
210 * Call with rcu_read_lock held.
211 * Threads calling this API need to be registered RCU read-side threads.
212 * This function acts as a rcu_dereference() to read the node pointer.
213 */
214 void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
215 cds_lfht_match_fct match, const void *key,
216 struct cds_lfht_iter *iter);
217
218 /*
219 * cds_lfht_next_duplicate - get the next item with same key, after iterator.
220 * @ht: the hash table.
221 * @match: the key match function.
222 * @key: the current node key.
223 * @iter: input: current iterator.
224 * output: node, if found. *iter->node set to NULL if not found.
225 *
226 * Uses an iterator initialized by a lookup or traversal. Important: the
227 * iterator _needs_ to be initialized before calling
228 * cds_lfht_next_duplicate.
229 * Sets *iter-node to the following node with same key.
230 * Sets *iter->node to NULL if no following node exists with same key.
231 * RCU read-side lock must be held across cds_lfht_lookup and
232 * cds_lfht_next calls, and also between cds_lfht_next calls using the
233 * node returned by a previous cds_lfht_next.
234 * Call with rcu_read_lock held.
235 * Threads calling this API need to be registered RCU read-side threads.
236 * This function acts as a rcu_dereference() to read the node pointer.
237 */
238 void cds_lfht_next_duplicate(struct cds_lfht *ht,
239 cds_lfht_match_fct match, const void *key,
240 struct cds_lfht_iter *iter);
241
242 /*
243 * cds_lfht_first - get the first node in the table.
244 * @ht: the hash table.
245 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
246 *
247 * Output in "*iter". *iter->node set to NULL if table is empty.
248 * Call with rcu_read_lock held.
249 * Threads calling this API need to be registered RCU read-side threads.
250 * This function acts as a rcu_dereference() to read the node pointer.
251 */
252 void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter);
253
254 /*
255 * cds_lfht_next - get the next node in the table.
256 * @ht: the hash table.
257 * @iter: input: current iterator.
258 * output: next node, if exists. *iter->node set to NULL if not found.
259 *
260 * Input/Output in "*iter". *iter->node set to NULL if *iter was
261 * pointing to the last table node.
262 * Call with rcu_read_lock held.
263 * Threads calling this API need to be registered RCU read-side threads.
264 * This function acts as a rcu_dereference() to read the node pointer.
265 */
266 void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
267
268 /*
269 * cds_lfht_add - add a node to the hash table.
270 * @ht: the hash table.
271 * @hash: the key hash.
272 * @node: the node to add.
273 *
274 * This function supports adding redundant keys into the table.
275 * Call with rcu_read_lock held.
276 * Threads calling this API need to be registered RCU read-side threads.
277 * This function issues a full memory barrier before and after its
278 * atomic commit.
279 */
280 void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
281 struct cds_lfht_node *node);
282
283 /*
284 * cds_lfht_add_unique - add a node to hash table, if key is not present.
285 * @ht: the hash table.
286 * @hash: the node's hash.
287 * @match: the key match function.
288 * @key: the node's key.
289 * @node: the node to try adding.
290 *
291 * Return the node added upon success.
292 * Return the unique node already present upon failure. If
293 * cds_lfht_add_unique fails, the node passed as parameter should be
294 * freed by the caller. In this case, the caller does NOT need to wait
295 * for a grace period before freeing the node.
296 * Call with rcu_read_lock held.
297 * Threads calling this API need to be registered RCU read-side threads.
298 *
299 * The semantic of this function is that if only this function is used
300 * to add keys into the table, no duplicated keys should ever be
301 * observable in the table. The same guarantee apply for combination of
302 * add_unique and add_replace (see below).
303 *
304 * Upon success, this function issues a full memory barrier before and
305 * after its atomic commit. Upon failure, this function acts like a
306 * simple lookup operation: it acts as a rcu_dereference() to read the
307 * node pointer. The failure case does not guarantee any other memory
308 * barrier.
309 */
310 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
311 unsigned long hash,
312 cds_lfht_match_fct match,
313 const void *key,
314 struct cds_lfht_node *node);
315
316 /*
317 * cds_lfht_add_replace - replace or add a node within hash table.
318 * @ht: the hash table.
319 * @hash: the node's hash.
320 * @match: the key match function.
321 * @key: the node's key.
322 * @node: the node to add.
323 *
324 * Return the node replaced upon success. If no node matching the key
325 * was present, return NULL, which also means the operation succeeded.
326 * This replacement operation should never fail.
327 * Call with rcu_read_lock held.
328 * Threads calling this API need to be registered RCU read-side threads.
329 * After successful replacement, a grace period must be waited for before
330 * freeing the memory reserved for the returned node.
331 *
332 * The semantic of replacement vs lookups and traversals is the
333 * following: if lookups and traversals are performed between a key
334 * unique insertion and its removal, we guarantee that the lookups and
335 * traversals will always find exactly one instance of the key if it is
336 * replaced concurrently with the lookups.
337 *
338 * Providing this semantic allows us to ensure that replacement-only
339 * schemes will never generate duplicated keys. It also allows us to
340 * guarantee that a combination of add_replace and add_unique updates
341 * will never generate duplicated keys.
342 *
343 * This function issues a full memory barrier before and after its
344 * atomic commit.
345 */
346 struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
347 unsigned long hash,
348 cds_lfht_match_fct match,
349 const void *key,
350 struct cds_lfht_node *node);
351
352 /*
353 * cds_lfht_replace - replace a node pointed to by iter within hash table.
354 * @ht: the hash table.
355 * @old_iter: the iterator position of the node to replace.
356 * @hash: the node's hash.
357 * @match: the key match function.
358 * @key: the node's key.
359 * @new_node: the new node to use as replacement.
360 *
361 * Return 0 if replacement is successful, negative value otherwise.
362 * Replacing a NULL old node or an already removed node will fail with
363 * -ENOENT.
364 * If the hash or value of the node to replace and the new node differ,
365 * this function returns -EINVAL without proceeding to the replacement.
366 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
367 * RCU read-side lock must be held between lookup and replacement.
368 * Call with rcu_read_lock held.
369 * Threads calling this API need to be registered RCU read-side threads.
370 * After successful replacement, a grace period must be waited for before
371 * freeing the memory reserved for the old node (which can be accessed
372 * with cds_lfht_iter_get_node).
373 *
374 * The semantic of replacement vs lookups is the same as
375 * cds_lfht_add_replace().
376 *
377 * Upon success, this function issues a full memory barrier before and
378 * after its atomic commit. Upon failure, this function does not issue
379 * any memory barrier.
380 */
381 int cds_lfht_replace(struct cds_lfht *ht,
382 struct cds_lfht_iter *old_iter,
383 unsigned long hash,
384 cds_lfht_match_fct match,
385 const void *key,
386 struct cds_lfht_node *new_node);
387
388 /*
389 * cds_lfht_del - remove node pointed to by iterator from hash table.
390 * @ht: the hash table.
391 * @node: the node to delete.
392 *
393 * Return 0 if the node is successfully removed, negative value
394 * otherwise.
395 * Deleting a NULL node or an already removed node will fail with a
396 * negative value.
397 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
398 * followed by use of cds_lfht_iter_get_node.
399 * RCU read-side lock must be held between lookup and removal.
400 * Call with rcu_read_lock held.
401 * Threads calling this API need to be registered RCU read-side threads.
402 * After successful removal, a grace period must be waited for before
403 * freeing the memory reserved for old node (which can be accessed with
404 * cds_lfht_iter_get_node).
405 * Upon success, this function issues a full memory barrier before and
406 * after its atomic commit. Upon failure, this function does not issue
407 * any memory barrier.
408 */
409 int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node);
410
411 /*
412 * cds_lfht_is_node_deleted - query whether a node is removed from hash table.
413 *
414 * Return non-zero if the node is deleted from the hash table, 0
415 * otherwise.
416 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
417 * followed by use of cds_lfht_iter_get_node.
418 * RCU read-side lock must be held between lookup and call to this
419 * function.
420 * Call with rcu_read_lock held.
421 * Threads calling this API need to be registered RCU read-side threads.
422 * This function does not issue any memory barrier.
423 */
424 int cds_lfht_is_node_deleted(struct cds_lfht_node *node);
425
426 /*
427 * cds_lfht_resize - Force a hash table resize
428 * @ht: the hash table.
429 * @new_size: update to this hash table size.
430 *
431 * Threads calling this API need to be registered RCU read-side threads.
432 * This function does not (necessarily) issue memory barriers.
433 * cds_lfht_resize should *not* be called from a RCU read-side critical
434 * section.
435 * In userspace RCU 0.7.x, for QSBR RCU flavor, cds_lfht_resize() has a
436 * side-effect: it puts the caller thread in "online" state. This will
437 * be fixed in userspace RCU 0.8.x.
438 */
439 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
440
441 /*
442 * Note: it is safe to perform element removal (del), replacement, or
443 * any hash table update operation during any of the following hash
444 * table traversals.
445 * These functions act as rcu_dereference() to read the node pointers.
446 */
447 #define cds_lfht_for_each(ht, iter, node) \
448 for (cds_lfht_first(ht, iter), \
449 node = cds_lfht_iter_get_node(iter); \
450 node != NULL; \
451 cds_lfht_next(ht, iter), \
452 node = cds_lfht_iter_get_node(iter))
453
454 #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
455 for (cds_lfht_lookup(ht, hash, match, key, iter), \
456 node = cds_lfht_iter_get_node(iter); \
457 node != NULL; \
458 cds_lfht_next_duplicate(ht, match, key, iter), \
459 node = cds_lfht_iter_get_node(iter))
460
461 #define cds_lfht_for_each_entry(ht, iter, pos, member) \
462 for (cds_lfht_first(ht, iter), \
463 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
464 __typeof__(*(pos)), member); \
465 &(pos)->member != NULL; \
466 cds_lfht_next(ht, iter), \
467 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
468 __typeof__(*(pos)), member))
469
470 #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
471 iter, pos, member) \
472 for (cds_lfht_lookup(ht, hash, match, key, iter), \
473 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
474 __typeof__(*(pos)), member); \
475 &(pos)->member != NULL; \
476 cds_lfht_next_duplicate(ht, match, key, iter), \
477 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
478 __typeof__(*(pos)), member))
479
480 #ifdef __cplusplus
481 }
482 #endif
483
484 #endif /* _URCU_RCULFHASH_H */
This page took 0.037315 seconds and 3 git commands to generate.