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