rculfhash: Move key out of struct lfht_test_node, move key_len to caller
[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 */
04db56f8 248 struct cds_lfht_node nodes[0];
1475579c
MD
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;
b8af5011 272 int flags;
5f511391
MD
273 /*
274 * We need to put the work threads offline (QSBR) when taking this
275 * mutex, because we use synchronize_rcu within this mutex critical
276 * section, which waits on read-side critical sections, and could
277 * therefore cause grace-period deadlock if we hold off RCU G.P.
278 * completion.
279 */
464a1ec9 280 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
33c7c748 281 unsigned int in_progress_resize, in_progress_destroy;
14044b37 282 void (*cds_lfht_call_rcu)(struct rcu_head *head,
abc490a1 283 void (*func)(struct rcu_head *head));
1475579c 284 void (*cds_lfht_synchronize_rcu)(void);
01dbfa62
MD
285 void (*cds_lfht_rcu_read_lock)(void);
286 void (*cds_lfht_rcu_read_unlock)(void);
5f511391
MD
287 void (*cds_lfht_rcu_thread_offline)(void);
288 void (*cds_lfht_rcu_thread_online)(void);
b7d619b0
MD
289 void (*cds_lfht_rcu_register_thread)(void);
290 void (*cds_lfht_rcu_unregister_thread)(void);
291 pthread_attr_t *resize_attr; /* Resize threads attributes */
7de5ccfd 292 long count; /* global approximate item count */
4c42f1b8 293 struct ht_items_count *split_count; /* split item count */
2ed95849
MD
294};
295
7f52427b
MD
296/*
297 * rcu_resize_work: Contains arguments passed to RCU worker thread
298 * responsible for performing lazy resize.
299 */
abc490a1
MD
300struct rcu_resize_work {
301 struct rcu_head head;
14044b37 302 struct cds_lfht *ht;
abc490a1 303};
2ed95849 304
7f52427b
MD
305/*
306 * partition_resize_work: Contains arguments passed to worker threads
307 * executing the hash table resize on partitions of the hash table
308 * assigned to each processor's worker thread.
309 */
b7d619b0 310struct partition_resize_work {
1af6e26e 311 pthread_t thread_id;
b7d619b0
MD
312 struct cds_lfht *ht;
313 unsigned long i, start, len;
314 void (*fct)(struct cds_lfht *ht, unsigned long i,
315 unsigned long start, unsigned long len);
316};
317
76a73da8 318static
83beee94 319void _cds_lfht_add(struct cds_lfht *ht,
0422d92c 320 cds_lfht_match_fct match,
04db56f8 321 void *key,
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 758static
04db56f8 759struct cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size,
f4a9cc0b
LJ
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 */
04db56f8 798 iter = rcu_dereference(iter_prev->next);
b4cb483f 799 assert(!is_removed(iter));
04db56f8 800 assert(iter_prev->reverse_hash <= node->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;
04db56f8 811 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
f9c80341 812 return;
04db56f8 813 next = rcu_dereference(clear_flag(iter)->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);
04db56f8 824 (void) uatomic_cmpxchg(&iter_prev->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{
04db56f8 835 struct cds_lfht_node *bucket, *ret_next;
9357c415
MD
836
837 if (!old_node) /* Return -ENOENT if asked to replace NULL node */
7801dadd 838 return -ENOENT;
9357c415
MD
839
840 assert(!is_removed(old_node));
841 assert(!is_dummy(old_node));
842 assert(!is_removed(new_node));
843 assert(!is_dummy(new_node));
844 assert(new_node != old_node);
3fb86f26 845 for (;;) {
9357c415 846 /* Insert after node to be replaced */
9357c415
MD
847 if (is_removed(old_next)) {
848 /*
849 * Too late, the old node has been removed under us
850 * between lookup and replace. Fail.
851 */
7801dadd 852 return -ENOENT;
9357c415
MD
853 }
854 assert(!is_dummy(old_next));
855 assert(new_node != clear_flag(old_next));
04db56f8 856 new_node->next = clear_flag(old_next);
9357c415
MD
857 /*
858 * Here is the whole trick for lock-free replace: we add
859 * the replacement node _after_ the node we want to
860 * replace by atomically setting its next pointer at the
861 * same time we set its removal flag. Given that
862 * the lookups/get next use an iterator aware of the
863 * next pointer, they will either skip the old node due
864 * to the removal flag and see the new node, or use
865 * the old node, but will not see the new one.
866 */
04db56f8 867 ret_next = uatomic_cmpxchg(&old_node->next,
9357c415 868 old_next, flag_removed(new_node));
3fb86f26 869 if (ret_next == old_next)
7801dadd 870 break; /* We performed the replacement. */
3fb86f26
LJ
871 old_next = ret_next;
872 }
9357c415 873
9357c415
MD
874 /*
875 * Ensure that the old node is not visible to readers anymore:
876 * lookup for the node, and remove it (along with any other
877 * logically removed node) if found.
878 */
04db56f8
LJ
879 bucket = lookup_bucket(ht, size, bit_reverse_ulong(old_node->reverse_hash));
880 _cds_lfht_gc_bucket(bucket, new_node);
7801dadd 881
04db56f8 882 assert(is_removed(rcu_dereference(old_node->next)));
7801dadd 883 return 0;
9357c415
MD
884}
885
83beee94
MD
886/*
887 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
888 * mode. A NULL unique_ret allows creation of duplicate keys.
889 */
abc490a1 890static
83beee94 891void _cds_lfht_add(struct cds_lfht *ht,
0422d92c 892 cds_lfht_match_fct match,
04db56f8 893 void *key,
83beee94
MD
894 unsigned long size,
895 struct cds_lfht_node *node,
896 struct cds_lfht_iter *unique_ret,
897 int dummy)
abc490a1 898{
14044b37 899 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
960c9e4f 900 *return_node;
04db56f8 901 struct cds_lfht_node *bucket;
abc490a1 902
c90201ac
MD
903 assert(!is_dummy(node));
904 assert(!is_removed(node));
04db56f8 905 bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash));
abc490a1 906 for (;;) {
adc0de68 907 uint32_t chain_len = 0;
abc490a1 908
11519af6
MD
909 /*
910 * iter_prev points to the non-removed node prior to the
911 * insert location.
11519af6 912 */
04db56f8 913 iter_prev = bucket;
11519af6 914 /* We can always skip the dummy node initially */
04db56f8
LJ
915 iter = rcu_dereference(iter_prev->next);
916 assert(iter_prev->reverse_hash <= node->reverse_hash);
abc490a1 917 for (;;) {
8ed51e04 918 if (caa_unlikely(is_end(iter)))
273399de 919 goto insert;
04db56f8 920 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
273399de 921 goto insert;
238cc06e 922
194fdbd1 923 /* dummy node is the first node of the identical-hash-value chain */
04db56f8 924 if (dummy && clear_flag(iter)->reverse_hash == node->reverse_hash)
194fdbd1 925 goto insert;
238cc06e 926
04db56f8 927 next = rcu_dereference(clear_flag(iter)->next);
8ed51e04 928 if (caa_unlikely(is_removed(next)))
9dba85be 929 goto gc_node;
238cc06e
LJ
930
931 /* uniquely add */
83beee94 932 if (unique_ret
1b81fe1a 933 && !is_dummy(next)
04db56f8 934 && clear_flag(iter)->reverse_hash == node->reverse_hash) {
238cc06e
LJ
935 struct cds_lfht_iter d_iter = { .node = node, .next = iter, };
936
937 /*
938 * uniquely adding inserts the node as the first
939 * node of the identical-hash-value node chain.
940 *
941 * This semantic ensures no duplicated keys
942 * should ever be observable in the table
943 * (including observe one node by one node
944 * by forward iterations)
945 */
04db56f8 946 cds_lfht_next_duplicate(ht, match, key, &d_iter);
238cc06e
LJ
947 if (!d_iter.node)
948 goto insert;
949
950 *unique_ret = d_iter;
83beee94 951 return;
48ed1c18 952 }
238cc06e 953
11519af6 954 /* Only account for identical reverse hash once */
04db56f8 955 if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash
24365af7 956 && !is_dummy(next))
4105056a 957 check_resize(ht, size, ++chain_len);
11519af6 958 iter_prev = clear_flag(iter);
273399de 959 iter = next;
abc490a1 960 }
48ed1c18 961
273399de 962 insert:
7ec59d3b 963 assert(node != clear_flag(iter));
11519af6 964 assert(!is_removed(iter_prev));
c90201ac 965 assert(!is_removed(iter));
f000907d 966 assert(iter_prev != node);
f9c80341 967 if (!dummy)
04db56f8 968 node->next = clear_flag(iter);
f9c80341 969 else
04db56f8 970 node->next = flag_dummy(clear_flag(iter));
f5596c94
MD
971 if (is_dummy(iter))
972 new_node = flag_dummy(node);
973 else
974 new_node = node;
04db56f8 975 if (uatomic_cmpxchg(&iter_prev->next, iter,
48ed1c18 976 new_node) != iter) {
273399de 977 continue; /* retry */
48ed1c18 978 } else {
83beee94 979 return_node = node;
960c9e4f 980 goto end;
48ed1c18
MD
981 }
982
9dba85be
MD
983 gc_node:
984 assert(!is_removed(iter));
f5596c94
MD
985 if (is_dummy(iter))
986 new_next = flag_dummy(clear_flag(next));
987 else
988 new_next = clear_flag(next);
04db56f8 989 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
273399de 990 /* retry */
464a1ec9 991 }
9357c415 992end:
83beee94
MD
993 if (unique_ret) {
994 unique_ret->node = return_node;
995 /* unique_ret->next left unset, never used. */
996 }
abc490a1 997}
464a1ec9 998
abc490a1 999static
860d07e8 1000int _cds_lfht_del(struct cds_lfht *ht, unsigned long size,
4105056a 1001 struct cds_lfht_node *node,
b198f0fd 1002 int dummy_removal)
abc490a1 1003{
04db56f8 1004 struct cds_lfht_node *bucket, *next, *old;
5e28c532 1005
9357c415 1006 if (!node) /* Return -ENOENT if asked to delete NULL node */
743f9143 1007 return -ENOENT;
9357c415 1008
7ec59d3b 1009 /* logically delete the node */
c90201ac
MD
1010 assert(!is_dummy(node));
1011 assert(!is_removed(node));
04db56f8 1012 old = rcu_dereference(node->next);
7ec59d3b 1013 do {
48ed1c18
MD
1014 struct cds_lfht_node *new_next;
1015
7ec59d3b 1016 next = old;
8ed51e04 1017 if (caa_unlikely(is_removed(next)))
743f9143 1018 return -ENOENT;
1475579c
MD
1019 if (dummy_removal)
1020 assert(is_dummy(next));
1021 else
1022 assert(!is_dummy(next));
48ed1c18 1023 new_next = flag_removed(next);
04db56f8 1024 old = uatomic_cmpxchg(&node->next, next, new_next);
7ec59d3b 1025 } while (old != next);
7ec59d3b 1026 /* We performed the (logical) deletion. */
7ec59d3b
MD
1027
1028 /*
1029 * Ensure that the node is not visible to readers anymore: lookup for
273399de
MD
1030 * the node, and remove it (along with any other logically removed node)
1031 * if found.
11519af6 1032 */
04db56f8
LJ
1033 bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash));
1034 _cds_lfht_gc_bucket(bucket, node);
743f9143 1035
04db56f8 1036 assert(is_removed(rcu_dereference(node->next)));
743f9143 1037 return 0;
abc490a1 1038}
2ed95849 1039
b7d619b0
MD
1040static
1041void *partition_resize_thread(void *arg)
1042{
1043 struct partition_resize_work *work = arg;
1044
1045 work->ht->cds_lfht_rcu_register_thread();
1046 work->fct(work->ht, work->i, work->start, work->len);
1047 work->ht->cds_lfht_rcu_unregister_thread();
1048 return NULL;
1049}
1050
1051static
1052void partition_resize_helper(struct cds_lfht *ht, unsigned long i,
1053 unsigned long len,
1054 void (*fct)(struct cds_lfht *ht, unsigned long i,
1055 unsigned long start, unsigned long len))
1056{
1057 unsigned long partition_len;
1058 struct partition_resize_work *work;
6083a889
MD
1059 int thread, ret;
1060 unsigned long nr_threads;
b7d619b0 1061
6083a889
MD
1062 /*
1063 * Note: nr_cpus_mask + 1 is always power of 2.
1064 * We spawn just the number of threads we need to satisfy the minimum
1065 * partition size, up to the number of CPUs in the system.
1066 */
91452a6a
MD
1067 if (nr_cpus_mask > 0) {
1068 nr_threads = min(nr_cpus_mask + 1,
1069 len >> MIN_PARTITION_PER_THREAD_ORDER);
1070 } else {
1071 nr_threads = 1;
1072 }
6083a889
MD
1073 partition_len = len >> get_count_order_ulong(nr_threads);
1074 work = calloc(nr_threads, sizeof(*work));
b7d619b0 1075 assert(work);
6083a889
MD
1076 for (thread = 0; thread < nr_threads; thread++) {
1077 work[thread].ht = ht;
1078 work[thread].i = i;
1079 work[thread].len = partition_len;
1080 work[thread].start = thread * partition_len;
1081 work[thread].fct = fct;
1af6e26e 1082 ret = pthread_create(&(work[thread].thread_id), ht->resize_attr,
6083a889 1083 partition_resize_thread, &work[thread]);
b7d619b0
MD
1084 assert(!ret);
1085 }
6083a889 1086 for (thread = 0; thread < nr_threads; thread++) {
1af6e26e 1087 ret = pthread_join(work[thread].thread_id, NULL);
b7d619b0
MD
1088 assert(!ret);
1089 }
1090 free(work);
b7d619b0
MD
1091}
1092
e8de508e
MD
1093/*
1094 * Holding RCU read lock to protect _cds_lfht_add against memory
1095 * reclaim that could be performed by other call_rcu worker threads (ABA
1096 * problem).
9ee0fc9a 1097 *
b7d619b0 1098 * When we reach a certain length, we can split this population phase over
9ee0fc9a
MD
1099 * many worker threads, based on the number of CPUs available in the system.
1100 * This should therefore take care of not having the expand lagging behind too
1101 * many concurrent insertion threads by using the scheduler's ability to
1102 * schedule dummy node population fairly with insertions.
e8de508e 1103 */
4105056a 1104static
b7d619b0
MD
1105void init_table_populate_partition(struct cds_lfht *ht, unsigned long i,
1106 unsigned long start, unsigned long len)
4105056a
MD
1107{
1108 unsigned long j;
1109
5488222b 1110 assert(i > ht->min_alloc_order);
4105056a 1111 ht->cds_lfht_rcu_read_lock();
b7d619b0 1112 for (j = start; j < start + len; j++) {
04db56f8 1113 struct cds_lfht_node *new_node = &ht->t.tbl[i]->nodes[j];
4105056a 1114
dc1da8f6 1115 dbg_printf("init populate: i %lu j %lu hash %lu\n",
4f6e90b7 1116 i, j, (1UL << (i - 1)) + j);
04db56f8 1117 new_node->reverse_hash =
4f6e90b7 1118 bit_reverse_ulong((1UL << (i - 1)) + j);
04db56f8 1119 _cds_lfht_add(ht, NULL, NULL, 1UL << (i - 1),
83beee94 1120 new_node, NULL, 1);
4105056a
MD
1121 }
1122 ht->cds_lfht_rcu_read_unlock();
b7d619b0
MD
1123}
1124
1125static
1126void init_table_populate(struct cds_lfht *ht, unsigned long i,
1127 unsigned long len)
1128{
1129 assert(nr_cpus_mask != -1);
6083a889 1130 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
b7d619b0
MD
1131 ht->cds_lfht_rcu_thread_online();
1132 init_table_populate_partition(ht, i, 0, len);
1133 ht->cds_lfht_rcu_thread_offline();
1134 return;
1135 }
1136 partition_resize_helper(ht, i, len, init_table_populate_partition);
4105056a
MD
1137}
1138
abc490a1 1139static
4105056a 1140void init_table(struct cds_lfht *ht,
93d46c39 1141 unsigned long first_order, unsigned long last_order)
24365af7 1142{
93d46c39 1143 unsigned long i;
24365af7 1144
93d46c39
LJ
1145 dbg_printf("init table: first_order %lu last_order %lu\n",
1146 first_order, last_order);
5488222b 1147 assert(first_order > ht->min_alloc_order);
93d46c39 1148 for (i = first_order; i <= last_order; i++) {
4105056a 1149 unsigned long len;
24365af7 1150
4f6e90b7 1151 len = 1UL << (i - 1);
f0c29ed7 1152 dbg_printf("init order %lu len: %lu\n", i, len);
4d676753
MD
1153
1154 /* Stop expand if the resize target changes under us */
4f6e90b7 1155 if (CMM_LOAD_SHARED(ht->t.resize_target) < (1UL << i))
4d676753
MD
1156 break;
1157
04db56f8 1158 ht->t.tbl[i] = calloc(1, len * sizeof(struct cds_lfht_node));
b7d619b0 1159 assert(ht->t.tbl[i]);
4105056a 1160
4105056a 1161 /*
dc1da8f6
MD
1162 * Set all dummy nodes reverse hash values for a level and
1163 * link all dummy nodes into the table.
4105056a 1164 */
dc1da8f6 1165 init_table_populate(ht, i, len);
4105056a 1166
f9c80341
MD
1167 /*
1168 * Update table size.
1169 */
1170 cmm_smp_wmb(); /* populate data before RCU size */
4f6e90b7 1171 CMM_STORE_SHARED(ht->t.size, 1UL << i);
f9c80341 1172
4f6e90b7 1173 dbg_printf("init new size: %lu\n", 1UL << i);
4105056a
MD
1174 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1175 break;
1176 }
1177}
1178
e8de508e
MD
1179/*
1180 * Holding RCU read lock to protect _cds_lfht_remove against memory
1181 * reclaim that could be performed by other call_rcu worker threads (ABA
1182 * problem).
1183 * For a single level, we logically remove and garbage collect each node.
1184 *
1185 * As a design choice, we perform logical removal and garbage collection on a
1186 * node-per-node basis to simplify this algorithm. We also assume keeping good
1187 * cache locality of the operation would overweight possible performance gain
1188 * that could be achieved by batching garbage collection for multiple levels.
1189 * However, this would have to be justified by benchmarks.
1190 *
1191 * Concurrent removal and add operations are helping us perform garbage
1192 * collection of logically removed nodes. We guarantee that all logically
1193 * removed nodes have been garbage-collected (unlinked) before call_rcu is
1194 * invoked to free a hole level of dummy nodes (after a grace period).
1195 *
1196 * Logical removal and garbage collection can therefore be done in batch or on a
1197 * node-per-node basis, as long as the guarantee above holds.
9ee0fc9a 1198 *
b7d619b0
MD
1199 * When we reach a certain length, we can split this removal over many worker
1200 * threads, based on the number of CPUs available in the system. This should
1201 * take care of not letting resize process lag behind too many concurrent
9ee0fc9a 1202 * updater threads actively inserting into the hash table.
e8de508e 1203 */
4105056a 1204static
b7d619b0
MD
1205void remove_table_partition(struct cds_lfht *ht, unsigned long i,
1206 unsigned long start, unsigned long len)
4105056a
MD
1207{
1208 unsigned long j;
1209
5488222b 1210 assert(i > ht->min_alloc_order);
4105056a 1211 ht->cds_lfht_rcu_read_lock();
b7d619b0 1212 for (j = start; j < start + len; j++) {
04db56f8 1213 struct cds_lfht_node *fini_node = &ht->t.tbl[i]->nodes[j];
4105056a
MD
1214
1215 dbg_printf("remove entry: i %lu j %lu hash %lu\n",
4f6e90b7 1216 i, j, (1UL << (i - 1)) + j);
04db56f8 1217 fini_node->reverse_hash =
4f6e90b7
LJ
1218 bit_reverse_ulong((1UL << (i - 1)) + j);
1219 (void) _cds_lfht_del(ht, 1UL << (i - 1), fini_node, 1);
abc490a1 1220 }
4105056a 1221 ht->cds_lfht_rcu_read_unlock();
b7d619b0
MD
1222}
1223
1224static
1225void remove_table(struct cds_lfht *ht, unsigned long i, unsigned long len)
1226{
1227
1228 assert(nr_cpus_mask != -1);
6083a889 1229 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
b7d619b0
MD
1230 ht->cds_lfht_rcu_thread_online();
1231 remove_table_partition(ht, i, 0, len);
1232 ht->cds_lfht_rcu_thread_offline();
1233 return;
1234 }
1235 partition_resize_helper(ht, i, len, remove_table_partition);
2ed95849
MD
1236}
1237
1475579c 1238static
4105056a 1239void fini_table(struct cds_lfht *ht,
93d46c39 1240 unsigned long first_order, unsigned long last_order)
1475579c 1241{
93d46c39 1242 long i;
0d14ceb2 1243 void *free_by_rcu = NULL;
1475579c 1244
93d46c39
LJ
1245 dbg_printf("fini table: first_order %lu last_order %lu\n",
1246 first_order, last_order);
5488222b 1247 assert(first_order > ht->min_alloc_order);
93d46c39 1248 for (i = last_order; i >= first_order; i--) {
4105056a 1249 unsigned long len;
1475579c 1250
4f6e90b7 1251 len = 1UL << (i - 1);
1475579c 1252 dbg_printf("fini order %lu len: %lu\n", i, len);
4105056a 1253
4d676753
MD
1254 /* Stop shrink if the resize target changes under us */
1255 if (CMM_LOAD_SHARED(ht->t.resize_target) > (1UL << (i - 1)))
1256 break;
1257
1258 cmm_smp_wmb(); /* populate data before RCU size */
1259 CMM_STORE_SHARED(ht->t.size, 1UL << (i - 1));
1260
1261 /*
1262 * We need to wait for all add operations to reach Q.S. (and
1263 * thus use the new table for lookups) before we can start
1264 * releasing the old dummy nodes. Otherwise their lookup will
1265 * return a logically removed node as insert position.
1266 */
1267 ht->cds_lfht_synchronize_rcu();
0d14ceb2
LJ
1268 if (free_by_rcu)
1269 free(free_by_rcu);
4d676753 1270
21263e21 1271 /*
4105056a
MD
1272 * Set "removed" flag in dummy nodes about to be removed.
1273 * Unlink all now-logically-removed dummy node pointers.
1274 * Concurrent add/remove operation are helping us doing
1275 * the gc.
21263e21 1276 */
4105056a
MD
1277 remove_table(ht, i, len);
1278
0d14ceb2 1279 free_by_rcu = ht->t.tbl[i];
4105056a
MD
1280
1281 dbg_printf("fini new size: %lu\n", 1UL << i);
1475579c
MD
1282 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1283 break;
1284 }
0d14ceb2
LJ
1285
1286 if (free_by_rcu) {
1287 ht->cds_lfht_synchronize_rcu();
1288 free(free_by_rcu);
1289 }
1475579c
MD
1290}
1291
ff0d69de
LJ
1292static
1293void cds_lfht_create_dummy(struct cds_lfht *ht, unsigned long size)
1294{
04db56f8 1295 struct cds_lfht_node *prev, *node;
ff0d69de
LJ
1296 unsigned long order, len, i, j;
1297
04db56f8 1298 ht->t.tbl[0] = calloc(1, ht->min_alloc_size * sizeof(struct cds_lfht_node));
ff0d69de
LJ
1299 assert(ht->t.tbl[0]);
1300
1301 dbg_printf("create dummy: order %lu index %lu hash %lu\n", 0, 0, 0);
1302 ht->t.tbl[0]->nodes[0].next = flag_dummy(get_end());
1303 ht->t.tbl[0]->nodes[0].reverse_hash = 0;
1304
1305 for (order = 1; order < get_count_order_ulong(size) + 1; order++) {
1306 len = 1UL << (order - 1);
5488222b 1307 if (order <= ht->min_alloc_order) {
eb631bf2 1308 ht->t.tbl[order] = (struct rcu_level *) (ht->t.tbl[0]->nodes + len);
5488222b 1309 } else {
04db56f8 1310 ht->t.tbl[order] = calloc(1, len * sizeof(struct cds_lfht_node));
5488222b
LJ
1311 assert(ht->t.tbl[order]);
1312 }
ff0d69de
LJ
1313
1314 i = 0;
1315 prev = ht->t.tbl[i]->nodes;
1316 for (j = 0; j < len; j++) {
1317 if (j & (j - 1)) { /* Between power of 2 */
1318 prev++;
1319 } else if (j) { /* At each power of 2 */
1320 i++;
1321 prev = ht->t.tbl[i]->nodes;
1322 }
1323
1324 node = &ht->t.tbl[order]->nodes[j];
1325 dbg_printf("create dummy: order %lu index %lu hash %lu\n",
1326 order, j, j + len);
1327 node->next = prev->next;
1328 assert(is_dummy(node->next));
1329 node->reverse_hash = bit_reverse_ulong(j + len);
04db56f8 1330 prev->next = flag_dummy(node);
ff0d69de
LJ
1331 }
1332 }
1333}
1334
0422d92c 1335struct cds_lfht *_cds_lfht_new(unsigned long init_size,
5488222b 1336 unsigned long min_alloc_size,
b8af5011 1337 int flags,
14044b37 1338 void (*cds_lfht_call_rcu)(struct rcu_head *head,
1475579c 1339 void (*func)(struct rcu_head *head)),
01dbfa62
MD
1340 void (*cds_lfht_synchronize_rcu)(void),
1341 void (*cds_lfht_rcu_read_lock)(void),
5f511391
MD
1342 void (*cds_lfht_rcu_read_unlock)(void),
1343 void (*cds_lfht_rcu_thread_offline)(void),
b7d619b0
MD
1344 void (*cds_lfht_rcu_thread_online)(void),
1345 void (*cds_lfht_rcu_register_thread)(void),
1346 void (*cds_lfht_rcu_unregister_thread)(void),
1347 pthread_attr_t *attr)
abc490a1 1348{
14044b37 1349 struct cds_lfht *ht;
24365af7 1350 unsigned long order;
abc490a1 1351
5488222b
LJ
1352 /* min_alloc_size must be power of two */
1353 if (!min_alloc_size || (min_alloc_size & (min_alloc_size - 1)))
1354 return NULL;
8129be4e 1355 /* init_size must be power of two */
5488222b 1356 if (!init_size || (init_size & (init_size - 1)))
8129be4e 1357 return NULL;
5488222b
LJ
1358 min_alloc_size = max(min_alloc_size, MIN_TABLE_SIZE);
1359 init_size = max(init_size, min_alloc_size);
14044b37 1360 ht = calloc(1, sizeof(struct cds_lfht));
b7d619b0 1361 assert(ht);
b5d6b20f 1362 ht->flags = flags;
14044b37 1363 ht->cds_lfht_call_rcu = cds_lfht_call_rcu;
1475579c 1364 ht->cds_lfht_synchronize_rcu = cds_lfht_synchronize_rcu;
01dbfa62
MD
1365 ht->cds_lfht_rcu_read_lock = cds_lfht_rcu_read_lock;
1366 ht->cds_lfht_rcu_read_unlock = cds_lfht_rcu_read_unlock;
5f511391
MD
1367 ht->cds_lfht_rcu_thread_offline = cds_lfht_rcu_thread_offline;
1368 ht->cds_lfht_rcu_thread_online = cds_lfht_rcu_thread_online;
b7d619b0
MD
1369 ht->cds_lfht_rcu_register_thread = cds_lfht_rcu_register_thread;
1370 ht->cds_lfht_rcu_unregister_thread = cds_lfht_rcu_unregister_thread;
1371 ht->resize_attr = attr;
5afadd12 1372 alloc_split_items_count(ht);
abc490a1
MD
1373 /* this mutex should not nest in read-side C.S. */
1374 pthread_mutex_init(&ht->resize_mutex, NULL);
5488222b 1375 order = get_count_order_ulong(init_size);
93d46c39 1376 ht->t.resize_target = 1UL << order;
5488222b
LJ
1377 ht->min_alloc_size = min_alloc_size;
1378 ht->min_alloc_order = get_count_order_ulong(min_alloc_size);
bcbd36fc
LJ
1379 cds_lfht_create_dummy(ht, 1UL << order);
1380 ht->t.size = 1UL << order;
abc490a1
MD
1381 return ht;
1382}
1383
0422d92c
MD
1384void cds_lfht_lookup(struct cds_lfht *ht, cds_lfht_match_fct match,
1385 unsigned long hash, void *key, struct cds_lfht_iter *iter)
2ed95849 1386{
04db56f8 1387 struct cds_lfht_node *node, *next, *bucket;
0422d92c 1388 unsigned long reverse_hash, size;
2ed95849 1389
abc490a1 1390 reverse_hash = bit_reverse_ulong(hash);
464a1ec9 1391
4105056a 1392 size = rcu_dereference(ht->t.size);
04db56f8 1393 bucket = lookup_bucket(ht, size, hash);
bb7b2f26 1394 /* We can always skip the dummy node initially */
04db56f8 1395 node = rcu_dereference(bucket->next);
bb7b2f26 1396 node = clear_flag(node);
2ed95849 1397 for (;;) {
8ed51e04 1398 if (caa_unlikely(is_end(node))) {
96ad1112 1399 node = next = NULL;
abc490a1 1400 break;
bb7b2f26 1401 }
04db56f8 1402 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
96ad1112 1403 node = next = NULL;
abc490a1 1404 break;
2ed95849 1405 }
04db56f8 1406 next = rcu_dereference(node->next);
7f52427b 1407 assert(node == clear_flag(node));
8ed51e04 1408 if (caa_likely(!is_removed(next))
1b81fe1a 1409 && !is_dummy(next)
04db56f8 1410 && node->reverse_hash == reverse_hash
0422d92c 1411 && caa_likely(match(node, key))) {
273399de 1412 break;
2ed95849 1413 }
1b81fe1a 1414 node = clear_flag(next);
2ed95849 1415 }
04db56f8 1416 assert(!node || !is_dummy(rcu_dereference(node->next)));
adc0de68
MD
1417 iter->node = node;
1418 iter->next = next;
abc490a1 1419}
e0ba718a 1420
0422d92c 1421void cds_lfht_next_duplicate(struct cds_lfht *ht, cds_lfht_match_fct match,
04db56f8 1422 void *key, struct cds_lfht_iter *iter)
a481e5ff 1423{
adc0de68 1424 struct cds_lfht_node *node, *next;
a481e5ff 1425 unsigned long reverse_hash;
a481e5ff 1426
adc0de68 1427 node = iter->node;
04db56f8 1428 reverse_hash = node->reverse_hash;
adc0de68 1429 next = iter->next;
a481e5ff
MD
1430 node = clear_flag(next);
1431
1432 for (;;) {
8ed51e04 1433 if (caa_unlikely(is_end(node))) {
96ad1112 1434 node = next = NULL;
a481e5ff 1435 break;
bb7b2f26 1436 }
04db56f8 1437 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
96ad1112 1438 node = next = NULL;
a481e5ff
MD
1439 break;
1440 }
04db56f8 1441 next = rcu_dereference(node->next);
8ed51e04 1442 if (caa_likely(!is_removed(next))
a481e5ff 1443 && !is_dummy(next)
04db56f8 1444 && caa_likely(match(node, key))) {
a481e5ff
MD
1445 break;
1446 }
1447 node = clear_flag(next);
1448 }
04db56f8 1449 assert(!node || !is_dummy(rcu_dereference(node->next)));
adc0de68
MD
1450 iter->node = node;
1451 iter->next = next;
a481e5ff
MD
1452}
1453
4e9b9fbf
MD
1454void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1455{
1456 struct cds_lfht_node *node, *next;
1457
853395e1 1458 node = clear_flag(iter->next);
4e9b9fbf 1459 for (;;) {
8ed51e04 1460 if (caa_unlikely(is_end(node))) {
4e9b9fbf
MD
1461 node = next = NULL;
1462 break;
1463 }
04db56f8 1464 next = rcu_dereference(node->next);
8ed51e04 1465 if (caa_likely(!is_removed(next))
4e9b9fbf
MD
1466 && !is_dummy(next)) {
1467 break;
1468 }
1469 node = clear_flag(next);
1470 }
04db56f8 1471 assert(!node || !is_dummy(rcu_dereference(node->next)));
4e9b9fbf
MD
1472 iter->node = node;
1473 iter->next = next;
1474}
1475
1476void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1477{
04db56f8 1478 struct cds_lfht_node *lookup;
4e9b9fbf
MD
1479
1480 /*
1481 * Get next after first dummy node. The first dummy node is the
1482 * first node of the linked list.
1483 */
1484 lookup = &ht->t.tbl[0]->nodes[0];
853395e1 1485 iter->next = lookup->next;
4e9b9fbf
MD
1486 cds_lfht_next(ht, iter);
1487}
1488
0422d92c
MD
1489void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
1490 struct cds_lfht_node *node)
abc490a1 1491{
0422d92c 1492 unsigned long size;
ab7d5fc6 1493
04db56f8 1494 node->reverse_hash = bit_reverse_ulong((unsigned long) hash);
4105056a 1495 size = rcu_dereference(ht->t.size);
04db56f8 1496 _cds_lfht_add(ht, NULL, NULL, size, node, NULL, 0);
14360f1c 1497 ht_count_add(ht, size, hash);
3eca1b8c
MD
1498}
1499
14044b37 1500struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
0422d92c 1501 cds_lfht_match_fct match,
04db56f8 1502 void *key,
0422d92c 1503 unsigned long hash,
48ed1c18 1504 struct cds_lfht_node *node)
3eca1b8c 1505{
0422d92c 1506 unsigned long size;
83beee94 1507 struct cds_lfht_iter iter;
3eca1b8c 1508
04db56f8 1509 node->reverse_hash = bit_reverse_ulong((unsigned long) hash);
4105056a 1510 size = rcu_dereference(ht->t.size);
04db56f8 1511 _cds_lfht_add(ht, match, key, size, node, &iter, 0);
83beee94 1512 if (iter.node == node)
14360f1c 1513 ht_count_add(ht, size, hash);
83beee94 1514 return iter.node;
2ed95849
MD
1515}
1516
9357c415 1517struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
0422d92c 1518 cds_lfht_match_fct match,
04db56f8 1519 void *key,
0422d92c 1520 unsigned long hash,
48ed1c18
MD
1521 struct cds_lfht_node *node)
1522{
0422d92c 1523 unsigned long size;
83beee94 1524 struct cds_lfht_iter iter;
48ed1c18 1525
04db56f8 1526 node->reverse_hash = bit_reverse_ulong((unsigned long) hash);
48ed1c18 1527 size = rcu_dereference(ht->t.size);
83beee94 1528 for (;;) {
04db56f8 1529 _cds_lfht_add(ht, match, key, size, node, &iter, 0);
83beee94 1530 if (iter.node == node) {
14360f1c 1531 ht_count_add(ht, size, hash);
83beee94
MD
1532 return NULL;
1533 }
1534
1535 if (!_cds_lfht_replace(ht, size, iter.node, iter.next, node))
1536 return iter.node;
1537 }
48ed1c18
MD
1538}
1539
9357c415
MD
1540int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter,
1541 struct cds_lfht_node *new_node)
1542{
1543 unsigned long size;
1544
1545 size = rcu_dereference(ht->t.size);
1546 return _cds_lfht_replace(ht, size, old_iter->node, old_iter->next,
1547 new_node);
1548}
1549
1550int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter)
2ed95849 1551{
14360f1c 1552 unsigned long size, hash;
df44348d 1553 int ret;
abc490a1 1554
4105056a 1555 size = rcu_dereference(ht->t.size);
9357c415 1556 ret = _cds_lfht_del(ht, size, iter->node, 0);
14360f1c 1557 if (!ret) {
04db56f8 1558 hash = bit_reverse_ulong(iter->node->reverse_hash);
14360f1c
LJ
1559 ht_count_del(ht, size, hash);
1560 }
df44348d 1561 return ret;
2ed95849 1562}
ab7d5fc6 1563
abc490a1 1564static
14044b37 1565int cds_lfht_delete_dummy(struct cds_lfht *ht)
674f7a69 1566{
14044b37 1567 struct cds_lfht_node *node;
4105056a 1568 unsigned long order, i, size;
674f7a69 1569
abc490a1 1570 /* Check that the table is empty */
04db56f8 1571 node = &ht->t.tbl[0]->nodes[0];
abc490a1 1572 do {
04db56f8 1573 node = clear_flag(node)->next;
1b81fe1a 1574 if (!is_dummy(node))
abc490a1 1575 return -EPERM;
273399de 1576 assert(!is_removed(node));
bb7b2f26 1577 } while (!is_end(node));
4105056a
MD
1578 /*
1579 * size accessed without rcu_dereference because hash table is
1580 * being destroyed.
1581 */
1582 size = ht->t.size;
abc490a1 1583 /* Internal sanity check: all nodes left should be dummy */
4105056a 1584 for (order = 0; order < get_count_order_ulong(size) + 1; order++) {
24365af7
MD
1585 unsigned long len;
1586
1587 len = !order ? 1 : 1UL << (order - 1);
1588 for (i = 0; i < len; i++) {
f0c29ed7 1589 dbg_printf("delete order %lu i %lu hash %lu\n",
24365af7 1590 order, i,
4105056a
MD
1591 bit_reverse_ulong(ht->t.tbl[order]->nodes[i].reverse_hash));
1592 assert(is_dummy(ht->t.tbl[order]->nodes[i].next));
24365af7 1593 }
5488222b
LJ
1594
1595 if (order == ht->min_alloc_order)
1596 poison_free(ht->t.tbl[0]);
1597 else if (order > ht->min_alloc_order)
1598 poison_free(ht->t.tbl[order]);
1599 /* Nothing to delete for order < ht->min_alloc_order */
674f7a69 1600 }
abc490a1 1601 return 0;
674f7a69
MD
1602}
1603
1604/*
1605 * Should only be called when no more concurrent readers nor writers can
1606 * possibly access the table.
1607 */
b7d619b0 1608int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr)
674f7a69 1609{
5e28c532
MD
1610 int ret;
1611
848d4088 1612 /* Wait for in-flight resize operations to complete */
24953e08
MD
1613 _CMM_STORE_SHARED(ht->in_progress_destroy, 1);
1614 cmm_smp_mb(); /* Store destroy before load resize */
848d4088
MD
1615 while (uatomic_read(&ht->in_progress_resize))
1616 poll(NULL, 0, 100); /* wait for 100ms */
14044b37 1617 ret = cds_lfht_delete_dummy(ht);
abc490a1
MD
1618 if (ret)
1619 return ret;
5afadd12 1620 free_split_items_count(ht);
b7d619b0
MD
1621 if (attr)
1622 *attr = ht->resize_attr;
98808fb1 1623 poison_free(ht);
5e28c532 1624 return ret;
674f7a69
MD
1625}
1626
14044b37 1627void cds_lfht_count_nodes(struct cds_lfht *ht,
d933dd0e 1628 long *approx_before,
273399de 1629 unsigned long *count,
973e5e1b 1630 unsigned long *removed,
d933dd0e 1631 long *approx_after)
273399de 1632{
14044b37 1633 struct cds_lfht_node *node, *next;
24365af7 1634 unsigned long nr_dummy = 0;
273399de 1635
7ed7682f 1636 *approx_before = 0;
5afadd12 1637 if (ht->split_count) {
973e5e1b
MD
1638 int i;
1639
4c42f1b8
LJ
1640 for (i = 0; i < split_count_mask + 1; i++) {
1641 *approx_before += uatomic_read(&ht->split_count[i].add);
1642 *approx_before -= uatomic_read(&ht->split_count[i].del);
973e5e1b
MD
1643 }
1644 }
1645
273399de
MD
1646 *count = 0;
1647 *removed = 0;
1648
24365af7 1649 /* Count non-dummy nodes in the table */
04db56f8 1650 node = &ht->t.tbl[0]->nodes[0];
273399de 1651 do {
04db56f8 1652 next = rcu_dereference(node->next);
b198f0fd 1653 if (is_removed(next)) {
973e5e1b
MD
1654 if (!is_dummy(next))
1655 (*removed)++;
1656 else
1657 (nr_dummy)++;
1b81fe1a 1658 } else if (!is_dummy(next))
273399de 1659 (*count)++;
24365af7
MD
1660 else
1661 (nr_dummy)++;
273399de 1662 node = clear_flag(next);
bb7b2f26 1663 } while (!is_end(node));
f0c29ed7 1664 dbg_printf("number of dummy nodes: %lu\n", nr_dummy);
7ed7682f 1665 *approx_after = 0;
5afadd12 1666 if (ht->split_count) {
973e5e1b
MD
1667 int i;
1668
4c42f1b8
LJ
1669 for (i = 0; i < split_count_mask + 1; i++) {
1670 *approx_after += uatomic_read(&ht->split_count[i].add);
1671 *approx_after -= uatomic_read(&ht->split_count[i].del);
973e5e1b
MD
1672 }
1673 }
273399de
MD
1674}
1675
1475579c 1676/* called with resize mutex held */
abc490a1 1677static
4105056a 1678void _do_cds_lfht_grow(struct cds_lfht *ht,
1475579c 1679 unsigned long old_size, unsigned long new_size)
abc490a1 1680{
1475579c 1681 unsigned long old_order, new_order;
1475579c 1682
93d46c39
LJ
1683 old_order = get_count_order_ulong(old_size);
1684 new_order = get_count_order_ulong(new_size);
1a401918
LJ
1685 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1686 old_size, old_order, new_size, new_order);
1475579c 1687 assert(new_size > old_size);
93d46c39 1688 init_table(ht, old_order + 1, new_order);
abc490a1
MD
1689}
1690
1691/* called with resize mutex held */
1692static
4105056a 1693void _do_cds_lfht_shrink(struct cds_lfht *ht,
1475579c 1694 unsigned long old_size, unsigned long new_size)
464a1ec9 1695{
1475579c 1696 unsigned long old_order, new_order;
464a1ec9 1697
5488222b 1698 new_size = max(new_size, ht->min_alloc_size);
93d46c39
LJ
1699 old_order = get_count_order_ulong(old_size);
1700 new_order = get_count_order_ulong(new_size);
1a401918
LJ
1701 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1702 old_size, old_order, new_size, new_order);
1475579c 1703 assert(new_size < old_size);
1475579c 1704
4105056a 1705 /* Remove and unlink all dummy nodes to remove. */
93d46c39 1706 fini_table(ht, new_order + 1, old_order);
464a1ec9
MD
1707}
1708
1475579c
MD
1709
1710/* called with resize mutex held */
1711static
1712void _do_cds_lfht_resize(struct cds_lfht *ht)
1713{
1714 unsigned long new_size, old_size;
4105056a
MD
1715
1716 /*
1717 * Resize table, re-do if the target size has changed under us.
1718 */
1719 do {
d2be3620
MD
1720 assert(uatomic_read(&ht->in_progress_resize));
1721 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1722 break;
4105056a
MD
1723 ht->t.resize_initiated = 1;
1724 old_size = ht->t.size;
1725 new_size = CMM_LOAD_SHARED(ht->t.resize_target);
1726 if (old_size < new_size)
1727 _do_cds_lfht_grow(ht, old_size, new_size);
1728 else if (old_size > new_size)
1729 _do_cds_lfht_shrink(ht, old_size, new_size);
1730 ht->t.resize_initiated = 0;
1731 /* write resize_initiated before read resize_target */
1732 cmm_smp_mb();
4d676753 1733 } while (ht->t.size != CMM_LOAD_SHARED(ht->t.resize_target));
1475579c
MD
1734}
1735
abc490a1 1736static
ab65b890 1737unsigned long resize_target_grow(struct cds_lfht *ht, unsigned long new_size)
464a1ec9 1738{
ab65b890 1739 return _uatomic_xchg_monotonic_increase(&ht->t.resize_target, new_size);
464a1ec9
MD
1740}
1741
1475579c 1742static
4105056a 1743void resize_target_update_count(struct cds_lfht *ht,
b8af5011 1744 unsigned long count)
1475579c 1745{
5488222b 1746 count = max(count, ht->min_alloc_size);
4105056a 1747 uatomic_set(&ht->t.resize_target, count);
1475579c
MD
1748}
1749
1750void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size)
464a1ec9 1751{
4105056a
MD
1752 resize_target_update_count(ht, new_size);
1753 CMM_STORE_SHARED(ht->t.resize_initiated, 1);
5f511391 1754 ht->cds_lfht_rcu_thread_offline();
1475579c
MD
1755 pthread_mutex_lock(&ht->resize_mutex);
1756 _do_cds_lfht_resize(ht);
1757 pthread_mutex_unlock(&ht->resize_mutex);
5f511391 1758 ht->cds_lfht_rcu_thread_online();
abc490a1 1759}
464a1ec9 1760
abc490a1
MD
1761static
1762void do_resize_cb(struct rcu_head *head)
1763{
1764 struct rcu_resize_work *work =
1765 caa_container_of(head, struct rcu_resize_work, head);
14044b37 1766 struct cds_lfht *ht = work->ht;
abc490a1 1767
5f511391 1768 ht->cds_lfht_rcu_thread_offline();
abc490a1 1769 pthread_mutex_lock(&ht->resize_mutex);
14044b37 1770 _do_cds_lfht_resize(ht);
abc490a1 1771 pthread_mutex_unlock(&ht->resize_mutex);
5f511391 1772 ht->cds_lfht_rcu_thread_online();
98808fb1 1773 poison_free(work);
848d4088
MD
1774 cmm_smp_mb(); /* finish resize before decrement */
1775 uatomic_dec(&ht->in_progress_resize);
464a1ec9
MD
1776}
1777
abc490a1 1778static
f1f119ee 1779void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht)
ab7d5fc6 1780{
abc490a1
MD
1781 struct rcu_resize_work *work;
1782
4105056a
MD
1783 /* Store resize_target before read resize_initiated */
1784 cmm_smp_mb();
ab65b890 1785 if (!CMM_LOAD_SHARED(ht->t.resize_initiated)) {
848d4088 1786 uatomic_inc(&ht->in_progress_resize);
59290e9d 1787 cmm_smp_mb(); /* increment resize count before load destroy */
ed35e6d8
MD
1788 if (CMM_LOAD_SHARED(ht->in_progress_destroy)) {
1789 uatomic_dec(&ht->in_progress_resize);
59290e9d 1790 return;
ed35e6d8 1791 }
f9830efd
MD
1792 work = malloc(sizeof(*work));
1793 work->ht = ht;
14044b37 1794 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
4105056a 1795 CMM_STORE_SHARED(ht->t.resize_initiated, 1);
f9830efd 1796 }
ab7d5fc6 1797}
3171717f 1798
f1f119ee
LJ
1799static
1800void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth)
1801{
1802 unsigned long target_size = size << growth;
1803
1804 if (resize_target_grow(ht, target_size) >= target_size)
1805 return;
1806
1807 __cds_lfht_resize_lazy_launch(ht);
1808}
1809
89bb121d
LJ
1810/*
1811 * We favor grow operations over shrink. A shrink operation never occurs
1812 * if a grow operation is queued for lazy execution. A grow operation
1813 * cancels any pending shrink lazy execution.
1814 */
3171717f 1815static
4105056a 1816void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
3171717f
MD
1817 unsigned long count)
1818{
b8af5011
MD
1819 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
1820 return;
89bb121d
LJ
1821 count = max(count, ht->min_alloc_size);
1822 if (count == size)
1823 return; /* Already the right size, no resize needed */
1824 if (count > size) { /* lazy grow */
1825 if (resize_target_grow(ht, count) >= count)
1826 return;
1827 } else { /* lazy shrink */
1828 for (;;) {
1829 unsigned long s;
1830
1831 s = uatomic_cmpxchg(&ht->t.resize_target, size, count);
1832 if (s == size)
1833 break; /* no resize needed */
1834 if (s > size)
1835 return; /* growing is/(was just) in progress */
1836 if (s <= count)
1837 return; /* some other thread do shrink */
1838 size = s;
1839 }
1840 }
f1f119ee 1841 __cds_lfht_resize_lazy_launch(ht);
3171717f 1842}
This page took 0.129009 seconds and 4 git commands to generate.