Add structure descriptions, remove redundant clear_flag()
[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;
9b35f1e4 65 /* cache-cold for iteration */
abc490a1
MD
66 struct rcu_head head;
67};
68
7f52427b 69/* cds_lfht_iter: Used to track state while traversing a hash chain. */
adc0de68
MD
70struct cds_lfht_iter {
71 struct cds_lfht_node *node, *next;
72};
73
74static inline
75struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
76{
77 return iter->node;
78}
79
14044b37 80struct cds_lfht;
ab7d5fc6 81
5e28c532
MD
82/*
83 * Caution !
abc490a1 84 * Ensure reader and writer threads are registered as urcu readers.
5e28c532
MD
85 */
86
14044b37
MD
87typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
88 unsigned long seed);
89typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len,
90 void *key2, size_t key2_len);
abc490a1 91
f0c29ed7 92/*
14044b37 93 * cds_lfht_node_init - initialize a hash table node
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 */
7a9dcf9b 114struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct,
14044b37
MD
115 cds_lfht_compare_fct compare_fct,
116 unsigned long hash_seed,
117 unsigned long init_size,
5488222b 118 unsigned long min_alloc_size,
b8af5011 119 int flags,
14044b37 120 void (*cds_lfht_call_rcu)(struct rcu_head *head,
1475579c 121 void (*func)(struct rcu_head *head)),
01dbfa62
MD
122 void (*cds_lfht_synchronize_rcu)(void),
123 void (*cds_lfht_rcu_read_lock)(void),
5f511391
MD
124 void (*cds_lfht_rcu_read_unlock)(void),
125 void (*cds_lfht_rcu_thread_offline)(void),
b7d619b0
MD
126 void (*cds_lfht_rcu_thread_online)(void),
127 void (*cds_lfht_rcu_register_thread)(void),
128 void (*cds_lfht_rcu_unregister_thread)(void),
129 pthread_attr_t *attr);
ab7d5fc6 130
7a9dcf9b
MD
131/*
132 * cds_lfht_new - allocate a hash table.
f22dd01d
MD
133 * @hash_fct: the hashing function.
134 * @compare_fct: the key comparison function.
135 * @hash_seed: the seed for hash function.
136 * @init_size: number of nodes to allocate initially. Must be power of two.
5488222b 137 * @min_alloc_size: the smallest allocation size to use. Must be power of two.
f22dd01d
MD
138 * @flags: hash table creation flags (can be combined with bitwise or: '|').
139 * 0: no flags.
140 * CDS_LFHT_AUTO_RESIZE: automatically resize hash 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.
3df0c49c 153 * Threads calling this API need to be registered RCU read-side threads.
7a9dcf9b
MD
154 */
155static inline
156struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
157 cds_lfht_compare_fct compare_fct,
158 unsigned long hash_seed,
159 unsigned long init_size,
5488222b 160 unsigned long min_alloc_size,
b7d619b0
MD
161 int flags,
162 pthread_attr_t *attr)
7a9dcf9b
MD
163{
164 return _cds_lfht_new(hash_fct, compare_fct, hash_seed,
5488222b 165 init_size, min_alloc_size, flags,
7a9dcf9b
MD
166 call_rcu, synchronize_rcu, rcu_read_lock,
167 rcu_read_unlock, rcu_thread_offline,
b7d619b0
MD
168 rcu_thread_online, rcu_register_thread,
169 rcu_unregister_thread, attr);
7a9dcf9b
MD
170}
171
f0c29ed7 172/*
14044b37 173 * cds_lfht_destroy - destroy a hash table.
b7d619b0
MD
174 * @ht: the hash table to destroy.
175 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
176 * The caller will typically want to free this pointer if dynamically
7f52427b
MD
177 * allocated. The attr point can be NULL if the caller does not
178 * need to be informed of the value passed to cds_lfht_new().
6878c72b
MD
179 *
180 * Return 0 on success, negative error value on error.
3df0c49c 181 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 182 */
b7d619b0 183int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
f0c29ed7
MD
184
185/*
14044b37 186 * cds_lfht_count_nodes - count the number of nodes in the hash table.
7f52427b
MD
187 * @ht: the hash table.
188 * @split_count_before: Sample the node count split-counter before traversal.
189 * @count: Traverse the hash table, count the number of nodes observed.
190 * @removed: Number of logically removed nodes observed during traversal.
191 * @split_count_after: Sample the node count split-counter after traversal.
f0c29ed7 192 * Call with rcu_read_lock held.
3df0c49c 193 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 194 */
14044b37 195void cds_lfht_count_nodes(struct cds_lfht *ht,
7f52427b 196 long *split_count_before,
adc0de68 197 unsigned long *count,
973e5e1b 198 unsigned long *removed,
7f52427b 199 long *split_count_after);
ab7d5fc6 200
f0c29ed7 201/*
14044b37 202 * cds_lfht_lookup - lookup a node by key.
f0c29ed7 203 *
adc0de68 204 * Output in "*iter". *iter->node set to NULL if not found.
f0c29ed7 205 * Call with rcu_read_lock held.
3df0c49c 206 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 207 */
adc0de68
MD
208void cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len,
209 struct cds_lfht_iter *iter);
ab7d5fc6 210
f0c29ed7 211/*
3883c0e5 212 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
f0c29ed7 213 *
adc0de68
MD
214 * Uses an iterator initialized by a lookup.
215 * Sets *iter-node to the following node with same key.
216 * Sets *iter->node to NULL if no following node exists with same key.
217 * RCU read-side lock must be held across cds_lfht_lookup and
218 * cds_lfht_next calls, and also between cds_lfht_next calls using the
219 * node returned by a previous cds_lfht_next.
220 * Call with rcu_read_lock held.
3df0c49c 221 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 222 */
3883c0e5 223void cds_lfht_next_duplicate(struct cds_lfht *ht, struct cds_lfht_iter *iter);
4e9b9fbf
MD
224
225/*
226 * cds_lfht_first - get the first node in the table.
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.
236 *
237 * Input/Output in "*iter". *iter->node set to NULL if *iter was
238 * pointing to the last table node.
239 * Call with rcu_read_lock held.
3df0c49c 240 * Threads calling this API need to be registered RCU read-side threads.
4e9b9fbf
MD
241 */
242void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
ab7d5fc6 243
18117871 244/*
14044b37 245 * cds_lfht_add - add a node to the hash table.
f0c29ed7 246 *
48ed1c18 247 * This function supports adding redundant keys into the table.
3df0c49c
MD
248 * Call with rcu_read_lock held.
249 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 250 */
14044b37 251void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node);
f0c29ed7
MD
252
253/*
14044b37 254 * cds_lfht_add_unique - add a node to hash table, if key is not present.
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
MD
268struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
269 struct cds_lfht_node *node);
3eca1b8c 270
48ed1c18 271/*
9357c415 272 * cds_lfht_add_replace - replace or add a node within hash table.
48ed1c18
MD
273 *
274 * Return the node replaced upon success. If no node matching the key
275 * was present, return NULL, which also means the operation succeeded.
276 * This replacement operation should never fail.
277 * Call with rcu_read_lock held.
3df0c49c 278 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18
MD
279 * After successful replacement, a grace period must be waited for before
280 * freeing the memory reserved for the returned node.
281 *
282 * The semantic of replacement vs lookups is the following: if lookups
9357c415
MD
283 * are performed between a key unique insertion and its removal, we
284 * guarantee that the lookups and get next will always find exactly one
285 * instance of the key if it is replaced concurrently with the lookups.
48ed1c18
MD
286 *
287 * Providing this semantic allows us to ensure that replacement-only
288 * schemes will never generate duplicated keys. It also allows us to
9357c415 289 * guarantee that a combination of add_replace and add_unique updates
48ed1c18
MD
290 * will never generate duplicated keys.
291 */
9357c415 292struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
adc0de68 293 struct cds_lfht_node *node);
48ed1c18 294
f0c29ed7 295/*
9357c415 296 * cds_lfht_replace - replace a node pointer to by iter within hash table.
f0c29ed7 297 *
9357c415
MD
298 * Return 0 if replacement is successful, negative value otherwise.
299 * Replacing a NULL old node or an already removed node will fail with a
300 * negative value.
301 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
302 * RCU read-side lock must be held between lookup and replacement.
303 * Call with rcu_read_lock held.
3df0c49c 304 * Threads calling this API need to be registered RCU read-side threads.
9357c415
MD
305 * After successful replacement, a grace period must be waited for before
306 * freeing the memory reserved for the old node (which can be accessed
307 * with cds_lfht_iter_get_node).
308 *
309 * The semantic of replacement vs lookups is the following: if lookups
310 * are performed between a key unique insertion and its removal, we
311 * guarantee that the lookups and get next will always find exactly one
312 * instance of the key if it is replaced concurrently with the lookups.
313 *
314 * Providing this semantic allows us to ensure that replacement-only
315 * schemes will never generate duplicated keys. It also allows us to
316 * guarantee that a combination of add_replace and add_unique updates
317 * will never generate duplicated keys.
318 */
319int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter,
320 struct cds_lfht_node *new_node);
321
322/*
323 * cds_lfht_del - remove node pointed to by iterator from hash table.
324 *
325 * Return 0 if the node is successfully removed, negative value
326 * otherwise.
327 * Replacing a NULL node or an already removed node will fail with a
328 * negative value.
329 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
330 * cds_lfht_iter_get_node.
331 * RCU read-side lock must be held between lookup and removal.
f0c29ed7 332 * Call with rcu_read_lock held.
3df0c49c 333 * Threads calling this API need to be registered RCU read-side threads.
48ed1c18 334 * After successful removal, a grace period must be waited for before
9357c415
MD
335 * freeing the memory reserved for old node (which can be accessed with
336 * cds_lfht_iter_get_node).
f0c29ed7 337 */
9357c415 338int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter);
ab7d5fc6 339
f0c29ed7 340/*
14044b37 341 * cds_lfht_resize - Force a hash table resize
1475579c 342 * @new_size: update to this hash table size.
3df0c49c
MD
343 *
344 * Threads calling this API need to be registered RCU read-side threads.
f0c29ed7 345 */
1475579c 346void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
464a1ec9 347
01389791
MD
348#ifdef __cplusplus
349}
350#endif
351
a42cc659 352#endif /* _URCU_RCULFHASH_H */
This page took 0.040301 seconds and 4 git commands to generate.