call_rcu: register work threads as rcu readers
[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.
56 * - Per-CPU Split-counters are used to keep track of the number of
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
94 * this order.
95 * - call_rcu is used to garbage-collect the old order table.
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.
1475579c
MD
99 *
100 * A bit of ascii art explanation:
101 *
102 * Order index is the off-by-one compare to the actual power of 2 because
103 * we use index 0 to deal with the 0 special-case.
104 *
105 * This shows the nodes for a small table ordered by reversed bits:
106 *
107 * bits reverse
108 * 0 000 000
109 * 4 100 001
110 * 2 010 010
111 * 6 110 011
112 * 1 001 100
113 * 5 101 101
114 * 3 011 110
115 * 7 111 111
116 *
117 * This shows the nodes in order of non-reversed bits, linked by
118 * reversed-bit order.
119 *
120 * order bits reverse
121 * 0 0 000 000
122 * |
f6fdd688
MD
123 * 1 | 1 001 100 <- <-
124 * | | | |
125 * 2 | | 2 010 010 | |
126 * | | | 3 011 110 | <- |
127 * | | | | | | |
1475579c
MD
128 * 3 -> | | | 4 100 001 | |
129 * -> | | 5 101 101 |
130 * -> | 6 110 011
131 * -> 7 111 111
e753ff5a
MD
132 */
133
2ed95849
MD
134#define _LGPL_SOURCE
135#include <stdlib.h>
e0ba718a
MD
136#include <errno.h>
137#include <assert.h>
138#include <stdio.h>
abc490a1 139#include <stdint.h>
f000907d 140#include <string.h>
e0ba718a 141
df44348d 142#include "config.h"
2ed95849 143#include <urcu.h>
abc490a1 144#include <urcu-call-rcu.h>
a42cc659
MD
145#include <urcu/arch.h>
146#include <urcu/uatomic.h>
674f7a69 147#include <urcu/jhash.h>
a42cc659 148#include <urcu/compiler.h>
abc490a1 149#include <urcu/rculfhash.h>
5e28c532 150#include <stdio.h>
464a1ec9 151#include <pthread.h>
44395fb7 152
f9830efd 153#ifdef DEBUG
f0c29ed7 154#define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
f9830efd 155#else
e753ff5a 156#define dbg_printf(fmt, args...)
f9830efd
MD
157#endif
158
98808fb1
MD
159/* For testing */
160#define POISON_FREE
161
f8994aee
MD
162/*
163 * Per-CPU split-counters lazily update the global counter each 1024
164 * addition/removal. It automatically keeps track of resize required.
165 * We use the bucket length as indicator for need to expand for small
166 * tables and machines lacking per-cpu data suppport.
167 */
168#define COUNT_COMMIT_ORDER 10
6ea6bc67
MD
169#define CHAIN_LEN_TARGET 1
170#define CHAIN_LEN_RESIZE_THRESHOLD 3
2ed95849 171
cd95516d
MD
172/*
173 * Define the minimum table size. Protects against hash table resize overload
174 * when too many entries are added quickly before the resize can complete.
175 * This is especially the case if the table could be shrinked to a size of 1.
176 * TODO: we might want to make the add/remove operations help the resize to
177 * add or remove dummy nodes when a resize is ongoing to ensure upper-bound on
178 * chain length.
179 */
180#define MIN_TABLE_SIZE 128
181
abc490a1
MD
182#ifndef max
183#define max(a, b) ((a) > (b) ? (a) : (b))
184#endif
2ed95849 185
d95bd160
MD
186/*
187 * The removed flag needs to be updated atomically with the pointer.
188 * The dummy flag does not require to be updated atomically with the
189 * pointer, but it is added as a pointer low bit flag to save space.
190 */
d37166c6 191#define REMOVED_FLAG (1UL << 0)
f5596c94
MD
192#define DUMMY_FLAG (1UL << 1)
193#define FLAGS_MASK ((1UL << 2) - 1)
d37166c6 194
df44348d 195struct ht_items_count {
3171717f 196 unsigned long add, remove;
df44348d
MD
197} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
198
1475579c
MD
199struct rcu_level {
200 struct rcu_head head;
201 struct _cds_lfht_node nodes[0];
202};
203
395270b6 204struct rcu_table {
abc490a1 205 unsigned long size; /* always a power of 2 */
f9830efd 206 unsigned long resize_target;
11519af6 207 int resize_initiated;
abc490a1 208 struct rcu_head head;
1475579c 209 struct rcu_level *tbl[0];
395270b6
MD
210};
211
14044b37 212struct cds_lfht {
395270b6 213 struct rcu_table *t; /* shared */
14044b37
MD
214 cds_lfht_hash_fct hash_fct;
215 cds_lfht_compare_fct compare_fct;
732ad076 216 unsigned long hash_seed;
b8af5011 217 int flags;
464a1ec9 218 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
33c7c748 219 unsigned int in_progress_resize, in_progress_destroy;
14044b37 220 void (*cds_lfht_call_rcu)(struct rcu_head *head,
abc490a1 221 void (*func)(struct rcu_head *head));
1475579c 222 void (*cds_lfht_synchronize_rcu)(void);
df44348d
MD
223 unsigned long count; /* global approximate item count */
224 struct ht_items_count *percpu_count; /* per-cpu item count */
2ed95849
MD
225};
226
abc490a1
MD
227struct rcu_resize_work {
228 struct rcu_head head;
14044b37 229 struct cds_lfht *ht;
abc490a1 230};
2ed95849 231
abc490a1
MD
232/*
233 * Algorithm to reverse bits in a word by lookup table, extended to
234 * 64-bit words.
f9830efd 235 * Source:
abc490a1 236 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
f9830efd 237 * Originally from Public Domain.
abc490a1
MD
238 */
239
240static const uint8_t BitReverseTable256[256] =
2ed95849 241{
abc490a1
MD
242#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
243#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
244#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
245 R6(0), R6(2), R6(1), R6(3)
246};
247#undef R2
248#undef R4
249#undef R6
2ed95849 250
abc490a1
MD
251static
252uint8_t bit_reverse_u8(uint8_t v)
253{
254 return BitReverseTable256[v];
255}
ab7d5fc6 256
abc490a1
MD
257static __attribute__((unused))
258uint32_t bit_reverse_u32(uint32_t v)
259{
260 return ((uint32_t) bit_reverse_u8(v) << 24) |
261 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
262 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
263 ((uint32_t) bit_reverse_u8(v >> 24));
2ed95849
MD
264}
265
abc490a1
MD
266static __attribute__((unused))
267uint64_t bit_reverse_u64(uint64_t v)
2ed95849 268{
abc490a1
MD
269 return ((uint64_t) bit_reverse_u8(v) << 56) |
270 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
271 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
272 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
273 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
274 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
275 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
276 ((uint64_t) bit_reverse_u8(v >> 56));
277}
278
279static
280unsigned long bit_reverse_ulong(unsigned long v)
281{
282#if (CAA_BITS_PER_LONG == 32)
283 return bit_reverse_u32(v);
284#else
285 return bit_reverse_u64(v);
286#endif
287}
288
f9830efd 289/*
24365af7
MD
290 * fls: returns the position of the most significant bit.
291 * Returns 0 if no bit is set, else returns the position of the most
292 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
f9830efd 293 */
24365af7
MD
294#if defined(__i386) || defined(__x86_64)
295static inline
296unsigned int fls_u32(uint32_t x)
f9830efd 297{
24365af7
MD
298 int r;
299
300 asm("bsrl %1,%0\n\t"
301 "jnz 1f\n\t"
302 "movl $-1,%0\n\t"
303 "1:\n\t"
304 : "=r" (r) : "rm" (x));
305 return r + 1;
306}
307#define HAS_FLS_U32
308#endif
309
310#if defined(__x86_64)
311static inline
312unsigned int fls_u64(uint64_t x)
313{
314 long r;
315
316 asm("bsrq %1,%0\n\t"
317 "jnz 1f\n\t"
318 "movq $-1,%0\n\t"
319 "1:\n\t"
320 : "=r" (r) : "rm" (x));
321 return r + 1;
322}
323#define HAS_FLS_U64
324#endif
325
326#ifndef HAS_FLS_U64
327static __attribute__((unused))
328unsigned int fls_u64(uint64_t x)
329{
330 unsigned int r = 64;
331
332 if (!x)
333 return 0;
334
335 if (!(x & 0xFFFFFFFF00000000ULL)) {
336 x <<= 32;
337 r -= 32;
338 }
339 if (!(x & 0xFFFF000000000000ULL)) {
340 x <<= 16;
341 r -= 16;
342 }
343 if (!(x & 0xFF00000000000000ULL)) {
344 x <<= 8;
345 r -= 8;
346 }
347 if (!(x & 0xF000000000000000ULL)) {
348 x <<= 4;
349 r -= 4;
350 }
351 if (!(x & 0xC000000000000000ULL)) {
352 x <<= 2;
353 r -= 2;
354 }
355 if (!(x & 0x8000000000000000ULL)) {
356 x <<= 1;
357 r -= 1;
358 }
359 return r;
360}
361#endif
362
363#ifndef HAS_FLS_U32
364static __attribute__((unused))
365unsigned int fls_u32(uint32_t x)
366{
367 unsigned int r = 32;
f9830efd 368
24365af7
MD
369 if (!x)
370 return 0;
371 if (!(x & 0xFFFF0000U)) {
372 x <<= 16;
373 r -= 16;
374 }
375 if (!(x & 0xFF000000U)) {
376 x <<= 8;
377 r -= 8;
378 }
379 if (!(x & 0xF0000000U)) {
380 x <<= 4;
381 r -= 4;
382 }
383 if (!(x & 0xC0000000U)) {
384 x <<= 2;
385 r -= 2;
386 }
387 if (!(x & 0x80000000U)) {
388 x <<= 1;
389 r -= 1;
390 }
391 return r;
392}
393#endif
394
395unsigned int fls_ulong(unsigned long x)
f9830efd 396{
24365af7
MD
397#if (CAA_BITS_PER_lONG == 32)
398 return fls_u32(x);
399#else
400 return fls_u64(x);
401#endif
402}
f9830efd 403
24365af7
MD
404int get_count_order_u32(uint32_t x)
405{
406 int order;
407
408 order = fls_u32(x) - 1;
409 if (x & (x - 1))
410 order++;
411 return order;
412}
413
414int get_count_order_ulong(unsigned long x)
415{
416 int order;
417
418 order = fls_ulong(x) - 1;
419 if (x & (x - 1))
420 order++;
421 return order;
f9830efd
MD
422}
423
98808fb1
MD
424#ifdef POISON_FREE
425#define poison_free(ptr) \
426 do { \
427 memset(ptr, 0x42, sizeof(*(ptr))); \
428 free(ptr); \
429 } while (0)
430#else
431#define poison_free(ptr) free(ptr)
432#endif
433
f9830efd 434static
14044b37 435void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth);
f9830efd 436
df44348d
MD
437/*
438 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
439 * available, then we support hash table item accounting.
440 * In the unfortunate event the number of CPUs reported would be
441 * inaccurate, we use modulo arithmetic on the number of CPUs we got.
442 */
df44348d
MD
443#if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
444
f8994aee
MD
445static
446void cds_lfht_resize_lazy_count(struct cds_lfht *ht, struct rcu_table *t,
447 unsigned long count);
448
df44348d
MD
449static long nr_cpus_mask = -1;
450
451static
452struct ht_items_count *alloc_per_cpu_items_count(void)
453{
454 struct ht_items_count *count;
455
456 switch (nr_cpus_mask) {
457 case -2:
458 return NULL;
459 case -1:
460 {
461 long maxcpus;
462
463 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
464 if (maxcpus <= 0) {
465 nr_cpus_mask = -2;
466 return NULL;
467 }
468 /*
469 * round up number of CPUs to next power of two, so we
470 * can use & for modulo.
471 */
472 maxcpus = 1UL << get_count_order_ulong(maxcpus);
473 nr_cpus_mask = maxcpus - 1;
474 }
475 /* Fall-through */
476 default:
477 return calloc(nr_cpus_mask + 1, sizeof(*count));
478 }
479}
480
481static
482void free_per_cpu_items_count(struct ht_items_count *count)
483{
98808fb1 484 poison_free(count);
df44348d
MD
485}
486
487static
488int ht_get_cpu(void)
489{
490 int cpu;
491
492 assert(nr_cpus_mask >= 0);
493 cpu = sched_getcpu();
494 if (unlikely(cpu < 0))
495 return cpu;
496 else
497 return cpu & nr_cpus_mask;
498}
499
500static
3171717f 501void ht_count_add(struct cds_lfht *ht, struct rcu_table *t)
df44348d 502{
3171717f 503 unsigned long percpu_count;
df44348d
MD
504 int cpu;
505
506 if (unlikely(!ht->percpu_count))
3171717f 507 return;
df44348d
MD
508 cpu = ht_get_cpu();
509 if (unlikely(cpu < 0))
3171717f
MD
510 return;
511 percpu_count = uatomic_add_return(&ht->percpu_count[cpu].add, 1);
df44348d
MD
512 if (unlikely(!(percpu_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
513 unsigned long count;
514
515 dbg_printf("add percpu %lu\n", percpu_count);
516 count = uatomic_add_return(&ht->count,
517 1UL << COUNT_COMMIT_ORDER);
518 /* If power of 2 */
519 if (!(count & (count - 1))) {
f8994aee
MD
520 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD)
521 < t->size)
522 return;
523 dbg_printf("add set global %lu\n", count);
524 cds_lfht_resize_lazy_count(ht, t,
6ea6bc67 525 count >> (CHAIN_LEN_TARGET - 1));
df44348d
MD
526 }
527 }
528}
529
530static
531void ht_count_remove(struct cds_lfht *ht, struct rcu_table *t)
532{
533 unsigned long percpu_count;
3171717f 534 int cpu;
df44348d 535
3171717f
MD
536 if (unlikely(!ht->percpu_count))
537 return;
538 cpu = ht_get_cpu();
539 if (unlikely(cpu < 0))
540 return;
541 percpu_count = uatomic_add_return(&ht->percpu_count[cpu].remove, -1);
df44348d
MD
542 if (unlikely(!(percpu_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
543 unsigned long count;
544
545 dbg_printf("remove percpu %lu\n", percpu_count);
546 count = uatomic_add_return(&ht->count,
3171717f 547 -(1UL << COUNT_COMMIT_ORDER));
df44348d
MD
548 /* If power of 2 */
549 if (!(count & (count - 1))) {
f8994aee
MD
550 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD)
551 >= t->size)
552 return;
553 dbg_printf("remove set global %lu\n", count);
554 cds_lfht_resize_lazy_count(ht, t,
6ea6bc67 555 count >> (CHAIN_LEN_TARGET - 1));
df44348d
MD
556 }
557 }
558}
559
560#else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
561
562static const long nr_cpus_mask = -1;
563
564static
565struct ht_items_count *alloc_per_cpu_items_count(void)
566{
567 return NULL;
568}
569
570static
571void free_per_cpu_items_count(struct ht_items_count *count)
572{
573}
574
575static
f8994aee 576void ht_count_add(struct cds_lfht *ht, struct rcu_table *t)
df44348d
MD
577{
578}
579
580static
f8994aee 581void ht_count_remove(struct cds_lfht *ht, struct rcu_table *t)
df44348d
MD
582{
583}
584
585#endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
586
587
f9830efd 588static
14044b37 589void check_resize(struct cds_lfht *ht, struct rcu_table *t,
f9830efd
MD
590 uint32_t chain_len)
591{
f8994aee
MD
592 unsigned long count;
593
b8af5011
MD
594 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
595 return;
f8994aee
MD
596 count = uatomic_read(&ht->count);
597 /*
598 * Use bucket-local length for small table expand and for
599 * environments lacking per-cpu data support.
600 */
601 if (count >= (1UL << COUNT_COMMIT_ORDER))
602 return;
24365af7 603 if (chain_len > 100)
f0c29ed7 604 dbg_printf("WARNING: large chain length: %u.\n",
24365af7 605 chain_len);
3390d470 606 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
14044b37 607 cds_lfht_resize_lazy(ht, t,
01370f0b 608 get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
f9830efd
MD
609}
610
abc490a1 611static
14044b37 612struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
abc490a1 613{
14044b37 614 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
abc490a1
MD
615}
616
617static
14044b37 618int is_removed(struct cds_lfht_node *node)
abc490a1 619{
d37166c6 620 return ((unsigned long) node) & REMOVED_FLAG;
abc490a1
MD
621}
622
623static
14044b37 624struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
abc490a1 625{
14044b37 626 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
abc490a1
MD
627}
628
f5596c94 629static
14044b37 630int is_dummy(struct cds_lfht_node *node)
f5596c94
MD
631{
632 return ((unsigned long) node) & DUMMY_FLAG;
633}
634
635static
14044b37 636struct cds_lfht_node *flag_dummy(struct cds_lfht_node *node)
f5596c94 637{
14044b37 638 return (struct cds_lfht_node *) (((unsigned long) node) | DUMMY_FLAG);
f5596c94
MD
639}
640
abc490a1 641static
f9830efd 642unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
abc490a1
MD
643{
644 unsigned long old1, old2;
645
646 old1 = uatomic_read(ptr);
647 do {
648 old2 = old1;
649 if (old2 >= v)
f9830efd 650 return old2;
abc490a1 651 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
f9830efd 652 return v;
abc490a1
MD
653}
654
1475579c
MD
655static
656void cds_lfht_free_table_cb(struct rcu_head *head)
657{
658 struct rcu_table *t =
659 caa_container_of(head, struct rcu_table, head);
98808fb1 660 poison_free(t);
1475579c
MD
661}
662
663static
664void cds_lfht_free_level(struct rcu_head *head)
665{
666 struct rcu_level *l =
667 caa_container_of(head, struct rcu_level, head);
98808fb1 668 poison_free(l);
1475579c
MD
669}
670
273399de
MD
671/*
672 * Remove all logically deleted nodes from a bucket up to a certain node key.
673 */
674static
14044b37 675void _cds_lfht_gc_bucket(struct cds_lfht_node *dummy, struct cds_lfht_node *node)
273399de 676{
14044b37 677 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
53960c9f
MD
678 struct cds_lfht_node *iter_trace[64];
679 unsigned long trace_idx = 0;
273399de 680
53960c9f 681 memset(iter_trace, 0, sizeof(iter_trace));
c90201ac
MD
682 assert(!is_dummy(dummy));
683 assert(!is_removed(dummy));
684 assert(!is_dummy(node));
685 assert(!is_removed(node));
273399de 686 for (;;) {
53960c9f 687 iter_trace[trace_idx++ & (64 - 1)] = (void *) 0x1;
273399de
MD
688 iter_prev = dummy;
689 /* We can always skip the dummy node initially */
cc4fcb10 690 iter = rcu_dereference(iter_prev->p.next);
53960c9f 691 iter_trace[trace_idx++ & (64 - 1)] = iter;
cc4fcb10 692 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
bd4db153
MD
693 /*
694 * We should never be called with dummy (start of chain)
695 * and logically removed node (end of path compression
696 * marker) being the actual same node. This would be a
697 * bug in the algorithm implementation.
698 */
699 assert(dummy != node);
273399de 700 for (;;) {
a2974903 701 if (unlikely(!clear_flag(iter)))
479c8a32 702 return;
76412f24 703 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
273399de 704 return;
cc4fcb10 705 next = rcu_dereference(clear_flag(iter)->p.next);
76412f24 706 if (likely(is_removed(next)))
273399de 707 break;
b453eae1 708 iter_prev = clear_flag(iter);
273399de 709 iter = next;
53960c9f 710 iter_trace[trace_idx++ & (64 - 1)] = iter;
273399de
MD
711 }
712 assert(!is_removed(iter));
f5596c94
MD
713 if (is_dummy(iter))
714 new_next = flag_dummy(clear_flag(next));
715 else
716 new_next = clear_flag(next);
717 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
53960c9f 718 iter_trace[trace_idx++ & (64 - 1)] = (void *) 0x2;
273399de
MD
719 }
720}
721
abc490a1 722static
14044b37
MD
723struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, struct rcu_table *t,
724 struct cds_lfht_node *node, int unique, int dummy)
abc490a1 725{
14044b37 726 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
f5596c94 727 *dummy_node;
14044b37 728 struct _cds_lfht_node *lookup;
24365af7 729 unsigned long hash, index, order;
abc490a1 730
c90201ac
MD
731 assert(!is_dummy(node));
732 assert(!is_removed(node));
18117871 733 if (!t->size) {
f5596c94
MD
734 assert(dummy);
735 node->p.next = flag_dummy(NULL);
18117871
MD
736 return node; /* Initial first add (head) */
737 }
cc4fcb10 738 hash = bit_reverse_ulong(node->p.reverse_hash);
abc490a1 739 for (;;) {
f9830efd 740 uint32_t chain_len = 0;
abc490a1 741
11519af6
MD
742 /*
743 * iter_prev points to the non-removed node prior to the
744 * insert location.
11519af6 745 */
24365af7
MD
746 index = hash & (t->size - 1);
747 order = get_count_order_ulong(index + 1);
554c284e 748 lookup = &t->tbl[order]->nodes[index & ((!order ? 0 : (1UL << (order - 1))) - 1)];
14044b37 749 iter_prev = (struct cds_lfht_node *) lookup;
11519af6 750 /* We can always skip the dummy node initially */
cc4fcb10
MD
751 iter = rcu_dereference(iter_prev->p.next);
752 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
abc490a1 753 for (;;) {
c90201ac 754 /* TODO: check if removed */
a2974903 755 if (unlikely(!clear_flag(iter)))
273399de 756 goto insert;
c90201ac 757 /* TODO: check if removed */
76412f24 758 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
273399de 759 goto insert;
cc4fcb10 760 next = rcu_dereference(clear_flag(iter)->p.next);
76412f24 761 if (unlikely(is_removed(next)))
9dba85be 762 goto gc_node;
e43f23f8 763 if (unique
1b81fe1a 764 && !is_dummy(next)
e43f23f8
MD
765 && !ht->compare_fct(node->key, node->key_len,
766 clear_flag(iter)->key,
767 clear_flag(iter)->key_len))
18117871 768 return clear_flag(iter);
11519af6 769 /* Only account for identical reverse hash once */
24365af7
MD
770 if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash
771 && !is_dummy(next))
11519af6
MD
772 check_resize(ht, t, ++chain_len);
773 iter_prev = clear_flag(iter);
273399de 774 iter = next;
abc490a1 775 }
273399de 776 insert:
7ec59d3b 777 assert(node != clear_flag(iter));
11519af6 778 assert(!is_removed(iter_prev));
c90201ac 779 assert(!is_removed(iter));
f000907d 780 assert(iter_prev != node);
f5596c94 781 if (!dummy)
1b81fe1a 782 node->p.next = clear_flag(iter);
f5596c94 783 else
1b81fe1a 784 node->p.next = flag_dummy(clear_flag(iter));
f5596c94
MD
785 if (is_dummy(iter))
786 new_node = flag_dummy(node);
787 else
788 new_node = node;
cc4fcb10 789 if (uatomic_cmpxchg(&iter_prev->p.next, iter,
f5596c94 790 new_node) != iter)
273399de 791 continue; /* retry */
11519af6 792 else
273399de 793 goto gc_end;
9dba85be
MD
794 gc_node:
795 assert(!is_removed(iter));
f5596c94
MD
796 if (is_dummy(iter))
797 new_next = flag_dummy(clear_flag(next));
798 else
799 new_next = clear_flag(next);
800 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
273399de 801 /* retry */
464a1ec9 802 }
273399de
MD
803gc_end:
804 /* Garbage collect logically removed nodes in the bucket */
24365af7
MD
805 index = hash & (t->size - 1);
806 order = get_count_order_ulong(index + 1);
554c284e 807 lookup = &t->tbl[order]->nodes[index & (!order ? 0 : ((1UL << (order - 1)) - 1))];
14044b37
MD
808 dummy_node = (struct cds_lfht_node *) lookup;
809 _cds_lfht_gc_bucket(dummy_node, node);
18117871 810 return node;
abc490a1 811}
464a1ec9 812
abc490a1 813static
14044b37 814int _cds_lfht_remove(struct cds_lfht *ht, struct rcu_table *t,
1475579c 815 struct cds_lfht_node *node, int dummy_removal)
abc490a1 816{
14044b37
MD
817 struct cds_lfht_node *dummy, *next, *old;
818 struct _cds_lfht_node *lookup;
abc490a1 819 int flagged = 0;
24365af7 820 unsigned long hash, index, order;
5e28c532 821
7ec59d3b 822 /* logically delete the node */
c90201ac
MD
823 assert(!is_dummy(node));
824 assert(!is_removed(node));
cc4fcb10 825 old = rcu_dereference(node->p.next);
7ec59d3b
MD
826 do {
827 next = old;
76412f24 828 if (unlikely(is_removed(next)))
7ec59d3b 829 goto end;
1475579c
MD
830 if (dummy_removal)
831 assert(is_dummy(next));
832 else
833 assert(!is_dummy(next));
cc4fcb10 834 old = uatomic_cmpxchg(&node->p.next, next,
7ec59d3b
MD
835 flag_removed(next));
836 } while (old != next);
837
838 /* We performed the (logical) deletion. */
839 flagged = 1;
840
841 /*
842 * Ensure that the node is not visible to readers anymore: lookup for
273399de
MD
843 * the node, and remove it (along with any other logically removed node)
844 * if found.
11519af6 845 */
cc4fcb10 846 hash = bit_reverse_ulong(node->p.reverse_hash);
4c3a0329
MD
847 assert(t->size > 0);
848 index = hash & (t->size - 1);
24365af7 849 order = get_count_order_ulong(index + 1);
554c284e 850 lookup = &t->tbl[order]->nodes[index & (!order ? 0 : ((1UL << (order - 1)) - 1))];
14044b37
MD
851 dummy = (struct cds_lfht_node *) lookup;
852 _cds_lfht_gc_bucket(dummy, node);
2ed95849 853end:
11519af6
MD
854 /*
855 * Only the flagging action indicated that we (and no other)
856 * removed the node from the hash.
857 */
7ec59d3b 858 if (flagged) {
cc4fcb10 859 assert(is_removed(rcu_dereference(node->p.next)));
11519af6 860 return 0;
7ec59d3b 861 } else
11519af6 862 return -ENOENT;
abc490a1 863}
2ed95849 864
abc490a1 865static
14044b37 866void init_table(struct cds_lfht *ht, struct rcu_table *t,
24365af7
MD
867 unsigned long first_order, unsigned long len_order)
868{
869 unsigned long i, end_order;
870
f0c29ed7 871 dbg_printf("init table: first_order %lu end_order %lu\n",
24365af7
MD
872 first_order, first_order + len_order);
873 end_order = first_order + len_order;
874 t->size = !first_order ? 0 : (1UL << (first_order - 1));
875 for (i = first_order; i < end_order; i++) {
876 unsigned long j, len;
877
878 len = !i ? 1 : 1UL << (i - 1);
f0c29ed7 879 dbg_printf("init order %lu len: %lu\n", i, len);
1475579c
MD
880 t->tbl[i] = calloc(1, sizeof(struct rcu_level)
881 + (len * sizeof(struct _cds_lfht_node)));
24365af7 882 for (j = 0; j < len; j++) {
1475579c
MD
883 struct cds_lfht_node *new_node =
884 (struct cds_lfht_node *) &t->tbl[i]->nodes[j];
885
f0c29ed7 886 dbg_printf("init entry: i %lu j %lu hash %lu\n",
24365af7 887 i, j, !i ? 0 : (1UL << (i - 1)) + j);
24365af7
MD
888 new_node->p.reverse_hash =
889 bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j);
14044b37 890 (void) _cds_lfht_add(ht, t, new_node, 0, 1);
33c7c748
MD
891 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
892 break;
24365af7
MD
893 }
894 /* Update table size */
895 t->size = !i ? 1 : (1UL << i);
f0c29ed7 896 dbg_printf("init new size: %lu\n", t->size);
33c7c748
MD
897 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
898 break;
abc490a1 899 }
24365af7 900 t->resize_target = t->size;
11519af6 901 t->resize_initiated = 0;
2ed95849
MD
902}
903
1475579c
MD
904static
905void fini_table(struct cds_lfht *ht, struct rcu_table *t,
906 unsigned long first_order, unsigned long len_order)
907{
908 long i, end_order;
909
910 dbg_printf("fini table: first_order %lu end_order %lu\n",
911 first_order, first_order + len_order);
912 end_order = first_order + len_order;
913 assert(first_order > 0);
914 assert(t->size == (1UL << (end_order - 1)));
915 for (i = end_order - 1; i >= first_order; i--) {
916 unsigned long j, len;
917
918 len = !i ? 1 : 1UL << (i - 1);
919 dbg_printf("fini order %lu len: %lu\n", i, len);
21263e21
MD
920 /*
921 * Update table size. Need to shrink this table prior to
922 * removal so gc lookups use non-logically-removed dummy
923 * nodes.
924 */
e1a68f1d 925 t->size = 1UL << (i - 1);
1475579c
MD
926 /* Unlink */
927 for (j = 0; j < len; j++) {
c90201ac 928 struct cds_lfht_node *fini_node =
1475579c
MD
929 (struct cds_lfht_node *) &t->tbl[i]->nodes[j];
930
931 dbg_printf("fini entry: i %lu j %lu hash %lu\n",
932 i, j, !i ? 0 : (1UL << (i - 1)) + j);
c90201ac 933 fini_node->p.reverse_hash =
1475579c 934 bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j);
c90201ac 935 (void) _cds_lfht_remove(ht, t, fini_node, 1);
1475579c
MD
936 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
937 break;
938 }
939 ht->cds_lfht_call_rcu(&t->tbl[i]->head, cds_lfht_free_level);
1475579c
MD
940 dbg_printf("fini new size: %lu\n", t->size);
941 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
942 break;
943 }
944 t->resize_target = t->size;
945 t->resize_initiated = 0;
946}
947
14044b37
MD
948struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
949 cds_lfht_compare_fct compare_fct,
950 unsigned long hash_seed,
951 unsigned long init_size,
b8af5011 952 int flags,
14044b37 953 void (*cds_lfht_call_rcu)(struct rcu_head *head,
1475579c
MD
954 void (*func)(struct rcu_head *head)),
955 void (*cds_lfht_synchronize_rcu)(void))
abc490a1 956{
14044b37 957 struct cds_lfht *ht;
24365af7 958 unsigned long order;
abc490a1 959
8129be4e 960 /* init_size must be power of two */
49619ea0 961 if (init_size && (init_size & (init_size - 1)))
8129be4e 962 return NULL;
14044b37 963 ht = calloc(1, sizeof(struct cds_lfht));
abc490a1 964 ht->hash_fct = hash_fct;
732ad076
MD
965 ht->compare_fct = compare_fct;
966 ht->hash_seed = hash_seed;
14044b37 967 ht->cds_lfht_call_rcu = cds_lfht_call_rcu;
1475579c 968 ht->cds_lfht_synchronize_rcu = cds_lfht_synchronize_rcu;
848d4088 969 ht->in_progress_resize = 0;
df44348d 970 ht->percpu_count = alloc_per_cpu_items_count();
abc490a1
MD
971 /* this mutex should not nest in read-side C.S. */
972 pthread_mutex_init(&ht->resize_mutex, NULL);
cd95516d 973 order = get_count_order_ulong(max(init_size, MIN_TABLE_SIZE)) + 1;
14044b37 974 ht->t = calloc(1, sizeof(struct cds_lfht)
1475579c 975 + (order * sizeof(struct rcu_level *)));
abc490a1 976 ht->t->size = 0;
b8af5011 977 ht->flags = flags;
f000907d 978 pthread_mutex_lock(&ht->resize_mutex);
24365af7 979 init_table(ht, ht->t, 0, order);
f000907d 980 pthread_mutex_unlock(&ht->resize_mutex);
abc490a1
MD
981 return ht;
982}
983
14044b37 984struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len)
2ed95849 985{
395270b6 986 struct rcu_table *t;
14044b37
MD
987 struct cds_lfht_node *node, *next;
988 struct _cds_lfht_node *lookup;
24365af7 989 unsigned long hash, reverse_hash, index, order;
2ed95849 990
732ad076 991 hash = ht->hash_fct(key, key_len, ht->hash_seed);
abc490a1 992 reverse_hash = bit_reverse_ulong(hash);
464a1ec9 993
395270b6 994 t = rcu_dereference(ht->t);
24365af7
MD
995 index = hash & (t->size - 1);
996 order = get_count_order_ulong(index + 1);
554c284e 997 lookup = &t->tbl[order]->nodes[index & (!order ? 0 : ((1UL << (order - 1))) - 1)];
f0c29ed7 998 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
554c284e 999 hash, index, order, index & (!order ? 0 : ((1UL << (order - 1)) - 1)));
14044b37 1000 node = (struct cds_lfht_node *) lookup;
2ed95849 1001 for (;;) {
abc490a1
MD
1002 if (unlikely(!node))
1003 break;
cc4fcb10 1004 if (unlikely(node->p.reverse_hash > reverse_hash)) {
abc490a1
MD
1005 node = NULL;
1006 break;
2ed95849 1007 }
1b81fe1a
MD
1008 next = rcu_dereference(node->p.next);
1009 if (likely(!is_removed(next))
1010 && !is_dummy(next)
49c2e2d6 1011 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
273399de 1012 break;
2ed95849 1013 }
1b81fe1a 1014 node = clear_flag(next);
2ed95849 1015 }
1b81fe1a 1016 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
abc490a1
MD
1017 return node;
1018}
e0ba718a 1019
a481e5ff
MD
1020struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht,
1021 struct cds_lfht_node *node)
1022{
1023 struct cds_lfht_node *next;
1024 unsigned long reverse_hash;
1025 void *key;
1026 size_t key_len;
1027
1028 reverse_hash = node->p.reverse_hash;
1029 key = node->key;
1030 key_len = node->key_len;
1031 next = rcu_dereference(node->p.next);
1032 node = clear_flag(next);
1033
1034 for (;;) {
1035 if (unlikely(!node))
1036 break;
1037 if (unlikely(node->p.reverse_hash > reverse_hash)) {
1038 node = NULL;
1039 break;
1040 }
1041 next = rcu_dereference(node->p.next);
1042 if (likely(!is_removed(next))
1043 && !is_dummy(next)
1044 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
1045 break;
1046 }
1047 node = clear_flag(next);
1048 }
1049 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
1050 return node;
1051}
1052
14044b37 1053void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node)
abc490a1
MD
1054{
1055 struct rcu_table *t;
49c2e2d6 1056 unsigned long hash;
ab7d5fc6 1057
49c2e2d6 1058 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
cc4fcb10 1059 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
2ed95849 1060
abc490a1 1061 t = rcu_dereference(ht->t);
14044b37 1062 (void) _cds_lfht_add(ht, t, node, 0, 0);
df44348d 1063 ht_count_add(ht, t);
3eca1b8c
MD
1064}
1065
14044b37
MD
1066struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
1067 struct cds_lfht_node *node)
3eca1b8c
MD
1068{
1069 struct rcu_table *t;
49c2e2d6 1070 unsigned long hash;
df44348d 1071 struct cds_lfht_node *ret;
3eca1b8c 1072
49c2e2d6 1073 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
cc4fcb10 1074 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
3eca1b8c
MD
1075
1076 t = rcu_dereference(ht->t);
df44348d
MD
1077 ret = _cds_lfht_add(ht, t, node, 1, 0);
1078 if (ret != node)
1079 ht_count_add(ht, t);
1080 return ret;
2ed95849
MD
1081}
1082
14044b37 1083int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node)
2ed95849 1084{
abc490a1 1085 struct rcu_table *t;
df44348d 1086 int ret;
abc490a1
MD
1087
1088 t = rcu_dereference(ht->t);
1475579c 1089 ret = _cds_lfht_remove(ht, t, node, 0);
df44348d
MD
1090 if (!ret)
1091 ht_count_remove(ht, t);
1092 return ret;
2ed95849 1093}
ab7d5fc6 1094
abc490a1 1095static
14044b37 1096int cds_lfht_delete_dummy(struct cds_lfht *ht)
674f7a69 1097{
395270b6 1098 struct rcu_table *t;
14044b37
MD
1099 struct cds_lfht_node *node;
1100 struct _cds_lfht_node *lookup;
24365af7 1101 unsigned long order, i;
674f7a69 1102
abc490a1
MD
1103 t = ht->t;
1104 /* Check that the table is empty */
1475579c 1105 lookup = &t->tbl[0]->nodes[0];
14044b37 1106 node = (struct cds_lfht_node *) lookup;
abc490a1 1107 do {
1b81fe1a
MD
1108 node = clear_flag(node)->p.next;
1109 if (!is_dummy(node))
abc490a1 1110 return -EPERM;
273399de 1111 assert(!is_removed(node));
a2974903 1112 } while (clear_flag(node));
abc490a1 1113 /* Internal sanity check: all nodes left should be dummy */
24365af7
MD
1114 for (order = 0; order < get_count_order_ulong(t->size) + 1; order++) {
1115 unsigned long len;
1116
1117 len = !order ? 1 : 1UL << (order - 1);
1118 for (i = 0; i < len; i++) {
f0c29ed7 1119 dbg_printf("delete order %lu i %lu hash %lu\n",
24365af7 1120 order, i,
1475579c
MD
1121 bit_reverse_ulong(t->tbl[order]->nodes[i].reverse_hash));
1122 assert(is_dummy(t->tbl[order]->nodes[i].next));
24365af7 1123 }
98808fb1 1124 poison_free(t->tbl[order]);
674f7a69 1125 }
abc490a1 1126 return 0;
674f7a69
MD
1127}
1128
1129/*
1130 * Should only be called when no more concurrent readers nor writers can
1131 * possibly access the table.
1132 */
14044b37 1133int cds_lfht_destroy(struct cds_lfht *ht)
674f7a69 1134{
5e28c532
MD
1135 int ret;
1136
848d4088 1137 /* Wait for in-flight resize operations to complete */
33c7c748 1138 CMM_STORE_SHARED(ht->in_progress_destroy, 1);
848d4088
MD
1139 while (uatomic_read(&ht->in_progress_resize))
1140 poll(NULL, 0, 100); /* wait for 100ms */
14044b37 1141 ret = cds_lfht_delete_dummy(ht);
abc490a1
MD
1142 if (ret)
1143 return ret;
98808fb1 1144 poison_free(ht->t);
df44348d 1145 free_per_cpu_items_count(ht->percpu_count);
98808fb1 1146 poison_free(ht);
5e28c532 1147 return ret;
674f7a69
MD
1148}
1149
14044b37 1150void cds_lfht_count_nodes(struct cds_lfht *ht,
273399de
MD
1151 unsigned long *count,
1152 unsigned long *removed)
1153{
1154 struct rcu_table *t;
14044b37
MD
1155 struct cds_lfht_node *node, *next;
1156 struct _cds_lfht_node *lookup;
24365af7 1157 unsigned long nr_dummy = 0;
273399de
MD
1158
1159 *count = 0;
1160 *removed = 0;
1161
1162 t = rcu_dereference(ht->t);
24365af7 1163 /* Count non-dummy nodes in the table */
1475579c 1164 lookup = &t->tbl[0]->nodes[0];
14044b37 1165 node = (struct cds_lfht_node *) lookup;
273399de 1166 do {
cc4fcb10 1167 next = rcu_dereference(node->p.next);
273399de 1168 if (is_removed(next)) {
1b81fe1a 1169 assert(!is_dummy(next));
273399de 1170 (*removed)++;
1b81fe1a 1171 } else if (!is_dummy(next))
273399de 1172 (*count)++;
24365af7
MD
1173 else
1174 (nr_dummy)++;
273399de
MD
1175 node = clear_flag(next);
1176 } while (node);
f0c29ed7 1177 dbg_printf("number of dummy nodes: %lu\n", nr_dummy);
273399de
MD
1178}
1179
1475579c 1180/* called with resize mutex held */
abc490a1 1181static
1475579c
MD
1182void _do_cds_lfht_grow(struct cds_lfht *ht, struct rcu_table *old_t,
1183 unsigned long old_size, unsigned long new_size)
abc490a1 1184{
1475579c
MD
1185 unsigned long old_order, new_order;
1186 struct rcu_table *new_t;
1187
1188 old_order = get_count_order_ulong(old_size) + 1;
1189 new_order = get_count_order_ulong(new_size) + 1;
1190 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1191 old_size, old_order, new_size, new_order);
1192 new_t = malloc(sizeof(struct cds_lfht)
1193 + (new_order * sizeof(struct rcu_level *)));
1194 assert(new_size > old_size);
1195 memcpy(&new_t->tbl, &old_t->tbl,
1196 old_order * sizeof(struct rcu_level *));
1197 init_table(ht, new_t, old_order, new_order - old_order);
1198 /* Changing table and size atomically wrt lookups */
1199 rcu_assign_pointer(ht->t, new_t);
1200 ht->cds_lfht_call_rcu(&old_t->head, cds_lfht_free_table_cb);
abc490a1
MD
1201}
1202
1203/* called with resize mutex held */
1204static
1475579c
MD
1205void _do_cds_lfht_shrink(struct cds_lfht *ht, struct rcu_table *old_t,
1206 unsigned long old_size, unsigned long new_size)
464a1ec9 1207{
1475579c
MD
1208 unsigned long old_order, new_order;
1209 struct rcu_table *new_t;
464a1ec9 1210
cd95516d 1211 new_size = max(new_size, MIN_TABLE_SIZE);
24365af7 1212 old_order = get_count_order_ulong(old_size) + 1;
24365af7 1213 new_order = get_count_order_ulong(new_size) + 1;
df44348d 1214 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
df03fab8 1215 old_size, old_order, new_size, new_order);
14044b37 1216 new_t = malloc(sizeof(struct cds_lfht)
1475579c
MD
1217 + (new_order * sizeof(struct rcu_level *)));
1218 assert(new_size < old_size);
f000907d 1219 memcpy(&new_t->tbl, &old_t->tbl,
1475579c
MD
1220 new_order * sizeof(struct rcu_level *));
1221 new_t->size = !new_order ? 1 : (1UL << (new_order - 1));
c90201ac 1222 assert(new_t->size == new_size);
1475579c
MD
1223 new_t->resize_target = new_t->size;
1224 new_t->resize_initiated = 0;
1225
f000907d
MD
1226 /* Changing table and size atomically wrt lookups */
1227 rcu_assign_pointer(ht->t, new_t);
1475579c
MD
1228
1229 /*
c90201ac 1230 * We need to wait for all add operations to reach Q.S. (and
1475579c 1231 * thus use the new table for lookups) before we can start
c90201ac
MD
1232 * releasing the old dummy nodes. Otherwise their lookup will
1233 * return a logically removed node as insert position.
1475579c
MD
1234 */
1235 ht->cds_lfht_synchronize_rcu();
1236
1237 /* Unlink and remove all now-unused dummy node pointers. */
1238 fini_table(ht, old_t, new_order, old_order - new_order);
14044b37 1239 ht->cds_lfht_call_rcu(&old_t->head, cds_lfht_free_table_cb);
464a1ec9
MD
1240}
1241
1475579c
MD
1242
1243/* called with resize mutex held */
1244static
1245void _do_cds_lfht_resize(struct cds_lfht *ht)
1246{
1247 unsigned long new_size, old_size;
1248 struct rcu_table *old_t;
1249
1250 old_t = ht->t;
1251 old_size = old_t->size;
1252 new_size = CMM_LOAD_SHARED(old_t->resize_target);
1253 if (old_size < new_size)
1254 _do_cds_lfht_grow(ht, old_t, old_size, new_size);
1255 else if (old_size > new_size)
1256 _do_cds_lfht_shrink(ht, old_t, old_size, new_size);
1257 else
1258 CMM_STORE_SHARED(old_t->resize_initiated, 0);
1259}
1260
abc490a1 1261static
f9830efd
MD
1262unsigned long resize_target_update(struct rcu_table *t,
1263 int growth_order)
464a1ec9 1264{
f9830efd
MD
1265 return _uatomic_max(&t->resize_target,
1266 t->size << growth_order);
464a1ec9
MD
1267}
1268
1475579c 1269static
b8af5011
MD
1270void resize_target_update_count(struct rcu_table *t,
1271 unsigned long count)
1475579c 1272{
cd95516d 1273 count = max(count, MIN_TABLE_SIZE);
b8af5011 1274 uatomic_set(&t->resize_target, count);
1475579c
MD
1275}
1276
1277void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size)
464a1ec9 1278{
f9830efd 1279 struct rcu_table *t = rcu_dereference(ht->t);
f9830efd 1280
b8af5011 1281 resize_target_update_count(t, new_size);
1475579c
MD
1282 CMM_STORE_SHARED(t->resize_initiated, 1);
1283 pthread_mutex_lock(&ht->resize_mutex);
1284 _do_cds_lfht_resize(ht);
1285 pthread_mutex_unlock(&ht->resize_mutex);
abc490a1 1286}
464a1ec9 1287
abc490a1
MD
1288static
1289void do_resize_cb(struct rcu_head *head)
1290{
1291 struct rcu_resize_work *work =
1292 caa_container_of(head, struct rcu_resize_work, head);
14044b37 1293 struct cds_lfht *ht = work->ht;
abc490a1
MD
1294
1295 pthread_mutex_lock(&ht->resize_mutex);
14044b37 1296 _do_cds_lfht_resize(ht);
abc490a1 1297 pthread_mutex_unlock(&ht->resize_mutex);
98808fb1 1298 poison_free(work);
848d4088
MD
1299 cmm_smp_mb(); /* finish resize before decrement */
1300 uatomic_dec(&ht->in_progress_resize);
464a1ec9
MD
1301}
1302
abc490a1 1303static
14044b37 1304void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth)
ab7d5fc6 1305{
abc490a1 1306 struct rcu_resize_work *work;
f9830efd 1307 unsigned long target_size;
abc490a1 1308
f9830efd 1309 target_size = resize_target_update(t, growth);
11519af6 1310 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
848d4088
MD
1311 uatomic_inc(&ht->in_progress_resize);
1312 cmm_smp_mb(); /* increment resize count before calling it */
f9830efd
MD
1313 work = malloc(sizeof(*work));
1314 work->ht = ht;
14044b37 1315 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
11519af6 1316 CMM_STORE_SHARED(t->resize_initiated, 1);
f9830efd 1317 }
ab7d5fc6 1318}
3171717f 1319
f8994aee
MD
1320#if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
1321
3171717f
MD
1322static
1323void cds_lfht_resize_lazy_count(struct cds_lfht *ht, struct rcu_table *t,
1324 unsigned long count)
1325{
1326 struct rcu_resize_work *work;
3171717f 1327
b8af5011
MD
1328 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
1329 return;
1330 resize_target_update_count(t, count);
1475579c 1331 if (!CMM_LOAD_SHARED(t->resize_initiated)) {
3171717f
MD
1332 uatomic_inc(&ht->in_progress_resize);
1333 cmm_smp_mb(); /* increment resize count before calling it */
1334 work = malloc(sizeof(*work));
1335 work->ht = ht;
1336 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
1337 CMM_STORE_SHARED(t->resize_initiated, 1);
1338 }
1339}
f8994aee
MD
1340
1341#endif
This page took 0.09056 seconds and 4 git commands to generate.