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 | ||
7f61a77f | 35 | /* |
14044b37 | 36 | * struct cds_lfht_node and struct _cds_lfht_node should be aligned on |
b198f0fd | 37 | * 4-bytes boundaries because the two lower bits are used as flags. |
7f61a77f MD |
38 | */ |
39 | ||
7f52427b MD |
40 | /* |
41 | * _cds_lfht_node: Contains the internal pointers and reverse-hash | |
42 | * value required for lookup and traversal of the hash table. | |
43 | */ | |
14044b37 | 44 | struct _cds_lfht_node { |
b198f0fd | 45 | struct cds_lfht_node *next; /* ptr | DUMMY_FLAG | REMOVED_FLAG */ |
abc490a1 | 46 | unsigned long reverse_hash; |
58ca0741 | 47 | } __attribute__((aligned(4))); |
cc4fcb10 | 48 | |
ae62aa6a | 49 | /* |
7f52427b MD |
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. | |
55 | * | |
ae62aa6a MD |
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. | |
59 | */ | |
14044b37 | 60 | struct cds_lfht_node { |
cc4fcb10 | 61 | /* cache-hot for iteration */ |
14044b37 | 62 | struct _cds_lfht_node p; /* needs to be first field */ |
9b35f1e4 MD |
63 | void *key; |
64 | unsigned int key_len; | |
abc490a1 MD |
65 | }; |
66 | ||
7f52427b | 67 | /* cds_lfht_iter: Used to track state while traversing a hash chain. */ |
adc0de68 MD |
68 | struct cds_lfht_iter { |
69 | struct cds_lfht_node *node, *next; | |
70 | }; | |
71 | ||
72 | static inline | |
73 | struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) | |
74 | { | |
75 | return iter->node; | |
76 | } | |
77 | ||
14044b37 | 78 | struct cds_lfht; |
ab7d5fc6 | 79 | |
5e28c532 MD |
80 | /* |
81 | * Caution ! | |
abc490a1 | 82 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
83 | */ |
84 | ||
14044b37 MD |
85 | typedef unsigned long (*cds_lfht_hash_fct)(void *key, size_t length, |
86 | unsigned long seed); | |
87 | typedef unsigned long (*cds_lfht_compare_fct)(void *key1, size_t key1_len, | |
88 | void *key2, size_t key2_len); | |
abc490a1 | 89 | |
f0c29ed7 | 90 | /* |
14044b37 | 91 | * cds_lfht_node_init - initialize a hash table node |
f0c29ed7 | 92 | */ |
abc490a1 | 93 | static inline |
14044b37 MD |
94 | void cds_lfht_node_init(struct cds_lfht_node *node, void *key, |
95 | size_t key_len) | |
abc490a1 MD |
96 | { |
97 | node->key = key; | |
732ad076 | 98 | node->key_len = key_len; |
abc490a1 | 99 | } |
674f7a69 | 100 | |
b8af5011 MD |
101 | /* |
102 | * Hash table creation flags. | |
103 | */ | |
104 | enum { | |
105 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
5afadd12 | 106 | CDS_LFHT_ACCOUNTING = (1U << 1), |
b8af5011 MD |
107 | }; |
108 | ||
674f7a69 | 109 | /* |
7a9dcf9b | 110 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
674f7a69 | 111 | */ |
7a9dcf9b | 112 | struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct, |
14044b37 MD |
113 | cds_lfht_compare_fct compare_fct, |
114 | unsigned long hash_seed, | |
115 | unsigned long init_size, | |
5488222b | 116 | unsigned long min_alloc_size, |
b8af5011 | 117 | int flags, |
14044b37 | 118 | void (*cds_lfht_call_rcu)(struct rcu_head *head, |
1475579c | 119 | void (*func)(struct rcu_head *head)), |
01dbfa62 MD |
120 | void (*cds_lfht_synchronize_rcu)(void), |
121 | void (*cds_lfht_rcu_read_lock)(void), | |
5f511391 MD |
122 | void (*cds_lfht_rcu_read_unlock)(void), |
123 | void (*cds_lfht_rcu_thread_offline)(void), | |
b7d619b0 MD |
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); | |
ab7d5fc6 | 128 | |
7a9dcf9b MD |
129 | /* |
130 | * cds_lfht_new - allocate a hash table. | |
f22dd01d MD |
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. | |
5488222b | 135 | * @min_alloc_size: the smallest allocation size to use. Must be power of two. |
f22dd01d MD |
136 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). |
137 | * 0: no flags. | |
138 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
b7d619b0 | 139 | * @attr: optional resize worker thread attributes. NULL for default. |
7a9dcf9b | 140 | * |
7a9dcf9b MD |
141 | * Return NULL on error. |
142 | * Note: the RCU flavor must be already included before the hash table header. | |
b7d619b0 MD |
143 | * |
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. | |
3df0c49c | 151 | * Threads calling this API need to be registered RCU read-side threads. |
7a9dcf9b MD |
152 | */ |
153 | static inline | |
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, | |
5488222b | 158 | unsigned long min_alloc_size, |
b7d619b0 MD |
159 | int flags, |
160 | pthread_attr_t *attr) | |
7a9dcf9b MD |
161 | { |
162 | return _cds_lfht_new(hash_fct, compare_fct, hash_seed, | |
5488222b | 163 | init_size, min_alloc_size, flags, |
7a9dcf9b MD |
164 | call_rcu, synchronize_rcu, rcu_read_lock, |
165 | rcu_read_unlock, rcu_thread_offline, | |
b7d619b0 MD |
166 | rcu_thread_online, rcu_register_thread, |
167 | rcu_unregister_thread, attr); | |
7a9dcf9b MD |
168 | } |
169 | ||
f0c29ed7 | 170 | /* |
14044b37 | 171 | * cds_lfht_destroy - destroy a hash table. |
b7d619b0 MD |
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 | |
7f52427b MD |
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(). | |
6878c72b MD |
177 | * |
178 | * Return 0 on success, negative error value on error. | |
3df0c49c | 179 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 180 | */ |
b7d619b0 | 181 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
f0c29ed7 MD |
182 | |
183 | /* | |
14044b37 | 184 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
7f52427b MD |
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. | |
f0c29ed7 | 190 | * Call with rcu_read_lock held. |
3df0c49c | 191 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 192 | */ |
14044b37 | 193 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
7f52427b | 194 | long *split_count_before, |
adc0de68 | 195 | unsigned long *count, |
973e5e1b | 196 | unsigned long *removed, |
7f52427b | 197 | long *split_count_after); |
ab7d5fc6 | 198 | |
f0c29ed7 | 199 | /* |
14044b37 | 200 | * cds_lfht_lookup - lookup a node by key. |
f0c29ed7 | 201 | * |
adc0de68 | 202 | * Output in "*iter". *iter->node set to NULL if not found. |
f0c29ed7 | 203 | * Call with rcu_read_lock held. |
3df0c49c | 204 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 205 | */ |
adc0de68 MD |
206 | void cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len, |
207 | struct cds_lfht_iter *iter); | |
ab7d5fc6 | 208 | |
f0c29ed7 | 209 | /* |
3883c0e5 | 210 | * cds_lfht_next_duplicate - get the next item with same key (after a lookup). |
f0c29ed7 | 211 | * |
adc0de68 MD |
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. | |
3df0c49c | 219 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 220 | */ |
3883c0e5 | 221 | void cds_lfht_next_duplicate(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
4e9b9fbf MD |
222 | |
223 | /* | |
224 | * cds_lfht_first - get the first node in the table. | |
225 | * | |
226 | * Output in "*iter". *iter->node set to NULL if table is empty. | |
227 | * Call with rcu_read_lock held. | |
3df0c49c | 228 | * Threads calling this API need to be registered RCU read-side threads. |
4e9b9fbf MD |
229 | */ |
230 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter); | |
231 | ||
232 | /* | |
233 | * cds_lfht_next - get the next node in the table. | |
234 | * | |
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. | |
3df0c49c | 238 | * Threads calling this API need to be registered RCU read-side threads. |
4e9b9fbf MD |
239 | */ |
240 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter); | |
ab7d5fc6 | 241 | |
18117871 | 242 | /* |
14044b37 | 243 | * cds_lfht_add - add a node to the hash table. |
f0c29ed7 | 244 | * |
48ed1c18 | 245 | * This function supports adding redundant keys into the table. |
3df0c49c MD |
246 | * Call with rcu_read_lock held. |
247 | * Threads calling this API need to be registered RCU read-side threads. | |
f0c29ed7 | 248 | */ |
14044b37 | 249 | void cds_lfht_add(struct cds_lfht *ht, 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. |
f0c29ed7 | 253 | * |
6878c72b | 254 | * Return the node added upon success. |
860d07e8 MD |
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. | |
f0c29ed7 | 258 | * Call with rcu_read_lock held. |
3df0c49c | 259 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
260 | * |
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 | |
9357c415 | 264 | * add_unique and add_replace (see below). |
18117871 | 265 | */ |
adc0de68 MD |
266 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
267 | struct cds_lfht_node *node); | |
3eca1b8c | 268 | |
48ed1c18 | 269 | /* |
9357c415 | 270 | * cds_lfht_add_replace - replace or add a node within hash table. |
48ed1c18 MD |
271 | * |
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. | |
3df0c49c | 276 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
277 | * After successful replacement, a grace period must be waited for before |
278 | * freeing the memory reserved for the returned node. | |
279 | * | |
280 | * The semantic of replacement vs lookups is the following: if lookups | |
9357c415 MD |
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. | |
48ed1c18 MD |
284 | * |
285 | * Providing this semantic allows us to ensure that replacement-only | |
286 | * schemes will never generate duplicated keys. It also allows us to | |
9357c415 | 287 | * guarantee that a combination of add_replace and add_unique updates |
48ed1c18 MD |
288 | * will never generate duplicated keys. |
289 | */ | |
9357c415 | 290 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
adc0de68 | 291 | struct cds_lfht_node *node); |
48ed1c18 | 292 | |
f0c29ed7 | 293 | /* |
9357c415 | 294 | * cds_lfht_replace - replace a node pointer to by iter within hash table. |
f0c29ed7 | 295 | * |
9357c415 MD |
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 | |
298 | * negative value. | |
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. | |
3df0c49c | 302 | * Threads calling this API need to be registered RCU read-side threads. |
9357c415 MD |
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). | |
306 | * | |
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. | |
311 | * | |
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. | |
316 | */ | |
317 | int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter, | |
318 | struct cds_lfht_node *new_node); | |
319 | ||
320 | /* | |
321 | * cds_lfht_del - remove node pointed to by iterator from hash table. | |
322 | * | |
323 | * Return 0 if the node is successfully removed, negative value | |
324 | * otherwise. | |
325 | * Replacing a NULL node or an already removed node will fail with a | |
326 | * negative value. | |
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. | |
f0c29ed7 | 330 | * Call with rcu_read_lock held. |
3df0c49c | 331 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 332 | * After successful removal, a grace period must be waited for before |
9357c415 MD |
333 | * freeing the memory reserved for old node (which can be accessed with |
334 | * cds_lfht_iter_get_node). | |
f0c29ed7 | 335 | */ |
9357c415 | 336 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
ab7d5fc6 | 337 | |
f0c29ed7 | 338 | /* |
14044b37 | 339 | * cds_lfht_resize - Force a hash table resize |
1475579c | 340 | * @new_size: update to this hash table size. |
3df0c49c MD |
341 | * |
342 | * Threads calling this API need to be registered RCU read-side threads. | |
f0c29ed7 | 343 | */ |
1475579c | 344 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 345 | |
01389791 MD |
346 | #ifdef __cplusplus |
347 | } | |
348 | #endif | |
349 | ||
a42cc659 | 350 | #endif /* _URCU_RCULFHASH_H */ |