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