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> | |
0dcf4847 | 10 | * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com> |
f0c29ed7 MD |
11 | * |
12 | * This library is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public | |
14 | * License as published by the Free Software Foundation; either | |
15 | * version 2.1 of the License, or (at your option) any later version. | |
16 | * | |
17 | * This library is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * Lesser General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this library; if not, write to the Free Software | |
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
cf77d1fa | 25 | * |
938465b4 MD |
26 | * For use with URCU_API_MAP (API mapping of liburcu), include this file |
27 | * _after_ including your URCU flavor. | |
f0c29ed7 MD |
28 | */ |
29 | ||
d7c76f85 | 30 | #include <urcu/config.h> |
674f7a69 | 31 | #include <stdint.h> |
6bcce235 | 32 | #include <pthread.h> |
6d320126 | 33 | #include <urcu/compiler.h> |
abc490a1 | 34 | |
01389791 MD |
35 | #ifdef __cplusplus |
36 | extern "C" { | |
37 | #endif | |
38 | ||
d7c76f85 MD |
39 | struct cds_lfht; |
40 | ||
7f52427b | 41 | /* |
04db56f8 | 42 | * cds_lfht_node: Contains the next pointers and reverse-hash |
7f52427b | 43 | * value required for lookup and traversal of the hash table. |
04db56f8 | 44 | * |
db00ccc3 | 45 | * struct cds_lfht_node should be aligned on 8-bytes boundaries because |
13f656f9 MD |
46 | * the three lower bits are used as flags. It is worth noting that the |
47 | * information contained within these three bits could be represented on | |
48 | * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and | |
49 | * BUCKET_FLAG. This can be done if we ensure that no iterator nor | |
50 | * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG | |
51 | * is set. Given the minimum size of struct cds_lfht_node is 8 bytes on | |
52 | * 32-bit architectures, we choose to go for simplicity and reserve | |
53 | * three bits. | |
7f52427b | 54 | * |
ae62aa6a MD |
55 | * struct cds_lfht_node can be embedded into a structure (as a field). |
56 | * caa_container_of() can be used to get the structure from the struct | |
57 | * cds_lfht_node after a lookup. | |
04db56f8 LJ |
58 | * |
59 | * The structure which embeds it typically holds the key (or key-value | |
60 | * pair) of the object. The caller code is responsible for calculation | |
61 | * of the hash value for cds_lfht APIs. | |
ae62aa6a | 62 | */ |
14044b37 | 63 | struct cds_lfht_node { |
3f2f3714 | 64 | struct cds_lfht_node *next; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */ |
04db56f8 | 65 | unsigned long reverse_hash; |
db00ccc3 | 66 | } __attribute__((aligned(8))); |
abc490a1 | 67 | |
7f52427b | 68 | /* cds_lfht_iter: Used to track state while traversing a hash chain. */ |
adc0de68 MD |
69 | struct cds_lfht_iter { |
70 | struct cds_lfht_node *node, *next; | |
d7c76f85 MD |
71 | /* |
72 | * For debugging purposes, build both API users and rculfhash | |
73 | * library with CDS_LFHT_ITER_DEBUG defined. This enables extra | |
74 | * consistency checks for calls to a cds_lfht_next() or | |
75 | * cds_lfht_next_duplicate() after the iterator has been | |
76 | * re-purposed to iterate on a different hash table. This is a | |
77 | * common programming mistake when performing hash table lookup | |
78 | * nested in a hash table traversal. | |
79 | */ | |
80 | #ifdef CONFIG_CDS_LFHT_ITER_DEBUG | |
81 | struct cds_lfht *lfht; | |
82 | #endif | |
adc0de68 MD |
83 | }; |
84 | ||
85 | static inline | |
86 | struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) | |
87 | { | |
88 | return iter->node; | |
89 | } | |
90 | ||
938465b4 | 91 | struct rcu_flavor_struct; |
ab7d5fc6 | 92 | |
5e28c532 MD |
93 | /* |
94 | * Caution ! | |
abc490a1 | 95 | * Ensure reader and writer threads are registered as urcu readers. |
5e28c532 MD |
96 | */ |
97 | ||
996ff57c | 98 | typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, const void *key); |
abc490a1 | 99 | |
f0c29ed7 | 100 | /* |
14044b37 | 101 | * cds_lfht_node_init - initialize a hash table node |
0422d92c | 102 | * @node: the node to initialize. |
04db56f8 LJ |
103 | * |
104 | * This function is kept to be eventually used for debugging purposes | |
105 | * (detection of memory corruption). | |
f0c29ed7 | 106 | */ |
abc490a1 | 107 | static inline |
70469b43 | 108 | void cds_lfht_node_init(struct cds_lfht_node *node __attribute__((unused))) |
abc490a1 | 109 | { |
abc490a1 | 110 | } |
674f7a69 | 111 | |
4c10e9af MD |
112 | /* |
113 | * cds_lfht_node_init_deleted - initialize a hash table node to "removed" state | |
114 | * @node: the node to initialize. | |
115 | * | |
116 | * Initialize the node such that cds_lfht_is_node_deleted() can be used | |
117 | * on the node before it is added to a hash table. | |
118 | */ | |
119 | extern | |
120 | void cds_lfht_node_init_deleted(struct cds_lfht_node *node); | |
121 | ||
b8af5011 MD |
122 | /* |
123 | * Hash table creation flags. | |
124 | */ | |
125 | enum { | |
126 | CDS_LFHT_AUTO_RESIZE = (1U << 0), | |
5afadd12 | 127 | CDS_LFHT_ACCOUNTING = (1U << 1), |
b8af5011 MD |
128 | }; |
129 | ||
0b6aa001 LJ |
130 | struct cds_lfht_mm_type { |
131 | struct cds_lfht *(*alloc_cds_lfht)(unsigned long min_nr_alloc_buckets, | |
132 | unsigned long max_nr_buckets); | |
133 | void (*alloc_bucket_table)(struct cds_lfht *ht, unsigned long order); | |
134 | void (*free_bucket_table)(struct cds_lfht *ht, unsigned long order); | |
135 | struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht, | |
136 | unsigned long index); | |
137 | }; | |
138 | ||
139 | extern const struct cds_lfht_mm_type cds_lfht_mm_order; | |
308d1cb3 | 140 | extern const struct cds_lfht_mm_type cds_lfht_mm_chunk; |
b0b55251 | 141 | extern const struct cds_lfht_mm_type cds_lfht_mm_mmap; |
0b6aa001 | 142 | |
674f7a69 | 143 | /* |
7a9dcf9b | 144 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
674f7a69 | 145 | */ |
b9c27904 | 146 | extern |
0422d92c | 147 | struct cds_lfht *_cds_lfht_new(unsigned long init_size, |
0722081a | 148 | unsigned long min_nr_alloc_buckets, |
747d725c | 149 | unsigned long max_nr_buckets, |
b8af5011 | 150 | int flags, |
0b6aa001 | 151 | const struct cds_lfht_mm_type *mm, |
7b17c13e | 152 | const struct rcu_flavor_struct *flavor, |
b7d619b0 | 153 | pthread_attr_t *attr); |
ab7d5fc6 | 154 | |
938465b4 MD |
155 | /* |
156 | * cds_lfht_new_flavor - allocate a hash table tied to a RCU flavor. | |
157 | * @init_size: number of buckets to allocate initially. Must be power of two. | |
158 | * @min_nr_alloc_buckets: the minimum number of allocated buckets. | |
159 | * (must be power of two) | |
160 | * @max_nr_buckets: the maximum number of hash table buckets allowed. | |
161 | * (must be power of two, 0 is accepted, means | |
162 | * "infinite") | |
163 | * @flavor: flavor of liburcu to use to synchronize the hash table | |
164 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). | |
165 | * 0: no flags. | |
166 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
167 | * CDS_LFHT_ACCOUNTING: count the number of node addition | |
168 | * and removal in the table | |
169 | * @attr: optional resize worker thread attributes. NULL for default. | |
170 | * | |
171 | * Return NULL on error. | |
172 | * Note: the RCU flavor must be already included before the hash table header. | |
173 | * | |
174 | * The programmer is responsible for ensuring that resize operation has a | |
175 | * priority equal to hash table updater threads. It should be performed by | |
176 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
177 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
178 | * this priority level. Having lower priority for call_rcu and resize threads | |
179 | * does not pose any correctness issue, but the resize operations could be | |
180 | * starved by updates, thus leading to long hash table bucket chains. | |
181 | * Threads calling cds_lfht_new are NOT required to be registered RCU | |
182 | * read-side threads. It can be called very early. (e.g. before RCU is | |
183 | * initialized) | |
184 | */ | |
185 | static inline | |
186 | struct cds_lfht *cds_lfht_new_flavor(unsigned long init_size, | |
187 | unsigned long min_nr_alloc_buckets, | |
188 | unsigned long max_nr_buckets, | |
189 | int flags, | |
190 | const struct rcu_flavor_struct *flavor, | |
191 | pthread_attr_t *attr) | |
192 | { | |
193 | return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets, | |
194 | flags, NULL, flavor, attr); | |
195 | } | |
196 | ||
197 | ||
198 | #ifdef URCU_API_MAP | |
7a9dcf9b MD |
199 | /* |
200 | * cds_lfht_new - allocate a hash table. | |
747d725c LJ |
201 | * @init_size: number of buckets to allocate initially. Must be power of two. |
202 | * @min_nr_alloc_buckets: the minimum number of allocated buckets. | |
203 | * (must be power of two) | |
204 | * @max_nr_buckets: the maximum number of hash table buckets allowed. | |
28d5c1b5 MD |
205 | * (must be power of two, 0 is accepted, means |
206 | * "infinite") | |
f22dd01d MD |
207 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). |
208 | * 0: no flags. | |
209 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. | |
44bbe7fa LJ |
210 | * CDS_LFHT_ACCOUNTING: count the number of node addition |
211 | * and removal in the table | |
b7d619b0 | 212 | * @attr: optional resize worker thread attributes. NULL for default. |
7a9dcf9b | 213 | * |
7a9dcf9b MD |
214 | * Return NULL on error. |
215 | * Note: the RCU flavor must be already included before the hash table header. | |
b7d619b0 MD |
216 | * |
217 | * The programmer is responsible for ensuring that resize operation has a | |
218 | * priority equal to hash table updater threads. It should be performed by | |
219 | * specifying the appropriate priority in the pthread "attr" argument, and, | |
220 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have | |
221 | * this priority level. Having lower priority for call_rcu and resize threads | |
222 | * does not pose any correctness issue, but the resize operations could be | |
223 | * starved by updates, thus leading to long hash table bucket chains. | |
3a9c66db MD |
224 | * Threads calling cds_lfht_new are NOT required to be registered RCU |
225 | * read-side threads. It can be called very early. (e.g. before RCU is | |
226 | * initialized) | |
7a9dcf9b MD |
227 | */ |
228 | static inline | |
0422d92c | 229 | struct cds_lfht *cds_lfht_new(unsigned long init_size, |
0722081a | 230 | unsigned long min_nr_alloc_buckets, |
747d725c | 231 | unsigned long max_nr_buckets, |
b7d619b0 MD |
232 | int flags, |
233 | pthread_attr_t *attr) | |
7a9dcf9b | 234 | { |
7b17c13e | 235 | return _cds_lfht_new(init_size, min_nr_alloc_buckets, max_nr_buckets, |
c1888f3a | 236 | flags, NULL, &rcu_flavor, attr); |
7a9dcf9b | 237 | } |
938465b4 | 238 | #endif /* URCU_API_MAP */ |
7a9dcf9b | 239 | |
f0c29ed7 | 240 | /* |
14044b37 | 241 | * cds_lfht_destroy - destroy a hash table. |
b7d619b0 MD |
242 | * @ht: the hash table to destroy. |
243 | * @attr: (output) resize worker thread attributes, as received by cds_lfht_new. | |
244 | * The caller will typically want to free this pointer if dynamically | |
7f52427b MD |
245 | * allocated. The attr point can be NULL if the caller does not |
246 | * need to be informed of the value passed to cds_lfht_new(). | |
6878c72b MD |
247 | * |
248 | * Return 0 on success, negative error value on error. | |
d0ec0ed2 | 249 | |
b047e7a7 MD |
250 | * Threads calling this API need to be registered RCU read-side threads. |
251 | * | |
d0ec0ed2 | 252 | * Prior to liburcu 0.10: |
d0ec0ed2 MD |
253 | * - cds_lfht_destroy should *not* be called from a RCU read-side |
254 | * critical section. It should *not* be called from a call_rcu thread | |
255 | * context neither. | |
256 | * | |
257 | * Starting from liburcu 0.10, rculfhash implements its own worker | |
b047e7a7 MD |
258 | * thread to handle resize operations, which removes the above RCU |
259 | * read-side critical section requirement on cds_lfht_destroy. | |
f0c29ed7 | 260 | */ |
b9c27904 | 261 | extern |
b7d619b0 | 262 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
f0c29ed7 MD |
263 | |
264 | /* | |
14044b37 | 265 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
7f52427b | 266 | * @ht: the hash table. |
3a9c66db MD |
267 | * @split_count_before: sample the node count split-counter before traversal. |
268 | * @count: traverse the hash table, count the number of nodes observed. | |
269 | * @split_count_after: sample the node count split-counter after traversal. | |
0422d92c | 270 | * |
f0c29ed7 | 271 | * Call with rcu_read_lock held. |
3df0c49c | 272 | * Threads calling this API need to be registered RCU read-side threads. |
f0c29ed7 | 273 | */ |
b9c27904 | 274 | extern |
14044b37 | 275 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
7f52427b | 276 | long *split_count_before, |
adc0de68 | 277 | unsigned long *count, |
7f52427b | 278 | long *split_count_after); |
ab7d5fc6 | 279 | |
f0c29ed7 | 280 | /* |
14044b37 | 281 | * cds_lfht_lookup - lookup a node by key. |
0422d92c | 282 | * @ht: the hash table. |
0422d92c | 283 | * @hash: the key hash. |
6f554439 LJ |
284 | * @match: the key match function. |
285 | * @key: the current node key. | |
3a9c66db | 286 | * @iter: node, if found (output). *iter->node set to NULL if not found. |
f0c29ed7 | 287 | * |
f0c29ed7 | 288 | * Call with rcu_read_lock held. |
3df0c49c | 289 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 290 | * This function acts as a rcu_dereference() to read the node pointer. |
f0c29ed7 | 291 | */ |
b9c27904 | 292 | extern |
6f554439 | 293 | void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash, |
996ff57c | 294 | cds_lfht_match_fct match, const void *key, |
6f554439 | 295 | struct cds_lfht_iter *iter); |
ab7d5fc6 | 296 | |
f0c29ed7 | 297 | /* |
3a9c66db | 298 | * cds_lfht_next_duplicate - get the next item with same key, after iterator. |
0422d92c MD |
299 | * @ht: the hash table. |
300 | * @match: the key match function. | |
04db56f8 | 301 | * @key: the current node key. |
3a9c66db MD |
302 | * @iter: input: current iterator. |
303 | * output: node, if found. *iter->node set to NULL if not found. | |
f0c29ed7 | 304 | * |
3a9c66db MD |
305 | * Uses an iterator initialized by a lookup or traversal. Important: the |
306 | * iterator _needs_ to be initialized before calling | |
307 | * cds_lfht_next_duplicate. | |
adc0de68 MD |
308 | * Sets *iter-node to the following node with same key. |
309 | * Sets *iter->node to NULL if no following node exists with same key. | |
310 | * RCU read-side lock must be held across cds_lfht_lookup and | |
311 | * cds_lfht_next calls, and also between cds_lfht_next calls using the | |
312 | * node returned by a previous cds_lfht_next. | |
313 | * Call with rcu_read_lock held. | |
3df0c49c | 314 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 315 | * This function acts as a rcu_dereference() to read the node pointer. |
f0c29ed7 | 316 | */ |
b9c27904 | 317 | extern |
0422d92c | 318 | void cds_lfht_next_duplicate(struct cds_lfht *ht, |
996ff57c | 319 | cds_lfht_match_fct match, const void *key, |
04db56f8 | 320 | struct cds_lfht_iter *iter); |
4e9b9fbf MD |
321 | |
322 | /* | |
323 | * cds_lfht_first - get the first node in the table. | |
0422d92c MD |
324 | * @ht: the hash table. |
325 | * @iter: First node, if exists (output). *iter->node set to NULL if not found. | |
4e9b9fbf MD |
326 | * |
327 | * Output in "*iter". *iter->node set to NULL if table is empty. | |
328 | * Call with rcu_read_lock held. | |
3df0c49c | 329 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 330 | * This function acts as a rcu_dereference() to read the node pointer. |
4e9b9fbf | 331 | */ |
b9c27904 | 332 | extern |
4e9b9fbf MD |
333 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
334 | ||
335 | /* | |
336 | * cds_lfht_next - get the next node in the table. | |
0422d92c | 337 | * @ht: the hash table. |
3a9c66db MD |
338 | * @iter: input: current iterator. |
339 | * output: next node, if exists. *iter->node set to NULL if not found. | |
4e9b9fbf MD |
340 | * |
341 | * Input/Output in "*iter". *iter->node set to NULL if *iter was | |
342 | * pointing to the last table node. | |
343 | * Call with rcu_read_lock held. | |
3df0c49c | 344 | * Threads calling this API need to be registered RCU read-side threads. |
7b783f81 | 345 | * This function acts as a rcu_dereference() to read the node pointer. |
4e9b9fbf | 346 | */ |
b9c27904 | 347 | extern |
4e9b9fbf | 348 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
ab7d5fc6 | 349 | |
18117871 | 350 | /* |
14044b37 | 351 | * cds_lfht_add - add a node to the hash table. |
0422d92c MD |
352 | * @ht: the hash table. |
353 | * @hash: the key hash. | |
354 | * @node: the node to add. | |
f0c29ed7 | 355 | * |
48ed1c18 | 356 | * This function supports adding redundant keys into the table. |
3df0c49c MD |
357 | * Call with rcu_read_lock held. |
358 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 MD |
359 | * This function issues a full memory barrier before and after its |
360 | * atomic commit. | |
f0c29ed7 | 361 | */ |
b9c27904 | 362 | extern |
0422d92c MD |
363 | void cds_lfht_add(struct cds_lfht *ht, unsigned long hash, |
364 | struct cds_lfht_node *node); | |
f0c29ed7 MD |
365 | |
366 | /* | |
14044b37 | 367 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
0422d92c | 368 | * @ht: the hash table. |
6f554439 | 369 | * @hash: the node's hash. |
0422d92c | 370 | * @match: the key match function. |
04db56f8 | 371 | * @key: the node's key. |
0422d92c | 372 | * @node: the node to try adding. |
f0c29ed7 | 373 | * |
6878c72b | 374 | * Return the node added upon success. |
860d07e8 MD |
375 | * Return the unique node already present upon failure. If |
376 | * cds_lfht_add_unique fails, the node passed as parameter should be | |
3a9c66db | 377 | * freed by the caller. In this case, the caller does NOT need to wait |
50024344 | 378 | * for a grace period before freeing or re-using the node. |
f0c29ed7 | 379 | * Call with rcu_read_lock held. |
3df0c49c | 380 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 MD |
381 | * |
382 | * The semantic of this function is that if only this function is used | |
383 | * to add keys into the table, no duplicated keys should ever be | |
384 | * observable in the table. The same guarantee apply for combination of | |
9357c415 | 385 | * add_unique and add_replace (see below). |
7b783f81 MD |
386 | * |
387 | * Upon success, this function issues a full memory barrier before and | |
388 | * after its atomic commit. Upon failure, this function acts like a | |
389 | * simple lookup operation: it acts as a rcu_dereference() to read the | |
390 | * node pointer. The failure case does not guarantee any other memory | |
391 | * barrier. | |
18117871 | 392 | */ |
b9c27904 | 393 | extern |
adc0de68 | 394 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
6f554439 | 395 | unsigned long hash, |
0422d92c | 396 | cds_lfht_match_fct match, |
996ff57c | 397 | const void *key, |
adc0de68 | 398 | struct cds_lfht_node *node); |
3eca1b8c | 399 | |
48ed1c18 | 400 | /* |
9357c415 | 401 | * cds_lfht_add_replace - replace or add a node within hash table. |
0422d92c | 402 | * @ht: the hash table. |
6f554439 | 403 | * @hash: the node's hash. |
0422d92c | 404 | * @match: the key match function. |
04db56f8 | 405 | * @key: the node's key. |
0422d92c | 406 | * @node: the node to add. |
48ed1c18 MD |
407 | * |
408 | * Return the node replaced upon success. If no node matching the key | |
409 | * was present, return NULL, which also means the operation succeeded. | |
410 | * This replacement operation should never fail. | |
411 | * Call with rcu_read_lock held. | |
3df0c49c | 412 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 413 | * After successful replacement, a grace period must be waited for before |
50024344 | 414 | * freeing or re-using the memory reserved for the returned node. |
48ed1c18 | 415 | * |
3a9c66db MD |
416 | * The semantic of replacement vs lookups and traversals is the |
417 | * following: if lookups and traversals are performed between a key | |
418 | * unique insertion and its removal, we guarantee that the lookups and | |
419 | * traversals will always find exactly one instance of the key if it is | |
420 | * replaced concurrently with the lookups. | |
48ed1c18 MD |
421 | * |
422 | * Providing this semantic allows us to ensure that replacement-only | |
423 | * schemes will never generate duplicated keys. It also allows us to | |
9357c415 | 424 | * guarantee that a combination of add_replace and add_unique updates |
48ed1c18 | 425 | * will never generate duplicated keys. |
7b783f81 MD |
426 | * |
427 | * This function issues a full memory barrier before and after its | |
428 | * atomic commit. | |
48ed1c18 | 429 | */ |
b9c27904 | 430 | extern |
9357c415 | 431 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
6f554439 | 432 | unsigned long hash, |
0422d92c | 433 | cds_lfht_match_fct match, |
996ff57c | 434 | const void *key, |
adc0de68 | 435 | struct cds_lfht_node *node); |
48ed1c18 | 436 | |
f0c29ed7 | 437 | /* |
929ad508 | 438 | * cds_lfht_replace - replace a node pointed to by iter within hash table. |
0422d92c MD |
439 | * @ht: the hash table. |
440 | * @old_iter: the iterator position of the node to replace. | |
2e79c445 MD |
441 | * @hash: the node's hash. |
442 | * @match: the key match function. | |
443 | * @key: the node's key. | |
444 | * @new_node: the new node to use as replacement. | |
f0c29ed7 | 445 | * |
9357c415 | 446 | * Return 0 if replacement is successful, negative value otherwise. |
2e79c445 MD |
447 | * Replacing a NULL old node or an already removed node will fail with |
448 | * -ENOENT. | |
449 | * If the hash or value of the node to replace and the new node differ, | |
450 | * this function returns -EINVAL without proceeding to the replacement. | |
9357c415 MD |
451 | * Old node can be looked up with cds_lfht_lookup and cds_lfht_next. |
452 | * RCU read-side lock must be held between lookup and replacement. | |
453 | * Call with rcu_read_lock held. | |
3df0c49c | 454 | * Threads calling this API need to be registered RCU read-side threads. |
9357c415 | 455 | * After successful replacement, a grace period must be waited for before |
50024344 MD |
456 | * freeing or re-using the memory reserved for the old node (which can |
457 | * be accessed with cds_lfht_iter_get_node). | |
9357c415 | 458 | * |
3a9c66db MD |
459 | * The semantic of replacement vs lookups is the same as |
460 | * cds_lfht_add_replace(). | |
7b783f81 MD |
461 | * |
462 | * Upon success, this function issues a full memory barrier before and | |
463 | * after its atomic commit. Upon failure, this function does not issue | |
464 | * any memory barrier. | |
9357c415 | 465 | */ |
b9c27904 | 466 | extern |
2e79c445 MD |
467 | int cds_lfht_replace(struct cds_lfht *ht, |
468 | struct cds_lfht_iter *old_iter, | |
469 | unsigned long hash, | |
470 | cds_lfht_match_fct match, | |
471 | const void *key, | |
9357c415 MD |
472 | struct cds_lfht_node *new_node); |
473 | ||
474 | /* | |
475 | * cds_lfht_del - remove node pointed to by iterator from hash table. | |
0422d92c | 476 | * @ht: the hash table. |
bc8c3c74 | 477 | * @node: the node to delete. |
9357c415 MD |
478 | * |
479 | * Return 0 if the node is successfully removed, negative value | |
480 | * otherwise. | |
bc8c3c74 | 481 | * Deleting a NULL node or an already removed node will fail with a |
9357c415 | 482 | * negative value. |
bc8c3c74 MD |
483 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next, |
484 | * followed by use of cds_lfht_iter_get_node. | |
9357c415 | 485 | * RCU read-side lock must be held between lookup and removal. |
f0c29ed7 | 486 | * Call with rcu_read_lock held. |
3df0c49c | 487 | * Threads calling this API need to be registered RCU read-side threads. |
48ed1c18 | 488 | * After successful removal, a grace period must be waited for before |
50024344 MD |
489 | * freeing or re-using the memory reserved for old node (which can be |
490 | * accessed with cds_lfht_iter_get_node). | |
7b783f81 MD |
491 | * Upon success, this function issues a full memory barrier before and |
492 | * after its atomic commit. Upon failure, this function does not issue | |
493 | * any memory barrier. | |
f0c29ed7 | 494 | */ |
b9c27904 | 495 | extern |
bc8c3c74 | 496 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node); |
ab7d5fc6 | 497 | |
df55172a | 498 | /* |
3a9c66db | 499 | * cds_lfht_is_node_deleted - query whether a node is removed from hash table. |
df55172a MD |
500 | * |
501 | * Return non-zero if the node is deleted from the hash table, 0 | |
502 | * otherwise. | |
503 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next, | |
504 | * followed by use of cds_lfht_iter_get_node. | |
505 | * RCU read-side lock must be held between lookup and call to this | |
506 | * function. | |
507 | * Call with rcu_read_lock held. | |
508 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 | 509 | * This function does not issue any memory barrier. |
df55172a | 510 | */ |
b9c27904 | 511 | extern |
afa5940d | 512 | int cds_lfht_is_node_deleted(const struct cds_lfht_node *node); |
df55172a | 513 | |
f0c29ed7 | 514 | /* |
14044b37 | 515 | * cds_lfht_resize - Force a hash table resize |
0422d92c | 516 | * @ht: the hash table. |
1475579c | 517 | * @new_size: update to this hash table size. |
3df0c49c MD |
518 | * |
519 | * Threads calling this API need to be registered RCU read-side threads. | |
7b783f81 | 520 | * This function does not (necessarily) issue memory barriers. |
43387742 MD |
521 | * cds_lfht_resize should *not* be called from a RCU read-side critical |
522 | * section. | |
f0c29ed7 | 523 | */ |
b9c27904 | 524 | extern |
1475579c | 525 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
464a1ec9 | 526 | |
6d320126 | 527 | /* |
3a9c66db MD |
528 | * Note: it is safe to perform element removal (del), replacement, or |
529 | * any hash table update operation during any of the following hash | |
530 | * table traversals. | |
7b783f81 | 531 | * These functions act as rcu_dereference() to read the node pointers. |
6d320126 MD |
532 | */ |
533 | #define cds_lfht_for_each(ht, iter, node) \ | |
534 | for (cds_lfht_first(ht, iter), \ | |
535 | node = cds_lfht_iter_get_node(iter); \ | |
536 | node != NULL; \ | |
537 | cds_lfht_next(ht, iter), \ | |
538 | node = cds_lfht_iter_get_node(iter)) | |
539 | ||
6f554439 LJ |
540 | #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \ |
541 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ | |
6d320126 MD |
542 | node = cds_lfht_iter_get_node(iter); \ |
543 | node != NULL; \ | |
544 | cds_lfht_next_duplicate(ht, match, key, iter), \ | |
545 | node = cds_lfht_iter_get_node(iter)) | |
546 | ||
e58cdfcf MD |
547 | #define cds_lfht_entry(ptr, type, member) \ |
548 | ({ \ | |
549 | caa_unqual_scalar_typeof(ptr) ___ptr = (ptr); \ | |
550 | ___ptr ? caa_container_of(___ptr, type, member) : NULL; \ | |
551 | }) | |
552 | ||
6d320126 MD |
553 | #define cds_lfht_for_each_entry(ht, iter, pos, member) \ |
554 | for (cds_lfht_first(ht, iter), \ | |
e58cdfcf MD |
555 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
556 | __typeof__(*(pos)), member); \ | |
557 | pos != NULL; \ | |
6d320126 | 558 | cds_lfht_next(ht, iter), \ |
e58cdfcf MD |
559 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
560 | __typeof__(*(pos)), member)) | |
6d320126 | 561 | |
6f554439 | 562 | #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \ |
6d320126 | 563 | iter, pos, member) \ |
6f554439 | 564 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ |
e58cdfcf MD |
565 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
566 | __typeof__(*(pos)), member); \ | |
567 | pos != NULL; \ | |
6d320126 | 568 | cds_lfht_next_duplicate(ht, match, key, iter), \ |
e58cdfcf MD |
569 | pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ |
570 | __typeof__(*(pos)), member)) | |
6d320126 | 571 | |
01389791 MD |
572 | #ifdef __cplusplus |
573 | } | |
574 | #endif | |
575 | ||
a42cc659 | 576 | #endif /* _URCU_RCULFHASH_H */ |