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