4 * Userspace RCU library - Lock-Free Expandable RCU Hash Table
6 * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
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,
32 * Some specificities of this Lock-Free Expandable RCU Hash Table
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
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.
51 * - The resize operation only allows expanding the hash table.
52 * It is triggered either through an API call or automatically by
53 * detecting long chains in the add operation.
54 * - Resize operation initiated by long chain detection is executed by a
55 * call_rcu thread, which keeps lock-freedom of add and remove.
56 * - Resize operations are protected by a mutex.
57 * - The removal operation is split in two parts: first, a "removed"
58 * flag is set in the next pointer within the node to remove. Then,
59 * a "garbage collection" is performed in the bucket containing the
60 * removed node (from the start of the bucket up to the removed node).
61 * All encountered nodes with "removed" flag set in their next
62 * pointers are removed from the linked-list. If the cmpxchg used for
63 * removal fails (due to concurrent garbage-collection or concurrent
64 * add), we retry from the beginning of the bucket. This ensures that
65 * the node with "removed" flag set is removed from the hash table
66 * (not visible to lookups anymore) before the RCU read-side critical
67 * section held across removal ends. Furthermore, this ensures that
68 * the node with "removed" flag set is removed from the linked-list
69 * before its memory is reclaimed. Only the thread which removal
70 * successfully set the "removed" flag (with a cmpxchg) into a node's
71 * next pointer is considered to have succeeded its removal (and thus
72 * owns the node to reclaim). Because we garbage-collect starting from
73 * an invariant node (the start-of-bucket dummy node) up to the
74 * "removed" node (or find a reverse-hash that is higher), we are sure
75 * that a successful traversal of the chain leads to a chain that is
76 * present in the linked-list (the start node is never removed) and
77 * that is does not contain the "removed" node anymore, even if
78 * concurrent delete/add operations are changing the structure of the
80 * - The add operation performs gargage collection of buckets if it
81 * encounters nodes with removed flag set in the bucket where it wants
82 * to add its new node. This ensures lock-freedom of add operation by
83 * helping the remover unlink nodes from the list rather than to wait
85 * - A RCU "order table" indexed by log2(hash index) is copied and
86 * expanded by the resize operation. This order table allows finding
87 * the "dummy node" tables.
88 * - There is one dummy node table per hash index order. The size of
89 * each dummy node table is half the number of hashes contained in
91 * - call_rcu is used to garbage-collect the old order table.
92 * - The per-order dummy node tables contain a compact version of the
93 * hash table nodes. These tables are invariant after they are
94 * populated into the hash table.
107 #include <urcu-call-rcu.h>
108 #include <urcu/arch.h>
109 #include <urcu/uatomic.h>
110 #include <urcu/jhash.h>
111 #include <urcu/compiler.h>
112 #include <urcu/rculfhash.h>
117 #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
119 #define dbg_printf(fmt, args...)
122 #define CHAIN_LEN_TARGET 4
123 #define CHAIN_LEN_RESIZE_THRESHOLD 8
125 /* Commit counter changes to global counter each 1024 steps */
126 #define COUNT_COMMIT_ORDER 10
129 #define max(a, b) ((a) > (b) ? (a) : (b))
133 * The removed flag needs to be updated atomically with the pointer.
134 * The dummy flag does not require to be updated atomically with the
135 * pointer, but it is added as a pointer low bit flag to save space.
137 #define REMOVED_FLAG (1UL << 0)
138 #define DUMMY_FLAG (1UL << 1)
139 #define FLAGS_MASK ((1UL << 2) - 1)
141 struct ht_items_count
{
142 unsigned long add
, remove
;
143 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
146 unsigned long size
; /* always a power of 2 */
147 unsigned long resize_target
;
148 int resize_initiated
;
149 struct rcu_head head
;
150 struct _cds_lfht_node
*tbl
[0];
154 struct rcu_table
*t
; /* shared */
155 cds_lfht_hash_fct hash_fct
;
156 cds_lfht_compare_fct compare_fct
;
157 unsigned long hash_seed
;
158 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
159 unsigned int in_progress_resize
, in_progress_destroy
;
160 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
161 void (*func
)(struct rcu_head
*head
));
162 unsigned long count
; /* global approximate item count */
163 struct ht_items_count
*percpu_count
; /* per-cpu item count */
166 struct rcu_resize_work
{
167 struct rcu_head head
;
172 * Algorithm to reverse bits in a word by lookup table, extended to
175 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
176 * Originally from Public Domain.
179 static const uint8_t BitReverseTable256
[256] =
181 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
182 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
183 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
184 R6(0), R6(2), R6(1), R6(3)
191 uint8_t bit_reverse_u8(uint8_t v
)
193 return BitReverseTable256
[v
];
196 static __attribute__((unused
))
197 uint32_t bit_reverse_u32(uint32_t v
)
199 return ((uint32_t) bit_reverse_u8(v
) << 24) |
200 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
201 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
202 ((uint32_t) bit_reverse_u8(v
>> 24));
205 static __attribute__((unused
))
206 uint64_t bit_reverse_u64(uint64_t v
)
208 return ((uint64_t) bit_reverse_u8(v
) << 56) |
209 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
210 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
211 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
212 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
213 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
214 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
215 ((uint64_t) bit_reverse_u8(v
>> 56));
219 unsigned long bit_reverse_ulong(unsigned long v
)
221 #if (CAA_BITS_PER_LONG == 32)
222 return bit_reverse_u32(v
);
224 return bit_reverse_u64(v
);
229 * fls: returns the position of the most significant bit.
230 * Returns 0 if no bit is set, else returns the position of the most
231 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
233 #if defined(__i386) || defined(__x86_64)
235 unsigned int fls_u32(uint32_t x
)
243 : "=r" (r
) : "rm" (x
));
249 #if defined(__x86_64)
251 unsigned int fls_u64(uint64_t x
)
259 : "=r" (r
) : "rm" (x
));
266 static __attribute__((unused
))
267 unsigned int fls_u64(uint64_t x
)
274 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
278 if (!(x
& 0xFFFF000000000000ULL
)) {
282 if (!(x
& 0xFF00000000000000ULL
)) {
286 if (!(x
& 0xF000000000000000ULL
)) {
290 if (!(x
& 0xC000000000000000ULL
)) {
294 if (!(x
& 0x8000000000000000ULL
)) {
303 static __attribute__((unused
))
304 unsigned int fls_u32(uint32_t x
)
310 if (!(x
& 0xFFFF0000U
)) {
314 if (!(x
& 0xFF000000U
)) {
318 if (!(x
& 0xF0000000U
)) {
322 if (!(x
& 0xC0000000U
)) {
326 if (!(x
& 0x80000000U
)) {
334 unsigned int fls_ulong(unsigned long x
)
336 #if (CAA_BITS_PER_lONG == 32)
343 int get_count_order_u32(uint32_t x
)
347 order
= fls_u32(x
) - 1;
353 int get_count_order_ulong(unsigned long x
)
357 order
= fls_ulong(x
) - 1;
364 void cds_lfht_resize_lazy(struct cds_lfht
*ht
, struct rcu_table
*t
, int growth
);
367 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, struct rcu_table
*t
,
368 unsigned long count
);
371 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
372 * available, then we support hash table item accounting.
373 * In the unfortunate event the number of CPUs reported would be
374 * inaccurate, we use modulo arithmetic on the number of CPUs we got.
377 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
379 static long nr_cpus_mask
= -1;
382 struct ht_items_count
*alloc_per_cpu_items_count(void)
384 struct ht_items_count
*count
;
386 switch (nr_cpus_mask
) {
393 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
399 * round up number of CPUs to next power of two, so we
400 * can use & for modulo.
402 maxcpus
= 1UL << get_count_order_ulong(maxcpus
);
403 nr_cpus_mask
= maxcpus
- 1;
407 return calloc(nr_cpus_mask
+ 1, sizeof(*count
));
412 void free_per_cpu_items_count(struct ht_items_count
*count
)
422 assert(nr_cpus_mask
>= 0);
423 cpu
= sched_getcpu();
424 if (unlikely(cpu
< 0))
427 return cpu
& nr_cpus_mask
;
431 void ht_count_add(struct cds_lfht
*ht
, struct rcu_table
*t
)
433 unsigned long percpu_count
;
436 if (unlikely(!ht
->percpu_count
))
439 if (unlikely(cpu
< 0))
441 percpu_count
= uatomic_add_return(&ht
->percpu_count
[cpu
].add
, 1);
442 if (unlikely(!(percpu_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
445 dbg_printf("add percpu %lu\n", percpu_count
);
446 count
= uatomic_add_return(&ht
->count
,
447 1UL << COUNT_COMMIT_ORDER
);
449 if (!(count
& (count
- 1))) {
450 dbg_printf("add global %lu\n", count
);
451 cds_lfht_resize_lazy_count(ht
, t
, count
);
457 void ht_count_remove(struct cds_lfht
*ht
, struct rcu_table
*t
)
459 unsigned long percpu_count
;
462 if (unlikely(!ht
->percpu_count
))
465 if (unlikely(cpu
< 0))
467 percpu_count
= uatomic_add_return(&ht
->percpu_count
[cpu
].remove
, -1);
468 if (unlikely(!(percpu_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
471 dbg_printf("remove percpu %lu\n", percpu_count
);
472 count
= uatomic_add_return(&ht
->count
,
473 -(1UL << COUNT_COMMIT_ORDER
));
475 if (!(count
& (count
- 1))) {
476 dbg_printf("remove global %lu\n", count
);
477 cds_lfht_resize_lazy_count(ht
, t
, count
);
482 #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
484 static const long nr_cpus_mask
= -1;
487 struct ht_items_count
*alloc_per_cpu_items_count(void)
493 void free_per_cpu_items_count(struct ht_items_count
*count
)
498 void ht_count_add(struct cds_lfht
*ht
)
503 void ht_count_remove(struct cds_lfht
*ht
)
507 #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
511 void check_resize(struct cds_lfht
*ht
, struct rcu_table
*t
,
516 dbg_printf("WARNING: large chain length: %u.\n",
518 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
519 cds_lfht_resize_lazy(ht
, t
,
520 get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
524 struct cds_lfht_node
*clear_flag(struct cds_lfht_node
*node
)
526 return (struct cds_lfht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
530 int is_removed(struct cds_lfht_node
*node
)
532 return ((unsigned long) node
) & REMOVED_FLAG
;
536 struct cds_lfht_node
*flag_removed(struct cds_lfht_node
*node
)
538 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
542 int is_dummy(struct cds_lfht_node
*node
)
544 return ((unsigned long) node
) & DUMMY_FLAG
;
548 struct cds_lfht_node
*flag_dummy(struct cds_lfht_node
*node
)
550 return (struct cds_lfht_node
*) (((unsigned long) node
) | DUMMY_FLAG
);
554 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
556 unsigned long old1
, old2
;
558 old1
= uatomic_read(ptr
);
563 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
568 * Remove all logically deleted nodes from a bucket up to a certain node key.
571 void _cds_lfht_gc_bucket(struct cds_lfht_node
*dummy
, struct cds_lfht_node
*node
)
573 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_next
;
577 /* We can always skip the dummy node initially */
578 iter
= rcu_dereference(iter_prev
->p
.next
);
579 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
581 if (unlikely(!clear_flag(iter
)))
583 if (likely(clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
))
585 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
586 if (likely(is_removed(next
)))
588 iter_prev
= clear_flag(iter
);
591 assert(!is_removed(iter
));
593 new_next
= flag_dummy(clear_flag(next
));
595 new_next
= clear_flag(next
);
596 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
601 struct cds_lfht_node
*_cds_lfht_add(struct cds_lfht
*ht
, struct rcu_table
*t
,
602 struct cds_lfht_node
*node
, int unique
, int dummy
)
604 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
606 struct _cds_lfht_node
*lookup
;
607 unsigned long hash
, index
, order
;
611 node
->p
.next
= flag_dummy(NULL
);
612 return node
; /* Initial first add (head) */
614 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
616 uint32_t chain_len
= 0;
619 * iter_prev points to the non-removed node prior to the
622 index
= hash
& (t
->size
- 1);
623 order
= get_count_order_ulong(index
+ 1);
624 lookup
= &t
->tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
625 iter_prev
= (struct cds_lfht_node
*) lookup
;
626 /* We can always skip the dummy node initially */
627 iter
= rcu_dereference(iter_prev
->p
.next
);
628 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
630 if (unlikely(!clear_flag(iter
)))
632 if (likely(clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
))
634 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
635 if (unlikely(is_removed(next
)))
639 && !ht
->compare_fct(node
->key
, node
->key_len
,
640 clear_flag(iter
)->key
,
641 clear_flag(iter
)->key_len
))
642 return clear_flag(iter
);
643 /* Only account for identical reverse hash once */
644 if (iter_prev
->p
.reverse_hash
!= clear_flag(iter
)->p
.reverse_hash
646 check_resize(ht
, t
, ++chain_len
);
647 iter_prev
= clear_flag(iter
);
651 assert(node
!= clear_flag(iter
));
652 assert(!is_removed(iter_prev
));
653 assert(iter_prev
!= node
);
655 node
->p
.next
= clear_flag(iter
);
657 node
->p
.next
= flag_dummy(clear_flag(iter
));
659 new_node
= flag_dummy(node
);
662 if (uatomic_cmpxchg(&iter_prev
->p
.next
, iter
,
664 continue; /* retry */
668 assert(!is_removed(iter
));
670 new_next
= flag_dummy(clear_flag(next
));
672 new_next
= clear_flag(next
);
673 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
677 /* Garbage collect logically removed nodes in the bucket */
678 index
= hash
& (t
->size
- 1);
679 order
= get_count_order_ulong(index
+ 1);
680 lookup
= &t
->tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
681 dummy_node
= (struct cds_lfht_node
*) lookup
;
682 _cds_lfht_gc_bucket(dummy_node
, node
);
687 int _cds_lfht_remove(struct cds_lfht
*ht
, struct rcu_table
*t
,
688 struct cds_lfht_node
*node
)
690 struct cds_lfht_node
*dummy
, *next
, *old
;
691 struct _cds_lfht_node
*lookup
;
693 unsigned long hash
, index
, order
;
695 /* logically delete the node */
696 old
= rcu_dereference(node
->p
.next
);
699 if (unlikely(is_removed(next
)))
701 assert(!is_dummy(next
));
702 old
= uatomic_cmpxchg(&node
->p
.next
, next
,
704 } while (old
!= next
);
706 /* We performed the (logical) deletion. */
710 * Ensure that the node is not visible to readers anymore: lookup for
711 * the node, and remove it (along with any other logically removed node)
714 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
715 index
= hash
& (t
->size
- 1);
716 order
= get_count_order_ulong(index
+ 1);
717 lookup
= &t
->tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
718 dummy
= (struct cds_lfht_node
*) lookup
;
719 _cds_lfht_gc_bucket(dummy
, node
);
722 * Only the flagging action indicated that we (and no other)
723 * removed the node from the hash.
726 assert(is_removed(rcu_dereference(node
->p
.next
)));
733 void init_table(struct cds_lfht
*ht
, struct rcu_table
*t
,
734 unsigned long first_order
, unsigned long len_order
)
736 unsigned long i
, end_order
;
738 dbg_printf("init table: first_order %lu end_order %lu\n",
739 first_order
, first_order
+ len_order
);
740 end_order
= first_order
+ len_order
;
741 t
->size
= !first_order
? 0 : (1UL << (first_order
- 1));
742 for (i
= first_order
; i
< end_order
; i
++) {
743 unsigned long j
, len
;
745 len
= !i
? 1 : 1UL << (i
- 1);
746 dbg_printf("init order %lu len: %lu\n", i
, len
);
747 t
->tbl
[i
] = calloc(len
, sizeof(struct _cds_lfht_node
));
748 for (j
= 0; j
< len
; j
++) {
749 dbg_printf("init entry: i %lu j %lu hash %lu\n",
750 i
, j
, !i
? 0 : (1UL << (i
- 1)) + j
);
751 struct cds_lfht_node
*new_node
=
752 (struct cds_lfht_node
*) &t
->tbl
[i
][j
];
753 new_node
->p
.reverse_hash
=
754 bit_reverse_ulong(!i
? 0 : (1UL << (i
- 1)) + j
);
755 (void) _cds_lfht_add(ht
, t
, new_node
, 0, 1);
756 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
759 /* Update table size */
760 t
->size
= !i
? 1 : (1UL << i
);
761 dbg_printf("init new size: %lu\n", t
->size
);
762 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
765 t
->resize_target
= t
->size
;
766 t
->resize_initiated
= 0;
769 struct cds_lfht
*cds_lfht_new(cds_lfht_hash_fct hash_fct
,
770 cds_lfht_compare_fct compare_fct
,
771 unsigned long hash_seed
,
772 unsigned long init_size
,
773 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
774 void (*func
)(struct rcu_head
*head
)))
779 /* init_size must be power of two */
780 if (init_size
&& (init_size
& (init_size
- 1)))
782 ht
= calloc(1, sizeof(struct cds_lfht
));
783 ht
->hash_fct
= hash_fct
;
784 ht
->compare_fct
= compare_fct
;
785 ht
->hash_seed
= hash_seed
;
786 ht
->cds_lfht_call_rcu
= cds_lfht_call_rcu
;
787 ht
->in_progress_resize
= 0;
788 ht
->percpu_count
= alloc_per_cpu_items_count();
789 /* this mutex should not nest in read-side C.S. */
790 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
791 order
= get_count_order_ulong(max(init_size
, 1)) + 1;
792 ht
->t
= calloc(1, sizeof(struct cds_lfht
)
793 + (order
* sizeof(struct _cds_lfht_node
*)));
795 pthread_mutex_lock(&ht
->resize_mutex
);
796 init_table(ht
, ht
->t
, 0, order
);
797 pthread_mutex_unlock(&ht
->resize_mutex
);
801 struct cds_lfht_node
*cds_lfht_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
)
804 struct cds_lfht_node
*node
, *next
;
805 struct _cds_lfht_node
*lookup
;
806 unsigned long hash
, reverse_hash
, index
, order
;
808 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
809 reverse_hash
= bit_reverse_ulong(hash
);
811 t
= rcu_dereference(ht
->t
);
812 index
= hash
& (t
->size
- 1);
813 order
= get_count_order_ulong(index
+ 1);
814 lookup
= &t
->tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
815 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
816 hash
, index
, order
, index
& ((1UL << (order
- 1)) - 1));
817 node
= (struct cds_lfht_node
*) lookup
;
821 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
825 next
= rcu_dereference(node
->p
.next
);
826 if (likely(!is_removed(next
))
828 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
831 node
= clear_flag(next
);
833 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
837 struct cds_lfht_node
*cds_lfht_next(struct cds_lfht
*ht
,
838 struct cds_lfht_node
*node
)
840 struct cds_lfht_node
*next
;
841 unsigned long reverse_hash
;
845 reverse_hash
= node
->p
.reverse_hash
;
847 key_len
= node
->key_len
;
848 next
= rcu_dereference(node
->p
.next
);
849 node
= clear_flag(next
);
854 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
858 next
= rcu_dereference(node
->p
.next
);
859 if (likely(!is_removed(next
))
861 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
864 node
= clear_flag(next
);
866 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
870 void cds_lfht_add(struct cds_lfht
*ht
, struct cds_lfht_node
*node
)
875 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
876 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
878 t
= rcu_dereference(ht
->t
);
879 (void) _cds_lfht_add(ht
, t
, node
, 0, 0);
883 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
884 struct cds_lfht_node
*node
)
888 struct cds_lfht_node
*ret
;
890 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
891 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
893 t
= rcu_dereference(ht
->t
);
894 ret
= _cds_lfht_add(ht
, t
, node
, 1, 0);
900 int cds_lfht_remove(struct cds_lfht
*ht
, struct cds_lfht_node
*node
)
905 t
= rcu_dereference(ht
->t
);
906 ret
= _cds_lfht_remove(ht
, t
, node
);
908 ht_count_remove(ht
, t
);
913 int cds_lfht_delete_dummy(struct cds_lfht
*ht
)
916 struct cds_lfht_node
*node
;
917 struct _cds_lfht_node
*lookup
;
918 unsigned long order
, i
;
921 /* Check that the table is empty */
922 lookup
= &t
->tbl
[0][0];
923 node
= (struct cds_lfht_node
*) lookup
;
925 node
= clear_flag(node
)->p
.next
;
928 assert(!is_removed(node
));
929 } while (clear_flag(node
));
930 /* Internal sanity check: all nodes left should be dummy */
931 for (order
= 0; order
< get_count_order_ulong(t
->size
) + 1; order
++) {
934 len
= !order
? 1 : 1UL << (order
- 1);
935 for (i
= 0; i
< len
; i
++) {
936 dbg_printf("delete order %lu i %lu hash %lu\n",
938 bit_reverse_ulong(t
->tbl
[order
][i
].reverse_hash
));
939 assert(is_dummy(t
->tbl
[order
][i
].next
));
947 * Should only be called when no more concurrent readers nor writers can
948 * possibly access the table.
950 int cds_lfht_destroy(struct cds_lfht
*ht
)
954 /* Wait for in-flight resize operations to complete */
955 CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
956 while (uatomic_read(&ht
->in_progress_resize
))
957 poll(NULL
, 0, 100); /* wait for 100ms */
958 ret
= cds_lfht_delete_dummy(ht
);
962 free_per_cpu_items_count(ht
->percpu_count
);
967 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
968 unsigned long *count
,
969 unsigned long *removed
)
972 struct cds_lfht_node
*node
, *next
;
973 struct _cds_lfht_node
*lookup
;
974 unsigned long nr_dummy
= 0;
979 t
= rcu_dereference(ht
->t
);
980 /* Count non-dummy nodes in the table */
981 lookup
= &t
->tbl
[0][0];
982 node
= (struct cds_lfht_node
*) lookup
;
984 next
= rcu_dereference(node
->p
.next
);
985 if (is_removed(next
)) {
986 assert(!is_dummy(next
));
988 } else if (!is_dummy(next
))
992 node
= clear_flag(next
);
994 dbg_printf("number of dummy nodes: %lu\n", nr_dummy
);
998 void cds_lfht_free_table_cb(struct rcu_head
*head
)
1000 struct rcu_table
*t
=
1001 caa_container_of(head
, struct rcu_table
, head
);
1005 /* called with resize mutex held */
1007 void _do_cds_lfht_resize(struct cds_lfht
*ht
)
1009 unsigned long new_size
, old_size
, old_order
, new_order
;
1010 struct rcu_table
*new_t
, *old_t
;
1013 old_size
= old_t
->size
;
1014 old_order
= get_count_order_ulong(old_size
) + 1;
1016 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
1017 if (old_size
== new_size
)
1019 new_order
= get_count_order_ulong(new_size
) + 1;
1020 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1021 old_size
, old_order
, new_size
, new_order
);
1022 new_t
= malloc(sizeof(struct cds_lfht
)
1023 + (new_order
* sizeof(struct _cds_lfht_node
*)));
1024 assert(new_size
> old_size
);
1025 memcpy(&new_t
->tbl
, &old_t
->tbl
,
1026 old_order
* sizeof(struct _cds_lfht_node
*));
1027 init_table(ht
, new_t
, old_order
, new_order
- old_order
);
1028 /* Changing table and size atomically wrt lookups */
1029 rcu_assign_pointer(ht
->t
, new_t
);
1030 ht
->cds_lfht_call_rcu(&old_t
->head
, cds_lfht_free_table_cb
);
1034 unsigned long resize_target_update(struct rcu_table
*t
,
1037 return _uatomic_max(&t
->resize_target
,
1038 t
->size
<< growth_order
);
1042 unsigned long resize_target_update_count(struct rcu_table
*t
,
1043 unsigned long count
)
1045 return _uatomic_max(&t
->resize_target
, count
);
1048 void cds_lfht_resize(struct cds_lfht
*ht
, int growth
)
1050 struct rcu_table
*t
= rcu_dereference(ht
->t
);
1051 unsigned long target_size
;
1055 * Silently refuse to shrink hash table. (not supported)
1057 dbg_printf("shrinking hash table not supported.\n");
1061 target_size
= resize_target_update(t
, growth
);
1062 if (t
->size
< target_size
) {
1063 CMM_STORE_SHARED(t
->resize_initiated
, 1);
1064 pthread_mutex_lock(&ht
->resize_mutex
);
1065 _do_cds_lfht_resize(ht
);
1066 pthread_mutex_unlock(&ht
->resize_mutex
);
1071 void do_resize_cb(struct rcu_head
*head
)
1073 struct rcu_resize_work
*work
=
1074 caa_container_of(head
, struct rcu_resize_work
, head
);
1075 struct cds_lfht
*ht
= work
->ht
;
1077 pthread_mutex_lock(&ht
->resize_mutex
);
1078 _do_cds_lfht_resize(ht
);
1079 pthread_mutex_unlock(&ht
->resize_mutex
);
1081 cmm_smp_mb(); /* finish resize before decrement */
1082 uatomic_dec(&ht
->in_progress_resize
);
1086 void cds_lfht_resize_lazy(struct cds_lfht
*ht
, struct rcu_table
*t
, int growth
)
1088 struct rcu_resize_work
*work
;
1089 unsigned long target_size
;
1091 target_size
= resize_target_update(t
, growth
);
1092 if (!CMM_LOAD_SHARED(t
->resize_initiated
) && t
->size
< target_size
) {
1093 uatomic_inc(&ht
->in_progress_resize
);
1094 cmm_smp_mb(); /* increment resize count before calling it */
1095 work
= malloc(sizeof(*work
));
1097 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1098 CMM_STORE_SHARED(t
->resize_initiated
, 1);
1103 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, struct rcu_table
*t
,
1104 unsigned long count
)
1106 struct rcu_resize_work
*work
;
1107 unsigned long target_size
;
1109 target_size
= resize_target_update_count(t
, count
);
1110 if (!CMM_LOAD_SHARED(t
->resize_initiated
) && t
->size
< target_size
) {
1111 uatomic_inc(&ht
->in_progress_resize
);
1112 cmm_smp_mb(); /* increment resize count before calling it */
1113 work
= malloc(sizeof(*work
));
1115 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1116 CMM_STORE_SHARED(t
->resize_initiated
, 1);