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