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