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> |
6d320126 | 29 | #include <urcu/compiler.h> |
abc490a1 MD |
30 | #include <urcu-call-rcu.h> |
31 | ||
01389791 MD |
32 | #ifdef __cplusplus |
33 | extern "C" { | |
34 | #endif | |
35 | ||
7f52427b | 36 | /* |
04db56f8 | 37 | * cds_lfht_node: Contains the next pointers and reverse-hash |
7f52427b | 38 | * value required for lookup and traversal of the hash table. |
04db56f8 LJ |
39 | * |
40 | * struct cds_lfht_node should be aligned on 4-bytes boundaries because | |
41 | * the two lower bits are used as flags. | |
7f52427b | 42 | * |
ae62aa6a MD |
43 | * struct cds_lfht_node can be embedded into a structure (as a field). |
44 | * caa_container_of() can be used to get the structure from the struct | |
45 | * cds_lfht_node after a lookup. | |
04db56f8 LJ |
46 | * |
47 | * The structure which embeds it typically holds the key (or key-value | |
48 | * pair) of the object. The caller code is responsible for calculation | |
49 | * of the hash value for cds_lfht APIs. | |
ae62aa6a | 50 | */ |
14044b37 | 51 | struct cds_lfht_node { |
1ee8f000 | 52 | struct cds_lfht_node *next; /* ptr | BUCKET_FLAG | REMOVED_FLAG */ |
04db56f8 LJ |
53 | unsigned long reverse_hash; |
54 | } __attribute__((aligned(4))); | |
abc490a1 | 55 | |
7f52427b | 56 | /* cds_lfht_iter: Used to track state while traversing a hash chain. */ |
adc0de68 MD |
57 | struct cds_lfht_iter { |
58 | struct cds_lfht_node *node, *next; | |
59 | }; | |
60 | ||
61 | static inline | |
62 | struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) | |
63 | { | |
64 | return iter->node; | |
65 | } | |
66 | ||
14044b37 | 67 | struct cds_lfht; |
ab7d5fc6 | 68 | |
5e28c532 MD |
69 | /* |
70 | * Caution ! | |
abc490a1 | 71 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
72 | */ |
73 | ||
996ff57c | 74 | typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key); |
abc490a1 | 75 | |
f0c29ed7 | 76 | /* |
14044b37 | 77 | * cds_lfht_node_init - initialize a hash table node |
0422d92c | 78 | * @node: the node to initialize. |
04db56f8 LJ |
79 | * |
80 | * This function is kept to be eventually used for debugging purposes | |
81 | * (detection of memory corruption). | |
f0c29ed7 | 82 | */ |
abc490a1 | 83 | static inline |
04db56f8 | 84 | void cds_lfht_node_init(struct cds_lfht_node *node) |
abc490a1 | 85 | { |
abc490a1 | 86 | } |
674f7a69 | 87 | |
b8af5011 MD |
88 | /* |
89 | * Hash table creation flags. | |
90 | */ | |
91 | enum { | |
92 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
5afadd12 | 93 | CDS_LFHT_ACCOUNTING = (1U << 1), |
b8af5011 MD |
94 | }; |
95 | ||
674f7a69 | 96 | /* |
7a9dcf9b | 97 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
674f7a69 | 98 | */ |
0422d92c | 99 | struct cds_lfht *_cds_lfht_new(unsigned long init_size, |
5488222b | 100 | unsigned long min_alloc_size, |
b8af5011 | 101 | int flags, |
14044b37 | 102 | void (*cds_lfht_call_rcu)(struct rcu_head *head, |
1475579c | 103 | void (*func)(struct rcu_head *head)), |
01dbfa62 MD |
104 | void (*cds_lfht_synchronize_rcu)(void), |
105 | void (*cds_lfht_rcu_read_lock)(void), | |
5f511391 MD |
106 | void (*cds_lfht_rcu_read_unlock)(void), |
107 | void (*cds_lfht_rcu_thread_offline)(void), | |
b7d619b0 MD |
108 | void (*cds_lfht_rcu_thread_online)(void), |
109 | void (*cds_lfht_rcu_register_thread)(void), | |
110 | void (*cds_lfht_rcu_unregister_thread)(void), | |
111 | pthread_attr_t *attr); | |
ab7d5fc6 | 112 | |
7a9dcf9b MD |
113 | /* |
114 | * cds_lfht_new - allocate a hash table. | |
f22dd01d | 115 | * @init_size: number of nodes to allocate initially. Must be power of two. |
5488222b | 116 | * @min_alloc_size: the smallest allocation size to use. Must be power of two. |
f22dd01d MD |
117 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). |
118 | * 0: no flags. | |
119 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
44bbe7fa LJ |
120 | * CDS_LFHT_ACCOUNTING: count the number of node addition |
121 | * and removal in the table | |
b7d619b0 | 122 | * @attr: optional resize worker thread attributes. NULL for default. |
7a9dcf9b | 123 | * |
7a9dcf9b MD |
124 | * Return NULL on error. |
125 | * Note: the RCU flavor must be already included before the hash table header. | |
b7d619b0 MD |
126 | * |
127 | * The programmer is responsible for ensuring that resize operation has a | |
128 | * priority equal to hash table updater threads. It should be performed by | |
129 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
130 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
131 | * this priority level. Having lower priority for call_rcu and resize threads | |
132 | * does not pose any correctness issue, but the resize operations could be | |
133 | * starved by updates, thus leading to long hash table bucket chains. | |
44bbe7fa LJ |
134 | * Threads calling this API are NOT required to be registered RCU read-side |
135 | * threads. It can be called very early.(before rcu is initialized ...etc.) | |
7a9dcf9b MD |
136 | */ |
137 | static inline | |
0422d92c | 138 | struct cds_lfht *cds_lfht_new(unsigned long init_size, |
5488222b | 139 | unsigned long min_alloc_size, |
b7d619b0 MD |
140 | int flags, |
141 | pthread_attr_t *attr) | |
7a9dcf9b | 142 | { |
0422d92c | 143 | return _cds_lfht_new(init_size, min_alloc_size, flags, |
7a9dcf9b MD |
144 | call_rcu, synchronize_rcu, rcu_read_lock, |
145 | rcu_read_unlock, rcu_thread_offline, | |
b7d619b0 MD |
146 | rcu_thread_online, rcu_register_thread, |
147 | rcu_unregister_thread, attr); | |
7a9dcf9b MD |
148 | } |
149 | ||
f0c29ed7 | 150 | /* |
14044b37 | 151 | * cds_lfht_destroy - destroy a hash table. |
b7d619b0 MD |
152 | * @ht: the hash table to destroy. |
153 | * @attr: (output) resize worker thread attributes, as received by cds_lfht_new. | |
154 | * The caller will typically want to free this pointer if dynamically | |
7f52427b MD |
155 | * allocated. The attr point can be NULL if the caller does not |
156 | * need to be informed of the value passed to cds_lfht_new(). | |
6878c72b MD |
157 | * |
158 | * Return 0 on success, negative error value on error. | |
3df0c49c | 159 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 160 | */ |
b7d619b0 | 161 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
f0c29ed7 MD |
162 | |
163 | /* | |
14044b37 | 164 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
7f52427b MD |
165 | * @ht: the hash table. |
166 | * @split_count_before: Sample the node count split-counter before traversal. | |
167 | * @count: Traverse the hash table, count the number of nodes observed. | |
168 | * @removed: Number of logically removed nodes observed during traversal. | |
169 | * @split_count_after: Sample the node count split-counter after traversal. | |
0422d92c | 170 | * |
f0c29ed7 | 171 | * Call with rcu_read_lock held. |
3df0c49c | 172 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 173 | */ |
14044b37 | 174 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
7f52427b | 175 | long *split_count_before, |
adc0de68 | 176 | unsigned long *count, |
973e5e1b | 177 | unsigned long *removed, |
7f52427b | 178 | long *split_count_after); |
ab7d5fc6 | 179 | |
f0c29ed7 | 180 | /* |
14044b37 | 181 | * cds_lfht_lookup - lookup a node by key. |
0422d92c | 182 | * @ht: the hash table. |
0422d92c | 183 | * @hash: the key hash. |
6f554439 LJ |
184 | * @match: the key match function. |
185 | * @key: the current node key. | |
0422d92c | 186 | * @iter: Node, if found (output). *iter->node set to NULL if not found. |
f0c29ed7 | 187 | * |
f0c29ed7 | 188 | * Call with rcu_read_lock held. |
3df0c49c | 189 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 190 | */ |
6f554439 | 191 | void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash, |
996ff57c | 192 | cds_lfht_match_fct match, const void *key, |
6f554439 | 193 | struct cds_lfht_iter *iter); |
ab7d5fc6 | 194 | |
f0c29ed7 | 195 | /* |
3883c0e5 | 196 | * cds_lfht_next_duplicate - get the next item with same key (after a lookup). |
0422d92c MD |
197 | * @ht: the hash table. |
198 | * @match: the key match function. | |
04db56f8 | 199 | * @key: the current node key. |
0422d92c | 200 | * @iter: Node, if found (output). *iter->node set to NULL if not found. |
f0c29ed7 | 201 | * |
adc0de68 MD |
202 | * Uses an iterator initialized by a lookup. |
203 | * Sets *iter-node to the following node with same key. | |
204 | * Sets *iter->node to NULL if no following node exists with same key. | |
205 | * RCU read-side lock must be held across cds_lfht_lookup and | |
206 | * cds_lfht_next calls, and also between cds_lfht_next calls using the | |
207 | * node returned by a previous cds_lfht_next. | |
208 | * Call with rcu_read_lock held. | |
3df0c49c | 209 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 210 | */ |
0422d92c | 211 | void cds_lfht_next_duplicate(struct cds_lfht *ht, |
996ff57c | 212 | cds_lfht_match_fct match, const void *key, |
04db56f8 | 213 | struct cds_lfht_iter *iter); |
4e9b9fbf MD |
214 | |
215 | /* | |
216 | * cds_lfht_first - get the first node in the table. | |
0422d92c MD |
217 | * @ht: the hash table. |
218 | * @iter: First node, if exists (output). *iter->node set to NULL if not found. | |
4e9b9fbf MD |
219 | * |
220 | * Output in "*iter". *iter->node set to NULL if table is empty. | |
221 | * Call with rcu_read_lock held. | |
3df0c49c | 222 | * Threads calling this API need to be registered RCU read-side threads. |
4e9b9fbf MD |
223 | */ |
224 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter); | |
225 | ||
226 | /* | |
227 | * cds_lfht_next - get the next node in the table. | |
0422d92c MD |
228 | * @ht: the hash table. |
229 | * @iter: Next node, if exists (output). *iter->node set to NULL if not found. | |
4e9b9fbf MD |
230 | * |
231 | * Input/Output in "*iter". *iter->node set to NULL if *iter was | |
232 | * pointing to the last table node. | |
233 | * Call with rcu_read_lock held. | |
3df0c49c | 234 | * Threads calling this API need to be registered RCU read-side threads. |
4e9b9fbf MD |
235 | */ |
236 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter); | |
ab7d5fc6 | 237 | |
18117871 | 238 | /* |
14044b37 | 239 | * cds_lfht_add - add a node to the hash table. |
0422d92c MD |
240 | * @ht: the hash table. |
241 | * @hash: the key hash. | |
242 | * @node: the node to add. | |
f0c29ed7 | 243 | * |
48ed1c18 | 244 | * This function supports adding redundant keys into the table. |
3df0c49c MD |
245 | * Call with rcu_read_lock held. |
246 | * Threads calling this API need to be registered RCU read-side threads. | |
f0c29ed7 | 247 | */ |
0422d92c MD |
248 | void cds_lfht_add(struct cds_lfht *ht, unsigned long hash, |
249 | struct cds_lfht_node *node); | |
f0c29ed7 MD |
250 | |
251 | /* | |
14044b37 | 252 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
0422d92c | 253 | * @ht: the hash table. |
6f554439 | 254 | * @hash: the node's hash. |
0422d92c | 255 | * @match: the key match function. |
04db56f8 | 256 | * @key: the node's key. |
0422d92c | 257 | * @node: the node to try adding. |
f0c29ed7 | 258 | * |
6878c72b | 259 | * Return the node added upon success. |
860d07e8 MD |
260 | * Return the unique node already present upon failure. If |
261 | * cds_lfht_add_unique fails, the node passed as parameter should be | |
262 | * freed by the caller. | |
f0c29ed7 | 263 | * Call with rcu_read_lock held. |
3df0c49c | 264 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
265 | * |
266 | * The semantic of this function is that if only this function is used | |
267 | * to add keys into the table, no duplicated keys should ever be | |
268 | * observable in the table. The same guarantee apply for combination of | |
9357c415 | 269 | * add_unique and add_replace (see below). |
18117871 | 270 | */ |
adc0de68 | 271 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
6f554439 | 272 | unsigned long hash, |
0422d92c | 273 | cds_lfht_match_fct match, |
996ff57c | 274 | const void *key, |
adc0de68 | 275 | struct cds_lfht_node *node); |
3eca1b8c | 276 | |
48ed1c18 | 277 | /* |
9357c415 | 278 | * cds_lfht_add_replace - replace or add a node within hash table. |
0422d92c | 279 | * @ht: the hash table. |
6f554439 | 280 | * @hash: the node's hash. |
0422d92c | 281 | * @match: the key match function. |
04db56f8 | 282 | * @key: the node's key. |
0422d92c | 283 | * @node: the node to add. |
48ed1c18 MD |
284 | * |
285 | * Return the node replaced upon success. If no node matching the key | |
286 | * was present, return NULL, which also means the operation succeeded. | |
287 | * This replacement operation should never fail. | |
288 | * Call with rcu_read_lock held. | |
3df0c49c | 289 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
290 | * After successful replacement, a grace period must be waited for before |
291 | * freeing the memory reserved for the returned node. | |
292 | * | |
293 | * The semantic of replacement vs lookups is the following: if lookups | |
9357c415 MD |
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. | |
48ed1c18 MD |
297 | * |
298 | * Providing this semantic allows us to ensure that replacement-only | |
299 | * schemes will never generate duplicated keys. It also allows us to | |
9357c415 | 300 | * guarantee that a combination of add_replace and add_unique updates |
48ed1c18 MD |
301 | * will never generate duplicated keys. |
302 | */ | |
9357c415 | 303 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
6f554439 | 304 | unsigned long hash, |
0422d92c | 305 | cds_lfht_match_fct match, |
996ff57c | 306 | const void *key, |
adc0de68 | 307 | struct cds_lfht_node *node); |
48ed1c18 | 308 | |
f0c29ed7 | 309 | /* |
9357c415 | 310 | * cds_lfht_replace - replace a node pointer to by iter within hash table. |
0422d92c MD |
311 | * @ht: the hash table. |
312 | * @old_iter: the iterator position of the node to replace. | |
313 | * @now_node: the new node to try using for replacement. | |
f0c29ed7 | 314 | * |
9357c415 MD |
315 | * Return 0 if replacement is successful, negative value otherwise. |
316 | * Replacing a NULL old node or an already removed node will fail with a | |
317 | * negative value. | |
318 | * Old node can be looked up with cds_lfht_lookup and cds_lfht_next. | |
319 | * RCU read-side lock must be held between lookup and replacement. | |
320 | * Call with rcu_read_lock held. | |
3df0c49c | 321 | * Threads calling this API need to be registered RCU read-side threads. |
9357c415 MD |
322 | * After successful replacement, a grace period must be waited for before |
323 | * freeing the memory reserved for the old node (which can be accessed | |
324 | * with cds_lfht_iter_get_node). | |
325 | * | |
326 | * The semantic of replacement vs lookups is the following: if lookups | |
327 | * are performed between a key unique insertion and its removal, we | |
328 | * guarantee that the lookups and get next will always find exactly one | |
329 | * instance of the key if it is replaced concurrently with the lookups. | |
330 | * | |
331 | * Providing this semantic allows us to ensure that replacement-only | |
332 | * schemes will never generate duplicated keys. It also allows us to | |
333 | * guarantee that a combination of add_replace and add_unique updates | |
334 | * will never generate duplicated keys. | |
335 | */ | |
336 | int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter, | |
337 | struct cds_lfht_node *new_node); | |
338 | ||
339 | /* | |
340 | * cds_lfht_del - remove node pointed to by iterator from hash table. | |
0422d92c MD |
341 | * @ht: the hash table. |
342 | * @iter: the iterator position of the node to delete. | |
9357c415 MD |
343 | * |
344 | * Return 0 if the node is successfully removed, negative value | |
345 | * otherwise. | |
346 | * Replacing a NULL node or an already removed node will fail with a | |
347 | * negative value. | |
348 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next. | |
349 | * cds_lfht_iter_get_node. | |
350 | * RCU read-side lock must be held between lookup and removal. | |
f0c29ed7 | 351 | * Call with rcu_read_lock held. |
3df0c49c | 352 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 353 | * After successful removal, a grace period must be waited for before |
9357c415 MD |
354 | * freeing the memory reserved for old node (which can be accessed with |
355 | * cds_lfht_iter_get_node). | |
f0c29ed7 | 356 | */ |
9357c415 | 357 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
ab7d5fc6 | 358 | |
f0c29ed7 | 359 | /* |
14044b37 | 360 | * cds_lfht_resize - Force a hash table resize |
0422d92c | 361 | * @ht: the hash table. |
1475579c | 362 | * @new_size: update to this hash table size. |
3df0c49c MD |
363 | * |
364 | * Threads calling this API need to be registered RCU read-side threads. | |
f0c29ed7 | 365 | */ |
1475579c | 366 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 367 | |
6d320126 MD |
368 | /* |
369 | * Note: cds_lfht_for_each are safe for element removal during | |
370 | * iteration. | |
371 | */ | |
372 | #define cds_lfht_for_each(ht, iter, node) \ | |
373 | for (cds_lfht_first(ht, iter), \ | |
374 | node = cds_lfht_iter_get_node(iter); \ | |
375 | node != NULL; \ | |
376 | cds_lfht_next(ht, iter), \ | |
377 | node = cds_lfht_iter_get_node(iter)) | |
378 | ||
6f554439 LJ |
379 | #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \ |
380 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ | |
6d320126 MD |
381 | node = cds_lfht_iter_get_node(iter); \ |
382 | node != NULL; \ | |
383 | cds_lfht_next_duplicate(ht, match, key, iter), \ | |
384 | node = cds_lfht_iter_get_node(iter)) | |
385 | ||
386 | #define cds_lfht_for_each_entry(ht, iter, pos, member) \ | |
387 | for (cds_lfht_first(ht, iter), \ | |
388 | pos = caa_container_of(cds_lfht_iter_get_node(iter), \ | |
389 | typeof(*(pos)), member); \ | |
0b0627bf | 390 | &(pos)->member != NULL; \ |
6d320126 MD |
391 | cds_lfht_next(ht, iter), \ |
392 | pos = caa_container_of(cds_lfht_iter_get_node(iter), \ | |
393 | typeof(*(pos)), member)) | |
394 | ||
6f554439 | 395 | #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \ |
6d320126 | 396 | iter, pos, member) \ |
6f554439 | 397 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ |
6d320126 MD |
398 | pos = caa_container_of(cds_lfht_iter_get_node(iter), \ |
399 | typeof(*(pos)), member); \ | |
0b0627bf | 400 | &(pos)->member != NULL; \ |
6d320126 MD |
401 | cds_lfht_next_duplicate(ht, match, key, iter), \ |
402 | pos = caa_container_of(cds_lfht_iter_get_node(iter), \ | |
403 | typeof(*(pos)), member)) | |
404 | ||
01389791 MD |
405 | #ifdef __cplusplus |
406 | } | |
407 | #endif | |
408 | ||
a42cc659 | 409 | #endif /* _URCU_RCULFHASH_H */ |