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