rculfhash: extract compare_fct and hash_fct from the hash table
[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;
5488222b
LJ
270 unsigned long min_alloc_order;
271 unsigned long min_alloc_size;
732ad076 272 unsigned long hash_seed;
b8af5011 273 int flags;
5f511391
MD
274 /*
275 * We need to put the work threads offline (QSBR) when taking this
276 * mutex, because we use synchronize_rcu within this mutex critical
277 * section, which waits on read-side critical sections, and could
278 * therefore cause grace-period deadlock if we hold off RCU G.P.
279 * completion.
280 */
464a1ec9 281 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
33c7c748 282 unsigned int in_progress_resize, in_progress_destroy;
14044b37 283 void (*cds_lfht_call_rcu)(struct rcu_head *head,
abc490a1 284 void (*func)(struct rcu_head *head));
1475579c 285 void (*cds_lfht_synchronize_rcu)(void);
01dbfa62
MD
286 void (*cds_lfht_rcu_read_lock)(void);
287 void (*cds_lfht_rcu_read_unlock)(void);
5f511391
MD
288 void (*cds_lfht_rcu_thread_offline)(void);
289 void (*cds_lfht_rcu_thread_online)(void);
b7d619b0
MD
290 void (*cds_lfht_rcu_register_thread)(void);
291 void (*cds_lfht_rcu_unregister_thread)(void);
292 pthread_attr_t *resize_attr; /* Resize threads attributes */
7de5ccfd 293 long count; /* global approximate item count */
4c42f1b8 294 struct ht_items_count *split_count; /* split item count */
2ed95849
MD
295};
296
7f52427b
MD
297/*
298 * rcu_resize_work: Contains arguments passed to RCU worker thread
299 * responsible for performing lazy resize.
300 */
abc490a1
MD
301struct rcu_resize_work {
302 struct rcu_head head;
14044b37 303 struct cds_lfht *ht;
abc490a1 304};
2ed95849 305
7f52427b
MD
306/*
307 * partition_resize_work: Contains arguments passed to worker threads
308 * executing the hash table resize on partitions of the hash table
309 * assigned to each processor's worker thread.
310 */
b7d619b0 311struct partition_resize_work {
1af6e26e 312 pthread_t thread_id;
b7d619b0
MD
313 struct cds_lfht *ht;
314 unsigned long i, start, len;
315 void (*fct)(struct cds_lfht *ht, unsigned long i,
316 unsigned long start, unsigned long len);
317};
318
76a73da8 319static
83beee94 320void _cds_lfht_add(struct cds_lfht *ht,
0422d92c 321 cds_lfht_match_fct match,
83beee94
MD
322 unsigned long size,
323 struct cds_lfht_node *node,
324 struct cds_lfht_iter *unique_ret,
325 int dummy);
48ed1c18 326
abc490a1
MD
327/*
328 * Algorithm to reverse bits in a word by lookup table, extended to
329 * 64-bit words.
f9830efd 330 * Source:
abc490a1 331 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
f9830efd 332 * Originally from Public Domain.
abc490a1
MD
333 */
334
335static const uint8_t BitReverseTable256[256] =
2ed95849 336{
abc490a1
MD
337#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
338#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
339#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
340 R6(0), R6(2), R6(1), R6(3)
341};
342#undef R2
343#undef R4
344#undef R6
2ed95849 345
abc490a1
MD
346static
347uint8_t bit_reverse_u8(uint8_t v)
348{
349 return BitReverseTable256[v];
350}
ab7d5fc6 351
abc490a1
MD
352static __attribute__((unused))
353uint32_t bit_reverse_u32(uint32_t v)
354{
355 return ((uint32_t) bit_reverse_u8(v) << 24) |
356 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
357 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
358 ((uint32_t) bit_reverse_u8(v >> 24));
2ed95849
MD
359}
360
abc490a1
MD
361static __attribute__((unused))
362uint64_t bit_reverse_u64(uint64_t v)
2ed95849 363{
abc490a1
MD
364 return ((uint64_t) bit_reverse_u8(v) << 56) |
365 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
366 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
367 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
368 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
369 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
370 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
371 ((uint64_t) bit_reverse_u8(v >> 56));
372}
373
374static
375unsigned long bit_reverse_ulong(unsigned long v)
376{
377#if (CAA_BITS_PER_LONG == 32)
378 return bit_reverse_u32(v);
379#else
380 return bit_reverse_u64(v);
381#endif
382}
383
f9830efd 384/*
24365af7
MD
385 * fls: returns the position of the most significant bit.
386 * Returns 0 if no bit is set, else returns the position of the most
387 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
f9830efd 388 */
24365af7
MD
389#if defined(__i386) || defined(__x86_64)
390static inline
391unsigned int fls_u32(uint32_t x)
f9830efd 392{
24365af7
MD
393 int r;
394
395 asm("bsrl %1,%0\n\t"
396 "jnz 1f\n\t"
397 "movl $-1,%0\n\t"
398 "1:\n\t"
399 : "=r" (r) : "rm" (x));
400 return r + 1;
401}
402#define HAS_FLS_U32
403#endif
404
405#if defined(__x86_64)
406static inline
407unsigned int fls_u64(uint64_t x)
408{
409 long r;
410
411 asm("bsrq %1,%0\n\t"
412 "jnz 1f\n\t"
413 "movq $-1,%0\n\t"
414 "1:\n\t"
415 : "=r" (r) : "rm" (x));
416 return r + 1;
417}
418#define HAS_FLS_U64
419#endif
420
421#ifndef HAS_FLS_U64
422static __attribute__((unused))
423unsigned int fls_u64(uint64_t x)
424{
425 unsigned int r = 64;
426
427 if (!x)
428 return 0;
429
430 if (!(x & 0xFFFFFFFF00000000ULL)) {
431 x <<= 32;
432 r -= 32;
433 }
434 if (!(x & 0xFFFF000000000000ULL)) {
435 x <<= 16;
436 r -= 16;
437 }
438 if (!(x & 0xFF00000000000000ULL)) {
439 x <<= 8;
440 r -= 8;
441 }
442 if (!(x & 0xF000000000000000ULL)) {
443 x <<= 4;
444 r -= 4;
445 }
446 if (!(x & 0xC000000000000000ULL)) {
447 x <<= 2;
448 r -= 2;
449 }
450 if (!(x & 0x8000000000000000ULL)) {
451 x <<= 1;
452 r -= 1;
453 }
454 return r;
455}
456#endif
457
458#ifndef HAS_FLS_U32
459static __attribute__((unused))
460unsigned int fls_u32(uint32_t x)
461{
462 unsigned int r = 32;
f9830efd 463
24365af7
MD
464 if (!x)
465 return 0;
466 if (!(x & 0xFFFF0000U)) {
467 x <<= 16;
468 r -= 16;
469 }
470 if (!(x & 0xFF000000U)) {
471 x <<= 8;
472 r -= 8;
473 }
474 if (!(x & 0xF0000000U)) {
475 x <<= 4;
476 r -= 4;
477 }
478 if (!(x & 0xC0000000U)) {
479 x <<= 2;
480 r -= 2;
481 }
482 if (!(x & 0x80000000U)) {
483 x <<= 1;
484 r -= 1;
485 }
486 return r;
487}
488#endif
489
490unsigned int fls_ulong(unsigned long x)
f9830efd 491{
6887cc5e 492#if (CAA_BITS_PER_LONG == 32)
24365af7
MD
493 return fls_u32(x);
494#else
495 return fls_u64(x);
496#endif
497}
f9830efd 498
920f8ef6
LJ
499/*
500 * Return the minimum order for which x <= (1UL << order).
501 * Return -1 if x is 0.
502 */
24365af7
MD
503int get_count_order_u32(uint32_t x)
504{
920f8ef6
LJ
505 if (!x)
506 return -1;
24365af7 507
920f8ef6 508 return fls_u32(x - 1);
24365af7
MD
509}
510
920f8ef6
LJ
511/*
512 * Return the minimum order for which x <= (1UL << order).
513 * Return -1 if x is 0.
514 */
24365af7
MD
515int get_count_order_ulong(unsigned long x)
516{
920f8ef6
LJ
517 if (!x)
518 return -1;
24365af7 519
920f8ef6 520 return fls_ulong(x - 1);
f9830efd
MD
521}
522
98808fb1 523#ifdef POISON_FREE
5afadd12
LJ
524#define poison_free(ptr) \
525 do { \
526 if (ptr) { \
527 memset(ptr, 0x42, sizeof(*(ptr))); \
528 free(ptr); \
529 } \
98808fb1
MD
530 } while (0)
531#else
532#define poison_free(ptr) free(ptr)
533#endif
534
f9830efd 535static
ab65b890 536void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth);
f9830efd 537
f8994aee 538static
4105056a 539void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
f8994aee
MD
540 unsigned long count);
541
df44348d 542static long nr_cpus_mask = -1;
4c42f1b8
LJ
543static long split_count_mask = -1;
544
4ddbb355 545#if defined(HAVE_SYSCONF)
4c42f1b8
LJ
546static void ht_init_nr_cpus_mask(void)
547{
548 long maxcpus;
549
550 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
551 if (maxcpus <= 0) {
552 nr_cpus_mask = -2;
553 return;
554 }
555 /*
556 * round up number of CPUs to next power of two, so we
557 * can use & for modulo.
558 */
559 maxcpus = 1UL << get_count_order_ulong(maxcpus);
560 nr_cpus_mask = maxcpus - 1;
561}
4ddbb355
LJ
562#else /* #if defined(HAVE_SYSCONF) */
563static void ht_init_nr_cpus_mask(void)
564{
565 nr_cpus_mask = -2;
566}
567#endif /* #else #if defined(HAVE_SYSCONF) */
df44348d
MD
568
569static
5afadd12 570void alloc_split_items_count(struct cds_lfht *ht)
df44348d
MD
571{
572 struct ht_items_count *count;
573
4c42f1b8
LJ
574 if (nr_cpus_mask == -1) {
575 ht_init_nr_cpus_mask();
4ddbb355
LJ
576 if (nr_cpus_mask < 0)
577 split_count_mask = DEFAULT_SPLIT_COUNT_MASK;
578 else
579 split_count_mask = nr_cpus_mask;
df44348d 580 }
4c42f1b8 581
4ddbb355 582 assert(split_count_mask >= 0);
5afadd12
LJ
583
584 if (ht->flags & CDS_LFHT_ACCOUNTING) {
585 ht->split_count = calloc(split_count_mask + 1, sizeof(*count));
586 assert(ht->split_count);
587 } else {
588 ht->split_count = NULL;
589 }
df44348d
MD
590}
591
592static
5afadd12 593void free_split_items_count(struct cds_lfht *ht)
df44348d 594{
5afadd12 595 poison_free(ht->split_count);
df44348d
MD
596}
597
14360f1c 598#if defined(HAVE_SCHED_GETCPU)
df44348d 599static
14360f1c 600int ht_get_split_count_index(unsigned long hash)
df44348d
MD
601{
602 int cpu;
603
4c42f1b8 604 assert(split_count_mask >= 0);
df44348d 605 cpu = sched_getcpu();
8ed51e04 606 if (caa_unlikely(cpu < 0))
14360f1c 607 return hash & split_count_mask;
df44348d 608 else
4c42f1b8 609 return cpu & split_count_mask;
df44348d 610}
14360f1c
LJ
611#else /* #if defined(HAVE_SCHED_GETCPU) */
612static
613int ht_get_split_count_index(unsigned long hash)
614{
615 return hash & split_count_mask;
616}
617#endif /* #else #if defined(HAVE_SCHED_GETCPU) */
df44348d
MD
618
619static
14360f1c 620void ht_count_add(struct cds_lfht *ht, unsigned long size, unsigned long hash)
df44348d 621{
4c42f1b8
LJ
622 unsigned long split_count;
623 int index;
df44348d 624
8ed51e04 625 if (caa_unlikely(!ht->split_count))
3171717f 626 return;
14360f1c 627 index = ht_get_split_count_index(hash);
4c42f1b8 628 split_count = uatomic_add_return(&ht->split_count[index].add, 1);
8ed51e04 629 if (caa_unlikely(!(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
e3ecefd6 630 long count;
df44348d 631
4c42f1b8 632 dbg_printf("add split count %lu\n", split_count);
df44348d
MD
633 count = uatomic_add_return(&ht->count,
634 1UL << COUNT_COMMIT_ORDER);
635 /* If power of 2 */
636 if (!(count & (count - 1))) {
4105056a 637 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) < size)
f8994aee 638 return;
e3ecefd6 639 dbg_printf("add set global %ld\n", count);
4105056a 640 cds_lfht_resize_lazy_count(ht, size,
6ea6bc67 641 count >> (CHAIN_LEN_TARGET - 1));
df44348d
MD
642 }
643 }
644}
645
646static
14360f1c 647void ht_count_del(struct cds_lfht *ht, unsigned long size, unsigned long hash)
df44348d 648{
4c42f1b8
LJ
649 unsigned long split_count;
650 int index;
df44348d 651
8ed51e04 652 if (caa_unlikely(!ht->split_count))
3171717f 653 return;
14360f1c 654 index = ht_get_split_count_index(hash);
4c42f1b8 655 split_count = uatomic_add_return(&ht->split_count[index].del, 1);
8ed51e04 656 if (caa_unlikely(!(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
e3ecefd6 657 long count;
df44348d 658
4c42f1b8 659 dbg_printf("del split count %lu\n", split_count);
df44348d 660 count = uatomic_add_return(&ht->count,
3171717f 661 -(1UL << COUNT_COMMIT_ORDER));
df44348d
MD
662 /* If power of 2 */
663 if (!(count & (count - 1))) {
4105056a 664 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) >= size)
f8994aee 665 return;
e3ecefd6
MD
666 dbg_printf("del set global %ld\n", count);
667 /*
c941bb9e 668 * Don't shrink table if the number of nodes is below a
e3ecefd6
MD
669 * certain threshold.
670 */
4c42f1b8 671 if (count < (1UL << COUNT_COMMIT_ORDER) * (split_count_mask + 1))
e3ecefd6 672 return;
4105056a 673 cds_lfht_resize_lazy_count(ht, size,
6ea6bc67 674 count >> (CHAIN_LEN_TARGET - 1));
df44348d
MD
675 }
676 }
677}
678
f9830efd 679static
4105056a 680void check_resize(struct cds_lfht *ht, unsigned long size, uint32_t chain_len)
f9830efd 681{
f8994aee
MD
682 unsigned long count;
683
b8af5011
MD
684 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
685 return;
f8994aee
MD
686 count = uatomic_read(&ht->count);
687 /*
688 * Use bucket-local length for small table expand and for
689 * environments lacking per-cpu data support.
690 */
691 if (count >= (1UL << COUNT_COMMIT_ORDER))
692 return;
24365af7 693 if (chain_len > 100)
f0c29ed7 694 dbg_printf("WARNING: large chain length: %u.\n",
24365af7 695 chain_len);
3390d470 696 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
ab65b890 697 cds_lfht_resize_lazy_grow(ht, size,
01370f0b 698 get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
f9830efd
MD
699}
700
abc490a1 701static
14044b37 702struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
abc490a1 703{
14044b37 704 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
abc490a1
MD
705}
706
707static
14044b37 708int is_removed(struct cds_lfht_node *node)
abc490a1 709{
d37166c6 710 return ((unsigned long) node) & REMOVED_FLAG;
abc490a1
MD
711}
712
713static
14044b37 714struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
abc490a1 715{
14044b37 716 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
abc490a1
MD
717}
718
f5596c94 719static
14044b37 720int is_dummy(struct cds_lfht_node *node)
f5596c94
MD
721{
722 return ((unsigned long) node) & DUMMY_FLAG;
723}
724
725static
14044b37 726struct cds_lfht_node *flag_dummy(struct cds_lfht_node *node)
f5596c94 727{
14044b37 728 return (struct cds_lfht_node *) (((unsigned long) node) | DUMMY_FLAG);
f5596c94 729}
bb7b2f26
MD
730
731static
732struct cds_lfht_node *get_end(void)
733{
734 return (struct cds_lfht_node *) END_VALUE;
735}
736
737static
738int is_end(struct cds_lfht_node *node)
739{
740 return clear_flag(node) == (struct cds_lfht_node *) END_VALUE;
741}
742
abc490a1 743static
ab65b890
LJ
744unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr,
745 unsigned long v)
abc490a1
MD
746{
747 unsigned long old1, old2;
748
749 old1 = uatomic_read(ptr);
750 do {
751 old2 = old1;
752 if (old2 >= v)
f9830efd 753 return old2;
abc490a1 754 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
ab65b890 755 return old2;
abc490a1
MD
756}
757
f4a9cc0b
LJ
758static
759struct _cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size,
760 unsigned long hash)
761{
762 unsigned long index, order;
763
764 assert(size > 0);
765 index = hash & (size - 1);
ef6e6171
LJ
766
767 if (index < ht->min_alloc_size) {
768 dbg_printf("lookup hash %lu index %lu order 0 aridx 0\n",
769 hash, index);
770 return &ht->t.tbl[0]->nodes[index];
771 }
a4ea2223
LJ
772 /*
773 * equivalent to get_count_order_ulong(index + 1), but optimizes
774 * away the non-existing 0 special-case for
775 * get_count_order_ulong.
776 */
777 order = fls_ulong(index);
f4a9cc0b 778 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
ef6e6171
LJ
779 hash, index, order, index & ((1UL << (order - 1)) - 1));
780 return &ht->t.tbl[order]->nodes[index & ((1UL << (order - 1)) - 1)];
f4a9cc0b
LJ
781}
782
273399de
MD
783/*
784 * Remove all logically deleted nodes from a bucket up to a certain node key.
785 */
786static
f9c80341 787void _cds_lfht_gc_bucket(struct cds_lfht_node *dummy, struct cds_lfht_node *node)
273399de 788{
14044b37 789 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
273399de 790
c90201ac
MD
791 assert(!is_dummy(dummy));
792 assert(!is_removed(dummy));
793 assert(!is_dummy(node));
794 assert(!is_removed(node));
273399de
MD
795 for (;;) {
796 iter_prev = dummy;
797 /* We can always skip the dummy node initially */
cc4fcb10 798 iter = rcu_dereference(iter_prev->p.next);
b4cb483f 799 assert(!is_removed(iter));
cc4fcb10 800 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
bd4db153
MD
801 /*
802 * We should never be called with dummy (start of chain)
803 * and logically removed node (end of path compression
804 * marker) being the actual same node. This would be a
805 * bug in the algorithm implementation.
806 */
807 assert(dummy != node);
273399de 808 for (;;) {
8ed51e04 809 if (caa_unlikely(is_end(iter)))
f9c80341 810 return;
8ed51e04 811 if (caa_likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
f9c80341 812 return;
cc4fcb10 813 next = rcu_dereference(clear_flag(iter)->p.next);
8ed51e04 814 if (caa_likely(is_removed(next)))
273399de 815 break;
b453eae1 816 iter_prev = clear_flag(iter);
273399de
MD
817 iter = next;
818 }
b198f0fd 819 assert(!is_removed(iter));
f5596c94
MD
820 if (is_dummy(iter))
821 new_next = flag_dummy(clear_flag(next));
822 else
823 new_next = clear_flag(next);
824 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
273399de 825 }
f9c80341 826 return;
273399de
MD
827}
828
9357c415
MD
829static
830int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size,
831 struct cds_lfht_node *old_node,
3fb86f26 832 struct cds_lfht_node *old_next,
9357c415
MD
833 struct cds_lfht_node *new_node)
834{
3fb86f26 835 struct cds_lfht_node *dummy, *ret_next;
9357c415 836 struct _cds_lfht_node *lookup;
9357c415
MD
837
838 if (!old_node) /* Return -ENOENT if asked to replace NULL node */
7801dadd 839 return -ENOENT;
9357c415
MD
840
841 assert(!is_removed(old_node));
842 assert(!is_dummy(old_node));
843 assert(!is_removed(new_node));
844 assert(!is_dummy(new_node));
845 assert(new_node != old_node);
3fb86f26 846 for (;;) {
9357c415 847 /* Insert after node to be replaced */
9357c415
MD
848 if (is_removed(old_next)) {
849 /*
850 * Too late, the old node has been removed under us
851 * between lookup and replace. Fail.
852 */
7801dadd 853 return -ENOENT;
9357c415
MD
854 }
855 assert(!is_dummy(old_next));
856 assert(new_node != clear_flag(old_next));
857 new_node->p.next = clear_flag(old_next);
858 /*
859 * Here is the whole trick for lock-free replace: we add
860 * the replacement node _after_ the node we want to
861 * replace by atomically setting its next pointer at the
862 * same time we set its removal flag. Given that
863 * the lookups/get next use an iterator aware of the
864 * next pointer, they will either skip the old node due
865 * to the removal flag and see the new node, or use
866 * the old node, but will not see the new one.
867 */
868 ret_next = uatomic_cmpxchg(&old_node->p.next,
869 old_next, flag_removed(new_node));
3fb86f26 870 if (ret_next == old_next)
7801dadd 871 break; /* We performed the replacement. */
3fb86f26
LJ
872 old_next = ret_next;
873 }
9357c415 874
9357c415
MD
875 /*
876 * Ensure that the old node is not visible to readers anymore:
877 * lookup for the node, and remove it (along with any other
878 * logically removed node) if found.
879 */
f4a9cc0b 880 lookup = lookup_bucket(ht, size, bit_reverse_ulong(old_node->p.reverse_hash));
9357c415
MD
881 dummy = (struct cds_lfht_node *) lookup;
882 _cds_lfht_gc_bucket(dummy, new_node);
7801dadd
LJ
883
884 assert(is_removed(rcu_dereference(old_node->p.next)));
885 return 0;
9357c415
MD
886}
887
83beee94
MD
888/*
889 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
890 * mode. A NULL unique_ret allows creation of duplicate keys.
891 */
abc490a1 892static
83beee94 893void _cds_lfht_add(struct cds_lfht *ht,
0422d92c 894 cds_lfht_match_fct match,
83beee94
MD
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 */
0422d92c 947 cds_lfht_next_duplicate(ht, match, &d_iter);
238cc06e
LJ
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 1122 bit_reverse_ulong((1UL << (i - 1)) + j);
0422d92c 1123 _cds_lfht_add(ht, NULL, 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
0422d92c 1340struct cds_lfht *_cds_lfht_new(unsigned long init_size,
5488222b 1341 unsigned long min_alloc_size,
b8af5011 1342 int flags,
14044b37 1343 void (*cds_lfht_call_rcu)(struct rcu_head *head,
1475579c 1344 void (*func)(struct rcu_head *head)),
01dbfa62
MD
1345 void (*cds_lfht_synchronize_rcu)(void),
1346 void (*cds_lfht_rcu_read_lock)(void),
5f511391
MD
1347 void (*cds_lfht_rcu_read_unlock)(void),
1348 void (*cds_lfht_rcu_thread_offline)(void),
b7d619b0
MD
1349 void (*cds_lfht_rcu_thread_online)(void),
1350 void (*cds_lfht_rcu_register_thread)(void),
1351 void (*cds_lfht_rcu_unregister_thread)(void),
1352 pthread_attr_t *attr)
abc490a1 1353{
14044b37 1354 struct cds_lfht *ht;
24365af7 1355 unsigned long order;
abc490a1 1356
5488222b
LJ
1357 /* min_alloc_size must be power of two */
1358 if (!min_alloc_size || (min_alloc_size & (min_alloc_size - 1)))
1359 return NULL;
8129be4e 1360 /* init_size must be power of two */
5488222b 1361 if (!init_size || (init_size & (init_size - 1)))
8129be4e 1362 return NULL;
5488222b
LJ
1363 min_alloc_size = max(min_alloc_size, MIN_TABLE_SIZE);
1364 init_size = max(init_size, min_alloc_size);
14044b37 1365 ht = calloc(1, sizeof(struct cds_lfht));
b7d619b0 1366 assert(ht);
b5d6b20f 1367 ht->flags = flags;
14044b37 1368 ht->cds_lfht_call_rcu = cds_lfht_call_rcu;
1475579c 1369 ht->cds_lfht_synchronize_rcu = cds_lfht_synchronize_rcu;
01dbfa62
MD
1370 ht->cds_lfht_rcu_read_lock = cds_lfht_rcu_read_lock;
1371 ht->cds_lfht_rcu_read_unlock = cds_lfht_rcu_read_unlock;
5f511391
MD
1372 ht->cds_lfht_rcu_thread_offline = cds_lfht_rcu_thread_offline;
1373 ht->cds_lfht_rcu_thread_online = cds_lfht_rcu_thread_online;
b7d619b0
MD
1374 ht->cds_lfht_rcu_register_thread = cds_lfht_rcu_register_thread;
1375 ht->cds_lfht_rcu_unregister_thread = cds_lfht_rcu_unregister_thread;
1376 ht->resize_attr = attr;
5afadd12 1377 alloc_split_items_count(ht);
abc490a1
MD
1378 /* this mutex should not nest in read-side C.S. */
1379 pthread_mutex_init(&ht->resize_mutex, NULL);
5488222b 1380 order = get_count_order_ulong(init_size);
93d46c39 1381 ht->t.resize_target = 1UL << order;
5488222b
LJ
1382 ht->min_alloc_size = min_alloc_size;
1383 ht->min_alloc_order = get_count_order_ulong(min_alloc_size);
bcbd36fc
LJ
1384 cds_lfht_create_dummy(ht, 1UL << order);
1385 ht->t.size = 1UL << order;
abc490a1
MD
1386 return ht;
1387}
1388
0422d92c
MD
1389void cds_lfht_lookup(struct cds_lfht *ht, cds_lfht_match_fct match,
1390 unsigned long hash, void *key, struct cds_lfht_iter *iter)
2ed95849 1391{
bb7b2f26 1392 struct cds_lfht_node *node, *next, *dummy_node;
14044b37 1393 struct _cds_lfht_node *lookup;
0422d92c 1394 unsigned long reverse_hash, size;
2ed95849 1395
abc490a1 1396 reverse_hash = bit_reverse_ulong(hash);
464a1ec9 1397
4105056a 1398 size = rcu_dereference(ht->t.size);
f4a9cc0b 1399 lookup = lookup_bucket(ht, size, hash);
bb7b2f26
MD
1400 dummy_node = (struct cds_lfht_node *) lookup;
1401 /* We can always skip the dummy node initially */
1402 node = rcu_dereference(dummy_node->p.next);
bb7b2f26 1403 node = clear_flag(node);
2ed95849 1404 for (;;) {
8ed51e04 1405 if (caa_unlikely(is_end(node))) {
96ad1112 1406 node = next = NULL;
abc490a1 1407 break;
bb7b2f26 1408 }
8ed51e04 1409 if (caa_unlikely(node->p.reverse_hash > reverse_hash)) {
96ad1112 1410 node = next = NULL;
abc490a1 1411 break;
2ed95849 1412 }
1b81fe1a 1413 next = rcu_dereference(node->p.next);
7f52427b 1414 assert(node == clear_flag(node));
8ed51e04 1415 if (caa_likely(!is_removed(next))
1b81fe1a 1416 && !is_dummy(next)
7f52427b 1417 && node->p.reverse_hash == reverse_hash
0422d92c 1418 && caa_likely(match(node, key))) {
273399de 1419 break;
2ed95849 1420 }
1b81fe1a 1421 node = clear_flag(next);
2ed95849 1422 }
1b81fe1a 1423 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
adc0de68
MD
1424 iter->node = node;
1425 iter->next = next;
abc490a1 1426}
e0ba718a 1427
0422d92c
MD
1428void cds_lfht_next_duplicate(struct cds_lfht *ht, cds_lfht_match_fct match,
1429 struct cds_lfht_iter *iter)
a481e5ff 1430{
adc0de68 1431 struct cds_lfht_node *node, *next;
a481e5ff
MD
1432 unsigned long reverse_hash;
1433 void *key;
a481e5ff 1434
adc0de68 1435 node = iter->node;
a481e5ff
MD
1436 reverse_hash = node->p.reverse_hash;
1437 key = node->key;
adc0de68 1438 next = iter->next;
a481e5ff
MD
1439 node = clear_flag(next);
1440
1441 for (;;) {
8ed51e04 1442 if (caa_unlikely(is_end(node))) {
96ad1112 1443 node = next = NULL;
a481e5ff 1444 break;
bb7b2f26 1445 }
8ed51e04 1446 if (caa_unlikely(node->p.reverse_hash > reverse_hash)) {
96ad1112 1447 node = next = NULL;
a481e5ff
MD
1448 break;
1449 }
1450 next = rcu_dereference(node->p.next);
8ed51e04 1451 if (caa_likely(!is_removed(next))
a481e5ff 1452 && !is_dummy(next)
0422d92c 1453 && caa_likely(match(node->key, key))) {
a481e5ff
MD
1454 break;
1455 }
1456 node = clear_flag(next);
1457 }
1458 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
adc0de68
MD
1459 iter->node = node;
1460 iter->next = next;
a481e5ff
MD
1461}
1462
4e9b9fbf
MD
1463void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1464{
1465 struct cds_lfht_node *node, *next;
1466
853395e1 1467 node = clear_flag(iter->next);
4e9b9fbf 1468 for (;;) {
8ed51e04 1469 if (caa_unlikely(is_end(node))) {
4e9b9fbf
MD
1470 node = next = NULL;
1471 break;
1472 }
1473 next = rcu_dereference(node->p.next);
8ed51e04 1474 if (caa_likely(!is_removed(next))
4e9b9fbf
MD
1475 && !is_dummy(next)) {
1476 break;
1477 }
1478 node = clear_flag(next);
1479 }
1480 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
1481 iter->node = node;
1482 iter->next = next;
1483}
1484
1485void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1486{
1487 struct _cds_lfht_node *lookup;
1488
1489 /*
1490 * Get next after first dummy node. The first dummy node is the
1491 * first node of the linked list.
1492 */
1493 lookup = &ht->t.tbl[0]->nodes[0];
853395e1 1494 iter->next = lookup->next;
4e9b9fbf
MD
1495 cds_lfht_next(ht, iter);
1496}
1497
0422d92c
MD
1498void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
1499 struct cds_lfht_node *node)
abc490a1 1500{
0422d92c 1501 unsigned long size;
ab7d5fc6 1502
cc4fcb10 1503 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
4105056a 1504 size = rcu_dereference(ht->t.size);
0422d92c 1505 _cds_lfht_add(ht, NULL, size, node, NULL, 0);
14360f1c 1506 ht_count_add(ht, size, hash);
3eca1b8c
MD
1507}
1508
14044b37 1509struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
0422d92c
MD
1510 cds_lfht_match_fct match,
1511 unsigned long hash,
48ed1c18 1512 struct cds_lfht_node *node)
3eca1b8c 1513{
0422d92c 1514 unsigned long size;
83beee94 1515 struct cds_lfht_iter iter;
3eca1b8c 1516
cc4fcb10 1517 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
4105056a 1518 size = rcu_dereference(ht->t.size);
0422d92c 1519 _cds_lfht_add(ht, match, size, node, &iter, 0);
83beee94 1520 if (iter.node == node)
14360f1c 1521 ht_count_add(ht, size, hash);
83beee94 1522 return iter.node;
2ed95849
MD
1523}
1524
9357c415 1525struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
0422d92c
MD
1526 cds_lfht_match_fct match,
1527 unsigned long hash,
48ed1c18
MD
1528 struct cds_lfht_node *node)
1529{
0422d92c 1530 unsigned long size;
83beee94 1531 struct cds_lfht_iter iter;
48ed1c18 1532
48ed1c18 1533 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
48ed1c18 1534 size = rcu_dereference(ht->t.size);
83beee94 1535 for (;;) {
0422d92c 1536 _cds_lfht_add(ht, match, size, node, &iter, 0);
83beee94 1537 if (iter.node == node) {
14360f1c 1538 ht_count_add(ht, size, hash);
83beee94
MD
1539 return NULL;
1540 }
1541
1542 if (!_cds_lfht_replace(ht, size, iter.node, iter.next, node))
1543 return iter.node;
1544 }
48ed1c18
MD
1545}
1546
9357c415
MD
1547int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter,
1548 struct cds_lfht_node *new_node)
1549{
1550 unsigned long size;
1551
1552 size = rcu_dereference(ht->t.size);
1553 return _cds_lfht_replace(ht, size, old_iter->node, old_iter->next,
1554 new_node);
1555}
1556
1557int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter)
2ed95849 1558{
14360f1c 1559 unsigned long size, hash;
df44348d 1560 int ret;
abc490a1 1561
4105056a 1562 size = rcu_dereference(ht->t.size);
9357c415 1563 ret = _cds_lfht_del(ht, size, iter->node, 0);
14360f1c
LJ
1564 if (!ret) {
1565 hash = bit_reverse_ulong(iter->node->p.reverse_hash);
1566 ht_count_del(ht, size, hash);
1567 }
df44348d 1568 return ret;
2ed95849 1569}
ab7d5fc6 1570
abc490a1 1571static
14044b37 1572int cds_lfht_delete_dummy(struct cds_lfht *ht)
674f7a69 1573{
14044b37
MD
1574 struct cds_lfht_node *node;
1575 struct _cds_lfht_node *lookup;
4105056a 1576 unsigned long order, i, size;
674f7a69 1577
abc490a1 1578 /* Check that the table is empty */
4105056a 1579 lookup = &ht->t.tbl[0]->nodes[0];
14044b37 1580 node = (struct cds_lfht_node *) lookup;
abc490a1 1581 do {
1b81fe1a
MD
1582 node = clear_flag(node)->p.next;
1583 if (!is_dummy(node))
abc490a1 1584 return -EPERM;
273399de 1585 assert(!is_removed(node));
bb7b2f26 1586 } while (!is_end(node));
4105056a
MD
1587 /*
1588 * size accessed without rcu_dereference because hash table is
1589 * being destroyed.
1590 */
1591 size = ht->t.size;
abc490a1 1592 /* Internal sanity check: all nodes left should be dummy */
4105056a 1593 for (order = 0; order < get_count_order_ulong(size) + 1; order++) {
24365af7
MD
1594 unsigned long len;
1595
1596 len = !order ? 1 : 1UL << (order - 1);
1597 for (i = 0; i < len; i++) {
f0c29ed7 1598 dbg_printf("delete order %lu i %lu hash %lu\n",
24365af7 1599 order, i,
4105056a
MD
1600 bit_reverse_ulong(ht->t.tbl[order]->nodes[i].reverse_hash));
1601 assert(is_dummy(ht->t.tbl[order]->nodes[i].next));
24365af7 1602 }
5488222b
LJ
1603
1604 if (order == ht->min_alloc_order)
1605 poison_free(ht->t.tbl[0]);
1606 else if (order > ht->min_alloc_order)
1607 poison_free(ht->t.tbl[order]);
1608 /* Nothing to delete for order < ht->min_alloc_order */
674f7a69 1609 }
abc490a1 1610 return 0;
674f7a69
MD
1611}
1612
1613/*
1614 * Should only be called when no more concurrent readers nor writers can
1615 * possibly access the table.
1616 */
b7d619b0 1617int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr)
674f7a69 1618{
5e28c532
MD
1619 int ret;
1620
848d4088 1621 /* Wait for in-flight resize operations to complete */
24953e08
MD
1622 _CMM_STORE_SHARED(ht->in_progress_destroy, 1);
1623 cmm_smp_mb(); /* Store destroy before load resize */
848d4088
MD
1624 while (uatomic_read(&ht->in_progress_resize))
1625 poll(NULL, 0, 100); /* wait for 100ms */
14044b37 1626 ret = cds_lfht_delete_dummy(ht);
abc490a1
MD
1627 if (ret)
1628 return ret;
5afadd12 1629 free_split_items_count(ht);
b7d619b0
MD
1630 if (attr)
1631 *attr = ht->resize_attr;
98808fb1 1632 poison_free(ht);
5e28c532 1633 return ret;
674f7a69
MD
1634}
1635
14044b37 1636void cds_lfht_count_nodes(struct cds_lfht *ht,
d933dd0e 1637 long *approx_before,
273399de 1638 unsigned long *count,
973e5e1b 1639 unsigned long *removed,
d933dd0e 1640 long *approx_after)
273399de 1641{
14044b37
MD
1642 struct cds_lfht_node *node, *next;
1643 struct _cds_lfht_node *lookup;
24365af7 1644 unsigned long nr_dummy = 0;
273399de 1645
7ed7682f 1646 *approx_before = 0;
5afadd12 1647 if (ht->split_count) {
973e5e1b
MD
1648 int i;
1649
4c42f1b8
LJ
1650 for (i = 0; i < split_count_mask + 1; i++) {
1651 *approx_before += uatomic_read(&ht->split_count[i].add);
1652 *approx_before -= uatomic_read(&ht->split_count[i].del);
973e5e1b
MD
1653 }
1654 }
1655
273399de
MD
1656 *count = 0;
1657 *removed = 0;
1658
24365af7 1659 /* Count non-dummy nodes in the table */
4105056a 1660 lookup = &ht->t.tbl[0]->nodes[0];
14044b37 1661 node = (struct cds_lfht_node *) lookup;
273399de 1662 do {
cc4fcb10 1663 next = rcu_dereference(node->p.next);
b198f0fd 1664 if (is_removed(next)) {
973e5e1b
MD
1665 if (!is_dummy(next))
1666 (*removed)++;
1667 else
1668 (nr_dummy)++;
1b81fe1a 1669 } else if (!is_dummy(next))
273399de 1670 (*count)++;
24365af7
MD
1671 else
1672 (nr_dummy)++;
273399de 1673 node = clear_flag(next);
bb7b2f26 1674 } while (!is_end(node));
f0c29ed7 1675 dbg_printf("number of dummy nodes: %lu\n", nr_dummy);
7ed7682f 1676 *approx_after = 0;
5afadd12 1677 if (ht->split_count) {
973e5e1b
MD
1678 int i;
1679
4c42f1b8
LJ
1680 for (i = 0; i < split_count_mask + 1; i++) {
1681 *approx_after += uatomic_read(&ht->split_count[i].add);
1682 *approx_after -= uatomic_read(&ht->split_count[i].del);
973e5e1b
MD
1683 }
1684 }
273399de
MD
1685}
1686
1475579c 1687/* called with resize mutex held */
abc490a1 1688static
4105056a 1689void _do_cds_lfht_grow(struct cds_lfht *ht,
1475579c 1690 unsigned long old_size, unsigned long new_size)
abc490a1 1691{
1475579c 1692 unsigned long old_order, new_order;
1475579c 1693
93d46c39
LJ
1694 old_order = get_count_order_ulong(old_size);
1695 new_order = get_count_order_ulong(new_size);
1a401918
LJ
1696 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1697 old_size, old_order, new_size, new_order);
1475579c 1698 assert(new_size > old_size);
93d46c39 1699 init_table(ht, old_order + 1, new_order);
abc490a1
MD
1700}
1701
1702/* called with resize mutex held */
1703static
4105056a 1704void _do_cds_lfht_shrink(struct cds_lfht *ht,
1475579c 1705 unsigned long old_size, unsigned long new_size)
464a1ec9 1706{
1475579c 1707 unsigned long old_order, new_order;
464a1ec9 1708
5488222b 1709 new_size = max(new_size, ht->min_alloc_size);
93d46c39
LJ
1710 old_order = get_count_order_ulong(old_size);
1711 new_order = get_count_order_ulong(new_size);
1a401918
LJ
1712 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1713 old_size, old_order, new_size, new_order);
1475579c 1714 assert(new_size < old_size);
1475579c 1715
4105056a 1716 /* Remove and unlink all dummy nodes to remove. */
93d46c39 1717 fini_table(ht, new_order + 1, old_order);
464a1ec9
MD
1718}
1719
1475579c
MD
1720
1721/* called with resize mutex held */
1722static
1723void _do_cds_lfht_resize(struct cds_lfht *ht)
1724{
1725 unsigned long new_size, old_size;
4105056a
MD
1726
1727 /*
1728 * Resize table, re-do if the target size has changed under us.
1729 */
1730 do {
d2be3620
MD
1731 assert(uatomic_read(&ht->in_progress_resize));
1732 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1733 break;
4105056a
MD
1734 ht->t.resize_initiated = 1;
1735 old_size = ht->t.size;
1736 new_size = CMM_LOAD_SHARED(ht->t.resize_target);
1737 if (old_size < new_size)
1738 _do_cds_lfht_grow(ht, old_size, new_size);
1739 else if (old_size > new_size)
1740 _do_cds_lfht_shrink(ht, old_size, new_size);
1741 ht->t.resize_initiated = 0;
1742 /* write resize_initiated before read resize_target */
1743 cmm_smp_mb();
4d676753 1744 } while (ht->t.size != CMM_LOAD_SHARED(ht->t.resize_target));
1475579c
MD
1745}
1746
abc490a1 1747static
ab65b890 1748unsigned long resize_target_grow(struct cds_lfht *ht, unsigned long new_size)
464a1ec9 1749{
ab65b890 1750 return _uatomic_xchg_monotonic_increase(&ht->t.resize_target, new_size);
464a1ec9
MD
1751}
1752
1475579c 1753static
4105056a 1754void resize_target_update_count(struct cds_lfht *ht,
b8af5011 1755 unsigned long count)
1475579c 1756{
5488222b 1757 count = max(count, ht->min_alloc_size);
4105056a 1758 uatomic_set(&ht->t.resize_target, count);
1475579c
MD
1759}
1760
1761void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size)
464a1ec9 1762{
4105056a
MD
1763 resize_target_update_count(ht, new_size);
1764 CMM_STORE_SHARED(ht->t.resize_initiated, 1);
5f511391 1765 ht->cds_lfht_rcu_thread_offline();
1475579c
MD
1766 pthread_mutex_lock(&ht->resize_mutex);
1767 _do_cds_lfht_resize(ht);
1768 pthread_mutex_unlock(&ht->resize_mutex);
5f511391 1769 ht->cds_lfht_rcu_thread_online();
abc490a1 1770}
464a1ec9 1771
abc490a1
MD
1772static
1773void do_resize_cb(struct rcu_head *head)
1774{
1775 struct rcu_resize_work *work =
1776 caa_container_of(head, struct rcu_resize_work, head);
14044b37 1777 struct cds_lfht *ht = work->ht;
abc490a1 1778
5f511391 1779 ht->cds_lfht_rcu_thread_offline();
abc490a1 1780 pthread_mutex_lock(&ht->resize_mutex);
14044b37 1781 _do_cds_lfht_resize(ht);
abc490a1 1782 pthread_mutex_unlock(&ht->resize_mutex);
5f511391 1783 ht->cds_lfht_rcu_thread_online();
98808fb1 1784 poison_free(work);
848d4088
MD
1785 cmm_smp_mb(); /* finish resize before decrement */
1786 uatomic_dec(&ht->in_progress_resize);
464a1ec9
MD
1787}
1788
abc490a1 1789static
f1f119ee 1790void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht)
ab7d5fc6 1791{
abc490a1
MD
1792 struct rcu_resize_work *work;
1793
4105056a
MD
1794 /* Store resize_target before read resize_initiated */
1795 cmm_smp_mb();
ab65b890 1796 if (!CMM_LOAD_SHARED(ht->t.resize_initiated)) {
848d4088 1797 uatomic_inc(&ht->in_progress_resize);
59290e9d 1798 cmm_smp_mb(); /* increment resize count before load destroy */
ed35e6d8
MD
1799 if (CMM_LOAD_SHARED(ht->in_progress_destroy)) {
1800 uatomic_dec(&ht->in_progress_resize);
59290e9d 1801 return;
ed35e6d8 1802 }
f9830efd
MD
1803 work = malloc(sizeof(*work));
1804 work->ht = ht;
14044b37 1805 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
4105056a 1806 CMM_STORE_SHARED(ht->t.resize_initiated, 1);
f9830efd 1807 }
ab7d5fc6 1808}
3171717f 1809
f1f119ee
LJ
1810static
1811void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth)
1812{
1813 unsigned long target_size = size << growth;
1814
1815 if (resize_target_grow(ht, target_size) >= target_size)
1816 return;
1817
1818 __cds_lfht_resize_lazy_launch(ht);
1819}
1820
89bb121d
LJ
1821/*
1822 * We favor grow operations over shrink. A shrink operation never occurs
1823 * if a grow operation is queued for lazy execution. A grow operation
1824 * cancels any pending shrink lazy execution.
1825 */
3171717f 1826static
4105056a 1827void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
3171717f
MD
1828 unsigned long count)
1829{
b8af5011
MD
1830 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
1831 return;
89bb121d
LJ
1832 count = max(count, ht->min_alloc_size);
1833 if (count == size)
1834 return; /* Already the right size, no resize needed */
1835 if (count > size) { /* lazy grow */
1836 if (resize_target_grow(ht, count) >= count)
1837 return;
1838 } else { /* lazy shrink */
1839 for (;;) {
1840 unsigned long s;
1841
1842 s = uatomic_cmpxchg(&ht->t.resize_target, size, count);
1843 if (s == size)
1844 break; /* no resize needed */
1845 if (s > size)
1846 return; /* growing is/(was just) in progress */
1847 if (s <= count)
1848 return; /* some other thread do shrink */
1849 size = s;
1850 }
1851 }
f1f119ee 1852 __cds_lfht_resize_lazy_launch(ht);
3171717f 1853}
This page took 0.129456 seconds and 4 git commands to generate.