4 * Userspace RCU library - Lock-Free Resizable 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 Resizable 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 for small tables only allows expanding the hash table.
52 * It is triggered automatically by detecting long chains in the add
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.
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
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
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
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.
100 * A bit of ascii art explanation:
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.
105 * This shows the nodes for a small table ordered by reversed bits:
117 * This shows the nodes in order of non-reversed bits, linked by
118 * reversed-bit order.
123 * 1 | 1 001 100 <- <-
125 * 2 | | 2 010 010 | |
126 * | | | 3 011 110 | <- |
128 * 3 -> | | | 4 100 001 | |
144 #include <urcu-call-rcu.h>
145 #include <urcu/arch.h>
146 #include <urcu/uatomic.h>
147 #include <urcu/jhash.h>
148 #include <urcu/compiler.h>
149 #include <urcu/rculfhash.h>
154 #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
156 #define dbg_printf(fmt, args...)
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.
165 #define COUNT_COMMIT_ORDER 10
166 #define CHAIN_LEN_TARGET 1
167 #define CHAIN_LEN_RESIZE_THRESHOLD 3
170 * Define the minimum table size. Protects against hash table resize overload
171 * when too many entries are added quickly before the resize can complete.
172 * This is especially the case if the table could be shrinked to a size of 1.
173 * TODO: we might want to make the add/remove operations help the resize to
174 * add or remove dummy nodes when a resize is ongoing to ensure upper-bound on
177 #define MIN_TABLE_SIZE 128
180 #define max(a, b) ((a) > (b) ? (a) : (b))
184 * The removed flag needs to be updated atomically with the pointer.
185 * The dummy flag does not require to be updated atomically with the
186 * pointer, but it is added as a pointer low bit flag to save space.
188 #define REMOVED_FLAG (1UL << 0)
189 #define DUMMY_FLAG (1UL << 1)
190 #define FLAGS_MASK ((1UL << 2) - 1)
192 struct ht_items_count
{
193 unsigned long add
, remove
;
194 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
197 struct rcu_head head
;
198 struct _cds_lfht_node nodes
[0];
202 unsigned long size
; /* always a power of 2 */
203 unsigned long resize_target
;
204 int resize_initiated
;
205 struct rcu_head head
;
206 struct rcu_level
*tbl
[0];
210 struct rcu_table
*t
; /* shared */
211 cds_lfht_hash_fct hash_fct
;
212 cds_lfht_compare_fct compare_fct
;
213 unsigned long hash_seed
;
215 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
216 unsigned int in_progress_resize
, in_progress_destroy
;
217 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
218 void (*func
)(struct rcu_head
*head
));
219 void (*cds_lfht_synchronize_rcu
)(void);
220 unsigned long count
; /* global approximate item count */
221 struct ht_items_count
*percpu_count
; /* per-cpu item count */
224 struct rcu_resize_work
{
225 struct rcu_head head
;
230 * Algorithm to reverse bits in a word by lookup table, extended to
233 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
234 * Originally from Public Domain.
237 static const uint8_t BitReverseTable256
[256] =
239 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
240 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
241 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
242 R6(0), R6(2), R6(1), R6(3)
249 uint8_t bit_reverse_u8(uint8_t v
)
251 return BitReverseTable256
[v
];
254 static __attribute__((unused
))
255 uint32_t bit_reverse_u32(uint32_t v
)
257 return ((uint32_t) bit_reverse_u8(v
) << 24) |
258 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
259 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
260 ((uint32_t) bit_reverse_u8(v
>> 24));
263 static __attribute__((unused
))
264 uint64_t bit_reverse_u64(uint64_t v
)
266 return ((uint64_t) bit_reverse_u8(v
) << 56) |
267 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
268 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
269 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
270 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
271 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
272 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
273 ((uint64_t) bit_reverse_u8(v
>> 56));
277 unsigned long bit_reverse_ulong(unsigned long v
)
279 #if (CAA_BITS_PER_LONG == 32)
280 return bit_reverse_u32(v
);
282 return bit_reverse_u64(v
);
287 * fls: returns the position of the most significant bit.
288 * Returns 0 if no bit is set, else returns the position of the most
289 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
291 #if defined(__i386) || defined(__x86_64)
293 unsigned int fls_u32(uint32_t x
)
301 : "=r" (r
) : "rm" (x
));
307 #if defined(__x86_64)
309 unsigned int fls_u64(uint64_t x
)
317 : "=r" (r
) : "rm" (x
));
324 static __attribute__((unused
))
325 unsigned int fls_u64(uint64_t x
)
332 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
336 if (!(x
& 0xFFFF000000000000ULL
)) {
340 if (!(x
& 0xFF00000000000000ULL
)) {
344 if (!(x
& 0xF000000000000000ULL
)) {
348 if (!(x
& 0xC000000000000000ULL
)) {
352 if (!(x
& 0x8000000000000000ULL
)) {
361 static __attribute__((unused
))
362 unsigned int fls_u32(uint32_t x
)
368 if (!(x
& 0xFFFF0000U
)) {
372 if (!(x
& 0xFF000000U
)) {
376 if (!(x
& 0xF0000000U
)) {
380 if (!(x
& 0xC0000000U
)) {
384 if (!(x
& 0x80000000U
)) {
392 unsigned int fls_ulong(unsigned long x
)
394 #if (CAA_BITS_PER_lONG == 32)
401 int get_count_order_u32(uint32_t x
)
405 order
= fls_u32(x
) - 1;
411 int get_count_order_ulong(unsigned long x
)
415 order
= fls_ulong(x
) - 1;
422 void cds_lfht_resize_lazy(struct cds_lfht
*ht
, struct rcu_table
*t
, int growth
);
425 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
426 * available, then we support hash table item accounting.
427 * In the unfortunate event the number of CPUs reported would be
428 * inaccurate, we use modulo arithmetic on the number of CPUs we got.
430 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
433 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, struct rcu_table
*t
,
434 unsigned long count
);
436 static long nr_cpus_mask
= -1;
439 struct ht_items_count
*alloc_per_cpu_items_count(void)
441 struct ht_items_count
*count
;
443 switch (nr_cpus_mask
) {
450 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
456 * round up number of CPUs to next power of two, so we
457 * can use & for modulo.
459 maxcpus
= 1UL << get_count_order_ulong(maxcpus
);
460 nr_cpus_mask
= maxcpus
- 1;
464 return calloc(nr_cpus_mask
+ 1, sizeof(*count
));
469 void free_per_cpu_items_count(struct ht_items_count
*count
)
479 assert(nr_cpus_mask
>= 0);
480 cpu
= sched_getcpu();
481 if (unlikely(cpu
< 0))
484 return cpu
& nr_cpus_mask
;
488 void ht_count_add(struct cds_lfht
*ht
, struct rcu_table
*t
)
490 unsigned long percpu_count
;
493 if (unlikely(!ht
->percpu_count
))
496 if (unlikely(cpu
< 0))
498 percpu_count
= uatomic_add_return(&ht
->percpu_count
[cpu
].add
, 1);
499 if (unlikely(!(percpu_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
502 dbg_printf("add percpu %lu\n", percpu_count
);
503 count
= uatomic_add_return(&ht
->count
,
504 1UL << COUNT_COMMIT_ORDER
);
506 if (!(count
& (count
- 1))) {
507 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
)
510 dbg_printf("add set global %lu\n", count
);
511 cds_lfht_resize_lazy_count(ht
, t
,
512 count
>> (CHAIN_LEN_TARGET
- 1));
518 void ht_count_remove(struct cds_lfht
*ht
, struct rcu_table
*t
)
520 unsigned long percpu_count
;
523 if (unlikely(!ht
->percpu_count
))
526 if (unlikely(cpu
< 0))
528 percpu_count
= uatomic_add_return(&ht
->percpu_count
[cpu
].remove
, -1);
529 if (unlikely(!(percpu_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
532 dbg_printf("remove percpu %lu\n", percpu_count
);
533 count
= uatomic_add_return(&ht
->count
,
534 -(1UL << COUNT_COMMIT_ORDER
));
536 if (!(count
& (count
- 1))) {
537 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
)
540 dbg_printf("remove set global %lu\n", count
);
541 cds_lfht_resize_lazy_count(ht
, t
,
542 count
>> (CHAIN_LEN_TARGET
- 1));
547 #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
549 static const long nr_cpus_mask
= -1;
552 struct ht_items_count
*alloc_per_cpu_items_count(void)
558 void free_per_cpu_items_count(struct ht_items_count
*count
)
563 void ht_count_add(struct cds_lfht
*ht
, struct rcu_table
*t
)
568 void ht_count_remove(struct cds_lfht
*ht
, struct rcu_table
*t
)
572 #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
576 void check_resize(struct cds_lfht
*ht
, struct rcu_table
*t
,
581 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
583 count
= uatomic_read(&ht
->count
);
585 * Use bucket-local length for small table expand and for
586 * environments lacking per-cpu data support.
588 if (count
>= (1UL << COUNT_COMMIT_ORDER
))
591 dbg_printf("WARNING: large chain length: %u.\n",
593 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
594 cds_lfht_resize_lazy(ht
, t
,
595 get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
599 struct cds_lfht_node
*clear_flag(struct cds_lfht_node
*node
)
601 return (struct cds_lfht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
605 int is_removed(struct cds_lfht_node
*node
)
607 return ((unsigned long) node
) & REMOVED_FLAG
;
611 struct cds_lfht_node
*flag_removed(struct cds_lfht_node
*node
)
613 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
617 int is_dummy(struct cds_lfht_node
*node
)
619 return ((unsigned long) node
) & DUMMY_FLAG
;
623 struct cds_lfht_node
*flag_dummy(struct cds_lfht_node
*node
)
625 return (struct cds_lfht_node
*) (((unsigned long) node
) | DUMMY_FLAG
);
629 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
631 unsigned long old1
, old2
;
633 old1
= uatomic_read(ptr
);
638 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
643 void cds_lfht_free_table_cb(struct rcu_head
*head
)
645 struct rcu_table
*t
=
646 caa_container_of(head
, struct rcu_table
, head
);
651 void cds_lfht_free_level(struct rcu_head
*head
)
653 struct rcu_level
*l
=
654 caa_container_of(head
, struct rcu_level
, head
);
659 * Remove all logically deleted nodes from a bucket up to a certain node key.
662 void _cds_lfht_gc_bucket(struct cds_lfht_node
*dummy
, struct cds_lfht_node
*node
)
664 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_next
;
666 assert(!is_dummy(dummy
));
667 assert(!is_removed(dummy
));
668 assert(!is_dummy(node
));
669 assert(!is_removed(node
));
672 /* We can always skip the dummy node initially */
673 iter
= rcu_dereference(iter_prev
->p
.next
);
674 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
676 * We should never be called with dummy (start of chain)
677 * and logically removed node (end of path compression
678 * marker) being the actual same node. This would be a
679 * bug in the algorithm implementation.
681 assert(dummy
!= node
);
683 if (unlikely(!clear_flag(iter
)))
685 if (likely(clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
))
687 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
688 if (likely(is_removed(next
)))
690 iter_prev
= clear_flag(iter
);
693 assert(!is_removed(iter
));
695 new_next
= flag_dummy(clear_flag(next
));
697 new_next
= clear_flag(next
);
698 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
703 struct cds_lfht_node
*_cds_lfht_add(struct cds_lfht
*ht
, struct rcu_table
*t
,
704 struct cds_lfht_node
*node
, int unique
, int dummy
)
706 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
708 struct _cds_lfht_node
*lookup
;
709 unsigned long hash
, index
, order
;
711 assert(!is_dummy(node
));
712 assert(!is_removed(node
));
715 node
->p
.next
= flag_dummy(NULL
);
716 return node
; /* Initial first add (head) */
718 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
720 uint32_t chain_len
= 0;
723 * iter_prev points to the non-removed node prior to the
726 index
= hash
& (t
->size
- 1);
727 order
= get_count_order_ulong(index
+ 1);
728 lookup
= &t
->tbl
[order
]->nodes
[index
& ((!order
? 0 : (1UL << (order
- 1))) - 1)];
729 iter_prev
= (struct cds_lfht_node
*) lookup
;
730 /* We can always skip the dummy node initially */
731 iter
= rcu_dereference(iter_prev
->p
.next
);
732 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
734 /* TODO: check if removed */
735 if (unlikely(!clear_flag(iter
)))
737 /* TODO: check if removed */
738 if (likely(clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
))
740 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
741 if (unlikely(is_removed(next
)))
745 && !ht
->compare_fct(node
->key
, node
->key_len
,
746 clear_flag(iter
)->key
,
747 clear_flag(iter
)->key_len
))
748 return clear_flag(iter
);
749 /* Only account for identical reverse hash once */
750 if (iter_prev
->p
.reverse_hash
!= clear_flag(iter
)->p
.reverse_hash
752 check_resize(ht
, t
, ++chain_len
);
753 iter_prev
= clear_flag(iter
);
757 assert(node
!= clear_flag(iter
));
758 assert(!is_removed(iter_prev
));
759 assert(!is_removed(iter
));
760 assert(iter_prev
!= node
);
762 node
->p
.next
= clear_flag(iter
);
764 node
->p
.next
= flag_dummy(clear_flag(iter
));
766 new_node
= flag_dummy(node
);
769 if (uatomic_cmpxchg(&iter_prev
->p
.next
, iter
,
771 continue; /* retry */
775 assert(!is_removed(iter
));
777 new_next
= flag_dummy(clear_flag(next
));
779 new_next
= clear_flag(next
);
780 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
784 /* Garbage collect logically removed nodes in the bucket */
785 index
= hash
& (t
->size
- 1);
786 order
= get_count_order_ulong(index
+ 1);
787 lookup
= &t
->tbl
[order
]->nodes
[index
& (!order
? 0 : ((1UL << (order
- 1)) - 1))];
788 dummy_node
= (struct cds_lfht_node
*) lookup
;
789 _cds_lfht_gc_bucket(dummy_node
, node
);
794 int _cds_lfht_remove(struct cds_lfht
*ht
, struct rcu_table
*t
,
795 struct cds_lfht_node
*node
, int dummy_removal
)
797 struct cds_lfht_node
*dummy
, *next
, *old
;
798 struct _cds_lfht_node
*lookup
;
800 unsigned long hash
, index
, order
;
802 /* logically delete the node */
803 assert(!is_dummy(node
));
804 assert(!is_removed(node
));
805 old
= rcu_dereference(node
->p
.next
);
808 if (unlikely(is_removed(next
)))
811 assert(is_dummy(next
));
813 assert(!is_dummy(next
));
814 old
= uatomic_cmpxchg(&node
->p
.next
, next
,
816 } while (old
!= next
);
818 /* We performed the (logical) deletion. */
822 * Ensure that the node is not visible to readers anymore: lookup for
823 * the node, and remove it (along with any other logically removed node)
826 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
828 index
= hash
& (t
->size
- 1);
829 order
= get_count_order_ulong(index
+ 1);
830 lookup
= &t
->tbl
[order
]->nodes
[index
& (!order
? 0 : ((1UL << (order
- 1)) - 1))];
831 dummy
= (struct cds_lfht_node
*) lookup
;
832 _cds_lfht_gc_bucket(dummy
, node
);
835 * Only the flagging action indicated that we (and no other)
836 * removed the node from the hash.
839 assert(is_removed(rcu_dereference(node
->p
.next
)));
846 void init_table(struct cds_lfht
*ht
, struct rcu_table
*t
,
847 unsigned long first_order
, unsigned long len_order
)
849 unsigned long i
, end_order
;
851 dbg_printf("init table: first_order %lu end_order %lu\n",
852 first_order
, first_order
+ len_order
);
853 end_order
= first_order
+ len_order
;
854 t
->size
= !first_order
? 0 : (1UL << (first_order
- 1));
855 for (i
= first_order
; i
< end_order
; i
++) {
856 unsigned long j
, len
;
858 len
= !i
? 1 : 1UL << (i
- 1);
859 dbg_printf("init order %lu len: %lu\n", i
, len
);
860 t
->tbl
[i
] = calloc(1, sizeof(struct rcu_level
)
861 + (len
* sizeof(struct _cds_lfht_node
)));
862 for (j
= 0; j
< len
; j
++) {
863 struct cds_lfht_node
*new_node
=
864 (struct cds_lfht_node
*) &t
->tbl
[i
]->nodes
[j
];
866 dbg_printf("init entry: i %lu j %lu hash %lu\n",
867 i
, j
, !i
? 0 : (1UL << (i
- 1)) + j
);
868 new_node
->p
.reverse_hash
=
869 bit_reverse_ulong(!i
? 0 : (1UL << (i
- 1)) + j
);
870 (void) _cds_lfht_add(ht
, t
, new_node
, 0, 1);
871 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
874 /* Update table size */
875 t
->size
= !i
? 1 : (1UL << i
);
876 dbg_printf("init new size: %lu\n", t
->size
);
877 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
880 t
->resize_target
= t
->size
;
881 t
->resize_initiated
= 0;
885 void fini_table(struct cds_lfht
*ht
, struct rcu_table
*t
,
886 unsigned long first_order
, unsigned long len_order
)
890 dbg_printf("fini table: first_order %lu end_order %lu\n",
891 first_order
, first_order
+ len_order
);
892 end_order
= first_order
+ len_order
;
893 assert(first_order
> 0);
894 assert(t
->size
== (1UL << (end_order
- 1)));
895 for (i
= end_order
- 1; i
>= first_order
; i
--) {
896 unsigned long j
, len
;
898 len
= !i
? 1 : 1UL << (i
- 1);
899 dbg_printf("fini order %lu len: %lu\n", i
, len
);
901 * Update table size. Need to shrink this table prior to
902 * removal so gc lookups use non-logically-removed dummy
905 t
->size
= 1UL << (i
- 1);
907 for (j
= 0; j
< len
; j
++) {
908 struct cds_lfht_node
*fini_node
=
909 (struct cds_lfht_node
*) &t
->tbl
[i
]->nodes
[j
];
911 dbg_printf("fini entry: i %lu j %lu hash %lu\n",
912 i
, j
, !i
? 0 : (1UL << (i
- 1)) + j
);
913 fini_node
->p
.reverse_hash
=
914 bit_reverse_ulong(!i
? 0 : (1UL << (i
- 1)) + j
);
915 (void) _cds_lfht_remove(ht
, t
, fini_node
, 1);
916 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
919 ht
->cds_lfht_call_rcu(&t
->tbl
[i
]->head
, cds_lfht_free_level
);
920 dbg_printf("fini new size: %lu\n", t
->size
);
921 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
924 t
->resize_target
= t
->size
;
925 t
->resize_initiated
= 0;
928 struct cds_lfht
*cds_lfht_new(cds_lfht_hash_fct hash_fct
,
929 cds_lfht_compare_fct compare_fct
,
930 unsigned long hash_seed
,
931 unsigned long init_size
,
933 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
934 void (*func
)(struct rcu_head
*head
)),
935 void (*cds_lfht_synchronize_rcu
)(void))
940 /* init_size must be power of two */
941 if (init_size
&& (init_size
& (init_size
- 1)))
943 ht
= calloc(1, sizeof(struct cds_lfht
));
944 ht
->hash_fct
= hash_fct
;
945 ht
->compare_fct
= compare_fct
;
946 ht
->hash_seed
= hash_seed
;
947 ht
->cds_lfht_call_rcu
= cds_lfht_call_rcu
;
948 ht
->cds_lfht_synchronize_rcu
= cds_lfht_synchronize_rcu
;
949 ht
->in_progress_resize
= 0;
950 ht
->percpu_count
= alloc_per_cpu_items_count();
951 /* this mutex should not nest in read-side C.S. */
952 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
953 order
= get_count_order_ulong(max(init_size
, MIN_TABLE_SIZE
)) + 1;
954 ht
->t
= calloc(1, sizeof(struct cds_lfht
)
955 + (order
* sizeof(struct rcu_level
*)));
958 pthread_mutex_lock(&ht
->resize_mutex
);
959 init_table(ht
, ht
->t
, 0, order
);
960 pthread_mutex_unlock(&ht
->resize_mutex
);
964 struct cds_lfht_node
*cds_lfht_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
)
967 struct cds_lfht_node
*node
, *next
;
968 struct _cds_lfht_node
*lookup
;
969 unsigned long hash
, reverse_hash
, index
, order
;
971 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
972 reverse_hash
= bit_reverse_ulong(hash
);
974 t
= rcu_dereference(ht
->t
);
975 index
= hash
& (t
->size
- 1);
976 order
= get_count_order_ulong(index
+ 1);
977 lookup
= &t
->tbl
[order
]->nodes
[index
& (!order
? 0 : ((1UL << (order
- 1))) - 1)];
978 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
979 hash
, index
, order
, index
& (!order
? 0 : ((1UL << (order
- 1)) - 1)));
980 node
= (struct cds_lfht_node
*) lookup
;
984 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
988 next
= rcu_dereference(node
->p
.next
);
989 if (likely(!is_removed(next
))
991 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
994 node
= clear_flag(next
);
996 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
1000 struct cds_lfht_node
*cds_lfht_next(struct cds_lfht
*ht
,
1001 struct cds_lfht_node
*node
)
1003 struct cds_lfht_node
*next
;
1004 unsigned long reverse_hash
;
1008 reverse_hash
= node
->p
.reverse_hash
;
1010 key_len
= node
->key_len
;
1011 next
= rcu_dereference(node
->p
.next
);
1012 node
= clear_flag(next
);
1015 if (unlikely(!node
))
1017 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
1021 next
= rcu_dereference(node
->p
.next
);
1022 if (likely(!is_removed(next
))
1024 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
1027 node
= clear_flag(next
);
1029 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
1033 void cds_lfht_add(struct cds_lfht
*ht
, struct cds_lfht_node
*node
)
1035 struct rcu_table
*t
;
1038 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
1039 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1041 t
= rcu_dereference(ht
->t
);
1042 (void) _cds_lfht_add(ht
, t
, node
, 0, 0);
1043 ht_count_add(ht
, t
);
1046 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
1047 struct cds_lfht_node
*node
)
1049 struct rcu_table
*t
;
1051 struct cds_lfht_node
*ret
;
1053 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
1054 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1056 t
= rcu_dereference(ht
->t
);
1057 ret
= _cds_lfht_add(ht
, t
, node
, 1, 0);
1059 ht_count_add(ht
, t
);
1063 int cds_lfht_remove(struct cds_lfht
*ht
, struct cds_lfht_node
*node
)
1065 struct rcu_table
*t
;
1068 t
= rcu_dereference(ht
->t
);
1069 ret
= _cds_lfht_remove(ht
, t
, node
, 0);
1071 ht_count_remove(ht
, t
);
1076 int cds_lfht_delete_dummy(struct cds_lfht
*ht
)
1078 struct rcu_table
*t
;
1079 struct cds_lfht_node
*node
;
1080 struct _cds_lfht_node
*lookup
;
1081 unsigned long order
, i
;
1084 /* Check that the table is empty */
1085 lookup
= &t
->tbl
[0]->nodes
[0];
1086 node
= (struct cds_lfht_node
*) lookup
;
1088 node
= clear_flag(node
)->p
.next
;
1089 if (!is_dummy(node
))
1091 assert(!is_removed(node
));
1092 } while (clear_flag(node
));
1093 /* Internal sanity check: all nodes left should be dummy */
1094 for (order
= 0; order
< get_count_order_ulong(t
->size
) + 1; order
++) {
1097 len
= !order
? 1 : 1UL << (order
- 1);
1098 for (i
= 0; i
< len
; i
++) {
1099 dbg_printf("delete order %lu i %lu hash %lu\n",
1101 bit_reverse_ulong(t
->tbl
[order
]->nodes
[i
].reverse_hash
));
1102 assert(is_dummy(t
->tbl
[order
]->nodes
[i
].next
));
1104 free(t
->tbl
[order
]);
1110 * Should only be called when no more concurrent readers nor writers can
1111 * possibly access the table.
1113 int cds_lfht_destroy(struct cds_lfht
*ht
)
1117 /* Wait for in-flight resize operations to complete */
1118 CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
1119 while (uatomic_read(&ht
->in_progress_resize
))
1120 poll(NULL
, 0, 100); /* wait for 100ms */
1121 ret
= cds_lfht_delete_dummy(ht
);
1125 free_per_cpu_items_count(ht
->percpu_count
);
1130 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
1131 unsigned long *count
,
1132 unsigned long *removed
)
1134 struct rcu_table
*t
;
1135 struct cds_lfht_node
*node
, *next
;
1136 struct _cds_lfht_node
*lookup
;
1137 unsigned long nr_dummy
= 0;
1142 t
= rcu_dereference(ht
->t
);
1143 /* Count non-dummy nodes in the table */
1144 lookup
= &t
->tbl
[0]->nodes
[0];
1145 node
= (struct cds_lfht_node
*) lookup
;
1147 next
= rcu_dereference(node
->p
.next
);
1148 if (is_removed(next
)) {
1149 assert(!is_dummy(next
));
1151 } else if (!is_dummy(next
))
1155 node
= clear_flag(next
);
1157 dbg_printf("number of dummy nodes: %lu\n", nr_dummy
);
1160 /* called with resize mutex held */
1162 void _do_cds_lfht_grow(struct cds_lfht
*ht
, struct rcu_table
*old_t
,
1163 unsigned long old_size
, unsigned long new_size
)
1165 unsigned long old_order
, new_order
;
1166 struct rcu_table
*new_t
;
1168 old_order
= get_count_order_ulong(old_size
) + 1;
1169 new_order
= get_count_order_ulong(new_size
) + 1;
1170 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1171 old_size
, old_order
, new_size
, new_order
);
1172 new_t
= malloc(sizeof(struct cds_lfht
)
1173 + (new_order
* sizeof(struct rcu_level
*)));
1174 assert(new_size
> old_size
);
1175 memcpy(&new_t
->tbl
, &old_t
->tbl
,
1176 old_order
* sizeof(struct rcu_level
*));
1177 init_table(ht
, new_t
, old_order
, new_order
- old_order
);
1178 /* Changing table and size atomically wrt lookups */
1179 rcu_assign_pointer(ht
->t
, new_t
);
1180 ht
->cds_lfht_call_rcu(&old_t
->head
, cds_lfht_free_table_cb
);
1183 /* called with resize mutex held */
1185 void _do_cds_lfht_shrink(struct cds_lfht
*ht
, struct rcu_table
*old_t
,
1186 unsigned long old_size
, unsigned long new_size
)
1188 unsigned long old_order
, new_order
;
1189 struct rcu_table
*new_t
;
1191 new_size
= max(new_size
, MIN_TABLE_SIZE
);
1192 old_order
= get_count_order_ulong(old_size
) + 1;
1193 new_order
= get_count_order_ulong(new_size
) + 1;
1194 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1195 old_size
, old_order
, new_size
, new_order
);
1196 new_t
= malloc(sizeof(struct cds_lfht
)
1197 + (new_order
* sizeof(struct rcu_level
*)));
1198 assert(new_size
< old_size
);
1199 memcpy(&new_t
->tbl
, &old_t
->tbl
,
1200 new_order
* sizeof(struct rcu_level
*));
1201 new_t
->size
= !new_order
? 1 : (1UL << (new_order
- 1));
1202 assert(new_t
->size
== new_size
);
1203 new_t
->resize_target
= new_t
->size
;
1204 new_t
->resize_initiated
= 0;
1206 /* Changing table and size atomically wrt lookups */
1207 rcu_assign_pointer(ht
->t
, new_t
);
1210 * We need to wait for all add operations to reach Q.S. (and
1211 * thus use the new table for lookups) before we can start
1212 * releasing the old dummy nodes. Otherwise their lookup will
1213 * return a logically removed node as insert position.
1215 ht
->cds_lfht_synchronize_rcu();
1217 /* Unlink and remove all now-unused dummy node pointers. */
1218 fini_table(ht
, old_t
, new_order
, old_order
- new_order
);
1219 ht
->cds_lfht_call_rcu(&old_t
->head
, cds_lfht_free_table_cb
);
1223 /* called with resize mutex held */
1225 void _do_cds_lfht_resize(struct cds_lfht
*ht
)
1227 unsigned long new_size
, old_size
;
1228 struct rcu_table
*old_t
;
1231 old_size
= old_t
->size
;
1232 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
1233 if (old_size
< new_size
)
1234 _do_cds_lfht_grow(ht
, old_t
, old_size
, new_size
);
1235 else if (old_size
> new_size
)
1236 _do_cds_lfht_shrink(ht
, old_t
, old_size
, new_size
);
1238 CMM_STORE_SHARED(old_t
->resize_initiated
, 0);
1242 unsigned long resize_target_update(struct rcu_table
*t
,
1245 return _uatomic_max(&t
->resize_target
,
1246 t
->size
<< growth_order
);
1250 void resize_target_update_count(struct rcu_table
*t
,
1251 unsigned long count
)
1253 count
= max(count
, MIN_TABLE_SIZE
);
1254 uatomic_set(&t
->resize_target
, count
);
1257 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
)
1259 struct rcu_table
*t
= rcu_dereference(ht
->t
);
1261 resize_target_update_count(t
, new_size
);
1262 CMM_STORE_SHARED(t
->resize_initiated
, 1);
1263 pthread_mutex_lock(&ht
->resize_mutex
);
1264 _do_cds_lfht_resize(ht
);
1265 pthread_mutex_unlock(&ht
->resize_mutex
);
1269 void do_resize_cb(struct rcu_head
*head
)
1271 struct rcu_resize_work
*work
=
1272 caa_container_of(head
, struct rcu_resize_work
, head
);
1273 struct cds_lfht
*ht
= work
->ht
;
1275 pthread_mutex_lock(&ht
->resize_mutex
);
1276 _do_cds_lfht_resize(ht
);
1277 pthread_mutex_unlock(&ht
->resize_mutex
);
1279 cmm_smp_mb(); /* finish resize before decrement */
1280 uatomic_dec(&ht
->in_progress_resize
);
1284 void cds_lfht_resize_lazy(struct cds_lfht
*ht
, struct rcu_table
*t
, int growth
)
1286 struct rcu_resize_work
*work
;
1287 unsigned long target_size
;
1289 target_size
= resize_target_update(t
, growth
);
1290 if (!CMM_LOAD_SHARED(t
->resize_initiated
) && t
->size
< target_size
) {
1291 uatomic_inc(&ht
->in_progress_resize
);
1292 cmm_smp_mb(); /* increment resize count before calling it */
1293 work
= malloc(sizeof(*work
));
1295 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1296 CMM_STORE_SHARED(t
->resize_initiated
, 1);
1300 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
1303 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, struct rcu_table
*t
,
1304 unsigned long count
)
1306 struct rcu_resize_work
*work
;
1308 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
1310 resize_target_update_count(t
, count
);
1311 if (!CMM_LOAD_SHARED(t
->resize_initiated
)) {
1312 uatomic_inc(&ht
->in_progress_resize
);
1313 cmm_smp_mb(); /* increment resize count before calling it */
1314 work
= malloc(sizeof(*work
));
1316 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1317 CMM_STORE_SHARED(t
->resize_initiated
, 1);