1 #ifndef _URCU_RCULFHASH_H
2 #define _URCU_RCULFHASH_H
7 * Userspace RCU library - Lock-Free RCU Hash Table
9 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
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.
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.
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
26 * Include this file _after_ including your URCU flavor.
30 #include <urcu/compiler.h>
31 #include <urcu-call-rcu.h>
32 #include <urcu-flavor.h>
39 * cds_lfht_node: Contains the next pointers and reverse-hash
40 * value required for lookup and traversal of the hash table.
42 * struct cds_lfht_node should be aligned on 4-bytes boundaries because
43 * the two lower bits are used as flags.
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.
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.
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)));
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
;
64 struct cds_lfht_node
*cds_lfht_iter_get_node(struct cds_lfht_iter
*iter
)
73 * Ensure reader and writer threads are registered as urcu readers.
76 typedef int (*cds_lfht_match_fct
)(struct cds_lfht_node
*node
, const void *key
);
79 * cds_lfht_node_init - initialize a hash table node
80 * @node: the node to initialize.
82 * This function is kept to be eventually used for debugging purposes
83 * (detection of memory corruption).
86 void cds_lfht_node_init(struct cds_lfht_node
*node
)
91 * Hash table creation flags.
94 CDS_LFHT_AUTO_RESIZE
= (1U << 0),
95 CDS_LFHT_ACCOUNTING
= (1U << 1),
99 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
101 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
102 unsigned long min_nr_alloc_buckets
,
103 unsigned long max_nr_buckets
,
105 const struct rcu_flavor_struct
*flavor
,
106 pthread_attr_t
*attr
);
109 * cds_lfht_new - allocate a hash table.
110 * @init_size: number of buckets to allocate initially. Must be power of two.
111 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
112 * (must be power of two)
113 * @max_nr_buckets: the maximum number of hash table buckets allowed.
114 * (must be power of two)
115 * @flags: hash table creation flags (can be combined with bitwise or: '|').
117 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
118 * CDS_LFHT_ACCOUNTING: count the number of node addition
119 * and removal in the table
120 * @attr: optional resize worker thread attributes. NULL for default.
122 * Return NULL on error.
123 * Note: the RCU flavor must be already included before the hash table header.
125 * The programmer is responsible for ensuring that resize operation has a
126 * priority equal to hash table updater threads. It should be performed by
127 * specifying the appropriate priority in the pthread "attr" argument, and,
128 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
129 * this priority level. Having lower priority for call_rcu and resize threads
130 * does not pose any correctness issue, but the resize operations could be
131 * starved by updates, thus leading to long hash table bucket chains.
132 * Threads calling this API are NOT required to be registered RCU read-side
133 * threads. It can be called very early.(before rcu is initialized ...etc.)
136 struct cds_lfht
*cds_lfht_new(unsigned long init_size
,
137 unsigned long min_nr_alloc_buckets
,
138 unsigned long max_nr_buckets
,
140 pthread_attr_t
*attr
)
142 return _cds_lfht_new(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
143 flags
, &rcu_flavor
, attr
);
147 * cds_lfht_destroy - destroy a hash table.
148 * @ht: the hash table to destroy.
149 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
150 * The caller will typically want to free this pointer if dynamically
151 * allocated. The attr point can be NULL if the caller does not
152 * need to be informed of the value passed to cds_lfht_new().
154 * Return 0 on success, negative error value on error.
155 * Threads calling this API need to be registered RCU read-side threads.
157 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
);
160 * cds_lfht_count_nodes - count the number of nodes in the hash table.
161 * @ht: the hash table.
162 * @split_count_before: Sample the node count split-counter before traversal.
163 * @count: Traverse the hash table, count the number of nodes observed.
164 * @removed: Number of logically removed nodes observed during traversal.
165 * @split_count_after: Sample the node count split-counter after traversal.
167 * Call with rcu_read_lock held.
168 * Threads calling this API need to be registered RCU read-side threads.
170 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
171 long *split_count_before
,
172 unsigned long *count
,
173 unsigned long *removed
,
174 long *split_count_after
);
177 * cds_lfht_lookup - lookup a node by key.
178 * @ht: the hash table.
179 * @hash: the key hash.
180 * @match: the key match function.
181 * @key: the current node key.
182 * @iter: Node, if found (output). *iter->node set to NULL if not found.
184 * Call with rcu_read_lock held.
185 * Threads calling this API need to be registered RCU read-side threads.
187 void cds_lfht_lookup(struct cds_lfht
*ht
, unsigned long hash
,
188 cds_lfht_match_fct match
, const void *key
,
189 struct cds_lfht_iter
*iter
);
192 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
193 * @ht: the hash table.
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.
198 * Uses an iterator initialized by a lookup.
199 * Sets *iter-node to the following node with same key.
200 * Sets *iter->node to NULL if no following node exists with same key.
201 * RCU read-side lock must be held across cds_lfht_lookup and
202 * cds_lfht_next calls, and also between cds_lfht_next calls using the
203 * node returned by a previous cds_lfht_next.
204 * Call with rcu_read_lock held.
205 * Threads calling this API need to be registered RCU read-side threads.
207 void cds_lfht_next_duplicate(struct cds_lfht
*ht
,
208 cds_lfht_match_fct match
, const void *key
,
209 struct cds_lfht_iter
*iter
);
212 * cds_lfht_first - get the first node in the table.
213 * @ht: the hash table.
214 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
216 * Output in "*iter". *iter->node set to NULL if table is empty.
217 * Call with rcu_read_lock held.
218 * Threads calling this API need to be registered RCU read-side threads.
220 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
223 * cds_lfht_next - get the next node in the table.
224 * @ht: the hash table.
225 * @iter: Next node, if exists (output). *iter->node set to NULL if not found.
227 * Input/Output in "*iter". *iter->node set to NULL if *iter was
228 * pointing to the last table node.
229 * Call with rcu_read_lock held.
230 * Threads calling this API need to be registered RCU read-side threads.
232 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
235 * cds_lfht_add - add a node to the hash table.
236 * @ht: the hash table.
237 * @hash: the key hash.
238 * @node: the node to add.
240 * This function supports adding redundant keys into the table.
241 * Call with rcu_read_lock held.
242 * Threads calling this API need to be registered RCU read-side threads.
244 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
245 struct cds_lfht_node
*node
);
248 * cds_lfht_add_unique - add a node to hash table, if key is not present.
249 * @ht: the hash table.
250 * @hash: the node's hash.
251 * @match: the key match function.
252 * @key: the node's key.
253 * @node: the node to try adding.
255 * Return the node added upon success.
256 * Return the unique node already present upon failure. If
257 * cds_lfht_add_unique fails, the node passed as parameter should be
258 * freed by the caller.
259 * Call with rcu_read_lock held.
260 * Threads calling this API need to be registered RCU read-side threads.
262 * The semantic of this function is that if only this function is used
263 * to add keys into the table, no duplicated keys should ever be
264 * observable in the table. The same guarantee apply for combination of
265 * add_unique and add_replace (see below).
267 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
269 cds_lfht_match_fct match
,
271 struct cds_lfht_node
*node
);
274 * cds_lfht_add_replace - replace or add a node within hash table.
275 * @ht: the hash table.
276 * @hash: the node's hash.
277 * @match: the key match function.
278 * @key: the node's key.
279 * @node: the node to add.
281 * Return the node replaced upon success. If no node matching the key
282 * was present, return NULL, which also means the operation succeeded.
283 * This replacement operation should never fail.
284 * Call with rcu_read_lock held.
285 * Threads calling this API need to be registered RCU read-side threads.
286 * After successful replacement, a grace period must be waited for before
287 * freeing the memory reserved for the returned node.
289 * The semantic of replacement vs lookups is the following: if lookups
290 * are performed between a key unique insertion and its removal, we
291 * guarantee that the lookups and get next will always find exactly one
292 * instance of the key if it is replaced concurrently with the lookups.
294 * Providing this semantic allows us to ensure that replacement-only
295 * schemes will never generate duplicated keys. It also allows us to
296 * guarantee that a combination of add_replace and add_unique updates
297 * will never generate duplicated keys.
299 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
301 cds_lfht_match_fct match
,
303 struct cds_lfht_node
*node
);
306 * cds_lfht_replace - replace a node pointer to by iter within hash table.
307 * @ht: the hash table.
308 * @old_iter: the iterator position of the node to replace.
309 * @now_node: the new node to try using for replacement.
311 * Return 0 if replacement is successful, negative value otherwise.
312 * Replacing a NULL old node or an already removed node will fail with a
314 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
315 * RCU read-side lock must be held between lookup and replacement.
316 * Call with rcu_read_lock held.
317 * Threads calling this API need to be registered RCU read-side threads.
318 * After successful replacement, a grace period must be waited for before
319 * freeing the memory reserved for the old node (which can be accessed
320 * with cds_lfht_iter_get_node).
322 * The semantic of replacement vs lookups is the following: if lookups
323 * are performed between a key unique insertion and its removal, we
324 * guarantee that the lookups and get next will always find exactly one
325 * instance of the key if it is replaced concurrently with the lookups.
327 * Providing this semantic allows us to ensure that replacement-only
328 * schemes will never generate duplicated keys. It also allows us to
329 * guarantee that a combination of add_replace and add_unique updates
330 * will never generate duplicated keys.
332 int cds_lfht_replace(struct cds_lfht
*ht
, struct cds_lfht_iter
*old_iter
,
333 struct cds_lfht_node
*new_node
);
336 * cds_lfht_del - remove node pointed to by iterator from hash table.
337 * @ht: the hash table.
338 * @iter: the iterator position of the node to delete.
340 * Return 0 if the node is successfully removed, negative value
342 * Replacing a NULL node or an already removed node will fail with a
344 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
345 * cds_lfht_iter_get_node.
346 * RCU read-side lock must be held between lookup and removal.
347 * Call with rcu_read_lock held.
348 * Threads calling this API need to be registered RCU read-side threads.
349 * After successful removal, a grace period must be waited for before
350 * freeing the memory reserved for old node (which can be accessed with
351 * cds_lfht_iter_get_node).
353 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
356 * cds_lfht_resize - Force a hash table resize
357 * @ht: the hash table.
358 * @new_size: update to this hash table size.
360 * Threads calling this API need to be registered RCU read-side threads.
362 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
);
365 * Note: cds_lfht_for_each are safe for element removal during
368 #define cds_lfht_for_each(ht, iter, node) \
369 for (cds_lfht_first(ht, iter), \
370 node = cds_lfht_iter_get_node(iter); \
372 cds_lfht_next(ht, iter), \
373 node = cds_lfht_iter_get_node(iter))
375 #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
376 for (cds_lfht_lookup(ht, hash, match, key, iter), \
377 node = cds_lfht_iter_get_node(iter); \
379 cds_lfht_next_duplicate(ht, match, key, iter), \
380 node = cds_lfht_iter_get_node(iter))
382 #define cds_lfht_for_each_entry(ht, iter, pos, member) \
383 for (cds_lfht_first(ht, iter), \
384 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
385 typeof(*(pos)), member); \
386 &(pos)->member != NULL; \
387 cds_lfht_next(ht, iter), \
388 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
389 typeof(*(pos)), member))
391 #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
393 for (cds_lfht_lookup(ht, hash, match, key, iter), \
394 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
395 typeof(*(pos)), member); \
396 &(pos)->member != NULL; \
397 cds_lfht_next_duplicate(ht, match, key, iter), \
398 pos = caa_container_of(cds_lfht_iter_get_node(iter), \
399 typeof(*(pos)), member))
405 #endif /* _URCU_RCULFHASH_H */
This page took 0.037502 seconds and 4 git commands to generate.