rculfhash: Move key out of struct lfht_test_node, move key_len to caller
[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>
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
cf77d1fa
MD
24 *
25 * Include this file _after_ including your URCU flavor.
f0c29ed7
MD
26 */
27
674f7a69 28#include <stdint.h>
abc490a1
MD
29#include <urcu-call-rcu.h>
30
01389791
MD
31#ifdef __cplusplus
32extern "C" {
33#endif
34
7f61a77f 35
7f52427b 36/*
04db56f8 37 * cds_lfht_node: Contains the next pointers and reverse-hash
7f52427b 38 * value required for lookup and traversal of the hash table.
04db56f8
LJ
39 *
40 * struct cds_lfht_node should be aligned on 4-bytes boundaries because
41 * the two lower bits are used as flags.
7f52427b 42 *
ae62aa6a
MD
43 * struct cds_lfht_node can be embedded into a structure (as a field).
44 * caa_container_of() can be used to get the structure from the struct
45 * cds_lfht_node after a lookup.
04db56f8
LJ
46 *
47 * The structure which embeds it typically holds the key (or key-value
48 * pair) of the object. The caller code is responsible for calculation
49 * of the hash value for cds_lfht APIs.
ae62aa6a 50 */
14044b37 51struct cds_lfht_node {
04db56f8
LJ
52 struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
53 unsigned long reverse_hash;
54} __attribute__((aligned(4)));
abc490a1 55
7f52427b 56/* cds_lfht_iter: Used to track state while traversing a hash chain. */
adc0de68
MD
57struct cds_lfht_iter {
58 struct cds_lfht_node *node, *next;
59};
60
61static inline
62struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
63{
64 return iter->node;
65}
66
14044b37 67struct cds_lfht;
ab7d5fc6 68
5e28c532
MD
69/*
70 * Caution !
abc490a1 71 * Ensure reader and writer threads are registered as urcu readers.
5e28c532
MD
72 */
73
14044b37
MD
74typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
75 unsigned long seed);
0422d92c 76typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, 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
674f7a69 98/*
7a9dcf9b 99 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
674f7a69 100 */
0422d92c 101struct cds_lfht *_cds_lfht_new(unsigned long init_size,
5488222b 102 unsigned long min_alloc_size,
b8af5011 103 int flags,
14044b37 104 void (*cds_lfht_call_rcu)(struct rcu_head *head,
1475579c 105 void (*func)(struct rcu_head *head)),
01dbfa62
MD
106 void (*cds_lfht_synchronize_rcu)(void),
107 void (*cds_lfht_rcu_read_lock)(void),
5f511391
MD
108 void (*cds_lfht_rcu_read_unlock)(void),
109 void (*cds_lfht_rcu_thread_offline)(void),
b7d619b0
MD
110 void (*cds_lfht_rcu_thread_online)(void),
111 void (*cds_lfht_rcu_register_thread)(void),
112 void (*cds_lfht_rcu_unregister_thread)(void),
113 pthread_attr_t *attr);
ab7d5fc6 114
7a9dcf9b
MD
115/*
116 * cds_lfht_new - allocate a hash table.
f22dd01d 117 * @init_size: number of nodes to allocate initially. Must be power of two.
5488222b 118 * @min_alloc_size: the smallest allocation size to use. Must be power of two.
f22dd01d
MD
119 * @flags: hash table creation flags (can be combined with bitwise or: '|').
120 * 0: no flags.
121 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
b7d619b0 122 * @attr: optional resize worker thread attributes. NULL for default.
7a9dcf9b 123 *
7a9dcf9b
MD
124 * Return NULL on error.
125 * Note: the RCU flavor must be already included before the hash table header.
b7d619b0
MD
126 *
127 * The programmer is responsible for ensuring that resize operation has a
128 * priority equal to hash table updater threads. It should be performed by
129 * specifying the appropriate priority in the pthread "attr" argument, and,
130 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
131 * this priority level. Having lower priority for call_rcu and resize threads
132 * does not pose any correctness issue, but the resize operations could be
133 * starved by updates, thus leading to long hash table bucket chains.
3df0c49c 134 * Threads calling this API need to be registered RCU read-side threads.
7a9dcf9b
MD
135 */
136static inline
0422d92c 137struct cds_lfht *cds_lfht_new(unsigned long init_size,
5488222b 138 unsigned long min_alloc_size,
b7d619b0
MD
139 int flags,
140 pthread_attr_t *attr)
7a9dcf9b 141{
0422d92c 142 return _cds_lfht_new(init_size, min_alloc_size, flags,
7a9dcf9b
MD
143 call_rcu, synchronize_rcu, rcu_read_lock,
144 rcu_read_unlock, rcu_thread_offline,
b7d619b0
MD
145 rcu_thread_online, rcu_register_thread,
146 rcu_unregister_thread, attr);
7a9dcf9b
MD
147}
148
f0c29ed7 149/*
14044b37 150 * cds_lfht_destroy - destroy a hash table.
b7d619b0
MD
151 * @ht: the hash table to destroy.
152 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
153 * The caller will typically want to free this pointer if dynamically
7f52427b
MD
154 * allocated. The attr point can be NULL if the caller does not
155 * need to be informed of the value passed to cds_lfht_new().
6878c72b
MD
156 *
157 * Return 0 on success, negative error value on error.
3df0c49c 158 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 159 */
b7d619b0 160int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
f0c29ed7
MD
161
162/*
14044b37 163 * cds_lfht_count_nodes - count the number of nodes in the hash table.
7f52427b
MD
164 * @ht: the hash table.
165 * @split_count_before: Sample the node count split-counter before traversal.
166 * @count: Traverse the hash table, count the number of nodes observed.
167 * @removed: Number of logically removed nodes observed during traversal.
168 * @split_count_after: Sample the node count split-counter after traversal.
0422d92c 169 *
f0c29ed7 170 * Call with rcu_read_lock held.
3df0c49c 171 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 172 */
14044b37 173void cds_lfht_count_nodes(struct cds_lfht *ht,
7f52427b 174 long *split_count_before,
adc0de68 175 unsigned long *count,
973e5e1b 176 unsigned long *removed,
7f52427b 177 long *split_count_after);
ab7d5fc6 178
f0c29ed7 179/*
14044b37 180 * cds_lfht_lookup - lookup a node by key.
0422d92c
MD
181 * @ht: the hash table.
182 * @match: the key match function.
183 * @hash: the key hash.
184 * @iter: Node, if found (output). *iter->node set to NULL if not found.
f0c29ed7 185 *
f0c29ed7 186 * Call with rcu_read_lock held.
3df0c49c 187 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 188 */
0422d92c
MD
189void cds_lfht_lookup(struct cds_lfht *ht, cds_lfht_match_fct match,
190 unsigned long hash, void *key, struct cds_lfht_iter *iter);
ab7d5fc6 191
f0c29ed7 192/*
3883c0e5 193 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
0422d92c
MD
194 * @ht: the hash table.
195 * @match: the key match function.
04db56f8 196 * @key: the current node key.
0422d92c 197 * @iter: Node, if found (output). *iter->node set to NULL if not found.
f0c29ed7 198 *
adc0de68
MD
199 * Uses an iterator initialized by a lookup.
200 * Sets *iter-node to the following node with same key.
201 * Sets *iter->node to NULL if no following node exists with same key.
202 * RCU read-side lock must be held across cds_lfht_lookup and
203 * cds_lfht_next calls, and also between cds_lfht_next calls using the
204 * node returned by a previous cds_lfht_next.
205 * Call with rcu_read_lock held.
3df0c49c 206 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 207 */
0422d92c 208void cds_lfht_next_duplicate(struct cds_lfht *ht,
04db56f8
LJ
209 cds_lfht_match_fct match, void *key,
210 struct cds_lfht_iter *iter);
4e9b9fbf
MD
211
212/*
213 * cds_lfht_first - get the first node in the table.
0422d92c
MD
214 * @ht: the hash table.
215 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
4e9b9fbf
MD
216 *
217 * Output in "*iter". *iter->node set to NULL if table is empty.
218 * Call with rcu_read_lock held.
3df0c49c 219 * Threads calling this API need to be registered RCU read-side threads.
4e9b9fbf
MD
220 */
221void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter);
222
223/*
224 * cds_lfht_next - get the next node in the table.
0422d92c
MD
225 * @ht: the hash table.
226 * @iter: Next node, if exists (output). *iter->node set to NULL if not found.
4e9b9fbf
MD
227 *
228 * Input/Output in "*iter". *iter->node set to NULL if *iter was
229 * pointing to the last table node.
230 * Call with rcu_read_lock held.
3df0c49c 231 * Threads calling this API need to be registered RCU read-side threads.
4e9b9fbf
MD
232 */
233void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
ab7d5fc6 234
18117871 235/*
14044b37 236 * cds_lfht_add - add a node to the hash table.
0422d92c
MD
237 * @ht: the hash table.
238 * @hash: the key hash.
239 * @node: the node to add.
f0c29ed7 240 *
48ed1c18 241 * This function supports adding redundant keys into the table.
3df0c49c
MD
242 * Call with rcu_read_lock held.
243 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 244 */
0422d92c
MD
245void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
246 struct cds_lfht_node *node);
f0c29ed7
MD
247
248/*
14044b37 249 * cds_lfht_add_unique - add a node to hash table, if key is not present.
0422d92c
MD
250 * @ht: the hash table.
251 * @match: the key match function.
04db56f8
LJ
252 * @key: the node's key.
253 * @hash: the node's hash.
0422d92c 254 * @node: the node to try adding.
f0c29ed7 255 *
6878c72b 256 * Return the node added upon success.
860d07e8
MD
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.
f0c29ed7 260 * Call with rcu_read_lock held.
3df0c49c 261 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18
MD
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
9357c415 266 * add_unique and add_replace (see below).
18117871 267 */
adc0de68 268struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
0422d92c 269 cds_lfht_match_fct match,
04db56f8 270 void *key,
0422d92c 271 unsigned long hash,
adc0de68 272 struct cds_lfht_node *node);
3eca1b8c 273
48ed1c18 274/*
9357c415 275 * cds_lfht_add_replace - replace or add a node within hash table.
0422d92c
MD
276 * @ht: the hash table.
277 * @match: the key match function.
04db56f8
LJ
278 * @key: the node's key.
279 * @hash: the node's hash.
0422d92c 280 * @node: the node to add.
48ed1c18
MD
281 *
282 * Return the node replaced upon success. If no node matching the key
283 * was present, return NULL, which also means the operation succeeded.
284 * This replacement operation should never fail.
285 * Call with rcu_read_lock held.
3df0c49c 286 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18
MD
287 * After successful replacement, a grace period must be waited for before
288 * freeing the memory reserved for the returned node.
289 *
290 * The semantic of replacement vs lookups is the following: if lookups
9357c415
MD
291 * are performed between a key unique insertion and its removal, we
292 * guarantee that the lookups and get next will always find exactly one
293 * instance of the key if it is replaced concurrently with the lookups.
48ed1c18
MD
294 *
295 * Providing this semantic allows us to ensure that replacement-only
296 * schemes will never generate duplicated keys. It also allows us to
9357c415 297 * guarantee that a combination of add_replace and add_unique updates
48ed1c18
MD
298 * will never generate duplicated keys.
299 */
9357c415 300struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
0422d92c 301 cds_lfht_match_fct match,
04db56f8 302 void *key,
0422d92c 303 unsigned long hash,
adc0de68 304 struct cds_lfht_node *node);
48ed1c18 305
f0c29ed7 306/*
9357c415 307 * cds_lfht_replace - replace a node pointer to by iter within hash table.
0422d92c
MD
308 * @ht: the hash table.
309 * @old_iter: the iterator position of the node to replace.
310 * @now_node: the new node to try using for replacement.
f0c29ed7 311 *
9357c415
MD
312 * Return 0 if replacement is successful, negative value otherwise.
313 * Replacing a NULL old node or an already removed node will fail with a
314 * negative value.
315 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
316 * RCU read-side lock must be held between lookup and replacement.
317 * Call with rcu_read_lock held.
3df0c49c 318 * Threads calling this API need to be registered RCU read-side threads.
9357c415
MD
319 * After successful replacement, a grace period must be waited for before
320 * freeing the memory reserved for the old node (which can be accessed
321 * with cds_lfht_iter_get_node).
322 *
323 * The semantic of replacement vs lookups is the following: if lookups
324 * are performed between a key unique insertion and its removal, we
325 * guarantee that the lookups and get next will always find exactly one
326 * instance of the key if it is replaced concurrently with the lookups.
327 *
328 * Providing this semantic allows us to ensure that replacement-only
329 * schemes will never generate duplicated keys. It also allows us to
330 * guarantee that a combination of add_replace and add_unique updates
331 * will never generate duplicated keys.
332 */
333int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter,
334 struct cds_lfht_node *new_node);
335
336/*
337 * cds_lfht_del - remove node pointed to by iterator from hash table.
0422d92c
MD
338 * @ht: the hash table.
339 * @iter: the iterator position of the node to delete.
9357c415
MD
340 *
341 * Return 0 if the node is successfully removed, negative value
342 * otherwise.
343 * Replacing a NULL node or an already removed node will fail with a
344 * negative value.
345 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
346 * cds_lfht_iter_get_node.
347 * RCU read-side lock must be held between lookup and removal.
f0c29ed7 348 * Call with rcu_read_lock held.
3df0c49c 349 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18 350 * After successful removal, a grace period must be waited for before
9357c415
MD
351 * freeing the memory reserved for old node (which can be accessed with
352 * cds_lfht_iter_get_node).
f0c29ed7 353 */
9357c415 354int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter);
ab7d5fc6 355
f0c29ed7 356/*
14044b37 357 * cds_lfht_resize - Force a hash table resize
0422d92c 358 * @ht: the hash table.
1475579c 359 * @new_size: update to this hash table size.
3df0c49c
MD
360 *
361 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 362 */
1475579c 363void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
464a1ec9 364
01389791
MD
365#ifdef __cplusplus
366}
367#endif
368
a42cc659 369#endif /* _URCU_RCULFHASH_H */
This page took 0.041453 seconds and 4 git commands to generate.