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