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