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