move memory management code out as rculfhash-mm-order.c
[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
LJ
41 *
42 * struct cds_lfht_node should be aligned on 4-bytes boundaries because
43 * the two lower bits are used as flags.
7f52427b 44 *
ae62aa6a
MD
45 * struct cds_lfht_node can be embedded into a structure (as a field).
46 * caa_container_of() can be used to get the structure from the struct
47 * cds_lfht_node after a lookup.
04db56f8
LJ
48 *
49 * The structure which embeds it typically holds the key (or key-value
50 * pair) of the object. The caller code is responsible for calculation
51 * of the hash value for cds_lfht APIs.
ae62aa6a 52 */
14044b37 53struct cds_lfht_node {
1ee8f000 54 struct cds_lfht_node *next; /* ptr | BUCKET_FLAG | REMOVED_FLAG */
04db56f8
LJ
55 unsigned long reverse_hash;
56} __attribute__((aligned(4)));
abc490a1 57
7f52427b 58/* cds_lfht_iter: Used to track state while traversing a hash chain. */
adc0de68
MD
59struct cds_lfht_iter {
60 struct cds_lfht_node *node, *next;
61};
62
63static inline
64struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
65{
66 return iter->node;
67}
68
14044b37 69struct cds_lfht;
ab7d5fc6 70
5e28c532
MD
71/*
72 * Caution !
abc490a1 73 * Ensure reader and writer threads are registered as urcu readers.
5e28c532
MD
74 */
75
996ff57c 76typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key);
abc490a1 77
f0c29ed7 78/*
14044b37 79 * cds_lfht_node_init - initialize a hash table node
0422d92c 80 * @node: the node to initialize.
04db56f8
LJ
81 *
82 * This function is kept to be eventually used for debugging purposes
83 * (detection of memory corruption).
f0c29ed7 84 */
abc490a1 85static inline
04db56f8 86void cds_lfht_node_init(struct cds_lfht_node *node)
abc490a1 87{
abc490a1 88}
674f7a69 89
b8af5011
MD
90/*
91 * Hash table creation flags.
92 */
93enum {
94 CDS_LFHT_AUTO_RESIZE = (1U << 0),
5afadd12 95 CDS_LFHT_ACCOUNTING = (1U << 1),
b8af5011
MD
96};
97
0b6aa001
LJ
98struct cds_lfht_mm_type {
99 struct cds_lfht *(*alloc_cds_lfht)(unsigned long min_nr_alloc_buckets,
100 unsigned long max_nr_buckets);
101 void (*alloc_bucket_table)(struct cds_lfht *ht, unsigned long order);
102 void (*free_bucket_table)(struct cds_lfht *ht, unsigned long order);
103 struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht,
104 unsigned long index);
105};
106
107extern const struct cds_lfht_mm_type cds_lfht_mm_order;
108
674f7a69 109/*
7a9dcf9b 110 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
674f7a69 111 */
0422d92c 112struct cds_lfht *_cds_lfht_new(unsigned long init_size,
0722081a 113 unsigned long min_nr_alloc_buckets,
747d725c 114 unsigned long max_nr_buckets,
b8af5011 115 int flags,
0b6aa001 116 const struct cds_lfht_mm_type *mm,
7b17c13e 117 const struct rcu_flavor_struct *flavor,
b7d619b0 118 pthread_attr_t *attr);
ab7d5fc6 119
7a9dcf9b
MD
120/*
121 * cds_lfht_new - allocate a hash table.
747d725c
LJ
122 * @init_size: number of buckets to allocate initially. Must be power of two.
123 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
124 * (must be power of two)
125 * @max_nr_buckets: the maximum number of hash table buckets allowed.
126 * (must be power of two)
f22dd01d
MD
127 * @flags: hash table creation flags (can be combined with bitwise or: '|').
128 * 0: no flags.
129 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
44bbe7fa
LJ
130 * CDS_LFHT_ACCOUNTING: count the number of node addition
131 * and removal in the table
b7d619b0 132 * @attr: optional resize worker thread attributes. NULL for default.
7a9dcf9b 133 *
7a9dcf9b
MD
134 * Return NULL on error.
135 * Note: the RCU flavor must be already included before the hash table header.
b7d619b0
MD
136 *
137 * The programmer is responsible for ensuring that resize operation has a
138 * priority equal to hash table updater threads. It should be performed by
139 * specifying the appropriate priority in the pthread "attr" argument, and,
140 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
141 * this priority level. Having lower priority for call_rcu and resize threads
142 * does not pose any correctness issue, but the resize operations could be
143 * starved by updates, thus leading to long hash table bucket chains.
44bbe7fa
LJ
144 * Threads calling this API are NOT required to be registered RCU read-side
145 * threads. It can be called very early.(before rcu is initialized ...etc.)
7a9dcf9b
MD
146 */
147static inline
0422d92c 148struct cds_lfht *cds_lfht_new(unsigned long init_size,
0722081a 149 unsigned long min_nr_alloc_buckets,
747d725c 150 unsigned long max_nr_buckets,
b7d619b0
MD
151 int flags,
152 pthread_attr_t *attr)
7a9dcf9b 153{
7b17c13e 154 return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets,
0b6aa001 155 flags, &cds_lfht_mm_order, &rcu_flavor, attr);
7a9dcf9b
MD
156}
157
f0c29ed7 158/*
14044b37 159 * cds_lfht_destroy - destroy a hash table.
b7d619b0
MD
160 * @ht: the hash table to destroy.
161 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
162 * The caller will typically want to free this pointer if dynamically
7f52427b
MD
163 * allocated. The attr point can be NULL if the caller does not
164 * need to be informed of the value passed to cds_lfht_new().
6878c72b
MD
165 *
166 * Return 0 on success, negative error value on error.
3df0c49c 167 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 168 */
b7d619b0 169int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
f0c29ed7
MD
170
171/*
14044b37 172 * cds_lfht_count_nodes - count the number of nodes in the hash table.
7f52427b
MD
173 * @ht: the hash table.
174 * @split_count_before: Sample the node count split-counter before traversal.
175 * @count: Traverse the hash table, count the number of nodes observed.
176 * @removed: Number of logically removed nodes observed during traversal.
177 * @split_count_after: Sample the node count split-counter after traversal.
0422d92c 178 *
f0c29ed7 179 * Call with rcu_read_lock held.
3df0c49c 180 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 181 */
14044b37 182void cds_lfht_count_nodes(struct cds_lfht *ht,
7f52427b 183 long *split_count_before,
adc0de68 184 unsigned long *count,
973e5e1b 185 unsigned long *removed,
7f52427b 186 long *split_count_after);
ab7d5fc6 187
f0c29ed7 188/*
14044b37 189 * cds_lfht_lookup - lookup a node by key.
0422d92c 190 * @ht: the hash table.
0422d92c 191 * @hash: the key hash.
6f554439
LJ
192 * @match: the key match function.
193 * @key: the current node key.
0422d92c 194 * @iter: Node, if found (output). *iter->node set to NULL if not found.
f0c29ed7 195 *
f0c29ed7 196 * Call with rcu_read_lock held.
3df0c49c 197 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 198 */
6f554439 199void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
996ff57c 200 cds_lfht_match_fct match, const void *key,
6f554439 201 struct cds_lfht_iter *iter);
ab7d5fc6 202
f0c29ed7 203/*
3883c0e5 204 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
0422d92c
MD
205 * @ht: the hash table.
206 * @match: the key match function.
04db56f8 207 * @key: the current node key.
0422d92c 208 * @iter: Node, if found (output). *iter->node set to NULL if not found.
f0c29ed7 209 *
adc0de68
MD
210 * Uses an iterator initialized by a lookup.
211 * Sets *iter-node to the following node with same key.
212 * Sets *iter->node to NULL if no following node exists with same key.
213 * RCU read-side lock must be held across cds_lfht_lookup and
214 * cds_lfht_next calls, and also between cds_lfht_next calls using the
215 * node returned by a previous cds_lfht_next.
216 * Call with rcu_read_lock held.
3df0c49c 217 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 218 */
0422d92c 219void cds_lfht_next_duplicate(struct cds_lfht *ht,
996ff57c 220 cds_lfht_match_fct match, const void *key,
04db56f8 221 struct cds_lfht_iter *iter);
4e9b9fbf
MD
222
223/*
224 * cds_lfht_first - get the first node in the table.
0422d92c
MD
225 * @ht: the hash table.
226 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
4e9b9fbf
MD
227 *
228 * Output in "*iter". *iter->node set to NULL if table is empty.
229 * Call with rcu_read_lock held.
3df0c49c 230 * Threads calling this API need to be registered RCU read-side threads.
4e9b9fbf
MD
231 */
232void 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.
0422d92c
MD
236 * @ht: the hash table.
237 * @iter: Next node, if exists (output). *iter->node set to NULL if not found.
4e9b9fbf
MD
238 *
239 * Input/Output in "*iter". *iter->node set to NULL if *iter was
240 * pointing to the last table node.
241 * Call with rcu_read_lock held.
3df0c49c 242 * Threads calling this API need to be registered RCU read-side threads.
4e9b9fbf
MD
243 */
244void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
ab7d5fc6 245
18117871 246/*
14044b37 247 * cds_lfht_add - add a node to the hash table.
0422d92c
MD
248 * @ht: the hash table.
249 * @hash: the key hash.
250 * @node: the node to add.
f0c29ed7 251 *
48ed1c18 252 * This function supports adding redundant keys into the table.
3df0c49c
MD
253 * Call with rcu_read_lock held.
254 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 255 */
0422d92c
MD
256void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
257 struct cds_lfht_node *node);
f0c29ed7
MD
258
259/*
14044b37 260 * cds_lfht_add_unique - add a node to hash table, if key is not present.
0422d92c 261 * @ht: the hash table.
6f554439 262 * @hash: the node's hash.
0422d92c 263 * @match: the key match function.
04db56f8 264 * @key: the node's key.
0422d92c 265 * @node: the node to try adding.
f0c29ed7 266 *
6878c72b 267 * Return the node added upon success.
860d07e8
MD
268 * Return the unique node already present upon failure. If
269 * cds_lfht_add_unique fails, the node passed as parameter should be
270 * freed by the caller.
f0c29ed7 271 * Call with rcu_read_lock held.
3df0c49c 272 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18
MD
273 *
274 * The semantic of this function is that if only this function is used
275 * to add keys into the table, no duplicated keys should ever be
276 * observable in the table. The same guarantee apply for combination of
9357c415 277 * add_unique and add_replace (see below).
18117871 278 */
adc0de68 279struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
6f554439 280 unsigned long hash,
0422d92c 281 cds_lfht_match_fct match,
996ff57c 282 const void *key,
adc0de68 283 struct cds_lfht_node *node);
3eca1b8c 284
48ed1c18 285/*
9357c415 286 * cds_lfht_add_replace - replace or add a node within hash table.
0422d92c 287 * @ht: the hash table.
6f554439 288 * @hash: the node's hash.
0422d92c 289 * @match: the key match function.
04db56f8 290 * @key: the node's key.
0422d92c 291 * @node: the node to add.
48ed1c18
MD
292 *
293 * Return the node replaced upon success. If no node matching the key
294 * was present, return NULL, which also means the operation succeeded.
295 * This replacement operation should never fail.
296 * Call with rcu_read_lock held.
3df0c49c 297 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18
MD
298 * After successful replacement, a grace period must be waited for before
299 * freeing the memory reserved for the returned node.
300 *
301 * The semantic of replacement vs lookups is the following: if lookups
9357c415
MD
302 * are performed between a key unique insertion and its removal, we
303 * guarantee that the lookups and get next will always find exactly one
304 * instance of the key if it is replaced concurrently with the lookups.
48ed1c18
MD
305 *
306 * Providing this semantic allows us to ensure that replacement-only
307 * schemes will never generate duplicated keys. It also allows us to
9357c415 308 * guarantee that a combination of add_replace and add_unique updates
48ed1c18
MD
309 * will never generate duplicated keys.
310 */
9357c415 311struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
6f554439 312 unsigned long hash,
0422d92c 313 cds_lfht_match_fct match,
996ff57c 314 const void *key,
adc0de68 315 struct cds_lfht_node *node);
48ed1c18 316
f0c29ed7 317/*
9357c415 318 * cds_lfht_replace - replace a node pointer to by iter within hash table.
0422d92c
MD
319 * @ht: the hash table.
320 * @old_iter: the iterator position of the node to replace.
321 * @now_node: the new node to try using for replacement.
f0c29ed7 322 *
9357c415
MD
323 * Return 0 if replacement is successful, negative value otherwise.
324 * Replacing a NULL old node or an already removed node will fail with a
325 * negative value.
326 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
327 * RCU read-side lock must be held between lookup and replacement.
328 * Call with rcu_read_lock held.
3df0c49c 329 * Threads calling this API need to be registered RCU read-side threads.
9357c415
MD
330 * After successful replacement, a grace period must be waited for before
331 * freeing the memory reserved for the old node (which can be accessed
332 * with cds_lfht_iter_get_node).
333 *
334 * The semantic of replacement vs lookups is the following: if lookups
335 * are performed between a key unique insertion and its removal, we
336 * guarantee that the lookups and get next will always find exactly one
337 * instance of the key if it is replaced concurrently with the lookups.
338 *
339 * Providing this semantic allows us to ensure that replacement-only
340 * schemes will never generate duplicated keys. It also allows us to
341 * guarantee that a combination of add_replace and add_unique updates
342 * will never generate duplicated keys.
343 */
344int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter,
345 struct cds_lfht_node *new_node);
346
347/*
348 * cds_lfht_del - remove node pointed to by iterator from hash table.
0422d92c
MD
349 * @ht: the hash table.
350 * @iter: the iterator position of the node to delete.
9357c415
MD
351 *
352 * Return 0 if the node is successfully removed, negative value
353 * otherwise.
354 * Replacing a NULL node or an already removed node will fail with a
355 * negative value.
356 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
357 * cds_lfht_iter_get_node.
358 * RCU read-side lock must be held between lookup and removal.
f0c29ed7 359 * Call with rcu_read_lock held.
3df0c49c 360 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18 361 * After successful removal, a grace period must be waited for before
9357c415
MD
362 * freeing the memory reserved for old node (which can be accessed with
363 * cds_lfht_iter_get_node).
f0c29ed7 364 */
9357c415 365int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter);
ab7d5fc6 366
f0c29ed7 367/*
14044b37 368 * cds_lfht_resize - Force a hash table resize
0422d92c 369 * @ht: the hash table.
1475579c 370 * @new_size: update to this hash table size.
3df0c49c
MD
371 *
372 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 373 */
1475579c 374void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
464a1ec9 375
6d320126
MD
376/*
377 * Note: cds_lfht_for_each are safe for element removal during
378 * iteration.
379 */
380#define cds_lfht_for_each(ht, iter, node) \
381 for (cds_lfht_first(ht, iter), \
382 node = cds_lfht_iter_get_node(iter); \
383 node != NULL; \
384 cds_lfht_next(ht, iter), \
385 node = cds_lfht_iter_get_node(iter))
386
6f554439
LJ
387#define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
388 for (cds_lfht_lookup(ht, hash, match, key, iter), \
6d320126
MD
389 node = cds_lfht_iter_get_node(iter); \
390 node != NULL; \
391 cds_lfht_next_duplicate(ht, match, key, iter), \
392 node = cds_lfht_iter_get_node(iter))
393
394#define cds_lfht_for_each_entry(ht, iter, pos, member) \
395 for (cds_lfht_first(ht, iter), \
396 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
397 typeof(*(pos)), member); \
0b0627bf 398 &(pos)->member != NULL; \
6d320126
MD
399 cds_lfht_next(ht, iter), \
400 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
401 typeof(*(pos)), member))
402
6f554439 403#define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
6d320126 404 iter, pos, member) \
6f554439 405 for (cds_lfht_lookup(ht, hash, match, key, iter), \
6d320126
MD
406 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
407 typeof(*(pos)), member); \
0b0627bf 408 &(pos)->member != NULL; \
6d320126
MD
409 cds_lfht_next_duplicate(ht, match, key, iter), \
410 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
411 typeof(*(pos)), member))
412
01389791
MD
413#ifdef __cplusplus
414}
415#endif
416
a42cc659 417#endif /* _URCU_RCULFHASH_H */
This page took 0.067123 seconds and 4 git commands to generate.