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