rculfhash: Remove leftover hash_seed field
[urcu.git] / urcu / rculfhash.h
1 #ifndef _URCU_RCULFHASH_H
2 #define _URCU_RCULFHASH_H
3
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
24 *
25 * Include this file _after_ including your URCU flavor.
26 */
27
28 #include <stdint.h>
29 #include <urcu-call-rcu.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*
36 * struct cds_lfht_node and struct _cds_lfht_node should be aligned on
37 * 4-bytes boundaries because the two lower bits are used as flags.
38 */
39
40 /*
41 * _cds_lfht_node: Contains the internal pointers and reverse-hash
42 * value required for lookup and traversal of the hash table.
43 */
44 struct _cds_lfht_node {
45 struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
46 unsigned long reverse_hash;
47 } __attribute__((aligned(4)));
48
49 /*
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 *
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 */
60 struct cds_lfht_node {
61 /* cache-hot for iteration */
62 struct _cds_lfht_node p; /* needs to be first field */
63 void *key;
64 unsigned int key_len;
65 };
66
67 /* cds_lfht_iter: Used to track state while traversing a hash chain. */
68 struct cds_lfht_iter {
69 struct cds_lfht_node *node, *next;
70 };
71
72 static inline
73 struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter)
74 {
75 return iter->node;
76 }
77
78 struct cds_lfht;
79
80 /*
81 * Caution !
82 * Ensure reader and writer threads are registered as urcu readers.
83 */
84
85 typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length,
86 unsigned long seed);
87 typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, void *key);
88
89 /*
90 * cds_lfht_node_init - initialize a hash table node
91 * @node: the node to initialize.
92 * @key: pointer to the key to use.
93 * @key_len: the length of the key, in bytes.
94 */
95 static inline
96 void cds_lfht_node_init(struct cds_lfht_node *node, void *key,
97 size_t key_len)
98 {
99 node->key = key;
100 node->key_len = key_len;
101 }
102
103 /*
104 * Hash table creation flags.
105 */
106 enum {
107 CDS_LFHT_AUTO_RESIZE = (1U << 0),
108 CDS_LFHT_ACCOUNTING = (1U << 1),
109 };
110
111 /*
112 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
113 */
114 struct cds_lfht *_cds_lfht_new(unsigned long init_size,
115 unsigned long min_alloc_size,
116 int flags,
117 void (*cds_lfht_call_rcu)(struct rcu_head *head,
118 void (*func)(struct rcu_head *head)),
119 void (*cds_lfht_synchronize_rcu)(void),
120 void (*cds_lfht_rcu_read_lock)(void),
121 void (*cds_lfht_rcu_read_unlock)(void),
122 void (*cds_lfht_rcu_thread_offline)(void),
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);
127
128 /*
129 * cds_lfht_new - allocate a hash table.
130 * @init_size: number of nodes to allocate initially. Must be power of two.
131 * @min_alloc_size: the smallest allocation size to use. Must be power of two.
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.
135 * @attr: optional resize worker thread attributes. NULL for default.
136 *
137 * Return NULL on error.
138 * Note: the RCU flavor must be already included before the hash table header.
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.
147 * Threads calling this API need to be registered RCU read-side threads.
148 */
149 static inline
150 struct cds_lfht *cds_lfht_new(unsigned long init_size,
151 unsigned long min_alloc_size,
152 int flags,
153 pthread_attr_t *attr)
154 {
155 return _cds_lfht_new(init_size, min_alloc_size, flags,
156 call_rcu, synchronize_rcu, rcu_read_lock,
157 rcu_read_unlock, rcu_thread_offline,
158 rcu_thread_online, rcu_register_thread,
159 rcu_unregister_thread, attr);
160 }
161
162 /*
163 * cds_lfht_destroy - destroy a hash table.
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
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().
169 *
170 * Return 0 on success, negative error value on error.
171 * Threads calling this API need to be registered RCU read-side threads.
172 */
173 int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr);
174
175 /*
176 * cds_lfht_count_nodes - count the number of nodes in the hash table.
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.
182 *
183 * Call with rcu_read_lock held.
184 * Threads calling this API need to be registered RCU read-side threads.
185 */
186 void cds_lfht_count_nodes(struct cds_lfht *ht,
187 long *split_count_before,
188 unsigned long *count,
189 unsigned long *removed,
190 long *split_count_after);
191
192 /*
193 * cds_lfht_lookup - lookup a node by key.
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.
198 *
199 * Call with rcu_read_lock held.
200 * Threads calling this API need to be registered RCU read-side threads.
201 */
202 void cds_lfht_lookup(struct cds_lfht *ht, cds_lfht_match_fct match,
203 unsigned long hash, void *key, struct cds_lfht_iter *iter);
204
205 /*
206 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
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.
210 *
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.
218 * Threads calling this API need to be registered RCU read-side threads.
219 */
220 void cds_lfht_next_duplicate(struct cds_lfht *ht,
221 cds_lfht_match_fct match, struct cds_lfht_iter *iter);
222
223 /*
224 * cds_lfht_first - get the first node in the table.
225 * @ht: the hash table.
226 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
227 *
228 * Output in "*iter". *iter->node set to NULL if table is empty.
229 * Call with rcu_read_lock held.
230 * Threads calling this API need to be registered RCU read-side threads.
231 */
232 void 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 * @ht: the hash table.
237 * @iter: Next node, if exists (output). *iter->node set to NULL if not found.
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.
242 * Threads calling this API need to be registered RCU read-side threads.
243 */
244 void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter);
245
246 /*
247 * cds_lfht_add - add a node to the hash table.
248 * @ht: the hash table.
249 * @hash: the key hash.
250 * @node: the node to add.
251 *
252 * This function supports adding redundant keys into the table.
253 * Call with rcu_read_lock held.
254 * Threads calling this API need to be registered RCU read-side threads.
255 */
256 void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
257 struct cds_lfht_node *node);
258
259 /*
260 * cds_lfht_add_unique - add a node to hash table, if key is not present.
261 * @ht: the hash table.
262 * @match: the key match function.
263 * @node: the node to try adding.
264 *
265 * Return the node added upon success.
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.
269 * Call with rcu_read_lock held.
270 * Threads calling this API need to be registered RCU read-side threads.
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
275 * add_unique and add_replace (see below).
276 */
277 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
278 cds_lfht_match_fct match,
279 unsigned long hash,
280 struct cds_lfht_node *node);
281
282 /*
283 * cds_lfht_add_replace - replace or add a node within hash table.
284 * @ht: the hash table.
285 * @match: the key match function.
286 * @node: the node to add.
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.
292 * Threads calling this API need to be registered RCU read-side threads.
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
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.
300 *
301 * Providing this semantic allows us to ensure that replacement-only
302 * schemes will never generate duplicated keys. It also allows us to
303 * guarantee that a combination of add_replace and add_unique updates
304 * will never generate duplicated keys.
305 */
306 struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
307 cds_lfht_match_fct match,
308 unsigned long hash,
309 struct cds_lfht_node *node);
310
311 /*
312 * cds_lfht_replace - replace a node pointer to by iter within hash table.
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.
316 *
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.
323 * Threads calling this API need to be registered RCU read-side threads.
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 */
338 int 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.
343 * @ht: the hash table.
344 * @iter: the iterator position of the node to delete.
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.
353 * Call with rcu_read_lock held.
354 * Threads calling this API need to be registered RCU read-side threads.
355 * After successful removal, a grace period must be waited for before
356 * freeing the memory reserved for old node (which can be accessed with
357 * cds_lfht_iter_get_node).
358 */
359 int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter);
360
361 /*
362 * cds_lfht_resize - Force a hash table resize
363 * @ht: the hash table.
364 * @new_size: update to this hash table size.
365 *
366 * Threads calling this API need to be registered RCU read-side threads.
367 */
368 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
369
370 #ifdef __cplusplus
371 }
372 #endif
373
374 #endif /* _URCU_RCULFHASH_H */
This page took 0.036773 seconds and 5 git commands to generate.