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.
40 struct _cds_lfht_node
{
41 struct cds_lfht_node
*next
; /* ptr | DUMMY_FLAG | REMOVED_FLAG */
42 unsigned long reverse_hash
;
43 } __attribute__((aligned(4)));
46 * struct cds_lfht_node can be embedded into a structure (as a field).
47 * caa_container_of() can be used to get the structure from the struct
48 * cds_lfht_node after a lookup.
50 struct cds_lfht_node
{
51 /* cache-hot for iteration */
52 struct _cds_lfht_node p
; /* needs to be first field */
55 /* cache-cold for iteration */
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 unsigned long (*cds_lfht_hash_fct
)(void *key
, size_t length
,
78 typedef unsigned long (*cds_lfht_compare_fct
)(void *key1
, size_t key1_len
,
79 void *key2
, size_t key2_len
);
82 * cds_lfht_node_init - initialize a hash table node
85 void cds_lfht_node_init(struct cds_lfht_node
*node
, void *key
,
89 node
->key_len
= key_len
;
93 * Hash table creation flags.
96 CDS_LFHT_AUTO_RESIZE
= (1U << 0),
100 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
102 struct cds_lfht
*_cds_lfht_new(cds_lfht_hash_fct hash_fct
,
103 cds_lfht_compare_fct compare_fct
,
104 unsigned long hash_seed
,
105 unsigned long init_size
,
107 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
108 void (*func
)(struct rcu_head
*head
)),
109 void (*cds_lfht_synchronize_rcu
)(void),
110 void (*cds_lfht_rcu_read_lock
)(void),
111 void (*cds_lfht_rcu_read_unlock
)(void),
112 void (*cds_lfht_rcu_thread_offline
)(void),
113 void (*cds_lfht_rcu_thread_online
)(void),
114 void (*cds_lfht_rcu_register_thread
)(void),
115 void (*cds_lfht_rcu_unregister_thread
)(void),
116 pthread_attr_t
*attr
);
119 * cds_lfht_new - allocate a hash table.
120 * @hash_fct: the hashing function.
121 * @compare_fct: the key comparison function.
122 * @hash_seed: the seed for hash function.
123 * @init_size: number of nodes to allocate initially. Must be power of two.
124 * @flags: hash table creation flags (can be combined with bitwise or: '|').
126 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
127 * @attr: optional resize worker thread attributes. NULL for default.
129 * Return NULL on error.
130 * Note: the RCU flavor must be already included before the hash table header.
132 * The programmer is responsible for ensuring that resize operation has a
133 * priority equal to hash table updater threads. It should be performed by
134 * specifying the appropriate priority in the pthread "attr" argument, and,
135 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
136 * this priority level. Having lower priority for call_rcu and resize threads
137 * does not pose any correctness issue, but the resize operations could be
138 * starved by updates, thus leading to long hash table bucket chains.
139 * Threads calling this API need to be registered RCU read-side threads.
142 struct cds_lfht
*cds_lfht_new(cds_lfht_hash_fct hash_fct
,
143 cds_lfht_compare_fct compare_fct
,
144 unsigned long hash_seed
,
145 unsigned long init_size
,
147 pthread_attr_t
*attr
)
149 return _cds_lfht_new(hash_fct
, compare_fct
, hash_seed
,
151 call_rcu
, synchronize_rcu
, rcu_read_lock
,
152 rcu_read_unlock
, rcu_thread_offline
,
153 rcu_thread_online
, rcu_register_thread
,
154 rcu_unregister_thread
, attr
);
158 * cds_lfht_destroy - destroy a hash table.
159 * @ht: the hash table to destroy.
160 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
161 * The caller will typically want to free this pointer if dynamically
164 * Return 0 on success, negative error value on error.
165 * Threads calling this API need to be registered RCU read-side threads.
167 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
);
170 * cds_lfht_count_nodes - count the number of nodes in the hash table.
172 * Call with rcu_read_lock held.
173 * Threads calling this API need to be registered RCU read-side threads.
175 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
177 unsigned long *count
,
178 unsigned long *removed
,
182 * cds_lfht_lookup - lookup a node by key.
184 * Output in "*iter". *iter->node set to NULL if not found.
185 * Call with rcu_read_lock held.
186 * Threads calling this API need to be registered RCU read-side threads.
188 void cds_lfht_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
189 struct cds_lfht_iter
*iter
);
192 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
194 * Uses an iterator initialized by a lookup.
195 * Sets *iter-node to the following node with same key.
196 * Sets *iter->node to NULL if no following node exists with same key.
197 * RCU read-side lock must be held across cds_lfht_lookup and
198 * cds_lfht_next calls, and also between cds_lfht_next calls using the
199 * node returned by a previous cds_lfht_next.
200 * Call with rcu_read_lock held.
201 * Threads calling this API need to be registered RCU read-side threads.
203 void cds_lfht_next_duplicate(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
206 * cds_lfht_first - get the first node in the table.
208 * Output in "*iter". *iter->node set to NULL if table is empty.
209 * Call with rcu_read_lock held.
210 * Threads calling this API need to be registered RCU read-side threads.
212 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
215 * cds_lfht_next - get the next node in the table.
217 * Input/Output in "*iter". *iter->node set to NULL if *iter was
218 * pointing to the last table node.
219 * Call with rcu_read_lock held.
220 * Threads calling this API need to be registered RCU read-side threads.
222 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
225 * cds_lfht_add - add a node to the hash table.
227 * This function supports adding redundant keys into the table.
228 * Call with rcu_read_lock held.
229 * Threads calling this API need to be registered RCU read-side threads.
231 void cds_lfht_add(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
234 * cds_lfht_add_unique - add a node to hash table, if key is not present.
236 * Return the node added upon success.
237 * Return the unique node already present upon failure. If
238 * cds_lfht_add_unique fails, the node passed as parameter should be
239 * freed by the caller.
240 * Call with rcu_read_lock held.
241 * Threads calling this API need to be registered RCU read-side threads.
243 * The semantic of this function is that if only this function is used
244 * to add keys into the table, no duplicated keys should ever be
245 * observable in the table. The same guarantee apply for combination of
246 * add_unique and add_replace (see below).
248 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
249 struct cds_lfht_node
*node
);
252 * cds_lfht_add_replace - replace or add a node within hash table.
254 * Return the node replaced upon success. If no node matching the key
255 * was present, return NULL, which also means the operation succeeded.
256 * This replacement operation should never fail.
257 * Call with rcu_read_lock held.
258 * Threads calling this API need to be registered RCU read-side threads.
259 * After successful replacement, a grace period must be waited for before
260 * freeing the memory reserved for the returned node.
262 * The semantic of replacement vs lookups is the following: if lookups
263 * are performed between a key unique insertion and its removal, we
264 * guarantee that the lookups and get next will always find exactly one
265 * instance of the key if it is replaced concurrently with the lookups.
267 * Providing this semantic allows us to ensure that replacement-only
268 * schemes will never generate duplicated keys. It also allows us to
269 * guarantee that a combination of add_replace and add_unique updates
270 * will never generate duplicated keys.
272 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
273 struct cds_lfht_node
*node
);
276 * cds_lfht_replace - replace a node pointer to by iter within hash table.
278 * Return 0 if replacement is successful, negative value otherwise.
279 * Replacing a NULL old node or an already removed node will fail with a
281 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
282 * RCU read-side lock must be held between lookup and replacement.
283 * Call with rcu_read_lock held.
284 * Threads calling this API need to be registered RCU read-side threads.
285 * After successful replacement, a grace period must be waited for before
286 * freeing the memory reserved for the old node (which can be accessed
287 * with cds_lfht_iter_get_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 int cds_lfht_replace(struct cds_lfht
*ht
, struct cds_lfht_iter
*old_iter
,
300 struct cds_lfht_node
*new_node
);
303 * cds_lfht_del - remove node pointed to by iterator from hash table.
305 * Return 0 if the node is successfully removed, negative value
307 * Replacing a NULL node or an already removed node will fail with a
309 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
310 * cds_lfht_iter_get_node.
311 * RCU read-side lock must be held between lookup and removal.
312 * Call with rcu_read_lock held.
313 * Threads calling this API need to be registered RCU read-side threads.
314 * After successful removal, a grace period must be waited for before
315 * freeing the memory reserved for old node (which can be accessed with
316 * cds_lfht_iter_get_node).
318 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
321 * cds_lfht_resize - Force a hash table resize
322 * @new_size: update to this hash table size.
324 * Threads calling this API need to be registered RCU read-side threads.
326 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
);
332 #endif /* _URCU_RCULFHASH_H */