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