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