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),
97 CDS_LFHT_ACCOUNTING
= (1U << 1),
101 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
103 struct cds_lfht
*_cds_lfht_new(cds_lfht_hash_fct hash_fct
,
104 cds_lfht_compare_fct compare_fct
,
105 unsigned long hash_seed
,
106 unsigned long init_size
,
107 unsigned long min_alloc_size
,
109 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
110 void (*func
)(struct rcu_head
*head
)),
111 void (*cds_lfht_synchronize_rcu
)(void),
112 void (*cds_lfht_rcu_read_lock
)(void),
113 void (*cds_lfht_rcu_read_unlock
)(void),
114 void (*cds_lfht_rcu_thread_offline
)(void),
115 void (*cds_lfht_rcu_thread_online
)(void),
116 void (*cds_lfht_rcu_register_thread
)(void),
117 void (*cds_lfht_rcu_unregister_thread
)(void),
118 pthread_attr_t
*attr
);
121 * cds_lfht_new - allocate a hash table.
122 * @hash_fct: the hashing function.
123 * @compare_fct: the key comparison function.
124 * @hash_seed: the seed for hash function.
125 * @init_size: number of nodes to allocate initially. Must be power of two.
126 * @min_alloc_size: the smallest allocation size to use. Must be power of two.
127 * @flags: hash table creation flags (can be combined with bitwise or: '|').
129 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
130 * @attr: optional resize worker thread attributes. NULL for default.
132 * Return NULL on error.
133 * Note: the RCU flavor must be already included before the hash table header.
135 * The programmer is responsible for ensuring that resize operation has a
136 * priority equal to hash table updater threads. It should be performed by
137 * specifying the appropriate priority in the pthread "attr" argument, and,
138 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
139 * this priority level. Having lower priority for call_rcu and resize threads
140 * does not pose any correctness issue, but the resize operations could be
141 * starved by updates, thus leading to long hash table bucket chains.
142 * Threads calling this API need to be registered RCU read-side threads.
145 struct cds_lfht
*cds_lfht_new(cds_lfht_hash_fct hash_fct
,
146 cds_lfht_compare_fct compare_fct
,
147 unsigned long hash_seed
,
148 unsigned long init_size
,
149 unsigned long min_alloc_size
,
151 pthread_attr_t
*attr
)
153 return _cds_lfht_new(hash_fct
, compare_fct
, hash_seed
,
154 init_size
, min_alloc_size
, flags
,
155 call_rcu
, synchronize_rcu
, rcu_read_lock
,
156 rcu_read_unlock
, rcu_thread_offline
,
157 rcu_thread_online
, rcu_register_thread
,
158 rcu_unregister_thread
, attr
);
162 * cds_lfht_destroy - destroy a hash table.
163 * @ht: the hash table to destroy.
164 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
165 * The caller will typically want to free this pointer if dynamically
168 * Return 0 on success, negative error value on error.
169 * Threads calling this API need to be registered RCU read-side threads.
171 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
);
174 * cds_lfht_count_nodes - count the number of nodes in the hash table.
176 * Call with rcu_read_lock held.
177 * Threads calling this API need to be registered RCU read-side threads.
179 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
181 unsigned long *count
,
182 unsigned long *removed
,
186 * cds_lfht_lookup - lookup a node by key.
188 * Output in "*iter". *iter->node set to NULL if not found.
189 * Call with rcu_read_lock held.
190 * Threads calling this API need to be registered RCU read-side threads.
192 void cds_lfht_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
,
193 struct cds_lfht_iter
*iter
);
196 * cds_lfht_next_duplicate - get the next item with same key (after a lookup).
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
, struct cds_lfht_iter
*iter
);
210 * cds_lfht_first - get the first node in the table.
212 * Output in "*iter". *iter->node set to NULL if table is empty.
213 * Call with rcu_read_lock held.
214 * Threads calling this API need to be registered RCU read-side threads.
216 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
219 * cds_lfht_next - get the next node in the table.
221 * Input/Output in "*iter". *iter->node set to NULL if *iter was
222 * pointing to the last table node.
223 * Call with rcu_read_lock held.
224 * Threads calling this API need to be registered RCU read-side threads.
226 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
229 * cds_lfht_add - add a node to the hash table.
231 * This function supports adding redundant keys into the table.
232 * Call with rcu_read_lock held.
233 * Threads calling this API need to be registered RCU read-side threads.
235 void cds_lfht_add(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
238 * cds_lfht_add_unique - add a node to hash table, if key is not present.
240 * Return the node added upon success.
241 * Return the unique node already present upon failure. If
242 * cds_lfht_add_unique fails, the node passed as parameter should be
243 * freed by the caller.
244 * Call with rcu_read_lock held.
245 * Threads calling this API need to be registered RCU read-side threads.
247 * The semantic of this function is that if only this function is used
248 * to add keys into the table, no duplicated keys should ever be
249 * observable in the table. The same guarantee apply for combination of
250 * add_unique and add_replace (see below).
252 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
253 struct cds_lfht_node
*node
);
256 * cds_lfht_add_replace - replace or add a node within hash table.
258 * Return the node replaced upon success. If no node matching the key
259 * was present, return NULL, which also means the operation succeeded.
260 * This replacement operation should never fail.
261 * Call with rcu_read_lock held.
262 * Threads calling this API need to be registered RCU read-side threads.
263 * After successful replacement, a grace period must be waited for before
264 * freeing the memory reserved for the returned node.
266 * The semantic of replacement vs lookups is the following: if lookups
267 * are performed between a key unique insertion and its removal, we
268 * guarantee that the lookups and get next will always find exactly one
269 * instance of the key if it is replaced concurrently with the lookups.
271 * Providing this semantic allows us to ensure that replacement-only
272 * schemes will never generate duplicated keys. It also allows us to
273 * guarantee that a combination of add_replace and add_unique updates
274 * will never generate duplicated keys.
276 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
277 struct cds_lfht_node
*node
);
280 * cds_lfht_replace - replace a node pointer to by iter within hash table.
282 * Return 0 if replacement is successful, negative value otherwise.
283 * Replacing a NULL old node or an already removed node will fail with a
285 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
286 * RCU read-side lock must be held between lookup and replacement.
287 * Call with rcu_read_lock held.
288 * Threads calling this API need to be registered RCU read-side threads.
289 * After successful replacement, a grace period must be waited for before
290 * freeing the memory reserved for the old node (which can be accessed
291 * with cds_lfht_iter_get_node).
293 * The semantic of replacement vs lookups is the following: if lookups
294 * are performed between a key unique insertion and its removal, we
295 * guarantee that the lookups and get next will always find exactly one
296 * instance of the key if it is replaced concurrently with the lookups.
298 * Providing this semantic allows us to ensure that replacement-only
299 * schemes will never generate duplicated keys. It also allows us to
300 * guarantee that a combination of add_replace and add_unique updates
301 * will never generate duplicated keys.
303 int cds_lfht_replace(struct cds_lfht
*ht
, struct cds_lfht_iter
*old_iter
,
304 struct cds_lfht_node
*new_node
);
307 * cds_lfht_del - remove node pointed to by iterator from hash table.
309 * Return 0 if the node is successfully removed, negative value
311 * Replacing a NULL node or an already removed node will fail with a
313 * Node can be looked up with cds_lfht_lookup and cds_lfht_next.
314 * cds_lfht_iter_get_node.
315 * RCU read-side lock must be held between lookup and removal.
316 * Call with rcu_read_lock held.
317 * Threads calling this API need to be registered RCU read-side threads.
318 * After successful removal, a grace period must be waited for before
319 * freeing the memory reserved for old node (which can be accessed with
320 * cds_lfht_iter_get_node).
322 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
325 * cds_lfht_resize - Force a hash table resize
326 * @new_size: update to this hash table size.
328 * Threads calling this API need to be registered RCU read-side threads.
330 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
);
336 #endif /* _URCU_RCULFHASH_H */