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