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