Commit | Line | Data |
---|---|---|
a42cc659 MD |
1 | #ifndef _URCU_RCULFHASH_H |
2 | #define _URCU_RCULFHASH_H | |
ab7d5fc6 | 3 | |
f0c29ed7 MD |
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 | * | |
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. | |
15 | * | |
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. | |
20 | * | |
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 | |
cf77d1fa MD |
24 | * |
25 | * Include this file _after_ including your URCU flavor. | |
f0c29ed7 MD |
26 | */ |
27 | ||
674f7a69 | 28 | #include <stdint.h> |
abc490a1 MD |
29 | #include <urcu-call-rcu.h> |
30 | ||
01389791 MD |
31 | #ifdef __cplusplus |
32 | extern "C" { | |
33 | #endif | |
34 | ||
7f52427b | 35 | /* |
04db56f8 | 36 | * cds_lfht_node: Contains the next pointers and reverse-hash |
7f52427b | 37 | * value required for lookup and traversal of the hash table. |
04db56f8 LJ |
38 | * |
39 | * struct cds_lfht_node should be aligned on 4-bytes boundaries because | |
40 | * the two lower bits are used as flags. | |
7f52427b | 41 | * |
ae62aa6a MD |
42 | * struct cds_lfht_node can be embedded into a structure (as a field). |
43 | * caa_container_of() can be used to get the structure from the struct | |
44 | * cds_lfht_node after a lookup. | |
04db56f8 LJ |
45 | * |
46 | * The structure which embeds it typically holds the key (or key-value | |
47 | * pair) of the object. The caller code is responsible for calculation | |
48 | * of the hash value for cds_lfht APIs. | |
ae62aa6a | 49 | */ |
14044b37 | 50 | struct cds_lfht_node { |
1ee8f000 | 51 | struct cds_lfht_node *next; /* ptr | BUCKET_FLAG | REMOVED_FLAG */ |
04db56f8 LJ |
52 | unsigned long reverse_hash; |
53 | } __attribute__((aligned(4))); | |
abc490a1 | 54 | |
7f52427b | 55 | /* cds_lfht_iter: Used to track state while traversing a hash chain. */ |
adc0de68 MD |
56 | struct cds_lfht_iter { |
57 | struct cds_lfht_node *node, *next; | |
58 | }; | |
59 | ||
60 | static inline | |
61 | struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) | |
62 | { | |
63 | return iter->node; | |
64 | } | |
65 | ||
14044b37 | 66 | struct cds_lfht; |
ab7d5fc6 | 67 | |
5e28c532 MD |
68 | /* |
69 | * Caution ! | |
abc490a1 | 70 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
71 | */ |
72 | ||
14044b37 MD |
73 | typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length, |
74 | unsigned long seed); | |
0422d92c | 75 | typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, void *key); |
abc490a1 | 76 | |
f0c29ed7 | 77 | /* |
14044b37 | 78 | * cds_lfht_node_init - initialize a hash table node |
0422d92c | 79 | * @node: the node to initialize. |
04db56f8 LJ |
80 | * |
81 | * This function is kept to be eventually used for debugging purposes | |
82 | * (detection of memory corruption). | |
f0c29ed7 | 83 | */ |
abc490a1 | 84 | static inline |
04db56f8 | 85 | void cds_lfht_node_init(struct cds_lfht_node *node) |
abc490a1 | 86 | { |
abc490a1 | 87 | } |
674f7a69 | 88 | |
b8af5011 MD |
89 | /* |
90 | * Hash table creation flags. | |
91 | */ | |
92 | enum { | |
93 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
5afadd12 | 94 | CDS_LFHT_ACCOUNTING = (1U << 1), |
b8af5011 MD |
95 | }; |
96 | ||
674f7a69 | 97 | /* |
7a9dcf9b | 98 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
674f7a69 | 99 | */ |
0422d92c | 100 | struct cds_lfht *_cds_lfht_new(unsigned long init_size, |
5488222b | 101 | unsigned long min_alloc_size, |
b8af5011 | 102 | int flags, |
14044b37 | 103 | void (*cds_lfht_call_rcu)(struct rcu_head *head, |
1475579c | 104 | void (*func)(struct rcu_head *head)), |
01dbfa62 MD |
105 | void (*cds_lfht_synchronize_rcu)(void), |
106 | void (*cds_lfht_rcu_read_lock)(void), | |
5f511391 MD |
107 | void (*cds_lfht_rcu_read_unlock)(void), |
108 | void (*cds_lfht_rcu_thread_offline)(void), | |
b7d619b0 MD |
109 | void (*cds_lfht_rcu_thread_online)(void), |
110 | void (*cds_lfht_rcu_register_thread)(void), | |
111 | void (*cds_lfht_rcu_unregister_thread)(void), | |
112 | pthread_attr_t *attr); | |
ab7d5fc6 | 113 | |
7a9dcf9b MD |
114 | /* |
115 | * cds_lfht_new - allocate a hash table. | |
f22dd01d | 116 | * @init_size: number of nodes to allocate initially. Must be power of two. |
5488222b | 117 | * @min_alloc_size: the smallest allocation size to use. Must be power of two. |
f22dd01d MD |
118 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). |
119 | * 0: no flags. | |
120 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
b7d619b0 | 121 | * @attr: optional resize worker thread attributes. NULL for default. |
7a9dcf9b | 122 | * |
7a9dcf9b MD |
123 | * Return NULL on error. |
124 | * Note: the RCU flavor must be already included before the hash table header. | |
b7d619b0 MD |
125 | * |
126 | * The programmer is responsible for ensuring that resize operation has a | |
127 | * priority equal to hash table updater threads. It should be performed by | |
128 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
129 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
130 | * this priority level. Having lower priority for call_rcu and resize threads | |
131 | * does not pose any correctness issue, but the resize operations could be | |
132 | * starved by updates, thus leading to long hash table bucket chains. | |
3df0c49c | 133 | * Threads calling this API need to be registered RCU read-side threads. |
7a9dcf9b MD |
134 | */ |
135 | static inline | |
0422d92c | 136 | struct cds_lfht *cds_lfht_new(unsigned long init_size, |
5488222b | 137 | unsigned long min_alloc_size, |
b7d619b0 MD |
138 | int flags, |
139 | pthread_attr_t *attr) | |
7a9dcf9b | 140 | { |
0422d92c | 141 | return _cds_lfht_new(init_size, min_alloc_size, flags, |
7a9dcf9b MD |
142 | call_rcu, synchronize_rcu, rcu_read_lock, |
143 | rcu_read_unlock, rcu_thread_offline, | |
b7d619b0 MD |
144 | rcu_thread_online, rcu_register_thread, |
145 | rcu_unregister_thread, attr); | |
7a9dcf9b MD |
146 | } |
147 | ||
f0c29ed7 | 148 | /* |
14044b37 | 149 | * cds_lfht_destroy - destroy a hash table. |
b7d619b0 MD |
150 | * @ht: the hash table to destroy. |
151 | * @attr: (output) resize worker thread attributes, as received by cds_lfht_new. | |
152 | * The caller will typically want to free this pointer if dynamically | |
7f52427b MD |
153 | * allocated. The attr point can be NULL if the caller does not |
154 | * need to be informed of the value passed to cds_lfht_new(). | |
6878c72b MD |
155 | * |
156 | * Return 0 on success, negative error value on error. | |
3df0c49c | 157 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 158 | */ |
b7d619b0 | 159 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
f0c29ed7 MD |
160 | |
161 | /* | |
14044b37 | 162 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
7f52427b MD |
163 | * @ht: the hash table. |
164 | * @split_count_before: Sample the node count split-counter before traversal. | |
165 | * @count: Traverse the hash table, count the number of nodes observed. | |
166 | * @removed: Number of logically removed nodes observed during traversal. | |
167 | * @split_count_after: Sample the node count split-counter after traversal. | |
0422d92c | 168 | * |
f0c29ed7 | 169 | * Call with rcu_read_lock held. |
3df0c49c | 170 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 171 | */ |
14044b37 | 172 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
7f52427b | 173 | long *split_count_before, |
adc0de68 | 174 | unsigned long *count, |
973e5e1b | 175 | unsigned long *removed, |
7f52427b | 176 | long *split_count_after); |
ab7d5fc6 | 177 | |
f0c29ed7 | 178 | /* |
14044b37 | 179 | * cds_lfht_lookup - lookup a node by key. |
0422d92c MD |
180 | * @ht: the hash table. |
181 | * @match: the key match function. | |
182 | * @hash: the key hash. | |
183 | * @iter: Node, if found (output). *iter->node set to NULL if not found. | |
f0c29ed7 | 184 | * |
f0c29ed7 | 185 | * Call with rcu_read_lock held. |
3df0c49c | 186 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 187 | */ |
0422d92c MD |
188 | void cds_lfht_lookup(struct cds_lfht *ht, cds_lfht_match_fct match, |
189 | unsigned long hash, void *key, struct cds_lfht_iter *iter); | |
ab7d5fc6 | 190 | |
f0c29ed7 | 191 | /* |
3883c0e5 | 192 | * cds_lfht_next_duplicate - get the next item with same key (after a lookup). |
0422d92c MD |
193 | * @ht: the hash table. |
194 | * @match: the key match function. | |
04db56f8 | 195 | * @key: the current node key. |
0422d92c | 196 | * @iter: Node, if found (output). *iter->node set to NULL if not found. |
f0c29ed7 | 197 | * |
adc0de68 MD |
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. | |
3df0c49c | 205 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 206 | */ |
0422d92c | 207 | void cds_lfht_next_duplicate(struct cds_lfht *ht, |
04db56f8 LJ |
208 | cds_lfht_match_fct match, void *key, |
209 | struct cds_lfht_iter *iter); | |
4e9b9fbf MD |
210 | |
211 | /* | |
212 | * cds_lfht_first - get the first node in the table. | |
0422d92c MD |
213 | * @ht: the hash table. |
214 | * @iter: First node, if exists (output). *iter->node set to NULL if not found. | |
4e9b9fbf MD |
215 | * |
216 | * Output in "*iter". *iter->node set to NULL if table is empty. | |
217 | * Call with rcu_read_lock held. | |
3df0c49c | 218 | * Threads calling this API need to be registered RCU read-side threads. |
4e9b9fbf MD |
219 | */ |
220 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter); | |
221 | ||
222 | /* | |
223 | * cds_lfht_next - get the next node in the table. | |
0422d92c MD |
224 | * @ht: the hash table. |
225 | * @iter: Next node, if exists (output). *iter->node set to NULL if not found. | |
4e9b9fbf MD |
226 | * |
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. | |
3df0c49c | 230 | * Threads calling this API need to be registered RCU read-side threads. |
4e9b9fbf MD |
231 | */ |
232 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter); | |
ab7d5fc6 | 233 | |
18117871 | 234 | /* |
14044b37 | 235 | * cds_lfht_add - add a node to the hash table. |
0422d92c MD |
236 | * @ht: the hash table. |
237 | * @hash: the key hash. | |
238 | * @node: the node to add. | |
f0c29ed7 | 239 | * |
48ed1c18 | 240 | * This function supports adding redundant keys into the table. |
3df0c49c MD |
241 | * Call with rcu_read_lock held. |
242 | * Threads calling this API need to be registered RCU read-side threads. | |
f0c29ed7 | 243 | */ |
0422d92c MD |
244 | void cds_lfht_add(struct cds_lfht *ht, unsigned long hash, |
245 | struct cds_lfht_node *node); | |
f0c29ed7 MD |
246 | |
247 | /* | |
14044b37 | 248 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
0422d92c MD |
249 | * @ht: the hash table. |
250 | * @match: the key match function. | |
04db56f8 LJ |
251 | * @key: the node's key. |
252 | * @hash: the node's hash. | |
0422d92c | 253 | * @node: the node to try adding. |
f0c29ed7 | 254 | * |
6878c72b | 255 | * Return the node added upon success. |
860d07e8 MD |
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. | |
f0c29ed7 | 259 | * Call with rcu_read_lock held. |
3df0c49c | 260 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
261 | * |
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 | |
9357c415 | 265 | * add_unique and add_replace (see below). |
18117871 | 266 | */ |
adc0de68 | 267 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
0422d92c | 268 | cds_lfht_match_fct match, |
04db56f8 | 269 | void *key, |
0422d92c | 270 | unsigned long hash, |
adc0de68 | 271 | struct cds_lfht_node *node); |
3eca1b8c | 272 | |
48ed1c18 | 273 | /* |
9357c415 | 274 | * cds_lfht_add_replace - replace or add a node within hash table. |
0422d92c MD |
275 | * @ht: the hash table. |
276 | * @match: the key match function. | |
04db56f8 LJ |
277 | * @key: the node's key. |
278 | * @hash: the node's hash. | |
0422d92c | 279 | * @node: the node to add. |
48ed1c18 MD |
280 | * |
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. | |
3df0c49c | 285 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
286 | * After successful replacement, a grace period must be waited for before |
287 | * freeing the memory reserved for the returned node. | |
288 | * | |
289 | * The semantic of replacement vs lookups is the following: if lookups | |
9357c415 MD |
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. | |
48ed1c18 MD |
293 | * |
294 | * Providing this semantic allows us to ensure that replacement-only | |
295 | * schemes will never generate duplicated keys. It also allows us to | |
9357c415 | 296 | * guarantee that a combination of add_replace and add_unique updates |
48ed1c18 MD |
297 | * will never generate duplicated keys. |
298 | */ | |
9357c415 | 299 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
0422d92c | 300 | cds_lfht_match_fct match, |
04db56f8 | 301 | void *key, |
0422d92c | 302 | unsigned long hash, |
adc0de68 | 303 | struct cds_lfht_node *node); |
48ed1c18 | 304 | |
f0c29ed7 | 305 | /* |
9357c415 | 306 | * cds_lfht_replace - replace a node pointer to by iter within hash table. |
0422d92c MD |
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. | |
f0c29ed7 | 310 | * |
9357c415 MD |
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 | |
313 | * negative value. | |
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. | |
3df0c49c | 317 | * Threads calling this API need to be registered RCU read-side threads. |
9357c415 MD |
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). | |
321 | * | |
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. | |
326 | * | |
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. | |
331 | */ | |
332 | int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter, | |
333 | struct cds_lfht_node *new_node); | |
334 | ||
335 | /* | |
336 | * cds_lfht_del - remove node pointed to by iterator from hash table. | |
0422d92c MD |
337 | * @ht: the hash table. |
338 | * @iter: the iterator position of the node to delete. | |
9357c415 MD |
339 | * |
340 | * Return 0 if the node is successfully removed, negative value | |
341 | * otherwise. | |
342 | * Replacing a NULL node or an already removed node will fail with a | |
343 | * negative value. | |
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. | |
f0c29ed7 | 347 | * Call with rcu_read_lock held. |
3df0c49c | 348 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 349 | * After successful removal, a grace period must be waited for before |
9357c415 MD |
350 | * freeing the memory reserved for old node (which can be accessed with |
351 | * cds_lfht_iter_get_node). | |
f0c29ed7 | 352 | */ |
9357c415 | 353 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
ab7d5fc6 | 354 | |
f0c29ed7 | 355 | /* |
14044b37 | 356 | * cds_lfht_resize - Force a hash table resize |
0422d92c | 357 | * @ht: the hash table. |
1475579c | 358 | * @new_size: update to this hash table size. |
3df0c49c MD |
359 | * |
360 | * Threads calling this API need to be registered RCU read-side threads. | |
f0c29ed7 | 361 | */ |
1475579c | 362 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 363 | |
01389791 MD |
364 | #ifdef __cplusplus |
365 | } | |
366 | #endif | |
367 | ||
a42cc659 | 368 | #endif /* _URCU_RCULFHASH_H */ |