Commit | Line | Data |
---|---|---|
5e28c532 | 1 | /* |
abc490a1 MD |
2 | * rculfhash.c |
3 | * | |
1475579c | 4 | * Userspace RCU library - Lock-Free Resizable RCU Hash Table |
abc490a1 MD |
5 | * |
6 | * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
0dcf4847 | 7 | * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com> |
abc490a1 MD |
8 | * |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
5e28c532 MD |
22 | */ |
23 | ||
e753ff5a MD |
24 | /* |
25 | * Based on the following articles: | |
26 | * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free | |
27 | * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405. | |
28 | * - Michael, M. M. High performance dynamic lock-free hash tables | |
29 | * and list-based sets. In Proceedings of the fourteenth annual ACM | |
30 | * symposium on Parallel algorithms and architectures, ACM Press, | |
31 | * (2002), 73-82. | |
32 | * | |
1475579c | 33 | * Some specificities of this Lock-Free Resizable RCU Hash Table |
e753ff5a MD |
34 | * implementation: |
35 | * | |
36 | * - RCU read-side critical section allows readers to perform hash | |
37 | * table lookups and use the returned objects safely by delaying | |
38 | * memory reclaim of a grace period. | |
39 | * - Add and remove operations are lock-free, and do not need to | |
40 | * allocate memory. They need to be executed within RCU read-side | |
41 | * critical section to ensure the objects they read are valid and to | |
42 | * deal with the cmpxchg ABA problem. | |
43 | * - add and add_unique operations are supported. add_unique checks if | |
44 | * the node key already exists in the hash table. It ensures no key | |
45 | * duplicata exists. | |
46 | * - The resize operation executes concurrently with add/remove/lookup. | |
47 | * - Hash table nodes are contained within a split-ordered list. This | |
48 | * list is ordered by incrementing reversed-bits-hash value. | |
1ee8f000 | 49 | * - An index of bucket nodes is kept. These bucket nodes are the hash |
e753ff5a MD |
50 | * table "buckets", and they are also chained together in the |
51 | * split-ordered list, which allows recursive expansion. | |
1475579c MD |
52 | * - The resize operation for small tables only allows expanding the hash table. |
53 | * It is triggered automatically by detecting long chains in the add | |
54 | * operation. | |
55 | * - The resize operation for larger tables (and available through an | |
56 | * API) allows both expanding and shrinking the hash table. | |
4c42f1b8 | 57 | * - Split-counters are used to keep track of the number of |
1475579c | 58 | * nodes within the hash table for automatic resize triggering. |
e753ff5a MD |
59 | * - Resize operation initiated by long chain detection is executed by a |
60 | * call_rcu thread, which keeps lock-freedom of add and remove. | |
61 | * - Resize operations are protected by a mutex. | |
62 | * - The removal operation is split in two parts: first, a "removed" | |
63 | * flag is set in the next pointer within the node to remove. Then, | |
64 | * a "garbage collection" is performed in the bucket containing the | |
65 | * removed node (from the start of the bucket up to the removed node). | |
66 | * All encountered nodes with "removed" flag set in their next | |
67 | * pointers are removed from the linked-list. If the cmpxchg used for | |
68 | * removal fails (due to concurrent garbage-collection or concurrent | |
69 | * add), we retry from the beginning of the bucket. This ensures that | |
70 | * the node with "removed" flag set is removed from the hash table | |
71 | * (not visible to lookups anymore) before the RCU read-side critical | |
72 | * section held across removal ends. Furthermore, this ensures that | |
73 | * the node with "removed" flag set is removed from the linked-list | |
74 | * before its memory is reclaimed. Only the thread which removal | |
75 | * successfully set the "removed" flag (with a cmpxchg) into a node's | |
76 | * next pointer is considered to have succeeded its removal (and thus | |
77 | * owns the node to reclaim). Because we garbage-collect starting from | |
1ee8f000 | 78 | * an invariant node (the start-of-bucket bucket node) up to the |
e753ff5a MD |
79 | * "removed" node (or find a reverse-hash that is higher), we are sure |
80 | * that a successful traversal of the chain leads to a chain that is | |
81 | * present in the linked-list (the start node is never removed) and | |
82 | * that is does not contain the "removed" node anymore, even if | |
83 | * concurrent delete/add operations are changing the structure of the | |
84 | * list concurrently. | |
29e669f6 MD |
85 | * - The add operation performs gargage collection of buckets if it |
86 | * encounters nodes with removed flag set in the bucket where it wants | |
87 | * to add its new node. This ensures lock-freedom of add operation by | |
88 | * helping the remover unlink nodes from the list rather than to wait | |
89 | * for it do to so. | |
e753ff5a MD |
90 | * - A RCU "order table" indexed by log2(hash index) is copied and |
91 | * expanded by the resize operation. This order table allows finding | |
1ee8f000 LJ |
92 | * the "bucket node" tables. |
93 | * - There is one bucket node table per hash index order. The size of | |
94 | * each bucket node table is half the number of hashes contained in | |
93d46c39 | 95 | * this order (except for order 0). |
1ee8f000 LJ |
96 | * - synchronzie_rcu is used to garbage-collect the old bucket node table. |
97 | * - The per-order bucket node tables contain a compact version of the | |
e753ff5a MD |
98 | * hash table nodes. These tables are invariant after they are |
99 | * populated into the hash table. | |
93d46c39 | 100 | * |
1ee8f000 | 101 | * Bucket node tables: |
93d46c39 | 102 | * |
1ee8f000 LJ |
103 | * hash table hash table the last all bucket node tables |
104 | * order size bucket node 0 1 2 3 4 5 6(index) | |
93d46c39 LJ |
105 | * table size |
106 | * 0 1 1 1 | |
107 | * 1 2 1 1 1 | |
108 | * 2 4 2 1 1 2 | |
109 | * 3 8 4 1 1 2 4 | |
110 | * 4 16 8 1 1 2 4 8 | |
111 | * 5 32 16 1 1 2 4 8 16 | |
112 | * 6 64 32 1 1 2 4 8 16 32 | |
113 | * | |
1ee8f000 | 114 | * When growing/shrinking, we only focus on the last bucket node table |
93d46c39 LJ |
115 | * which size is (!order ? 1 : (1 << (order -1))). |
116 | * | |
117 | * Example for growing/shrinking: | |
1ee8f000 LJ |
118 | * grow hash table from order 5 to 6: init the index=6 bucket node table |
119 | * shrink hash table from order 6 to 5: fini the index=6 bucket node table | |
93d46c39 | 120 | * |
1475579c MD |
121 | * A bit of ascii art explanation: |
122 | * | |
123 | * Order index is the off-by-one compare to the actual power of 2 because | |
124 | * we use index 0 to deal with the 0 special-case. | |
125 | * | |
126 | * This shows the nodes for a small table ordered by reversed bits: | |
127 | * | |
128 | * bits reverse | |
129 | * 0 000 000 | |
130 | * 4 100 001 | |
131 | * 2 010 010 | |
132 | * 6 110 011 | |
133 | * 1 001 100 | |
134 | * 5 101 101 | |
135 | * 3 011 110 | |
136 | * 7 111 111 | |
137 | * | |
138 | * This shows the nodes in order of non-reversed bits, linked by | |
139 | * reversed-bit order. | |
140 | * | |
141 | * order bits reverse | |
142 | * 0 0 000 000 | |
0adc36a8 LJ |
143 | * 1 | 1 001 100 <- |
144 | * 2 | | 2 010 010 <- | | |
f6fdd688 | 145 | * | | | 3 011 110 | <- | |
1475579c MD |
146 | * 3 -> | | | 4 100 001 | | |
147 | * -> | | 5 101 101 | | |
148 | * -> | 6 110 011 | |
149 | * -> 7 111 111 | |
e753ff5a MD |
150 | */ |
151 | ||
2ed95849 MD |
152 | #define _LGPL_SOURCE |
153 | #include <stdlib.h> | |
e0ba718a MD |
154 | #include <errno.h> |
155 | #include <assert.h> | |
156 | #include <stdio.h> | |
abc490a1 | 157 | #include <stdint.h> |
f000907d | 158 | #include <string.h> |
e0ba718a | 159 | |
15cfbec7 | 160 | #include "config.h" |
2ed95849 | 161 | #include <urcu.h> |
abc490a1 | 162 | #include <urcu-call-rcu.h> |
7b17c13e | 163 | #include <urcu-flavor.h> |
a42cc659 MD |
164 | #include <urcu/arch.h> |
165 | #include <urcu/uatomic.h> | |
a42cc659 | 166 | #include <urcu/compiler.h> |
abc490a1 | 167 | #include <urcu/rculfhash.h> |
5e28c532 | 168 | #include <stdio.h> |
464a1ec9 | 169 | #include <pthread.h> |
44395fb7 | 170 | |
f9830efd | 171 | #ifdef DEBUG |
f0c29ed7 | 172 | #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args) |
f9830efd | 173 | #else |
e753ff5a | 174 | #define dbg_printf(fmt, args...) |
f9830efd MD |
175 | #endif |
176 | ||
f8994aee | 177 | /* |
4c42f1b8 | 178 | * Split-counters lazily update the global counter each 1024 |
f8994aee MD |
179 | * addition/removal. It automatically keeps track of resize required. |
180 | * We use the bucket length as indicator for need to expand for small | |
181 | * tables and machines lacking per-cpu data suppport. | |
182 | */ | |
183 | #define COUNT_COMMIT_ORDER 10 | |
4ddbb355 | 184 | #define DEFAULT_SPLIT_COUNT_MASK 0xFUL |
6ea6bc67 MD |
185 | #define CHAIN_LEN_TARGET 1 |
186 | #define CHAIN_LEN_RESIZE_THRESHOLD 3 | |
2ed95849 | 187 | |
cd95516d | 188 | /* |
76a73da8 | 189 | * Define the minimum table size. |
cd95516d | 190 | */ |
d0d8f9aa LJ |
191 | #define MIN_TABLE_ORDER 0 |
192 | #define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER) | |
cd95516d | 193 | |
4105056a MD |
194 | #if (CAA_BITS_PER_LONG == 32) |
195 | #define MAX_TABLE_ORDER 32 | |
196 | #else | |
197 | #define MAX_TABLE_ORDER 64 | |
198 | #endif | |
199 | ||
b7d619b0 | 200 | /* |
1ee8f000 | 201 | * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink. |
b7d619b0 | 202 | */ |
6083a889 MD |
203 | #define MIN_PARTITION_PER_THREAD_ORDER 12 |
204 | #define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER) | |
b7d619b0 | 205 | |
4105056a MD |
206 | #ifndef min |
207 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
208 | #endif | |
209 | ||
abc490a1 MD |
210 | #ifndef max |
211 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
212 | #endif | |
2ed95849 | 213 | |
d95bd160 MD |
214 | /* |
215 | * The removed flag needs to be updated atomically with the pointer. | |
48ed1c18 | 216 | * It indicates that no node must attach to the node scheduled for |
b198f0fd | 217 | * removal, and that node garbage collection must be performed. |
1ee8f000 | 218 | * The bucket flag does not require to be updated atomically with the |
d95bd160 MD |
219 | * pointer, but it is added as a pointer low bit flag to save space. |
220 | */ | |
d37166c6 | 221 | #define REMOVED_FLAG (1UL << 0) |
1ee8f000 | 222 | #define BUCKET_FLAG (1UL << 1) |
b198f0fd | 223 | #define FLAGS_MASK ((1UL << 2) - 1) |
d37166c6 | 224 | |
bb7b2f26 | 225 | /* Value of the end pointer. Should not interact with flags. */ |
f9c80341 | 226 | #define END_VALUE NULL |
bb7b2f26 | 227 | |
7f52427b MD |
228 | /* |
229 | * ht_items_count: Split-counters counting the number of node addition | |
230 | * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag | |
231 | * is set at hash table creation. | |
232 | * | |
233 | * These are free-running counters, never reset to zero. They count the | |
234 | * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER) | |
235 | * operations to update the global counter. We choose a power-of-2 value | |
236 | * for the trigger to deal with 32 or 64-bit overflow of the counter. | |
237 | */ | |
df44348d | 238 | struct ht_items_count { |
860d07e8 | 239 | unsigned long add, del; |
df44348d MD |
240 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
241 | ||
7f52427b MD |
242 | /* |
243 | * rcu_table: Contains the size and desired new size if a resize | |
244 | * operation is in progress, as well as the statically-sized array of | |
dcc26ef4 | 245 | * bucket table pointers. |
7f52427b | 246 | */ |
395270b6 | 247 | struct rcu_table { |
4105056a | 248 | unsigned long size; /* always a power of 2, shared (RCU) */ |
f9830efd | 249 | unsigned long resize_target; |
11519af6 | 250 | int resize_initiated; |
dcc26ef4 LJ |
251 | |
252 | /* | |
253 | * Contains the per order-index-level bucket node table. The size | |
254 | * of each bucket node table is half the number of hashes contained | |
255 | * in this order (except for order 0). The minimum allocation size | |
256 | * parameter allows combining the bucket node arrays of the lowermost | |
257 | * levels to improve cache locality for small index orders. | |
258 | */ | |
259 | struct cds_lfht_node *tbl[MAX_TABLE_ORDER]; | |
395270b6 MD |
260 | }; |
261 | ||
7f52427b MD |
262 | /* |
263 | * cds_lfht: Top-level data structure representing a lock-free hash | |
264 | * table. Defined in the implementation file to make it be an opaque | |
265 | * cookie to users. | |
266 | */ | |
14044b37 | 267 | struct cds_lfht { |
4105056a | 268 | struct rcu_table t; |
0722081a LJ |
269 | unsigned long min_alloc_buckets_order; |
270 | unsigned long min_nr_alloc_buckets; | |
747d725c | 271 | unsigned long max_nr_buckets; |
b8af5011 | 272 | int flags; |
5f511391 MD |
273 | /* |
274 | * We need to put the work threads offline (QSBR) when taking this | |
275 | * mutex, because we use synchronize_rcu within this mutex critical | |
276 | * section, which waits on read-side critical sections, and could | |
277 | * therefore cause grace-period deadlock if we hold off RCU G.P. | |
278 | * completion. | |
279 | */ | |
464a1ec9 | 280 | pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */ |
33c7c748 | 281 | unsigned int in_progress_resize, in_progress_destroy; |
7b17c13e | 282 | const struct rcu_flavor_struct *flavor; |
b7d619b0 | 283 | pthread_attr_t *resize_attr; /* Resize threads attributes */ |
7de5ccfd | 284 | long count; /* global approximate item count */ |
4c42f1b8 | 285 | struct ht_items_count *split_count; /* split item count */ |
2ed95849 MD |
286 | }; |
287 | ||
7f52427b MD |
288 | /* |
289 | * rcu_resize_work: Contains arguments passed to RCU worker thread | |
290 | * responsible for performing lazy resize. | |
291 | */ | |
abc490a1 MD |
292 | struct rcu_resize_work { |
293 | struct rcu_head head; | |
14044b37 | 294 | struct cds_lfht *ht; |
abc490a1 | 295 | }; |
2ed95849 | 296 | |
7f52427b MD |
297 | /* |
298 | * partition_resize_work: Contains arguments passed to worker threads | |
299 | * executing the hash table resize on partitions of the hash table | |
300 | * assigned to each processor's worker thread. | |
301 | */ | |
b7d619b0 | 302 | struct partition_resize_work { |
1af6e26e | 303 | pthread_t thread_id; |
b7d619b0 MD |
304 | struct cds_lfht *ht; |
305 | unsigned long i, start, len; | |
306 | void (*fct)(struct cds_lfht *ht, unsigned long i, | |
307 | unsigned long start, unsigned long len); | |
308 | }; | |
309 | ||
76a73da8 | 310 | static |
83beee94 | 311 | void _cds_lfht_add(struct cds_lfht *ht, |
0422d92c | 312 | cds_lfht_match_fct match, |
996ff57c | 313 | const void *key, |
83beee94 MD |
314 | unsigned long size, |
315 | struct cds_lfht_node *node, | |
316 | struct cds_lfht_iter *unique_ret, | |
1ee8f000 | 317 | int bucket); |
48ed1c18 | 318 | |
abc490a1 MD |
319 | /* |
320 | * Algorithm to reverse bits in a word by lookup table, extended to | |
321 | * 64-bit words. | |
f9830efd | 322 | * Source: |
abc490a1 | 323 | * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable |
f9830efd | 324 | * Originally from Public Domain. |
abc490a1 MD |
325 | */ |
326 | ||
327 | static const uint8_t BitReverseTable256[256] = | |
2ed95849 | 328 | { |
abc490a1 MD |
329 | #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64 |
330 | #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16) | |
331 | #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 ) | |
332 | R6(0), R6(2), R6(1), R6(3) | |
333 | }; | |
334 | #undef R2 | |
335 | #undef R4 | |
336 | #undef R6 | |
2ed95849 | 337 | |
abc490a1 MD |
338 | static |
339 | uint8_t bit_reverse_u8(uint8_t v) | |
340 | { | |
341 | return BitReverseTable256[v]; | |
342 | } | |
ab7d5fc6 | 343 | |
abc490a1 MD |
344 | static __attribute__((unused)) |
345 | uint32_t bit_reverse_u32(uint32_t v) | |
346 | { | |
347 | return ((uint32_t) bit_reverse_u8(v) << 24) | | |
348 | ((uint32_t) bit_reverse_u8(v >> 8) << 16) | | |
349 | ((uint32_t) bit_reverse_u8(v >> 16) << 8) | | |
350 | ((uint32_t) bit_reverse_u8(v >> 24)); | |
2ed95849 MD |
351 | } |
352 | ||
abc490a1 MD |
353 | static __attribute__((unused)) |
354 | uint64_t bit_reverse_u64(uint64_t v) | |
2ed95849 | 355 | { |
abc490a1 MD |
356 | return ((uint64_t) bit_reverse_u8(v) << 56) | |
357 | ((uint64_t) bit_reverse_u8(v >> 8) << 48) | | |
358 | ((uint64_t) bit_reverse_u8(v >> 16) << 40) | | |
359 | ((uint64_t) bit_reverse_u8(v >> 24) << 32) | | |
360 | ((uint64_t) bit_reverse_u8(v >> 32) << 24) | | |
361 | ((uint64_t) bit_reverse_u8(v >> 40) << 16) | | |
362 | ((uint64_t) bit_reverse_u8(v >> 48) << 8) | | |
363 | ((uint64_t) bit_reverse_u8(v >> 56)); | |
364 | } | |
365 | ||
366 | static | |
367 | unsigned long bit_reverse_ulong(unsigned long v) | |
368 | { | |
369 | #if (CAA_BITS_PER_LONG == 32) | |
370 | return bit_reverse_u32(v); | |
371 | #else | |
372 | return bit_reverse_u64(v); | |
373 | #endif | |
374 | } | |
375 | ||
f9830efd | 376 | /* |
24365af7 MD |
377 | * fls: returns the position of the most significant bit. |
378 | * Returns 0 if no bit is set, else returns the position of the most | |
379 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). | |
f9830efd | 380 | */ |
24365af7 MD |
381 | #if defined(__i386) || defined(__x86_64) |
382 | static inline | |
383 | unsigned int fls_u32(uint32_t x) | |
f9830efd | 384 | { |
24365af7 MD |
385 | int r; |
386 | ||
387 | asm("bsrl %1,%0\n\t" | |
388 | "jnz 1f\n\t" | |
389 | "movl $-1,%0\n\t" | |
390 | "1:\n\t" | |
391 | : "=r" (r) : "rm" (x)); | |
392 | return r + 1; | |
393 | } | |
394 | #define HAS_FLS_U32 | |
395 | #endif | |
396 | ||
397 | #if defined(__x86_64) | |
398 | static inline | |
399 | unsigned int fls_u64(uint64_t x) | |
400 | { | |
401 | long r; | |
402 | ||
403 | asm("bsrq %1,%0\n\t" | |
404 | "jnz 1f\n\t" | |
405 | "movq $-1,%0\n\t" | |
406 | "1:\n\t" | |
407 | : "=r" (r) : "rm" (x)); | |
408 | return r + 1; | |
409 | } | |
410 | #define HAS_FLS_U64 | |
411 | #endif | |
412 | ||
413 | #ifndef HAS_FLS_U64 | |
414 | static __attribute__((unused)) | |
415 | unsigned int fls_u64(uint64_t x) | |
416 | { | |
417 | unsigned int r = 64; | |
418 | ||
419 | if (!x) | |
420 | return 0; | |
421 | ||
422 | if (!(x & 0xFFFFFFFF00000000ULL)) { | |
423 | x <<= 32; | |
424 | r -= 32; | |
425 | } | |
426 | if (!(x & 0xFFFF000000000000ULL)) { | |
427 | x <<= 16; | |
428 | r -= 16; | |
429 | } | |
430 | if (!(x & 0xFF00000000000000ULL)) { | |
431 | x <<= 8; | |
432 | r -= 8; | |
433 | } | |
434 | if (!(x & 0xF000000000000000ULL)) { | |
435 | x <<= 4; | |
436 | r -= 4; | |
437 | } | |
438 | if (!(x & 0xC000000000000000ULL)) { | |
439 | x <<= 2; | |
440 | r -= 2; | |
441 | } | |
442 | if (!(x & 0x8000000000000000ULL)) { | |
443 | x <<= 1; | |
444 | r -= 1; | |
445 | } | |
446 | return r; | |
447 | } | |
448 | #endif | |
449 | ||
450 | #ifndef HAS_FLS_U32 | |
451 | static __attribute__((unused)) | |
452 | unsigned int fls_u32(uint32_t x) | |
453 | { | |
454 | unsigned int r = 32; | |
f9830efd | 455 | |
24365af7 MD |
456 | if (!x) |
457 | return 0; | |
458 | if (!(x & 0xFFFF0000U)) { | |
459 | x <<= 16; | |
460 | r -= 16; | |
461 | } | |
462 | if (!(x & 0xFF000000U)) { | |
463 | x <<= 8; | |
464 | r -= 8; | |
465 | } | |
466 | if (!(x & 0xF0000000U)) { | |
467 | x <<= 4; | |
468 | r -= 4; | |
469 | } | |
470 | if (!(x & 0xC0000000U)) { | |
471 | x <<= 2; | |
472 | r -= 2; | |
473 | } | |
474 | if (!(x & 0x80000000U)) { | |
475 | x <<= 1; | |
476 | r -= 1; | |
477 | } | |
478 | return r; | |
479 | } | |
480 | #endif | |
481 | ||
482 | unsigned int fls_ulong(unsigned long x) | |
f9830efd | 483 | { |
6887cc5e | 484 | #if (CAA_BITS_PER_LONG == 32) |
24365af7 MD |
485 | return fls_u32(x); |
486 | #else | |
487 | return fls_u64(x); | |
488 | #endif | |
489 | } | |
f9830efd | 490 | |
920f8ef6 LJ |
491 | /* |
492 | * Return the minimum order for which x <= (1UL << order). | |
493 | * Return -1 if x is 0. | |
494 | */ | |
24365af7 MD |
495 | int get_count_order_u32(uint32_t x) |
496 | { | |
920f8ef6 LJ |
497 | if (!x) |
498 | return -1; | |
24365af7 | 499 | |
920f8ef6 | 500 | return fls_u32(x - 1); |
24365af7 MD |
501 | } |
502 | ||
920f8ef6 LJ |
503 | /* |
504 | * Return the minimum order for which x <= (1UL << order). | |
505 | * Return -1 if x is 0. | |
506 | */ | |
24365af7 MD |
507 | int get_count_order_ulong(unsigned long x) |
508 | { | |
920f8ef6 LJ |
509 | if (!x) |
510 | return -1; | |
24365af7 | 511 | |
920f8ef6 | 512 | return fls_ulong(x - 1); |
f9830efd MD |
513 | } |
514 | ||
98808fb1 | 515 | #ifdef POISON_FREE |
5afadd12 LJ |
516 | #define poison_free(ptr) \ |
517 | do { \ | |
518 | if (ptr) { \ | |
519 | memset(ptr, 0x42, sizeof(*(ptr))); \ | |
520 | free(ptr); \ | |
521 | } \ | |
98808fb1 MD |
522 | } while (0) |
523 | #else | |
524 | #define poison_free(ptr) free(ptr) | |
525 | #endif | |
526 | ||
f9830efd | 527 | static |
ab65b890 | 528 | void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth); |
f9830efd | 529 | |
f8994aee | 530 | static |
4105056a | 531 | void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size, |
f8994aee MD |
532 | unsigned long count); |
533 | ||
df44348d | 534 | static long nr_cpus_mask = -1; |
4c42f1b8 LJ |
535 | static long split_count_mask = -1; |
536 | ||
4ddbb355 | 537 | #if defined(HAVE_SYSCONF) |
4c42f1b8 LJ |
538 | static void ht_init_nr_cpus_mask(void) |
539 | { | |
540 | long maxcpus; | |
541 | ||
542 | maxcpus = sysconf(_SC_NPROCESSORS_CONF); | |
543 | if (maxcpus <= 0) { | |
544 | nr_cpus_mask = -2; | |
545 | return; | |
546 | } | |
547 | /* | |
548 | * round up number of CPUs to next power of two, so we | |
549 | * can use & for modulo. | |
550 | */ | |
551 | maxcpus = 1UL << get_count_order_ulong(maxcpus); | |
552 | nr_cpus_mask = maxcpus - 1; | |
553 | } | |
4ddbb355 LJ |
554 | #else /* #if defined(HAVE_SYSCONF) */ |
555 | static void ht_init_nr_cpus_mask(void) | |
556 | { | |
557 | nr_cpus_mask = -2; | |
558 | } | |
559 | #endif /* #else #if defined(HAVE_SYSCONF) */ | |
df44348d MD |
560 | |
561 | static | |
5afadd12 | 562 | void alloc_split_items_count(struct cds_lfht *ht) |
df44348d MD |
563 | { |
564 | struct ht_items_count *count; | |
565 | ||
4c42f1b8 LJ |
566 | if (nr_cpus_mask == -1) { |
567 | ht_init_nr_cpus_mask(); | |
4ddbb355 LJ |
568 | if (nr_cpus_mask < 0) |
569 | split_count_mask = DEFAULT_SPLIT_COUNT_MASK; | |
570 | else | |
571 | split_count_mask = nr_cpus_mask; | |
df44348d | 572 | } |
4c42f1b8 | 573 | |
4ddbb355 | 574 | assert(split_count_mask >= 0); |
5afadd12 LJ |
575 | |
576 | if (ht->flags & CDS_LFHT_ACCOUNTING) { | |
577 | ht->split_count = calloc(split_count_mask + 1, sizeof(*count)); | |
578 | assert(ht->split_count); | |
579 | } else { | |
580 | ht->split_count = NULL; | |
581 | } | |
df44348d MD |
582 | } |
583 | ||
584 | static | |
5afadd12 | 585 | void free_split_items_count(struct cds_lfht *ht) |
df44348d | 586 | { |
5afadd12 | 587 | poison_free(ht->split_count); |
df44348d MD |
588 | } |
589 | ||
14360f1c | 590 | #if defined(HAVE_SCHED_GETCPU) |
df44348d | 591 | static |
14360f1c | 592 | int ht_get_split_count_index(unsigned long hash) |
df44348d MD |
593 | { |
594 | int cpu; | |
595 | ||
4c42f1b8 | 596 | assert(split_count_mask >= 0); |
df44348d | 597 | cpu = sched_getcpu(); |
8ed51e04 | 598 | if (caa_unlikely(cpu < 0)) |
14360f1c | 599 | return hash & split_count_mask; |
df44348d | 600 | else |
4c42f1b8 | 601 | return cpu & split_count_mask; |
df44348d | 602 | } |
14360f1c LJ |
603 | #else /* #if defined(HAVE_SCHED_GETCPU) */ |
604 | static | |
605 | int ht_get_split_count_index(unsigned long hash) | |
606 | { | |
607 | return hash & split_count_mask; | |
608 | } | |
609 | #endif /* #else #if defined(HAVE_SCHED_GETCPU) */ | |
df44348d MD |
610 | |
611 | static | |
14360f1c | 612 | void ht_count_add(struct cds_lfht *ht, unsigned long size, unsigned long hash) |
df44348d | 613 | { |
4c42f1b8 LJ |
614 | unsigned long split_count; |
615 | int index; | |
df44348d | 616 | |
8ed51e04 | 617 | if (caa_unlikely(!ht->split_count)) |
3171717f | 618 | return; |
14360f1c | 619 | index = ht_get_split_count_index(hash); |
4c42f1b8 | 620 | split_count = uatomic_add_return(&ht->split_count[index].add, 1); |
8ed51e04 | 621 | if (caa_unlikely(!(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) { |
e3ecefd6 | 622 | long count; |
df44348d | 623 | |
4c42f1b8 | 624 | dbg_printf("add split count %lu\n", split_count); |
df44348d MD |
625 | count = uatomic_add_return(&ht->count, |
626 | 1UL << COUNT_COMMIT_ORDER); | |
627 | /* If power of 2 */ | |
628 | if (!(count & (count - 1))) { | |
4105056a | 629 | if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) < size) |
f8994aee | 630 | return; |
e3ecefd6 | 631 | dbg_printf("add set global %ld\n", count); |
4105056a | 632 | cds_lfht_resize_lazy_count(ht, size, |
6ea6bc67 | 633 | count >> (CHAIN_LEN_TARGET - 1)); |
df44348d MD |
634 | } |
635 | } | |
636 | } | |
637 | ||
638 | static | |
14360f1c | 639 | void ht_count_del(struct cds_lfht *ht, unsigned long size, unsigned long hash) |
df44348d | 640 | { |
4c42f1b8 LJ |
641 | unsigned long split_count; |
642 | int index; | |
df44348d | 643 | |
8ed51e04 | 644 | if (caa_unlikely(!ht->split_count)) |
3171717f | 645 | return; |
14360f1c | 646 | index = ht_get_split_count_index(hash); |
4c42f1b8 | 647 | split_count = uatomic_add_return(&ht->split_count[index].del, 1); |
8ed51e04 | 648 | if (caa_unlikely(!(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) { |
e3ecefd6 | 649 | long count; |
df44348d | 650 | |
4c42f1b8 | 651 | dbg_printf("del split count %lu\n", split_count); |
df44348d | 652 | count = uatomic_add_return(&ht->count, |
3171717f | 653 | -(1UL << COUNT_COMMIT_ORDER)); |
df44348d MD |
654 | /* If power of 2 */ |
655 | if (!(count & (count - 1))) { | |
4105056a | 656 | if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) >= size) |
f8994aee | 657 | return; |
e3ecefd6 MD |
658 | dbg_printf("del set global %ld\n", count); |
659 | /* | |
c941bb9e | 660 | * Don't shrink table if the number of nodes is below a |
e3ecefd6 MD |
661 | * certain threshold. |
662 | */ | |
4c42f1b8 | 663 | if (count < (1UL << COUNT_COMMIT_ORDER) * (split_count_mask + 1)) |
e3ecefd6 | 664 | return; |
4105056a | 665 | cds_lfht_resize_lazy_count(ht, size, |
6ea6bc67 | 666 | count >> (CHAIN_LEN_TARGET - 1)); |
df44348d MD |
667 | } |
668 | } | |
669 | } | |
670 | ||
f9830efd | 671 | static |
4105056a | 672 | void check_resize(struct cds_lfht *ht, unsigned long size, uint32_t chain_len) |
f9830efd | 673 | { |
f8994aee MD |
674 | unsigned long count; |
675 | ||
b8af5011 MD |
676 | if (!(ht->flags & CDS_LFHT_AUTO_RESIZE)) |
677 | return; | |
f8994aee MD |
678 | count = uatomic_read(&ht->count); |
679 | /* | |
680 | * Use bucket-local length for small table expand and for | |
681 | * environments lacking per-cpu data support. | |
682 | */ | |
683 | if (count >= (1UL << COUNT_COMMIT_ORDER)) | |
684 | return; | |
24365af7 | 685 | if (chain_len > 100) |
f0c29ed7 | 686 | dbg_printf("WARNING: large chain length: %u.\n", |
24365af7 | 687 | chain_len); |
3390d470 | 688 | if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD) |
ab65b890 | 689 | cds_lfht_resize_lazy_grow(ht, size, |
01370f0b | 690 | get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1))); |
f9830efd MD |
691 | } |
692 | ||
abc490a1 | 693 | static |
14044b37 | 694 | struct cds_lfht_node *clear_flag(struct cds_lfht_node *node) |
abc490a1 | 695 | { |
14044b37 | 696 | return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK); |
abc490a1 MD |
697 | } |
698 | ||
699 | static | |
14044b37 | 700 | int is_removed(struct cds_lfht_node *node) |
abc490a1 | 701 | { |
d37166c6 | 702 | return ((unsigned long) node) & REMOVED_FLAG; |
abc490a1 MD |
703 | } |
704 | ||
705 | static | |
14044b37 | 706 | struct cds_lfht_node *flag_removed(struct cds_lfht_node *node) |
abc490a1 | 707 | { |
14044b37 | 708 | return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG); |
abc490a1 MD |
709 | } |
710 | ||
f5596c94 | 711 | static |
1ee8f000 | 712 | int is_bucket(struct cds_lfht_node *node) |
f5596c94 | 713 | { |
1ee8f000 | 714 | return ((unsigned long) node) & BUCKET_FLAG; |
f5596c94 MD |
715 | } |
716 | ||
717 | static | |
1ee8f000 | 718 | struct cds_lfht_node *flag_bucket(struct cds_lfht_node *node) |
f5596c94 | 719 | { |
1ee8f000 | 720 | return (struct cds_lfht_node *) (((unsigned long) node) | BUCKET_FLAG); |
f5596c94 | 721 | } |
bb7b2f26 MD |
722 | |
723 | static | |
724 | struct cds_lfht_node *get_end(void) | |
725 | { | |
726 | return (struct cds_lfht_node *) END_VALUE; | |
727 | } | |
728 | ||
729 | static | |
730 | int is_end(struct cds_lfht_node *node) | |
731 | { | |
732 | return clear_flag(node) == (struct cds_lfht_node *) END_VALUE; | |
733 | } | |
734 | ||
abc490a1 | 735 | static |
ab65b890 LJ |
736 | unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr, |
737 | unsigned long v) | |
abc490a1 MD |
738 | { |
739 | unsigned long old1, old2; | |
740 | ||
741 | old1 = uatomic_read(ptr); | |
742 | do { | |
743 | old2 = old1; | |
744 | if (old2 >= v) | |
f9830efd | 745 | return old2; |
abc490a1 | 746 | } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2); |
ab65b890 | 747 | return old2; |
abc490a1 MD |
748 | } |
749 | ||
48f1b16d LJ |
750 | static |
751 | void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order) | |
752 | { | |
753 | if (order == 0) { | |
0722081a | 754 | ht->t.tbl[0] = calloc(ht->min_nr_alloc_buckets, |
48f1b16d LJ |
755 | sizeof(struct cds_lfht_node)); |
756 | assert(ht->t.tbl[0]); | |
0722081a | 757 | } else if (order > ht->min_alloc_buckets_order) { |
48f1b16d LJ |
758 | ht->t.tbl[order] = calloc(1UL << (order -1), |
759 | sizeof(struct cds_lfht_node)); | |
760 | assert(ht->t.tbl[order]); | |
761 | } | |
0722081a | 762 | /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */ |
48f1b16d LJ |
763 | } |
764 | ||
765 | /* | |
766 | * cds_lfht_free_bucket_table() should be called with decreasing order. | |
767 | * When cds_lfht_free_bucket_table(0) is called, it means the whole | |
768 | * lfht is destroyed. | |
769 | */ | |
770 | static | |
771 | void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order) | |
772 | { | |
773 | if (order == 0) | |
774 | poison_free(ht->t.tbl[0]); | |
0722081a | 775 | else if (order > ht->min_alloc_buckets_order) |
48f1b16d | 776 | poison_free(ht->t.tbl[order]); |
0722081a | 777 | /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */ |
48f1b16d LJ |
778 | } |
779 | ||
9d72a73f LJ |
780 | static inline |
781 | struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index) | |
f4a9cc0b | 782 | { |
9d72a73f | 783 | unsigned long order; |
ef6e6171 | 784 | |
9d72a73f | 785 | if ((__builtin_constant_p(index) && index == 0) |
0722081a | 786 | || index < ht->min_nr_alloc_buckets) { |
9d72a73f | 787 | dbg_printf("bucket index %lu order 0 aridx 0\n", index); |
dcc26ef4 | 788 | return &ht->t.tbl[0][index]; |
ef6e6171 | 789 | } |
a4ea2223 LJ |
790 | /* |
791 | * equivalent to get_count_order_ulong(index + 1), but optimizes | |
792 | * away the non-existing 0 special-case for | |
793 | * get_count_order_ulong. | |
794 | */ | |
795 | order = fls_ulong(index); | |
9d72a73f LJ |
796 | dbg_printf("bucket index %lu order %lu aridx %lu\n", |
797 | index, order, index & ((1UL << (order - 1)) - 1)); | |
dcc26ef4 | 798 | return &ht->t.tbl[order][index & ((1UL << (order - 1)) - 1)]; |
f4a9cc0b LJ |
799 | } |
800 | ||
9d72a73f LJ |
801 | static inline |
802 | struct cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size, | |
803 | unsigned long hash) | |
804 | { | |
805 | assert(size > 0); | |
806 | return bucket_at(ht, hash & (size - 1)); | |
807 | } | |
808 | ||
273399de MD |
809 | /* |
810 | * Remove all logically deleted nodes from a bucket up to a certain node key. | |
811 | */ | |
812 | static | |
1ee8f000 | 813 | void _cds_lfht_gc_bucket(struct cds_lfht_node *bucket, struct cds_lfht_node *node) |
273399de | 814 | { |
14044b37 | 815 | struct cds_lfht_node *iter_prev, *iter, *next, *new_next; |
273399de | 816 | |
1ee8f000 LJ |
817 | assert(!is_bucket(bucket)); |
818 | assert(!is_removed(bucket)); | |
819 | assert(!is_bucket(node)); | |
c90201ac | 820 | assert(!is_removed(node)); |
273399de | 821 | for (;;) { |
1ee8f000 LJ |
822 | iter_prev = bucket; |
823 | /* We can always skip the bucket node initially */ | |
04db56f8 | 824 | iter = rcu_dereference(iter_prev->next); |
b4cb483f | 825 | assert(!is_removed(iter)); |
04db56f8 | 826 | assert(iter_prev->reverse_hash <= node->reverse_hash); |
bd4db153 | 827 | /* |
1ee8f000 | 828 | * We should never be called with bucket (start of chain) |
bd4db153 MD |
829 | * and logically removed node (end of path compression |
830 | * marker) being the actual same node. This would be a | |
831 | * bug in the algorithm implementation. | |
832 | */ | |
1ee8f000 | 833 | assert(bucket != node); |
273399de | 834 | for (;;) { |
8ed51e04 | 835 | if (caa_unlikely(is_end(iter))) |
f9c80341 | 836 | return; |
04db56f8 | 837 | if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash)) |
f9c80341 | 838 | return; |
04db56f8 | 839 | next = rcu_dereference(clear_flag(iter)->next); |
8ed51e04 | 840 | if (caa_likely(is_removed(next))) |
273399de | 841 | break; |
b453eae1 | 842 | iter_prev = clear_flag(iter); |
273399de MD |
843 | iter = next; |
844 | } | |
b198f0fd | 845 | assert(!is_removed(iter)); |
1ee8f000 LJ |
846 | if (is_bucket(iter)) |
847 | new_next = flag_bucket(clear_flag(next)); | |
f5596c94 MD |
848 | else |
849 | new_next = clear_flag(next); | |
04db56f8 | 850 | (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next); |
273399de | 851 | } |
f9c80341 | 852 | return; |
273399de MD |
853 | } |
854 | ||
9357c415 MD |
855 | static |
856 | int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size, | |
857 | struct cds_lfht_node *old_node, | |
3fb86f26 | 858 | struct cds_lfht_node *old_next, |
9357c415 MD |
859 | struct cds_lfht_node *new_node) |
860 | { | |
04db56f8 | 861 | struct cds_lfht_node *bucket, *ret_next; |
9357c415 MD |
862 | |
863 | if (!old_node) /* Return -ENOENT if asked to replace NULL node */ | |
7801dadd | 864 | return -ENOENT; |
9357c415 MD |
865 | |
866 | assert(!is_removed(old_node)); | |
1ee8f000 | 867 | assert(!is_bucket(old_node)); |
9357c415 | 868 | assert(!is_removed(new_node)); |
1ee8f000 | 869 | assert(!is_bucket(new_node)); |
9357c415 | 870 | assert(new_node != old_node); |
3fb86f26 | 871 | for (;;) { |
9357c415 | 872 | /* Insert after node to be replaced */ |
9357c415 MD |
873 | if (is_removed(old_next)) { |
874 | /* | |
875 | * Too late, the old node has been removed under us | |
876 | * between lookup and replace. Fail. | |
877 | */ | |
7801dadd | 878 | return -ENOENT; |
9357c415 | 879 | } |
1ee8f000 | 880 | assert(!is_bucket(old_next)); |
9357c415 | 881 | assert(new_node != clear_flag(old_next)); |
04db56f8 | 882 | new_node->next = clear_flag(old_next); |
9357c415 MD |
883 | /* |
884 | * Here is the whole trick for lock-free replace: we add | |
885 | * the replacement node _after_ the node we want to | |
886 | * replace by atomically setting its next pointer at the | |
887 | * same time we set its removal flag. Given that | |
888 | * the lookups/get next use an iterator aware of the | |
889 | * next pointer, they will either skip the old node due | |
890 | * to the removal flag and see the new node, or use | |
891 | * the old node, but will not see the new one. | |
892 | */ | |
04db56f8 | 893 | ret_next = uatomic_cmpxchg(&old_node->next, |
9357c415 | 894 | old_next, flag_removed(new_node)); |
3fb86f26 | 895 | if (ret_next == old_next) |
7801dadd | 896 | break; /* We performed the replacement. */ |
3fb86f26 LJ |
897 | old_next = ret_next; |
898 | } | |
9357c415 | 899 | |
9357c415 MD |
900 | /* |
901 | * Ensure that the old node is not visible to readers anymore: | |
902 | * lookup for the node, and remove it (along with any other | |
903 | * logically removed node) if found. | |
904 | */ | |
04db56f8 LJ |
905 | bucket = lookup_bucket(ht, size, bit_reverse_ulong(old_node->reverse_hash)); |
906 | _cds_lfht_gc_bucket(bucket, new_node); | |
7801dadd | 907 | |
04db56f8 | 908 | assert(is_removed(rcu_dereference(old_node->next))); |
7801dadd | 909 | return 0; |
9357c415 MD |
910 | } |
911 | ||
83beee94 MD |
912 | /* |
913 | * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add | |
914 | * mode. A NULL unique_ret allows creation of duplicate keys. | |
915 | */ | |
abc490a1 | 916 | static |
83beee94 | 917 | void _cds_lfht_add(struct cds_lfht *ht, |
0422d92c | 918 | cds_lfht_match_fct match, |
996ff57c | 919 | const void *key, |
83beee94 MD |
920 | unsigned long size, |
921 | struct cds_lfht_node *node, | |
922 | struct cds_lfht_iter *unique_ret, | |
1ee8f000 | 923 | int bucket_flag) |
abc490a1 | 924 | { |
14044b37 | 925 | struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next, |
960c9e4f | 926 | *return_node; |
04db56f8 | 927 | struct cds_lfht_node *bucket; |
abc490a1 | 928 | |
1ee8f000 | 929 | assert(!is_bucket(node)); |
c90201ac | 930 | assert(!is_removed(node)); |
04db56f8 | 931 | bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash)); |
abc490a1 | 932 | for (;;) { |
adc0de68 | 933 | uint32_t chain_len = 0; |
abc490a1 | 934 | |
11519af6 MD |
935 | /* |
936 | * iter_prev points to the non-removed node prior to the | |
937 | * insert location. | |
11519af6 | 938 | */ |
04db56f8 | 939 | iter_prev = bucket; |
1ee8f000 | 940 | /* We can always skip the bucket node initially */ |
04db56f8 LJ |
941 | iter = rcu_dereference(iter_prev->next); |
942 | assert(iter_prev->reverse_hash <= node->reverse_hash); | |
abc490a1 | 943 | for (;;) { |
8ed51e04 | 944 | if (caa_unlikely(is_end(iter))) |
273399de | 945 | goto insert; |
04db56f8 | 946 | if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash)) |
273399de | 947 | goto insert; |
238cc06e | 948 | |
1ee8f000 LJ |
949 | /* bucket node is the first node of the identical-hash-value chain */ |
950 | if (bucket_flag && clear_flag(iter)->reverse_hash == node->reverse_hash) | |
194fdbd1 | 951 | goto insert; |
238cc06e | 952 | |
04db56f8 | 953 | next = rcu_dereference(clear_flag(iter)->next); |
8ed51e04 | 954 | if (caa_unlikely(is_removed(next))) |
9dba85be | 955 | goto gc_node; |
238cc06e LJ |
956 | |
957 | /* uniquely add */ | |
83beee94 | 958 | if (unique_ret |
1ee8f000 | 959 | && !is_bucket(next) |
04db56f8 | 960 | && clear_flag(iter)->reverse_hash == node->reverse_hash) { |
238cc06e LJ |
961 | struct cds_lfht_iter d_iter = { .node = node, .next = iter, }; |
962 | ||
963 | /* | |
964 | * uniquely adding inserts the node as the first | |
965 | * node of the identical-hash-value node chain. | |
966 | * | |
967 | * This semantic ensures no duplicated keys | |
968 | * should ever be observable in the table | |
969 | * (including observe one node by one node | |
970 | * by forward iterations) | |
971 | */ | |
04db56f8 | 972 | cds_lfht_next_duplicate(ht, match, key, &d_iter); |
238cc06e LJ |
973 | if (!d_iter.node) |
974 | goto insert; | |
975 | ||
976 | *unique_ret = d_iter; | |
83beee94 | 977 | return; |
48ed1c18 | 978 | } |
238cc06e | 979 | |
11519af6 | 980 | /* Only account for identical reverse hash once */ |
04db56f8 | 981 | if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash |
1ee8f000 | 982 | && !is_bucket(next)) |
4105056a | 983 | check_resize(ht, size, ++chain_len); |
11519af6 | 984 | iter_prev = clear_flag(iter); |
273399de | 985 | iter = next; |
abc490a1 | 986 | } |
48ed1c18 | 987 | |
273399de | 988 | insert: |
7ec59d3b | 989 | assert(node != clear_flag(iter)); |
11519af6 | 990 | assert(!is_removed(iter_prev)); |
c90201ac | 991 | assert(!is_removed(iter)); |
f000907d | 992 | assert(iter_prev != node); |
1ee8f000 | 993 | if (!bucket_flag) |
04db56f8 | 994 | node->next = clear_flag(iter); |
f9c80341 | 995 | else |
1ee8f000 LJ |
996 | node->next = flag_bucket(clear_flag(iter)); |
997 | if (is_bucket(iter)) | |
998 | new_node = flag_bucket(node); | |
f5596c94 MD |
999 | else |
1000 | new_node = node; | |
04db56f8 | 1001 | if (uatomic_cmpxchg(&iter_prev->next, iter, |
48ed1c18 | 1002 | new_node) != iter) { |
273399de | 1003 | continue; /* retry */ |
48ed1c18 | 1004 | } else { |
83beee94 | 1005 | return_node = node; |
960c9e4f | 1006 | goto end; |
48ed1c18 MD |
1007 | } |
1008 | ||
9dba85be MD |
1009 | gc_node: |
1010 | assert(!is_removed(iter)); | |
1ee8f000 LJ |
1011 | if (is_bucket(iter)) |
1012 | new_next = flag_bucket(clear_flag(next)); | |
f5596c94 MD |
1013 | else |
1014 | new_next = clear_flag(next); | |
04db56f8 | 1015 | (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next); |
273399de | 1016 | /* retry */ |
464a1ec9 | 1017 | } |
9357c415 | 1018 | end: |
83beee94 MD |
1019 | if (unique_ret) { |
1020 | unique_ret->node = return_node; | |
1021 | /* unique_ret->next left unset, never used. */ | |
1022 | } | |
abc490a1 | 1023 | } |
464a1ec9 | 1024 | |
abc490a1 | 1025 | static |
860d07e8 | 1026 | int _cds_lfht_del(struct cds_lfht *ht, unsigned long size, |
4105056a | 1027 | struct cds_lfht_node *node, |
1ee8f000 | 1028 | int bucket_removal) |
abc490a1 | 1029 | { |
04db56f8 | 1030 | struct cds_lfht_node *bucket, *next, *old; |
5e28c532 | 1031 | |
9357c415 | 1032 | if (!node) /* Return -ENOENT if asked to delete NULL node */ |
743f9143 | 1033 | return -ENOENT; |
9357c415 | 1034 | |
7ec59d3b | 1035 | /* logically delete the node */ |
1ee8f000 | 1036 | assert(!is_bucket(node)); |
c90201ac | 1037 | assert(!is_removed(node)); |
04db56f8 | 1038 | old = rcu_dereference(node->next); |
7ec59d3b | 1039 | do { |
48ed1c18 MD |
1040 | struct cds_lfht_node *new_next; |
1041 | ||
7ec59d3b | 1042 | next = old; |
8ed51e04 | 1043 | if (caa_unlikely(is_removed(next))) |
743f9143 | 1044 | return -ENOENT; |
1ee8f000 LJ |
1045 | if (bucket_removal) |
1046 | assert(is_bucket(next)); | |
1475579c | 1047 | else |
1ee8f000 | 1048 | assert(!is_bucket(next)); |
48ed1c18 | 1049 | new_next = flag_removed(next); |
04db56f8 | 1050 | old = uatomic_cmpxchg(&node->next, next, new_next); |
7ec59d3b | 1051 | } while (old != next); |
7ec59d3b | 1052 | /* We performed the (logical) deletion. */ |
7ec59d3b MD |
1053 | |
1054 | /* | |
1055 | * Ensure that the node is not visible to readers anymore: lookup for | |
273399de MD |
1056 | * the node, and remove it (along with any other logically removed node) |
1057 | * if found. | |
11519af6 | 1058 | */ |
04db56f8 LJ |
1059 | bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash)); |
1060 | _cds_lfht_gc_bucket(bucket, node); | |
743f9143 | 1061 | |
04db56f8 | 1062 | assert(is_removed(rcu_dereference(node->next))); |
743f9143 | 1063 | return 0; |
abc490a1 | 1064 | } |
2ed95849 | 1065 | |
b7d619b0 MD |
1066 | static |
1067 | void *partition_resize_thread(void *arg) | |
1068 | { | |
1069 | struct partition_resize_work *work = arg; | |
1070 | ||
7b17c13e | 1071 | work->ht->flavor->register_thread(); |
b7d619b0 | 1072 | work->fct(work->ht, work->i, work->start, work->len); |
7b17c13e | 1073 | work->ht->flavor->unregister_thread(); |
b7d619b0 MD |
1074 | return NULL; |
1075 | } | |
1076 | ||
1077 | static | |
1078 | void partition_resize_helper(struct cds_lfht *ht, unsigned long i, | |
1079 | unsigned long len, | |
1080 | void (*fct)(struct cds_lfht *ht, unsigned long i, | |
1081 | unsigned long start, unsigned long len)) | |
1082 | { | |
1083 | unsigned long partition_len; | |
1084 | struct partition_resize_work *work; | |
6083a889 MD |
1085 | int thread, ret; |
1086 | unsigned long nr_threads; | |
b7d619b0 | 1087 | |
6083a889 MD |
1088 | /* |
1089 | * Note: nr_cpus_mask + 1 is always power of 2. | |
1090 | * We spawn just the number of threads we need to satisfy the minimum | |
1091 | * partition size, up to the number of CPUs in the system. | |
1092 | */ | |
91452a6a MD |
1093 | if (nr_cpus_mask > 0) { |
1094 | nr_threads = min(nr_cpus_mask + 1, | |
1095 | len >> MIN_PARTITION_PER_THREAD_ORDER); | |
1096 | } else { | |
1097 | nr_threads = 1; | |
1098 | } | |
6083a889 MD |
1099 | partition_len = len >> get_count_order_ulong(nr_threads); |
1100 | work = calloc(nr_threads, sizeof(*work)); | |
b7d619b0 | 1101 | assert(work); |
6083a889 MD |
1102 | for (thread = 0; thread < nr_threads; thread++) { |
1103 | work[thread].ht = ht; | |
1104 | work[thread].i = i; | |
1105 | work[thread].len = partition_len; | |
1106 | work[thread].start = thread * partition_len; | |
1107 | work[thread].fct = fct; | |
1af6e26e | 1108 | ret = pthread_create(&(work[thread].thread_id), ht->resize_attr, |
6083a889 | 1109 | partition_resize_thread, &work[thread]); |
b7d619b0 MD |
1110 | assert(!ret); |
1111 | } | |
6083a889 | 1112 | for (thread = 0; thread < nr_threads; thread++) { |
1af6e26e | 1113 | ret = pthread_join(work[thread].thread_id, NULL); |
b7d619b0 MD |
1114 | assert(!ret); |
1115 | } | |
1116 | free(work); | |
b7d619b0 MD |
1117 | } |
1118 | ||
e8de508e MD |
1119 | /* |
1120 | * Holding RCU read lock to protect _cds_lfht_add against memory | |
1121 | * reclaim that could be performed by other call_rcu worker threads (ABA | |
1122 | * problem). | |
9ee0fc9a | 1123 | * |
b7d619b0 | 1124 | * When we reach a certain length, we can split this population phase over |
9ee0fc9a MD |
1125 | * many worker threads, based on the number of CPUs available in the system. |
1126 | * This should therefore take care of not having the expand lagging behind too | |
1127 | * many concurrent insertion threads by using the scheduler's ability to | |
1ee8f000 | 1128 | * schedule bucket node population fairly with insertions. |
e8de508e | 1129 | */ |
4105056a | 1130 | static |
b7d619b0 MD |
1131 | void init_table_populate_partition(struct cds_lfht *ht, unsigned long i, |
1132 | unsigned long start, unsigned long len) | |
4105056a | 1133 | { |
9d72a73f | 1134 | unsigned long j, size = 1UL << (i - 1); |
4105056a | 1135 | |
d0d8f9aa | 1136 | assert(i > MIN_TABLE_ORDER); |
7b17c13e | 1137 | ht->flavor->read_lock(); |
9d72a73f LJ |
1138 | for (j = size + start; j < size + start + len; j++) { |
1139 | struct cds_lfht_node *new_node = bucket_at(ht, j); | |
1140 | ||
1141 | assert(j >= size && j < (size << 1)); | |
1142 | dbg_printf("init populate: order %lu index %lu hash %lu\n", | |
1143 | i, j, j); | |
1144 | new_node->reverse_hash = bit_reverse_ulong(j); | |
1145 | _cds_lfht_add(ht, NULL, NULL, size, new_node, NULL, 1); | |
4105056a | 1146 | } |
7b17c13e | 1147 | ht->flavor->read_unlock(); |
b7d619b0 MD |
1148 | } |
1149 | ||
1150 | static | |
1151 | void init_table_populate(struct cds_lfht *ht, unsigned long i, | |
1152 | unsigned long len) | |
1153 | { | |
1154 | assert(nr_cpus_mask != -1); | |
6083a889 | 1155 | if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) { |
7b17c13e | 1156 | ht->flavor->thread_online(); |
b7d619b0 | 1157 | init_table_populate_partition(ht, i, 0, len); |
7b17c13e | 1158 | ht->flavor->thread_offline(); |
b7d619b0 MD |
1159 | return; |
1160 | } | |
1161 | partition_resize_helper(ht, i, len, init_table_populate_partition); | |
4105056a MD |
1162 | } |
1163 | ||
abc490a1 | 1164 | static |
4105056a | 1165 | void init_table(struct cds_lfht *ht, |
93d46c39 | 1166 | unsigned long first_order, unsigned long last_order) |
24365af7 | 1167 | { |
93d46c39 | 1168 | unsigned long i; |
24365af7 | 1169 | |
93d46c39 LJ |
1170 | dbg_printf("init table: first_order %lu last_order %lu\n", |
1171 | first_order, last_order); | |
d0d8f9aa | 1172 | assert(first_order > MIN_TABLE_ORDER); |
93d46c39 | 1173 | for (i = first_order; i <= last_order; i++) { |
4105056a | 1174 | unsigned long len; |
24365af7 | 1175 | |
4f6e90b7 | 1176 | len = 1UL << (i - 1); |
f0c29ed7 | 1177 | dbg_printf("init order %lu len: %lu\n", i, len); |
4d676753 MD |
1178 | |
1179 | /* Stop expand if the resize target changes under us */ | |
4f6e90b7 | 1180 | if (CMM_LOAD_SHARED(ht->t.resize_target) < (1UL << i)) |
4d676753 MD |
1181 | break; |
1182 | ||
48f1b16d | 1183 | cds_lfht_alloc_bucket_table(ht, i); |
4105056a | 1184 | |
4105056a | 1185 | /* |
1ee8f000 LJ |
1186 | * Set all bucket nodes reverse hash values for a level and |
1187 | * link all bucket nodes into the table. | |
4105056a | 1188 | */ |
dc1da8f6 | 1189 | init_table_populate(ht, i, len); |
4105056a | 1190 | |
f9c80341 MD |
1191 | /* |
1192 | * Update table size. | |
1193 | */ | |
1194 | cmm_smp_wmb(); /* populate data before RCU size */ | |
4f6e90b7 | 1195 | CMM_STORE_SHARED(ht->t.size, 1UL << i); |
f9c80341 | 1196 | |
4f6e90b7 | 1197 | dbg_printf("init new size: %lu\n", 1UL << i); |
4105056a MD |
1198 | if (CMM_LOAD_SHARED(ht->in_progress_destroy)) |
1199 | break; | |
1200 | } | |
1201 | } | |
1202 | ||
e8de508e MD |
1203 | /* |
1204 | * Holding RCU read lock to protect _cds_lfht_remove against memory | |
1205 | * reclaim that could be performed by other call_rcu worker threads (ABA | |
1206 | * problem). | |
1207 | * For a single level, we logically remove and garbage collect each node. | |
1208 | * | |
1209 | * As a design choice, we perform logical removal and garbage collection on a | |
1210 | * node-per-node basis to simplify this algorithm. We also assume keeping good | |
1211 | * cache locality of the operation would overweight possible performance gain | |
1212 | * that could be achieved by batching garbage collection for multiple levels. | |
1213 | * However, this would have to be justified by benchmarks. | |
1214 | * | |
1215 | * Concurrent removal and add operations are helping us perform garbage | |
1216 | * collection of logically removed nodes. We guarantee that all logically | |
1217 | * removed nodes have been garbage-collected (unlinked) before call_rcu is | |
1ee8f000 | 1218 | * invoked to free a hole level of bucket nodes (after a grace period). |
e8de508e MD |
1219 | * |
1220 | * Logical removal and garbage collection can therefore be done in batch or on a | |
1221 | * node-per-node basis, as long as the guarantee above holds. | |
9ee0fc9a | 1222 | * |
b7d619b0 MD |
1223 | * When we reach a certain length, we can split this removal over many worker |
1224 | * threads, based on the number of CPUs available in the system. This should | |
1225 | * take care of not letting resize process lag behind too many concurrent | |
9ee0fc9a | 1226 | * updater threads actively inserting into the hash table. |
e8de508e | 1227 | */ |
4105056a | 1228 | static |
b7d619b0 MD |
1229 | void remove_table_partition(struct cds_lfht *ht, unsigned long i, |
1230 | unsigned long start, unsigned long len) | |
4105056a | 1231 | { |
9d72a73f | 1232 | unsigned long j, size = 1UL << (i - 1); |
4105056a | 1233 | |
d0d8f9aa | 1234 | assert(i > MIN_TABLE_ORDER); |
7b17c13e | 1235 | ht->flavor->read_lock(); |
9d72a73f LJ |
1236 | for (j = size + start; j < size + start + len; j++) { |
1237 | struct cds_lfht_node *fini_node = bucket_at(ht, j); | |
1238 | ||
1239 | assert(j >= size && j < (size << 1)); | |
1240 | dbg_printf("remove entry: order %lu index %lu hash %lu\n", | |
1241 | i, j, j); | |
1242 | fini_node->reverse_hash = bit_reverse_ulong(j); | |
1243 | (void) _cds_lfht_del(ht, size, fini_node, 1); | |
abc490a1 | 1244 | } |
7b17c13e | 1245 | ht->flavor->read_unlock(); |
b7d619b0 MD |
1246 | } |
1247 | ||
1248 | static | |
1249 | void remove_table(struct cds_lfht *ht, unsigned long i, unsigned long len) | |
1250 | { | |
1251 | ||
1252 | assert(nr_cpus_mask != -1); | |
6083a889 | 1253 | if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) { |
7b17c13e | 1254 | ht->flavor->thread_online(); |
b7d619b0 | 1255 | remove_table_partition(ht, i, 0, len); |
7b17c13e | 1256 | ht->flavor->thread_offline(); |
b7d619b0 MD |
1257 | return; |
1258 | } | |
1259 | partition_resize_helper(ht, i, len, remove_table_partition); | |
2ed95849 MD |
1260 | } |
1261 | ||
1475579c | 1262 | static |
4105056a | 1263 | void fini_table(struct cds_lfht *ht, |
93d46c39 | 1264 | unsigned long first_order, unsigned long last_order) |
1475579c | 1265 | { |
93d46c39 | 1266 | long i; |
48f1b16d | 1267 | unsigned long free_by_rcu_order = 0; |
1475579c | 1268 | |
93d46c39 LJ |
1269 | dbg_printf("fini table: first_order %lu last_order %lu\n", |
1270 | first_order, last_order); | |
d0d8f9aa | 1271 | assert(first_order > MIN_TABLE_ORDER); |
93d46c39 | 1272 | for (i = last_order; i >= first_order; i--) { |
4105056a | 1273 | unsigned long len; |
1475579c | 1274 | |
4f6e90b7 | 1275 | len = 1UL << (i - 1); |
1475579c | 1276 | dbg_printf("fini order %lu len: %lu\n", i, len); |
4105056a | 1277 | |
4d676753 MD |
1278 | /* Stop shrink if the resize target changes under us */ |
1279 | if (CMM_LOAD_SHARED(ht->t.resize_target) > (1UL << (i - 1))) | |
1280 | break; | |
1281 | ||
1282 | cmm_smp_wmb(); /* populate data before RCU size */ | |
1283 | CMM_STORE_SHARED(ht->t.size, 1UL << (i - 1)); | |
1284 | ||
1285 | /* | |
1286 | * We need to wait for all add operations to reach Q.S. (and | |
1287 | * thus use the new table for lookups) before we can start | |
1ee8f000 | 1288 | * releasing the old bucket nodes. Otherwise their lookup will |
4d676753 MD |
1289 | * return a logically removed node as insert position. |
1290 | */ | |
7b17c13e | 1291 | ht->flavor->update_synchronize_rcu(); |
48f1b16d LJ |
1292 | if (free_by_rcu_order) |
1293 | cds_lfht_free_bucket_table(ht, free_by_rcu_order); | |
4d676753 | 1294 | |
21263e21 | 1295 | /* |
1ee8f000 LJ |
1296 | * Set "removed" flag in bucket nodes about to be removed. |
1297 | * Unlink all now-logically-removed bucket node pointers. | |
4105056a MD |
1298 | * Concurrent add/remove operation are helping us doing |
1299 | * the gc. | |
21263e21 | 1300 | */ |
4105056a MD |
1301 | remove_table(ht, i, len); |
1302 | ||
48f1b16d | 1303 | free_by_rcu_order = i; |
4105056a MD |
1304 | |
1305 | dbg_printf("fini new size: %lu\n", 1UL << i); | |
1475579c MD |
1306 | if (CMM_LOAD_SHARED(ht->in_progress_destroy)) |
1307 | break; | |
1308 | } | |
0d14ceb2 | 1309 | |
48f1b16d | 1310 | if (free_by_rcu_order) { |
7b17c13e | 1311 | ht->flavor->update_synchronize_rcu(); |
48f1b16d | 1312 | cds_lfht_free_bucket_table(ht, free_by_rcu_order); |
0d14ceb2 | 1313 | } |
1475579c MD |
1314 | } |
1315 | ||
ff0d69de | 1316 | static |
1ee8f000 | 1317 | void cds_lfht_create_bucket(struct cds_lfht *ht, unsigned long size) |
ff0d69de | 1318 | { |
04db56f8 | 1319 | struct cds_lfht_node *prev, *node; |
9d72a73f | 1320 | unsigned long order, len, i; |
ff0d69de | 1321 | |
48f1b16d | 1322 | cds_lfht_alloc_bucket_table(ht, 0); |
ff0d69de | 1323 | |
9d72a73f LJ |
1324 | dbg_printf("create bucket: order 0 index 0 hash 0\n"); |
1325 | node = bucket_at(ht, 0); | |
1326 | node->next = flag_bucket(get_end()); | |
1327 | node->reverse_hash = 0; | |
ff0d69de LJ |
1328 | |
1329 | for (order = 1; order < get_count_order_ulong(size) + 1; order++) { | |
1330 | len = 1UL << (order - 1); | |
48f1b16d | 1331 | cds_lfht_alloc_bucket_table(ht, order); |
ff0d69de | 1332 | |
9d72a73f LJ |
1333 | for (i = 0; i < len; i++) { |
1334 | /* | |
1335 | * Now, we are trying to init the node with the | |
1336 | * hash=(len+i) (which is also a bucket with the | |
1337 | * index=(len+i)) and insert it into the hash table, | |
1338 | * so this node has to be inserted after the bucket | |
1339 | * with the index=(len+i)&(len-1)=i. And because there | |
1340 | * is no other non-bucket node nor bucket node with | |
1341 | * larger index/hash inserted, so the bucket node | |
1342 | * being inserted should be inserted directly linked | |
1343 | * after the bucket node with index=i. | |
1344 | */ | |
1345 | prev = bucket_at(ht, i); | |
1346 | node = bucket_at(ht, len + i); | |
ff0d69de | 1347 | |
1ee8f000 | 1348 | dbg_printf("create bucket: order %lu index %lu hash %lu\n", |
9d72a73f LJ |
1349 | order, len + i, len + i); |
1350 | node->reverse_hash = bit_reverse_ulong(len + i); | |
1351 | ||
1352 | /* insert after prev */ | |
1353 | assert(is_bucket(prev->next)); | |
ff0d69de | 1354 | node->next = prev->next; |
1ee8f000 | 1355 | prev->next = flag_bucket(node); |
ff0d69de LJ |
1356 | } |
1357 | } | |
1358 | } | |
1359 | ||
0422d92c | 1360 | struct cds_lfht *_cds_lfht_new(unsigned long init_size, |
0722081a | 1361 | unsigned long min_nr_alloc_buckets, |
747d725c | 1362 | unsigned long max_nr_buckets, |
b8af5011 | 1363 | int flags, |
7b17c13e | 1364 | const struct rcu_flavor_struct *flavor, |
b7d619b0 | 1365 | pthread_attr_t *attr) |
abc490a1 | 1366 | { |
14044b37 | 1367 | struct cds_lfht *ht; |
24365af7 | 1368 | unsigned long order; |
abc490a1 | 1369 | |
0722081a LJ |
1370 | /* min_nr_alloc_buckets must be power of two */ |
1371 | if (!min_nr_alloc_buckets || (min_nr_alloc_buckets & (min_nr_alloc_buckets - 1))) | |
5488222b | 1372 | return NULL; |
747d725c | 1373 | |
8129be4e | 1374 | /* init_size must be power of two */ |
5488222b | 1375 | if (!init_size || (init_size & (init_size - 1))) |
8129be4e | 1376 | return NULL; |
747d725c LJ |
1377 | |
1378 | if (!max_nr_buckets) | |
1379 | max_nr_buckets = 1UL << (MAX_TABLE_ORDER - 1); | |
1380 | ||
1381 | /* max_nr_buckets must be power of two */ | |
1382 | if (!max_nr_buckets || (max_nr_buckets & (max_nr_buckets - 1))) | |
1383 | return NULL; | |
1384 | ||
0722081a | 1385 | min_nr_alloc_buckets = max(min_nr_alloc_buckets, MIN_TABLE_SIZE); |
d0d8f9aa | 1386 | init_size = max(init_size, MIN_TABLE_SIZE); |
747d725c LJ |
1387 | max_nr_buckets = max(max_nr_buckets, min_nr_alloc_buckets); |
1388 | init_size = min(init_size, max_nr_buckets); | |
14044b37 | 1389 | ht = calloc(1, sizeof(struct cds_lfht)); |
b7d619b0 | 1390 | assert(ht); |
b5d6b20f | 1391 | ht->flags = flags; |
7b17c13e | 1392 | ht->flavor = flavor; |
b7d619b0 | 1393 | ht->resize_attr = attr; |
5afadd12 | 1394 | alloc_split_items_count(ht); |
abc490a1 MD |
1395 | /* this mutex should not nest in read-side C.S. */ |
1396 | pthread_mutex_init(&ht->resize_mutex, NULL); | |
5488222b | 1397 | order = get_count_order_ulong(init_size); |
93d46c39 | 1398 | ht->t.resize_target = 1UL << order; |
0722081a LJ |
1399 | ht->min_nr_alloc_buckets = min_nr_alloc_buckets; |
1400 | ht->min_alloc_buckets_order = get_count_order_ulong(min_nr_alloc_buckets); | |
747d725c | 1401 | ht->max_nr_buckets = max_nr_buckets; |
1ee8f000 | 1402 | cds_lfht_create_bucket(ht, 1UL << order); |
bcbd36fc | 1403 | ht->t.size = 1UL << order; |
abc490a1 MD |
1404 | return ht; |
1405 | } | |
1406 | ||
6f554439 | 1407 | void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash, |
996ff57c | 1408 | cds_lfht_match_fct match, const void *key, |
6f554439 | 1409 | struct cds_lfht_iter *iter) |
2ed95849 | 1410 | { |
04db56f8 | 1411 | struct cds_lfht_node *node, *next, *bucket; |
0422d92c | 1412 | unsigned long reverse_hash, size; |
2ed95849 | 1413 | |
abc490a1 | 1414 | reverse_hash = bit_reverse_ulong(hash); |
464a1ec9 | 1415 | |
4105056a | 1416 | size = rcu_dereference(ht->t.size); |
04db56f8 | 1417 | bucket = lookup_bucket(ht, size, hash); |
1ee8f000 | 1418 | /* We can always skip the bucket node initially */ |
04db56f8 | 1419 | node = rcu_dereference(bucket->next); |
bb7b2f26 | 1420 | node = clear_flag(node); |
2ed95849 | 1421 | for (;;) { |
8ed51e04 | 1422 | if (caa_unlikely(is_end(node))) { |
96ad1112 | 1423 | node = next = NULL; |
abc490a1 | 1424 | break; |
bb7b2f26 | 1425 | } |
04db56f8 | 1426 | if (caa_unlikely(node->reverse_hash > reverse_hash)) { |
96ad1112 | 1427 | node = next = NULL; |
abc490a1 | 1428 | break; |
2ed95849 | 1429 | } |
04db56f8 | 1430 | next = rcu_dereference(node->next); |
7f52427b | 1431 | assert(node == clear_flag(node)); |
8ed51e04 | 1432 | if (caa_likely(!is_removed(next)) |
1ee8f000 | 1433 | && !is_bucket(next) |
04db56f8 | 1434 | && node->reverse_hash == reverse_hash |
0422d92c | 1435 | && caa_likely(match(node, key))) { |
273399de | 1436 | break; |
2ed95849 | 1437 | } |
1b81fe1a | 1438 | node = clear_flag(next); |
2ed95849 | 1439 | } |
1ee8f000 | 1440 | assert(!node || !is_bucket(rcu_dereference(node->next))); |
adc0de68 MD |
1441 | iter->node = node; |
1442 | iter->next = next; | |
abc490a1 | 1443 | } |
e0ba718a | 1444 | |
0422d92c | 1445 | void cds_lfht_next_duplicate(struct cds_lfht *ht, cds_lfht_match_fct match, |
996ff57c | 1446 | const void *key, struct cds_lfht_iter *iter) |
a481e5ff | 1447 | { |
adc0de68 | 1448 | struct cds_lfht_node *node, *next; |
a481e5ff | 1449 | unsigned long reverse_hash; |
a481e5ff | 1450 | |
adc0de68 | 1451 | node = iter->node; |
04db56f8 | 1452 | reverse_hash = node->reverse_hash; |
adc0de68 | 1453 | next = iter->next; |
a481e5ff MD |
1454 | node = clear_flag(next); |
1455 | ||
1456 | for (;;) { | |
8ed51e04 | 1457 | if (caa_unlikely(is_end(node))) { |
96ad1112 | 1458 | node = next = NULL; |
a481e5ff | 1459 | break; |
bb7b2f26 | 1460 | } |
04db56f8 | 1461 | if (caa_unlikely(node->reverse_hash > reverse_hash)) { |
96ad1112 | 1462 | node = next = NULL; |
a481e5ff MD |
1463 | break; |
1464 | } | |
04db56f8 | 1465 | next = rcu_dereference(node->next); |
8ed51e04 | 1466 | if (caa_likely(!is_removed(next)) |
1ee8f000 | 1467 | && !is_bucket(next) |
04db56f8 | 1468 | && caa_likely(match(node, key))) { |
a481e5ff MD |
1469 | break; |
1470 | } | |
1471 | node = clear_flag(next); | |
1472 | } | |
1ee8f000 | 1473 | assert(!node || !is_bucket(rcu_dereference(node->next))); |
adc0de68 MD |
1474 | iter->node = node; |
1475 | iter->next = next; | |
a481e5ff MD |
1476 | } |
1477 | ||
4e9b9fbf MD |
1478 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter) |
1479 | { | |
1480 | struct cds_lfht_node *node, *next; | |
1481 | ||
853395e1 | 1482 | node = clear_flag(iter->next); |
4e9b9fbf | 1483 | for (;;) { |
8ed51e04 | 1484 | if (caa_unlikely(is_end(node))) { |
4e9b9fbf MD |
1485 | node = next = NULL; |
1486 | break; | |
1487 | } | |
04db56f8 | 1488 | next = rcu_dereference(node->next); |
8ed51e04 | 1489 | if (caa_likely(!is_removed(next)) |
1ee8f000 | 1490 | && !is_bucket(next)) { |
4e9b9fbf MD |
1491 | break; |
1492 | } | |
1493 | node = clear_flag(next); | |
1494 | } | |
1ee8f000 | 1495 | assert(!node || !is_bucket(rcu_dereference(node->next))); |
4e9b9fbf MD |
1496 | iter->node = node; |
1497 | iter->next = next; | |
1498 | } | |
1499 | ||
1500 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter) | |
1501 | { | |
4e9b9fbf | 1502 | /* |
1ee8f000 | 1503 | * Get next after first bucket node. The first bucket node is the |
4e9b9fbf MD |
1504 | * first node of the linked list. |
1505 | */ | |
9d72a73f | 1506 | iter->next = bucket_at(ht, 0)->next; |
4e9b9fbf MD |
1507 | cds_lfht_next(ht, iter); |
1508 | } | |
1509 | ||
0422d92c MD |
1510 | void cds_lfht_add(struct cds_lfht *ht, unsigned long hash, |
1511 | struct cds_lfht_node *node) | |
abc490a1 | 1512 | { |
0422d92c | 1513 | unsigned long size; |
ab7d5fc6 | 1514 | |
04db56f8 | 1515 | node->reverse_hash = bit_reverse_ulong((unsigned long) hash); |
4105056a | 1516 | size = rcu_dereference(ht->t.size); |
04db56f8 | 1517 | _cds_lfht_add(ht, NULL, NULL, size, node, NULL, 0); |
14360f1c | 1518 | ht_count_add(ht, size, hash); |
3eca1b8c MD |
1519 | } |
1520 | ||
14044b37 | 1521 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
6f554439 | 1522 | unsigned long hash, |
0422d92c | 1523 | cds_lfht_match_fct match, |
996ff57c | 1524 | const void *key, |
48ed1c18 | 1525 | struct cds_lfht_node *node) |
3eca1b8c | 1526 | { |
0422d92c | 1527 | unsigned long size; |
83beee94 | 1528 | struct cds_lfht_iter iter; |
3eca1b8c | 1529 | |
04db56f8 | 1530 | node->reverse_hash = bit_reverse_ulong((unsigned long) hash); |
4105056a | 1531 | size = rcu_dereference(ht->t.size); |
04db56f8 | 1532 | _cds_lfht_add(ht, match, key, size, node, &iter, 0); |
83beee94 | 1533 | if (iter.node == node) |
14360f1c | 1534 | ht_count_add(ht, size, hash); |
83beee94 | 1535 | return iter.node; |
2ed95849 MD |
1536 | } |
1537 | ||
9357c415 | 1538 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
6f554439 | 1539 | unsigned long hash, |
0422d92c | 1540 | cds_lfht_match_fct match, |
996ff57c | 1541 | const void *key, |
48ed1c18 MD |
1542 | struct cds_lfht_node *node) |
1543 | { | |
0422d92c | 1544 | unsigned long size; |
83beee94 | 1545 | struct cds_lfht_iter iter; |
48ed1c18 | 1546 | |
04db56f8 | 1547 | node->reverse_hash = bit_reverse_ulong((unsigned long) hash); |
48ed1c18 | 1548 | size = rcu_dereference(ht->t.size); |
83beee94 | 1549 | for (;;) { |
04db56f8 | 1550 | _cds_lfht_add(ht, match, key, size, node, &iter, 0); |
83beee94 | 1551 | if (iter.node == node) { |
14360f1c | 1552 | ht_count_add(ht, size, hash); |
83beee94 MD |
1553 | return NULL; |
1554 | } | |
1555 | ||
1556 | if (!_cds_lfht_replace(ht, size, iter.node, iter.next, node)) | |
1557 | return iter.node; | |
1558 | } | |
48ed1c18 MD |
1559 | } |
1560 | ||
9357c415 MD |
1561 | int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter, |
1562 | struct cds_lfht_node *new_node) | |
1563 | { | |
1564 | unsigned long size; | |
1565 | ||
1566 | size = rcu_dereference(ht->t.size); | |
1567 | return _cds_lfht_replace(ht, size, old_iter->node, old_iter->next, | |
1568 | new_node); | |
1569 | } | |
1570 | ||
1571 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter) | |
2ed95849 | 1572 | { |
14360f1c | 1573 | unsigned long size, hash; |
df44348d | 1574 | int ret; |
abc490a1 | 1575 | |
4105056a | 1576 | size = rcu_dereference(ht->t.size); |
9357c415 | 1577 | ret = _cds_lfht_del(ht, size, iter->node, 0); |
14360f1c | 1578 | if (!ret) { |
04db56f8 | 1579 | hash = bit_reverse_ulong(iter->node->reverse_hash); |
14360f1c LJ |
1580 | ht_count_del(ht, size, hash); |
1581 | } | |
df44348d | 1582 | return ret; |
2ed95849 | 1583 | } |
ab7d5fc6 | 1584 | |
abc490a1 | 1585 | static |
1ee8f000 | 1586 | int cds_lfht_delete_bucket(struct cds_lfht *ht) |
674f7a69 | 1587 | { |
14044b37 | 1588 | struct cds_lfht_node *node; |
4105056a | 1589 | unsigned long order, i, size; |
674f7a69 | 1590 | |
abc490a1 | 1591 | /* Check that the table is empty */ |
9d72a73f | 1592 | node = bucket_at(ht, 0); |
abc490a1 | 1593 | do { |
04db56f8 | 1594 | node = clear_flag(node)->next; |
1ee8f000 | 1595 | if (!is_bucket(node)) |
abc490a1 | 1596 | return -EPERM; |
273399de | 1597 | assert(!is_removed(node)); |
bb7b2f26 | 1598 | } while (!is_end(node)); |
4105056a MD |
1599 | /* |
1600 | * size accessed without rcu_dereference because hash table is | |
1601 | * being destroyed. | |
1602 | */ | |
1603 | size = ht->t.size; | |
1ee8f000 | 1604 | /* Internal sanity check: all nodes left should be bucket */ |
48f1b16d LJ |
1605 | for (i = 0; i < size; i++) { |
1606 | node = bucket_at(ht, i); | |
1607 | dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n", | |
1608 | i, i, bit_reverse_ulong(node->reverse_hash)); | |
1609 | assert(is_bucket(node->next)); | |
1610 | } | |
24365af7 | 1611 | |
48f1b16d LJ |
1612 | for (order = get_count_order_ulong(size); (long)order >= 0; order--) |
1613 | cds_lfht_free_bucket_table(ht, order); | |
5488222b | 1614 | |
abc490a1 | 1615 | return 0; |
674f7a69 MD |
1616 | } |
1617 | ||
1618 | /* | |
1619 | * Should only be called when no more concurrent readers nor writers can | |
1620 | * possibly access the table. | |
1621 | */ | |
b7d619b0 | 1622 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr) |
674f7a69 | 1623 | { |
5e28c532 MD |
1624 | int ret; |
1625 | ||
848d4088 | 1626 | /* Wait for in-flight resize operations to complete */ |
24953e08 MD |
1627 | _CMM_STORE_SHARED(ht->in_progress_destroy, 1); |
1628 | cmm_smp_mb(); /* Store destroy before load resize */ | |
848d4088 MD |
1629 | while (uatomic_read(&ht->in_progress_resize)) |
1630 | poll(NULL, 0, 100); /* wait for 100ms */ | |
1ee8f000 | 1631 | ret = cds_lfht_delete_bucket(ht); |
abc490a1 MD |
1632 | if (ret) |
1633 | return ret; | |
5afadd12 | 1634 | free_split_items_count(ht); |
b7d619b0 MD |
1635 | if (attr) |
1636 | *attr = ht->resize_attr; | |
98808fb1 | 1637 | poison_free(ht); |
5e28c532 | 1638 | return ret; |
674f7a69 MD |
1639 | } |
1640 | ||
14044b37 | 1641 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
d933dd0e | 1642 | long *approx_before, |
273399de | 1643 | unsigned long *count, |
973e5e1b | 1644 | unsigned long *removed, |
d933dd0e | 1645 | long *approx_after) |
273399de | 1646 | { |
14044b37 | 1647 | struct cds_lfht_node *node, *next; |
1ee8f000 | 1648 | unsigned long nr_bucket = 0; |
273399de | 1649 | |
7ed7682f | 1650 | *approx_before = 0; |
5afadd12 | 1651 | if (ht->split_count) { |
973e5e1b MD |
1652 | int i; |
1653 | ||
4c42f1b8 LJ |
1654 | for (i = 0; i < split_count_mask + 1; i++) { |
1655 | *approx_before += uatomic_read(&ht->split_count[i].add); | |
1656 | *approx_before -= uatomic_read(&ht->split_count[i].del); | |
973e5e1b MD |
1657 | } |
1658 | } | |
1659 | ||
273399de MD |
1660 | *count = 0; |
1661 | *removed = 0; | |
1662 | ||
1ee8f000 | 1663 | /* Count non-bucket nodes in the table */ |
9d72a73f | 1664 | node = bucket_at(ht, 0); |
273399de | 1665 | do { |
04db56f8 | 1666 | next = rcu_dereference(node->next); |
b198f0fd | 1667 | if (is_removed(next)) { |
1ee8f000 | 1668 | if (!is_bucket(next)) |
973e5e1b MD |
1669 | (*removed)++; |
1670 | else | |
1ee8f000 LJ |
1671 | (nr_bucket)++; |
1672 | } else if (!is_bucket(next)) | |
273399de | 1673 | (*count)++; |
24365af7 | 1674 | else |
1ee8f000 | 1675 | (nr_bucket)++; |
273399de | 1676 | node = clear_flag(next); |
bb7b2f26 | 1677 | } while (!is_end(node)); |
1ee8f000 | 1678 | dbg_printf("number of bucket nodes: %lu\n", nr_bucket); |
7ed7682f | 1679 | *approx_after = 0; |
5afadd12 | 1680 | if (ht->split_count) { |
973e5e1b MD |
1681 | int i; |
1682 | ||
4c42f1b8 LJ |
1683 | for (i = 0; i < split_count_mask + 1; i++) { |
1684 | *approx_after += uatomic_read(&ht->split_count[i].add); | |
1685 | *approx_after -= uatomic_read(&ht->split_count[i].del); | |
973e5e1b MD |
1686 | } |
1687 | } | |
273399de MD |
1688 | } |
1689 | ||
1475579c | 1690 | /* called with resize mutex held */ |
abc490a1 | 1691 | static |
4105056a | 1692 | void _do_cds_lfht_grow(struct cds_lfht *ht, |
1475579c | 1693 | unsigned long old_size, unsigned long new_size) |
abc490a1 | 1694 | { |
1475579c | 1695 | unsigned long old_order, new_order; |
1475579c | 1696 | |
93d46c39 LJ |
1697 | old_order = get_count_order_ulong(old_size); |
1698 | new_order = get_count_order_ulong(new_size); | |
1a401918 LJ |
1699 | dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n", |
1700 | old_size, old_order, new_size, new_order); | |
1475579c | 1701 | assert(new_size > old_size); |
93d46c39 | 1702 | init_table(ht, old_order + 1, new_order); |
abc490a1 MD |
1703 | } |
1704 | ||
1705 | /* called with resize mutex held */ | |
1706 | static | |
4105056a | 1707 | void _do_cds_lfht_shrink(struct cds_lfht *ht, |
1475579c | 1708 | unsigned long old_size, unsigned long new_size) |
464a1ec9 | 1709 | { |
1475579c | 1710 | unsigned long old_order, new_order; |
464a1ec9 | 1711 | |
d0d8f9aa | 1712 | new_size = max(new_size, MIN_TABLE_SIZE); |
93d46c39 LJ |
1713 | old_order = get_count_order_ulong(old_size); |
1714 | new_order = get_count_order_ulong(new_size); | |
1a401918 LJ |
1715 | dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n", |
1716 | old_size, old_order, new_size, new_order); | |
1475579c | 1717 | assert(new_size < old_size); |
1475579c | 1718 | |
1ee8f000 | 1719 | /* Remove and unlink all bucket nodes to remove. */ |
93d46c39 | 1720 | fini_table(ht, new_order + 1, old_order); |
464a1ec9 MD |
1721 | } |
1722 | ||
1475579c MD |
1723 | |
1724 | /* called with resize mutex held */ | |
1725 | static | |
1726 | void _do_cds_lfht_resize(struct cds_lfht *ht) | |
1727 | { | |
1728 | unsigned long new_size, old_size; | |
4105056a MD |
1729 | |
1730 | /* | |
1731 | * Resize table, re-do if the target size has changed under us. | |
1732 | */ | |
1733 | do { | |
d2be3620 MD |
1734 | assert(uatomic_read(&ht->in_progress_resize)); |
1735 | if (CMM_LOAD_SHARED(ht->in_progress_destroy)) | |
1736 | break; | |
4105056a MD |
1737 | ht->t.resize_initiated = 1; |
1738 | old_size = ht->t.size; | |
1739 | new_size = CMM_LOAD_SHARED(ht->t.resize_target); | |
1740 | if (old_size < new_size) | |
1741 | _do_cds_lfht_grow(ht, old_size, new_size); | |
1742 | else if (old_size > new_size) | |
1743 | _do_cds_lfht_shrink(ht, old_size, new_size); | |
1744 | ht->t.resize_initiated = 0; | |
1745 | /* write resize_initiated before read resize_target */ | |
1746 | cmm_smp_mb(); | |
4d676753 | 1747 | } while (ht->t.size != CMM_LOAD_SHARED(ht->t.resize_target)); |
1475579c MD |
1748 | } |
1749 | ||
abc490a1 | 1750 | static |
ab65b890 | 1751 | unsigned long resize_target_grow(struct cds_lfht *ht, unsigned long new_size) |
464a1ec9 | 1752 | { |
ab65b890 | 1753 | return _uatomic_xchg_monotonic_increase(&ht->t.resize_target, new_size); |
464a1ec9 MD |
1754 | } |
1755 | ||
1475579c | 1756 | static |
4105056a | 1757 | void resize_target_update_count(struct cds_lfht *ht, |
b8af5011 | 1758 | unsigned long count) |
1475579c | 1759 | { |
d0d8f9aa | 1760 | count = max(count, MIN_TABLE_SIZE); |
747d725c | 1761 | count = min(count, ht->max_nr_buckets); |
4105056a | 1762 | uatomic_set(&ht->t.resize_target, count); |
1475579c MD |
1763 | } |
1764 | ||
1765 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size) | |
464a1ec9 | 1766 | { |
4105056a MD |
1767 | resize_target_update_count(ht, new_size); |
1768 | CMM_STORE_SHARED(ht->t.resize_initiated, 1); | |
7b17c13e | 1769 | ht->flavor->thread_offline(); |
1475579c MD |
1770 | pthread_mutex_lock(&ht->resize_mutex); |
1771 | _do_cds_lfht_resize(ht); | |
1772 | pthread_mutex_unlock(&ht->resize_mutex); | |
7b17c13e | 1773 | ht->flavor->thread_online(); |
abc490a1 | 1774 | } |
464a1ec9 | 1775 | |
abc490a1 MD |
1776 | static |
1777 | void do_resize_cb(struct rcu_head *head) | |
1778 | { | |
1779 | struct rcu_resize_work *work = | |
1780 | caa_container_of(head, struct rcu_resize_work, head); | |
14044b37 | 1781 | struct cds_lfht *ht = work->ht; |
abc490a1 | 1782 | |
7b17c13e | 1783 | ht->flavor->thread_offline(); |
abc490a1 | 1784 | pthread_mutex_lock(&ht->resize_mutex); |
14044b37 | 1785 | _do_cds_lfht_resize(ht); |
abc490a1 | 1786 | pthread_mutex_unlock(&ht->resize_mutex); |
7b17c13e | 1787 | ht->flavor->thread_online(); |
98808fb1 | 1788 | poison_free(work); |
848d4088 MD |
1789 | cmm_smp_mb(); /* finish resize before decrement */ |
1790 | uatomic_dec(&ht->in_progress_resize); | |
464a1ec9 MD |
1791 | } |
1792 | ||
abc490a1 | 1793 | static |
f1f119ee | 1794 | void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht) |
ab7d5fc6 | 1795 | { |
abc490a1 MD |
1796 | struct rcu_resize_work *work; |
1797 | ||
4105056a MD |
1798 | /* Store resize_target before read resize_initiated */ |
1799 | cmm_smp_mb(); | |
ab65b890 | 1800 | if (!CMM_LOAD_SHARED(ht->t.resize_initiated)) { |
848d4088 | 1801 | uatomic_inc(&ht->in_progress_resize); |
59290e9d | 1802 | cmm_smp_mb(); /* increment resize count before load destroy */ |
ed35e6d8 MD |
1803 | if (CMM_LOAD_SHARED(ht->in_progress_destroy)) { |
1804 | uatomic_dec(&ht->in_progress_resize); | |
59290e9d | 1805 | return; |
ed35e6d8 | 1806 | } |
f9830efd MD |
1807 | work = malloc(sizeof(*work)); |
1808 | work->ht = ht; | |
7b17c13e | 1809 | ht->flavor->update_call_rcu(&work->head, do_resize_cb); |
4105056a | 1810 | CMM_STORE_SHARED(ht->t.resize_initiated, 1); |
f9830efd | 1811 | } |
ab7d5fc6 | 1812 | } |
3171717f | 1813 | |
f1f119ee LJ |
1814 | static |
1815 | void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth) | |
1816 | { | |
1817 | unsigned long target_size = size << growth; | |
1818 | ||
747d725c | 1819 | target_size = min(target_size, ht->max_nr_buckets); |
f1f119ee LJ |
1820 | if (resize_target_grow(ht, target_size) >= target_size) |
1821 | return; | |
1822 | ||
1823 | __cds_lfht_resize_lazy_launch(ht); | |
1824 | } | |
1825 | ||
89bb121d LJ |
1826 | /* |
1827 | * We favor grow operations over shrink. A shrink operation never occurs | |
1828 | * if a grow operation is queued for lazy execution. A grow operation | |
1829 | * cancels any pending shrink lazy execution. | |
1830 | */ | |
3171717f | 1831 | static |
4105056a | 1832 | void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size, |
3171717f MD |
1833 | unsigned long count) |
1834 | { | |
b8af5011 MD |
1835 | if (!(ht->flags & CDS_LFHT_AUTO_RESIZE)) |
1836 | return; | |
d0d8f9aa | 1837 | count = max(count, MIN_TABLE_SIZE); |
747d725c | 1838 | count = min(count, ht->max_nr_buckets); |
89bb121d LJ |
1839 | if (count == size) |
1840 | return; /* Already the right size, no resize needed */ | |
1841 | if (count > size) { /* lazy grow */ | |
1842 | if (resize_target_grow(ht, count) >= count) | |
1843 | return; | |
1844 | } else { /* lazy shrink */ | |
1845 | for (;;) { | |
1846 | unsigned long s; | |
1847 | ||
1848 | s = uatomic_cmpxchg(&ht->t.resize_target, size, count); | |
1849 | if (s == size) | |
1850 | break; /* no resize needed */ | |
1851 | if (s > size) | |
1852 | return; /* growing is/(was just) in progress */ | |
1853 | if (s <= count) | |
1854 | return; /* some other thread do shrink */ | |
1855 | size = s; | |
1856 | } | |
1857 | } | |
f1f119ee | 1858 | __cds_lfht_resize_lazy_launch(ht); |
3171717f | 1859 | } |