cds_lfht_replace: add checks for old/new node hash/value match
[urcu.git] / urcu / rculfhash.h
CommitLineData
a42cc659
MD
1#ifndef _URCU_RCULFHASH_H
2#define _URCU_RCULFHASH_H
ab7d5fc6 3
f0c29ed7
MD
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>
0dcf4847 10 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
f0c29ed7
MD
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
cf77d1fa
MD
25 *
26 * Include this file _after_ including your URCU flavor.
f0c29ed7
MD
27 */
28
674f7a69 29#include <stdint.h>
6d320126 30#include <urcu/compiler.h>
abc490a1 31#include <urcu-call-rcu.h>
7b17c13e 32#include <urcu-flavor.h>
abc490a1 33
01389791
MD
34#ifdef __cplusplus
35extern "C" {
36#endif
37
7f52427b 38/*
04db56f8 39 * cds_lfht_node: Contains the next pointers and reverse-hash
7f52427b 40 * value required for lookup and traversal of the hash table.
04db56f8 41 *
db00ccc3 42 * struct cds_lfht_node should be aligned on 8-bytes boundaries because
13f656f9
MD
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.
7f52427b 51 *
ae62aa6a
MD
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.
04db56f8
LJ
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.
ae62aa6a 59 */
14044b37 60struct cds_lfht_node {
3f2f3714 61 struct cds_lfht_node *next; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
04db56f8 62 unsigned long reverse_hash;
db00ccc3 63} __attribute__((aligned(8)));
abc490a1 64
7f52427b 65/* cds_lfht_iter: Used to track state while traversing a hash chain. */
adc0de68
MD
66struct cds_lfht_iter {
67 struct cds_lfht_node *node, *next;
68};
69
70static inline
71struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
72{
73 return iter->node;
74}
75
14044b37 76struct cds_lfht;
ab7d5fc6 77
5e28c532
MD
78/*
79 * Caution !
abc490a1 80 * Ensure reader and writer threads are registered as urcu readers.
5e28c532
MD
81 */
82
996ff57c 83typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key);
abc490a1 84
f0c29ed7 85/*
14044b37 86 * cds_lfht_node_init - initialize a hash table node
0422d92c 87 * @node: the node to initialize.
04db56f8
LJ
88 *
89 * This function is kept to be eventually used for debugging purposes
90 * (detection of memory corruption).
f0c29ed7 91 */
abc490a1 92static inline
04db56f8 93void cds_lfht_node_init(struct cds_lfht_node *node)
abc490a1 94{
abc490a1 95}
674f7a69 96
b8af5011
MD
97/*
98 * Hash table creation flags.
99 */
100enum {
101 CDS_LFHT_AUTO_RESIZE = (1U << 0),
5afadd12 102 CDS_LFHT_ACCOUNTING = (1U << 1),
b8af5011
MD
103};
104
0b6aa001
LJ
105struct 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
114extern const struct cds_lfht_mm_type cds_lfht_mm_order;
308d1cb3 115extern const struct cds_lfht_mm_type cds_lfht_mm_chunk;
b0b55251 116extern const struct cds_lfht_mm_type cds_lfht_mm_mmap;
0b6aa001 117
674f7a69 118/*
7a9dcf9b 119 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
674f7a69 120 */
0422d92c 121struct cds_lfht *_cds_lfht_new(unsigned long init_size,
0722081a 122 unsigned long min_nr_alloc_buckets,
747d725c 123 unsigned long max_nr_buckets,
b8af5011 124 int flags,
0b6aa001 125 const struct cds_lfht_mm_type *mm,
7b17c13e 126 const struct rcu_flavor_struct *flavor,
b7d619b0 127 pthread_attr_t *attr);
ab7d5fc6 128
7a9dcf9b
MD
129/*
130 * cds_lfht_new - allocate a hash table.
747d725c
LJ
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)
f22dd01d
MD
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.
44bbe7fa
LJ
139 * CDS_LFHT_ACCOUNTING: count the number of node addition
140 * and removal in the table
b7d619b0 141 * @attr: optional resize worker thread attributes. NULL for default.
7a9dcf9b 142 *
7a9dcf9b
MD
143 * Return NULL on error.
144 * Note: the RCU flavor must be already included before the hash table header.
b7d619b0
MD
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.
44bbe7fa
LJ
153 * Threads calling this API are NOT required to be registered RCU read-side
154 * threads. It can be called very early.(before rcu is initialized ...etc.)
7a9dcf9b
MD
155 */
156static inline
0422d92c 157struct cds_lfht *cds_lfht_new(unsigned long init_size,
0722081a 158 unsigned long min_nr_alloc_buckets,
747d725c 159 unsigned long max_nr_buckets,
b7d619b0
MD
160 int flags,
161 pthread_attr_t *attr)
7a9dcf9b 162{
7b17c13e 163 return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets,
c1888f3a 164 flags, NULL, &rcu_flavor, attr);
7a9dcf9b
MD
165}
166
f0c29ed7 167/*
14044b37 168 * cds_lfht_destroy - destroy a hash table.
b7d619b0
MD
169 * @ht: the hash table to destroy.
170 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
171 * The caller will typically want to free this pointer if dynamically
7f52427b
MD
172 * allocated. The attr point can be NULL if the caller does not
173 * need to be informed of the value passed to cds_lfht_new().
6878c72b
MD
174 *
175 * Return 0 on success, negative error value on error.
3df0c49c 176 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 177 */
b7d619b0 178int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
f0c29ed7
MD
179
180/*
14044b37 181 * cds_lfht_count_nodes - count the number of nodes in the hash table.
7f52427b
MD
182 * @ht: the hash table.
183 * @split_count_before: Sample the node count split-counter before traversal.
184 * @count: Traverse the hash table, count the number of nodes observed.
7f52427b 185 * @split_count_after: Sample the node count split-counter after traversal.
0422d92c 186 *
f0c29ed7 187 * Call with rcu_read_lock held.
3df0c49c 188 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 189 */
14044b37 190void cds_lfht_count_nodes(struct cds_lfht *ht,
7f52427b 191 long *split_count_before,
adc0de68 192 unsigned long *count,
7f52427b 193 long *split_count_after);
ab7d5fc6 194
f0c29ed7 195/*
14044b37 196 * cds_lfht_lookup - lookup a node by key.
0422d92c 197 * @ht: the hash table.
0422d92c 198 * @hash: the key hash.
6f554439
LJ
199 * @match: the key match function.
200 * @key: the current node key.
0422d92c 201 * @iter: Node, if found (output). *iter->node set to NULL if not found.
f0c29ed7 202 *
f0c29ed7 203 * Call with rcu_read_lock held.
3df0c49c 204 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 205 */
6f554439 206void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
996ff57c 207 cds_lfht_match_fct match, const void *key,
6f554439 208 struct cds_lfht_iter *iter);
ab7d5fc6 209
f0c29ed7 210/*
3883c0e5 211 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
0422d92c
MD
212 * @ht: the hash table.
213 * @match: the key match function.
04db56f8 214 * @key: the current node key.
0422d92c 215 * @iter: Node, if found (output). *iter->node set to NULL if not found.
f0c29ed7 216 *
adc0de68
MD
217 * Uses an iterator initialized by a lookup.
218 * Sets *iter-node to the following node with same key.
219 * Sets *iter->node to NULL if no following node exists with same key.
220 * RCU read-side lock must be held across cds_lfht_lookup and
221 * cds_lfht_next calls, and also between cds_lfht_next calls using the
222 * node returned by a previous cds_lfht_next.
223 * Call with rcu_read_lock held.
3df0c49c 224 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 225 */
0422d92c 226void cds_lfht_next_duplicate(struct cds_lfht *ht,
996ff57c 227 cds_lfht_match_fct match, const void *key,
04db56f8 228 struct cds_lfht_iter *iter);
4e9b9fbf
MD
229
230/*
231 * cds_lfht_first - get the first node in the table.
0422d92c
MD
232 * @ht: the hash table.
233 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
4e9b9fbf
MD
234 *
235 * Output in "*iter". *iter->node set to NULL if table is empty.
236 * Call with rcu_read_lock held.
3df0c49c 237 * Threads calling this API need to be registered RCU read-side threads.
4e9b9fbf
MD
238 */
239void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter);
240
241/*
242 * cds_lfht_next - get the next node in the table.
0422d92c
MD
243 * @ht: the hash table.
244 * @iter: Next node, if exists (output). *iter->node set to NULL if not found.
4e9b9fbf
MD
245 *
246 * Input/Output in "*iter". *iter->node set to NULL if *iter was
247 * pointing to the last table node.
248 * Call with rcu_read_lock held.
3df0c49c 249 * Threads calling this API need to be registered RCU read-side threads.
4e9b9fbf
MD
250 */
251void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
ab7d5fc6 252
18117871 253/*
14044b37 254 * cds_lfht_add - add a node to the hash table.
0422d92c
MD
255 * @ht: the hash table.
256 * @hash: the key hash.
257 * @node: the node to add.
f0c29ed7 258 *
48ed1c18 259 * This function supports adding redundant keys into the table.
3df0c49c
MD
260 * Call with rcu_read_lock held.
261 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 262 */
0422d92c
MD
263void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
264 struct cds_lfht_node *node);
f0c29ed7
MD
265
266/*
14044b37 267 * cds_lfht_add_unique - add a node to hash table, if key is not present.
0422d92c 268 * @ht: the hash table.
6f554439 269 * @hash: the node's hash.
0422d92c 270 * @match: the key match function.
04db56f8 271 * @key: the node's key.
0422d92c 272 * @node: the node to try adding.
f0c29ed7 273 *
6878c72b 274 * Return the node added upon success.
860d07e8
MD
275 * Return the unique node already present upon failure. If
276 * cds_lfht_add_unique fails, the node passed as parameter should be
277 * freed by the caller.
f0c29ed7 278 * Call with rcu_read_lock held.
3df0c49c 279 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18
MD
280 *
281 * The semantic of this function is that if only this function is used
282 * to add keys into the table, no duplicated keys should ever be
283 * observable in the table. The same guarantee apply for combination of
9357c415 284 * add_unique and add_replace (see below).
18117871 285 */
adc0de68 286struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
6f554439 287 unsigned long hash,
0422d92c 288 cds_lfht_match_fct match,
996ff57c 289 const void *key,
adc0de68 290 struct cds_lfht_node *node);
3eca1b8c 291
48ed1c18 292/*
9357c415 293 * cds_lfht_add_replace - replace or add a node within hash table.
0422d92c 294 * @ht: the hash table.
6f554439 295 * @hash: the node's hash.
0422d92c 296 * @match: the key match function.
04db56f8 297 * @key: the node's key.
0422d92c 298 * @node: the node to add.
48ed1c18
MD
299 *
300 * Return the node replaced upon success. If no node matching the key
301 * was present, return NULL, which also means the operation succeeded.
302 * This replacement operation should never fail.
303 * Call with rcu_read_lock held.
3df0c49c 304 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18
MD
305 * After successful replacement, a grace period must be waited for before
306 * freeing the memory reserved for the returned node.
307 *
308 * The semantic of replacement vs lookups is the following: if lookups
9357c415
MD
309 * are performed between a key unique insertion and its removal, we
310 * guarantee that the lookups and get next will always find exactly one
311 * instance of the key if it is replaced concurrently with the lookups.
48ed1c18
MD
312 *
313 * Providing this semantic allows us to ensure that replacement-only
314 * schemes will never generate duplicated keys. It also allows us to
9357c415 315 * guarantee that a combination of add_replace and add_unique updates
48ed1c18
MD
316 * will never generate duplicated keys.
317 */
9357c415 318struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
6f554439 319 unsigned long hash,
0422d92c 320 cds_lfht_match_fct match,
996ff57c 321 const void *key,
adc0de68 322 struct cds_lfht_node *node);
48ed1c18 323
f0c29ed7 324/*
9357c415 325 * cds_lfht_replace - replace a node pointer to by iter within hash table.
0422d92c
MD
326 * @ht: the hash table.
327 * @old_iter: the iterator position of the node to replace.
2e79c445
MD
328 * @hash: the node's hash.
329 * @match: the key match function.
330 * @key: the node's key.
331 * @new_node: the new node to use as replacement.
f0c29ed7 332 *
9357c415 333 * Return 0 if replacement is successful, negative value otherwise.
2e79c445
MD
334 * Replacing a NULL old node or an already removed node will fail with
335 * -ENOENT.
336 * If the hash or value of the node to replace and the new node differ,
337 * this function returns -EINVAL without proceeding to the replacement.
9357c415
MD
338 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
339 * RCU read-side lock must be held between lookup and replacement.
340 * Call with rcu_read_lock held.
3df0c49c 341 * Threads calling this API need to be registered RCU read-side threads.
9357c415
MD
342 * After successful replacement, a grace period must be waited for before
343 * freeing the memory reserved for the old node (which can be accessed
344 * with cds_lfht_iter_get_node).
345 *
346 * The semantic of replacement vs lookups is the following: if lookups
347 * are performed between a key unique insertion and its removal, we
348 * guarantee that the lookups and get next will always find exactly one
349 * instance of the key if it is replaced concurrently with the lookups.
350 *
351 * Providing this semantic allows us to ensure that replacement-only
352 * schemes will never generate duplicated keys. It also allows us to
353 * guarantee that a combination of add_replace and add_unique updates
354 * will never generate duplicated keys.
355 */
2e79c445
MD
356int cds_lfht_replace(struct cds_lfht *ht,
357 struct cds_lfht_iter *old_iter,
358 unsigned long hash,
359 cds_lfht_match_fct match,
360 const void *key,
9357c415
MD
361 struct cds_lfht_node *new_node);
362
363/*
364 * cds_lfht_del - remove node pointed to by iterator from hash table.
0422d92c 365 * @ht: the hash table.
bc8c3c74 366 * @node: the node to delete.
9357c415
MD
367 *
368 * Return 0 if the node is successfully removed, negative value
369 * otherwise.
bc8c3c74 370 * Deleting a NULL node or an already removed node will fail with a
9357c415 371 * negative value.
bc8c3c74
MD
372 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
373 * followed by use of cds_lfht_iter_get_node.
9357c415 374 * RCU read-side lock must be held between lookup and removal.
f0c29ed7 375 * Call with rcu_read_lock held.
3df0c49c 376 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18 377 * After successful removal, a grace period must be waited for before
9357c415
MD
378 * freeing the memory reserved for old node (which can be accessed with
379 * cds_lfht_iter_get_node).
f0c29ed7 380 */
bc8c3c74 381int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node);
ab7d5fc6 382
f0c29ed7 383/*
14044b37 384 * cds_lfht_resize - Force a hash table resize
0422d92c 385 * @ht: the hash table.
1475579c 386 * @new_size: update to this hash table size.
3df0c49c
MD
387 *
388 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 389 */
1475579c 390void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
464a1ec9 391
6d320126
MD
392/*
393 * Note: cds_lfht_for_each are safe for element removal during
394 * iteration.
395 */
396#define cds_lfht_for_each(ht, iter, node) \
397 for (cds_lfht_first(ht, iter), \
398 node = cds_lfht_iter_get_node(iter); \
399 node != NULL; \
400 cds_lfht_next(ht, iter), \
401 node = cds_lfht_iter_get_node(iter))
402
6f554439
LJ
403#define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
404 for (cds_lfht_lookup(ht, hash, match, key, iter), \
6d320126
MD
405 node = cds_lfht_iter_get_node(iter); \
406 node != NULL; \
407 cds_lfht_next_duplicate(ht, match, key, iter), \
408 node = cds_lfht_iter_get_node(iter))
409
410#define cds_lfht_for_each_entry(ht, iter, pos, member) \
411 for (cds_lfht_first(ht, iter), \
412 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
413 typeof(*(pos)), member); \
0b0627bf 414 &(pos)->member != NULL; \
6d320126
MD
415 cds_lfht_next(ht, iter), \
416 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
417 typeof(*(pos)), member))
418
6f554439 419#define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
6d320126 420 iter, pos, member) \
6f554439 421 for (cds_lfht_lookup(ht, hash, match, key, iter), \
6d320126
MD
422 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
423 typeof(*(pos)), member); \
0b0627bf 424 &(pos)->member != NULL; \
6d320126
MD
425 cds_lfht_next_duplicate(ht, match, key, iter), \
426 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
427 typeof(*(pos)), member))
428
01389791
MD
429#ifdef __cplusplus
430}
431#endif
432
a42cc659 433#endif /* _URCU_RCULFHASH_H */
This page took 0.046683 seconds and 4 git commands to generate.