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