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>
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.
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.
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
25 * Include this file _after_ including your URCU flavor.
29 #include <urcu-call-rcu.h>
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.
41 * _cds_lfht_node: Contains the internal pointers and reverse-hash
42 * value required for lookup and traversal of the hash table.
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)));
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.
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.
60 struct cds_lfht_node
{
61 /* cache-hot for iteration */
62 struct _cds_lfht_node p
; /* needs to be first field */
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
;
73 struct cds_lfht_node
*cds_lfht_iter_get_node(struct cds_lfht_iter
*iter
)
82 * Ensure reader and writer threads are registered as urcu readers.
85 typedef unsigned long (*cds_lfht_hash_fct
)(void *key
, size_t length
,
87 typedef unsigned long (*cds_lfht_compare_fct
)(void *key1
, size_t key1_len
,
88 void *key2
, size_t key2_len
);
91 * cds_lfht_node_init - initialize a hash table node
94 void cds_lfht_node_init(struct cds_lfht_node
*node
, void *key
,
98 node
->key_len
= key_len
;
102 * Hash table creation flags.
105 CDS_LFHT_AUTO_RESIZE
= (1U << 0),
106 CDS_LFHT_ACCOUNTING
= (1U << 1),
110 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
112 struct cds_lfht
*_cds_lfht_new(cds_lfht_hash_fct hash_fct
,
113 cds_lfht_compare_fct compare_fct
,
114 unsigned long hash_seed
,
115 unsigned long init_size
,
116 unsigned long min_alloc_size
,
118 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
119 void (*func
)(struct rcu_head
*head
)),
120 void (*cds_lfht_synchronize_rcu
)(void),
121 void (*cds_lfht_rcu_read_lock
)(void),
122 void (*cds_lfht_rcu_read_unlock
)(void),
123 void (*cds_lfht_rcu_thread_offline
)(void),
124 void (*cds_lfht_rcu_thread_online
)(void),
125 void (*cds_lfht_rcu_register_thread
)(void),
126 void (*cds_lfht_rcu_unregister_thread
)(void),
127 pthread_attr_t
*attr
);
130 * cds_lfht_new - allocate a hash table.
131 * @hash_fct: the hashing function.
132 * @compare_fct: the key comparison function.
133 * @hash_seed: the seed for hash function.
134 * @init_size: number of nodes to allocate initially. Must be power of two.
135 * @min_alloc_size: the smallest allocation size to use. Must be power of two.
136 * @flags: hash table creation flags (can be combined with bitwise or: '|').
138 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
139 * @attr: optional resize worker thread attributes. NULL for default.
141 * Return NULL on error.
142 * Note: the RCU flavor must be already included before the hash table header.
144 * The programmer is responsible for ensuring that resize operation has a
145 * priority equal to hash table updater threads. It should be performed by
146 * specifying the appropriate priority in the pthread "attr" argument, and,
147 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
148 * this priority level. Having lower priority for call_rcu and resize threads
149 * does not pose any correctness issue, but the resize operations could be
150 * starved by updates, thus leading to long hash table bucket chains.
151 * Threads calling this API need to be registered RCU read-side threads.
154 struct cds_lfht
*cds_lfht_new(cds_lfht_hash_fct hash_fct
,
155 cds_lfht_compare_fct compare_fct
,
156 unsigned long hash_seed
,
157 unsigned long init_size
,
158 unsigned long min_alloc_size
,
160 pthread_attr_t
*attr
)
162 return _cds_lfht_new(hash_fct
, compare_fct
, hash_seed
,
163 init_size
, min_alloc_size
, flags
,
164 call_rcu
, synchronize_rcu
, rcu_read_lock
,
165 rcu_read_unlock
, rcu_thread_offline
,
166 rcu_thread_online
, rcu_register_thread
,
167 rcu_unregister_thread
, attr
);
171 * cds_lfht_destroy - destroy a hash table.
172 * @ht: the hash table to destroy.
173 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
174 * The caller will typically want to free this pointer if dynamically
175 * allocated. The attr point can be NULL if the caller does not
176 * need to be informed of the value passed to cds_lfht_new().
178 * Return 0 on success, negative error value on error.
179 * Threads calling this API need to be registered RCU read-side threads.
181 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
);
184 * cds_lfht_count_nodes - count the number of nodes in the hash table.
185 * @ht: the hash table.
186 * @split_count_before: Sample the node count split-counter before traversal.
187 * @count: Traverse the hash table, count the number of nodes observed.
188 * @removed: Number of logically removed nodes observed during traversal.
189 * @split_count_after: Sample the node count split-counter after traversal.
190 * Call with rcu_read_lock held.
191 * Threads calling this API need to be registered RCU read-side threads.
193 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
194 long *split_count_before
,
195 unsigned long *count
,
196 unsigned long *removed
,
197 long *split_count_after
);
200 * cds_lfht_lookup - lookup a node by key.
202 * Output in "*iter". *iter->node set to NULL if not found.
203 * Call with rcu_read_lock held.
204 * Threads calling this API need to be registered RCU read-side threads.
206 void cds_lfht_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
207 struct cds_lfht_iter
*iter
);
210 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
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.
221 void cds_lfht_next_duplicate(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
224 * cds_lfht_first - get the first node in the table.
226 * Output in "*iter". *iter->node set to NULL if table is empty.
227 * Call with rcu_read_lock held.
228 * Threads calling this API need to be registered RCU read-side threads.
230 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
233 * cds_lfht_next - get the next node in the table.
235 * Input/Output in "*iter". *iter->node set to NULL if *iter was
236 * pointing to the last table node.
237 * Call with rcu_read_lock held.
238 * Threads calling this API need to be registered RCU read-side threads.
240 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
243 * cds_lfht_add - add a node to the hash table.
245 * This function supports adding redundant keys into the table.
246 * Call with rcu_read_lock held.
247 * Threads calling this API need to be registered RCU read-side threads.
249 void cds_lfht_add(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
252 * cds_lfht_add_unique - add a node to hash table, if key is not present.
254 * Return the node added upon success.
255 * Return the unique node already present upon failure. If
256 * cds_lfht_add_unique fails, the node passed as parameter should be
257 * freed by the caller.
258 * Call with rcu_read_lock held.
259 * Threads calling this API need to be registered RCU read-side threads.
261 * The semantic of this function is that if only this function is used
262 * to add keys into the table, no duplicated keys should ever be
263 * observable in the table. The same guarantee apply for combination of
264 * add_unique and add_replace (see below).
266 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
267 struct cds_lfht_node
*node
);
270 * cds_lfht_add_replace - replace or add a node within hash table.
272 * Return the node replaced upon success. If no node matching the key
273 * was present, return NULL, which also means the operation succeeded.
274 * This replacement operation should never fail.
275 * Call with rcu_read_lock held.
276 * Threads calling this API need to be registered RCU read-side threads.
277 * After successful replacement, a grace period must be waited for before
278 * freeing the memory reserved for the returned node.
280 * The semantic of replacement vs lookups is the following: if lookups
281 * are performed between a key unique insertion and its removal, we
282 * guarantee that the lookups and get next will always find exactly one
283 * instance of the key if it is replaced concurrently with the lookups.
285 * Providing this semantic allows us to ensure that replacement-only
286 * schemes will never generate duplicated keys. It also allows us to
287 * guarantee that a combination of add_replace and add_unique updates
288 * will never generate duplicated keys.
290 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
291 struct cds_lfht_node
*node
);
294 * cds_lfht_replace - replace a node pointer to by iter within hash table.
296 * Return 0 if replacement is successful, negative value otherwise.
297 * Replacing a NULL old node or an already removed node will fail with a
299 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
300 * RCU read-side lock must be held between lookup and replacement.
301 * Call with rcu_read_lock held.
302 * Threads calling this API need to be registered RCU read-side threads.
303 * After successful replacement, a grace period must be waited for before
304 * freeing the memory reserved for the old node (which can be accessed
305 * with cds_lfht_iter_get_node).
307 * The semantic of replacement vs lookups is the following: if lookups
308 * are performed between a key unique insertion and its removal, we
309 * guarantee that the lookups and get next will always find exactly one
310 * instance of the key if it is replaced concurrently with the lookups.
312 * Providing this semantic allows us to ensure that replacement-only
313 * schemes will never generate duplicated keys. It also allows us to
314 * guarantee that a combination of add_replace and add_unique updates
315 * will never generate duplicated keys.
317 int cds_lfht_replace(struct cds_lfht
*ht
, struct cds_lfht_iter
*old_iter
,
318 struct cds_lfht_node
*new_node
);
321 * cds_lfht_del - remove node pointed to by iterator from hash table.
323 * Return 0 if the node is successfully removed, negative value
325 * Replacing a NULL node or an already removed node will fail with a
327 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
328 * cds_lfht_iter_get_node.
329 * RCU read-side lock must be held between lookup and removal.
330 * Call with rcu_read_lock held.
331 * Threads calling this API need to be registered RCU read-side threads.
332 * After successful removal, a grace period must be waited for before
333 * freeing the memory reserved for old node (which can be accessed with
334 * cds_lfht_iter_get_node).
336 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
339 * cds_lfht_resize - Force a hash table resize
340 * @new_size: update to this hash table size.
342 * Threads calling this API need to be registered RCU read-side threads.
344 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
);
350 #endif /* _URCU_RCULFHASH_H */