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 * - 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
94 * this order (except for order 0).
95 * - synchronzie_rcu is used to garbage-collect the old dummy node 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.
102 * hash table hash table the last all dummy node tables
103 * order size dummy node 0 1 2 3 4 5 6(index)
110 * 5 32 16 1 1 2 4 8 16
111 * 6 64 32 1 1 2 4 8 16 32
113 * When growing/shrinking, we only focus on the last dummy node table
114 * which size is (!order ? 1 : (1 << (order -1))).
116 * Example for growing/shrinking:
117 * grow hash table from order 5 to 6: init the index=6 dummy node table
118 * shrink hash table from order 6 to 5: fini the index=6 dummy node table
120 * A bit of ascii art explanation:
122 * Order index is the off-by-one compare to the actual power of 2 because
123 * we use index 0 to deal with the 0 special-case.
125 * This shows the nodes for a small table ordered by reversed bits:
137 * This shows the nodes in order of non-reversed bits, linked by
138 * reversed-bit order.
143 * 2 | | 2 010 010 <- |
144 * | | | 3 011 110 | <- |
145 * 3 -> | | | 4 100 001 | |
161 #include <urcu-call-rcu.h>
162 #include <urcu/arch.h>
163 #include <urcu/uatomic.h>
164 #include <urcu/compiler.h>
165 #include <urcu/rculfhash.h>
170 #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
172 #define dbg_printf(fmt, args...)
176 * Split-counters lazily update the global counter each 1024
177 * addition/removal. It automatically keeps track of resize required.
178 * We use the bucket length as indicator for need to expand for small
179 * tables and machines lacking per-cpu data suppport.
181 #define COUNT_COMMIT_ORDER 10
182 #define DEFAULT_SPLIT_COUNT_MASK 0xFUL
183 #define CHAIN_LEN_TARGET 1
184 #define CHAIN_LEN_RESIZE_THRESHOLD 3
187 * Define the minimum table size.
189 #define MIN_TABLE_SIZE 1
191 #if (CAA_BITS_PER_LONG == 32)
192 #define MAX_TABLE_ORDER 32
194 #define MAX_TABLE_ORDER 64
198 * Minimum number of dummy nodes to touch per thread to parallelize grow/shrink.
200 #define MIN_PARTITION_PER_THREAD_ORDER 12
201 #define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
204 #define min(a, b) ((a) < (b) ? (a) : (b))
208 #define max(a, b) ((a) > (b) ? (a) : (b))
212 * The removed flag needs to be updated atomically with the pointer.
213 * It indicates that no node must attach to the node scheduled for
214 * removal, and that node garbage collection must be performed.
215 * The dummy flag does not require to be updated atomically with the
216 * pointer, but it is added as a pointer low bit flag to save space.
218 #define REMOVED_FLAG (1UL << 0)
219 #define DUMMY_FLAG (1UL << 1)
220 #define FLAGS_MASK ((1UL << 2) - 1)
222 /* Value of the end pointer. Should not interact with flags. */
223 #define END_VALUE NULL
226 * ht_items_count: Split-counters counting the number of node addition
227 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
228 * is set at hash table creation.
230 * These are free-running counters, never reset to zero. They count the
231 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
232 * operations to update the global counter. We choose a power-of-2 value
233 * for the trigger to deal with 32 or 64-bit overflow of the counter.
235 struct ht_items_count
{
236 unsigned long add
, del
;
237 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
240 * rcu_level: Contains the per order-index-level dummy node table. The
241 * size of each dummy node table is half the number of hashes contained
242 * in this order (except for order 0). The minimum allocation size
243 * parameter allows combining the dummy node arrays of the lowermost
244 * levels to improve cache locality for small index orders.
247 /* Note: manually update allocation length when adding a field */
248 struct _cds_lfht_node nodes
[0];
252 * rcu_table: Contains the size and desired new size if a resize
253 * operation is in progress, as well as the statically-sized array of
254 * rcu_level pointers.
257 unsigned long size
; /* always a power of 2, shared (RCU) */
258 unsigned long resize_target
;
259 int resize_initiated
;
260 struct rcu_level
*tbl
[MAX_TABLE_ORDER
];
264 * cds_lfht: Top-level data structure representing a lock-free hash
265 * table. Defined in the implementation file to make it be an opaque
270 unsigned long min_alloc_order
;
271 unsigned long min_alloc_size
;
274 * We need to put the work threads offline (QSBR) when taking this
275 * mutex, because we use synchronize_rcu within this mutex critical
276 * section, which waits on read-side critical sections, and could
277 * therefore cause grace-period deadlock if we hold off RCU G.P.
280 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
281 unsigned int in_progress_resize
, in_progress_destroy
;
282 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
283 void (*func
)(struct rcu_head
*head
));
284 void (*cds_lfht_synchronize_rcu
)(void);
285 void (*cds_lfht_rcu_read_lock
)(void);
286 void (*cds_lfht_rcu_read_unlock
)(void);
287 void (*cds_lfht_rcu_thread_offline
)(void);
288 void (*cds_lfht_rcu_thread_online
)(void);
289 void (*cds_lfht_rcu_register_thread
)(void);
290 void (*cds_lfht_rcu_unregister_thread
)(void);
291 pthread_attr_t
*resize_attr
; /* Resize threads attributes */
292 long count
; /* global approximate item count */
293 struct ht_items_count
*split_count
; /* split item count */
297 * rcu_resize_work: Contains arguments passed to RCU worker thread
298 * responsible for performing lazy resize.
300 struct rcu_resize_work
{
301 struct rcu_head head
;
306 * partition_resize_work: Contains arguments passed to worker threads
307 * executing the hash table resize on partitions of the hash table
308 * assigned to each processor's worker thread.
310 struct partition_resize_work
{
313 unsigned long i
, start
, len
;
314 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
315 unsigned long start
, unsigned long len
);
319 void _cds_lfht_add(struct cds_lfht
*ht
,
320 cds_lfht_match_fct match
,
322 struct cds_lfht_node
*node
,
323 struct cds_lfht_iter
*unique_ret
,
327 * Algorithm to reverse bits in a word by lookup table, extended to
330 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
331 * Originally from Public Domain.
334 static const uint8_t BitReverseTable256
[256] =
336 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
337 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
338 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
339 R6(0), R6(2), R6(1), R6(3)
346 uint8_t bit_reverse_u8(uint8_t v
)
348 return BitReverseTable256
[v
];
351 static __attribute__((unused
))
352 uint32_t bit_reverse_u32(uint32_t v
)
354 return ((uint32_t) bit_reverse_u8(v
) << 24) |
355 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
356 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
357 ((uint32_t) bit_reverse_u8(v
>> 24));
360 static __attribute__((unused
))
361 uint64_t bit_reverse_u64(uint64_t v
)
363 return ((uint64_t) bit_reverse_u8(v
) << 56) |
364 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
365 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
366 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
367 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
368 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
369 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
370 ((uint64_t) bit_reverse_u8(v
>> 56));
374 unsigned long bit_reverse_ulong(unsigned long v
)
376 #if (CAA_BITS_PER_LONG == 32)
377 return bit_reverse_u32(v
);
379 return bit_reverse_u64(v
);
384 * fls: returns the position of the most significant bit.
385 * Returns 0 if no bit is set, else returns the position of the most
386 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
388 #if defined(__i386) || defined(__x86_64)
390 unsigned int fls_u32(uint32_t x
)
398 : "=r" (r
) : "rm" (x
));
404 #if defined(__x86_64)
406 unsigned int fls_u64(uint64_t x
)
414 : "=r" (r
) : "rm" (x
));
421 static __attribute__((unused
))
422 unsigned int fls_u64(uint64_t x
)
429 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
433 if (!(x
& 0xFFFF000000000000ULL
)) {
437 if (!(x
& 0xFF00000000000000ULL
)) {
441 if (!(x
& 0xF000000000000000ULL
)) {
445 if (!(x
& 0xC000000000000000ULL
)) {
449 if (!(x
& 0x8000000000000000ULL
)) {
458 static __attribute__((unused
))
459 unsigned int fls_u32(uint32_t x
)
465 if (!(x
& 0xFFFF0000U
)) {
469 if (!(x
& 0xFF000000U
)) {
473 if (!(x
& 0xF0000000U
)) {
477 if (!(x
& 0xC0000000U
)) {
481 if (!(x
& 0x80000000U
)) {
489 unsigned int fls_ulong(unsigned long x
)
491 #if (CAA_BITS_PER_LONG == 32)
499 * Return the minimum order for which x <= (1UL << order).
500 * Return -1 if x is 0.
502 int get_count_order_u32(uint32_t x
)
507 return fls_u32(x
- 1);
511 * Return the minimum order for which x <= (1UL << order).
512 * Return -1 if x is 0.
514 int get_count_order_ulong(unsigned long x
)
519 return fls_ulong(x
- 1);
523 #define poison_free(ptr) \
526 memset(ptr, 0x42, sizeof(*(ptr))); \
531 #define poison_free(ptr) free(ptr)
535 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
);
538 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
539 unsigned long count
);
541 static long nr_cpus_mask
= -1;
542 static long split_count_mask
= -1;
544 #if defined(HAVE_SYSCONF)
545 static void ht_init_nr_cpus_mask(void)
549 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
555 * round up number of CPUs to next power of two, so we
556 * can use & for modulo.
558 maxcpus
= 1UL << get_count_order_ulong(maxcpus
);
559 nr_cpus_mask
= maxcpus
- 1;
561 #else /* #if defined(HAVE_SYSCONF) */
562 static void ht_init_nr_cpus_mask(void)
566 #endif /* #else #if defined(HAVE_SYSCONF) */
569 void alloc_split_items_count(struct cds_lfht
*ht
)
571 struct ht_items_count
*count
;
573 if (nr_cpus_mask
== -1) {
574 ht_init_nr_cpus_mask();
575 if (nr_cpus_mask
< 0)
576 split_count_mask
= DEFAULT_SPLIT_COUNT_MASK
;
578 split_count_mask
= nr_cpus_mask
;
581 assert(split_count_mask
>= 0);
583 if (ht
->flags
& CDS_LFHT_ACCOUNTING
) {
584 ht
->split_count
= calloc(split_count_mask
+ 1, sizeof(*count
));
585 assert(ht
->split_count
);
587 ht
->split_count
= NULL
;
592 void free_split_items_count(struct cds_lfht
*ht
)
594 poison_free(ht
->split_count
);
597 #if defined(HAVE_SCHED_GETCPU)
599 int ht_get_split_count_index(unsigned long hash
)
603 assert(split_count_mask
>= 0);
604 cpu
= sched_getcpu();
605 if (caa_unlikely(cpu
< 0))
606 return hash
& split_count_mask
;
608 return cpu
& split_count_mask
;
610 #else /* #if defined(HAVE_SCHED_GETCPU) */
612 int ht_get_split_count_index(unsigned long hash
)
614 return hash
& split_count_mask
;
616 #endif /* #else #if defined(HAVE_SCHED_GETCPU) */
619 void ht_count_add(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
621 unsigned long split_count
;
624 if (caa_unlikely(!ht
->split_count
))
626 index
= ht_get_split_count_index(hash
);
627 split_count
= uatomic_add_return(&ht
->split_count
[index
].add
, 1);
628 if (caa_unlikely(!(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
631 dbg_printf("add split count %lu\n", split_count
);
632 count
= uatomic_add_return(&ht
->count
,
633 1UL << COUNT_COMMIT_ORDER
);
635 if (!(count
& (count
- 1))) {
636 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) < size
)
638 dbg_printf("add set global %ld\n", count
);
639 cds_lfht_resize_lazy_count(ht
, size
,
640 count
>> (CHAIN_LEN_TARGET
- 1));
646 void ht_count_del(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
648 unsigned long split_count
;
651 if (caa_unlikely(!ht
->split_count
))
653 index
= ht_get_split_count_index(hash
);
654 split_count
= uatomic_add_return(&ht
->split_count
[index
].del
, 1);
655 if (caa_unlikely(!(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
658 dbg_printf("del split count %lu\n", split_count
);
659 count
= uatomic_add_return(&ht
->count
,
660 -(1UL << COUNT_COMMIT_ORDER
));
662 if (!(count
& (count
- 1))) {
663 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) >= size
)
665 dbg_printf("del set global %ld\n", count
);
667 * Don't shrink table if the number of nodes is below a
670 if (count
< (1UL << COUNT_COMMIT_ORDER
) * (split_count_mask
+ 1))
672 cds_lfht_resize_lazy_count(ht
, size
,
673 count
>> (CHAIN_LEN_TARGET
- 1));
679 void check_resize(struct cds_lfht
*ht
, unsigned long size
, uint32_t chain_len
)
683 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
685 count
= uatomic_read(&ht
->count
);
687 * Use bucket-local length for small table expand and for
688 * environments lacking per-cpu data support.
690 if (count
>= (1UL << COUNT_COMMIT_ORDER
))
693 dbg_printf("WARNING: large chain length: %u.\n",
695 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
696 cds_lfht_resize_lazy_grow(ht
, size
,
697 get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
701 struct cds_lfht_node
*clear_flag(struct cds_lfht_node
*node
)
703 return (struct cds_lfht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
707 int is_removed(struct cds_lfht_node
*node
)
709 return ((unsigned long) node
) & REMOVED_FLAG
;
713 struct cds_lfht_node
*flag_removed(struct cds_lfht_node
*node
)
715 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
719 int is_dummy(struct cds_lfht_node
*node
)
721 return ((unsigned long) node
) & DUMMY_FLAG
;
725 struct cds_lfht_node
*flag_dummy(struct cds_lfht_node
*node
)
727 return (struct cds_lfht_node
*) (((unsigned long) node
) | DUMMY_FLAG
);
731 struct cds_lfht_node
*get_end(void)
733 return (struct cds_lfht_node
*) END_VALUE
;
737 int is_end(struct cds_lfht_node
*node
)
739 return clear_flag(node
) == (struct cds_lfht_node
*) END_VALUE
;
743 unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr
,
746 unsigned long old1
, old2
;
748 old1
= uatomic_read(ptr
);
753 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
758 struct _cds_lfht_node
*lookup_bucket(struct cds_lfht
*ht
, unsigned long size
,
761 unsigned long index
, order
;
764 index
= hash
& (size
- 1);
766 if (index
< ht
->min_alloc_size
) {
767 dbg_printf("lookup hash %lu index %lu order 0 aridx 0\n",
769 return &ht
->t
.tbl
[0]->nodes
[index
];
772 * equivalent to get_count_order_ulong(index + 1), but optimizes
773 * away the non-existing 0 special-case for
774 * get_count_order_ulong.
776 order
= fls_ulong(index
);
777 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
778 hash
, index
, order
, index
& ((1UL << (order
- 1)) - 1));
779 return &ht
->t
.tbl
[order
]->nodes
[index
& ((1UL << (order
- 1)) - 1)];
783 * Remove all logically deleted nodes from a bucket up to a certain node key.
786 void _cds_lfht_gc_bucket(struct cds_lfht_node
*dummy
, struct cds_lfht_node
*node
)
788 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_next
;
790 assert(!is_dummy(dummy
));
791 assert(!is_removed(dummy
));
792 assert(!is_dummy(node
));
793 assert(!is_removed(node
));
796 /* We can always skip the dummy node initially */
797 iter
= rcu_dereference(iter_prev
->p
.next
);
798 assert(!is_removed(iter
));
799 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
801 * We should never be called with dummy (start of chain)
802 * and logically removed node (end of path compression
803 * marker) being the actual same node. This would be a
804 * bug in the algorithm implementation.
806 assert(dummy
!= node
);
808 if (caa_unlikely(is_end(iter
)))
810 if (caa_likely(clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
))
812 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
813 if (caa_likely(is_removed(next
)))
815 iter_prev
= clear_flag(iter
);
818 assert(!is_removed(iter
));
820 new_next
= flag_dummy(clear_flag(next
));
822 new_next
= clear_flag(next
);
823 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
829 int _cds_lfht_replace(struct cds_lfht
*ht
, unsigned long size
,
830 struct cds_lfht_node
*old_node
,
831 struct cds_lfht_node
*old_next
,
832 struct cds_lfht_node
*new_node
)
834 struct cds_lfht_node
*dummy
, *ret_next
;
835 struct _cds_lfht_node
*lookup
;
837 if (!old_node
) /* Return -ENOENT if asked to replace NULL node */
840 assert(!is_removed(old_node
));
841 assert(!is_dummy(old_node
));
842 assert(!is_removed(new_node
));
843 assert(!is_dummy(new_node
));
844 assert(new_node
!= old_node
);
846 /* Insert after node to be replaced */
847 if (is_removed(old_next
)) {
849 * Too late, the old node has been removed under us
850 * between lookup and replace. Fail.
854 assert(!is_dummy(old_next
));
855 assert(new_node
!= clear_flag(old_next
));
856 new_node
->p
.next
= clear_flag(old_next
);
858 * Here is the whole trick for lock-free replace: we add
859 * the replacement node _after_ the node we want to
860 * replace by atomically setting its next pointer at the
861 * same time we set its removal flag. Given that
862 * the lookups/get next use an iterator aware of the
863 * next pointer, they will either skip the old node due
864 * to the removal flag and see the new node, or use
865 * the old node, but will not see the new one.
867 ret_next
= uatomic_cmpxchg(&old_node
->p
.next
,
868 old_next
, flag_removed(new_node
));
869 if (ret_next
== old_next
)
870 break; /* We performed the replacement. */
875 * Ensure that the old node is not visible to readers anymore:
876 * lookup for the node, and remove it (along with any other
877 * logically removed node) if found.
879 lookup
= lookup_bucket(ht
, size
, bit_reverse_ulong(old_node
->p
.reverse_hash
));
880 dummy
= (struct cds_lfht_node
*) lookup
;
881 _cds_lfht_gc_bucket(dummy
, new_node
);
883 assert(is_removed(rcu_dereference(old_node
->p
.next
)));
888 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
889 * mode. A NULL unique_ret allows creation of duplicate keys.
892 void _cds_lfht_add(struct cds_lfht
*ht
,
893 cds_lfht_match_fct match
,
895 struct cds_lfht_node
*node
,
896 struct cds_lfht_iter
*unique_ret
,
899 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
901 struct _cds_lfht_node
*lookup
;
903 assert(!is_dummy(node
));
904 assert(!is_removed(node
));
905 lookup
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->p
.reverse_hash
));
907 uint32_t chain_len
= 0;
910 * iter_prev points to the non-removed node prior to the
913 iter_prev
= (struct cds_lfht_node
*) lookup
;
914 /* We can always skip the dummy node initially */
915 iter
= rcu_dereference(iter_prev
->p
.next
);
916 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
918 if (caa_unlikely(is_end(iter
)))
920 if (caa_likely(clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
))
923 /* dummy node is the first node of the identical-hash-value chain */
924 if (dummy
&& clear_flag(iter
)->p
.reverse_hash
== node
->p
.reverse_hash
)
927 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
928 if (caa_unlikely(is_removed(next
)))
934 && clear_flag(iter
)->p
.reverse_hash
== node
->p
.reverse_hash
) {
935 struct cds_lfht_iter d_iter
= { .node
= node
, .next
= iter
, };
938 * uniquely adding inserts the node as the first
939 * node of the identical-hash-value node chain.
941 * This semantic ensures no duplicated keys
942 * should ever be observable in the table
943 * (including observe one node by one node
944 * by forward iterations)
946 cds_lfht_next_duplicate(ht
, match
, &d_iter
);
950 *unique_ret
= d_iter
;
954 /* Only account for identical reverse hash once */
955 if (iter_prev
->p
.reverse_hash
!= clear_flag(iter
)->p
.reverse_hash
957 check_resize(ht
, size
, ++chain_len
);
958 iter_prev
= clear_flag(iter
);
963 assert(node
!= clear_flag(iter
));
964 assert(!is_removed(iter_prev
));
965 assert(!is_removed(iter
));
966 assert(iter_prev
!= node
);
968 node
->p
.next
= clear_flag(iter
);
970 node
->p
.next
= flag_dummy(clear_flag(iter
));
972 new_node
= flag_dummy(node
);
975 if (uatomic_cmpxchg(&iter_prev
->p
.next
, iter
,
977 continue; /* retry */
984 assert(!is_removed(iter
));
986 new_next
= flag_dummy(clear_flag(next
));
988 new_next
= clear_flag(next
);
989 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
994 unique_ret
->node
= return_node
;
995 /* unique_ret->next left unset, never used. */
1000 int _cds_lfht_del(struct cds_lfht
*ht
, unsigned long size
,
1001 struct cds_lfht_node
*node
,
1004 struct cds_lfht_node
*dummy
, *next
, *old
;
1005 struct _cds_lfht_node
*lookup
;
1007 if (!node
) /* Return -ENOENT if asked to delete NULL node */
1010 /* logically delete the node */
1011 assert(!is_dummy(node
));
1012 assert(!is_removed(node
));
1013 old
= rcu_dereference(node
->p
.next
);
1015 struct cds_lfht_node
*new_next
;
1018 if (caa_unlikely(is_removed(next
)))
1021 assert(is_dummy(next
));
1023 assert(!is_dummy(next
));
1024 new_next
= flag_removed(next
);
1025 old
= uatomic_cmpxchg(&node
->p
.next
, next
, new_next
);
1026 } while (old
!= next
);
1027 /* We performed the (logical) deletion. */
1030 * Ensure that the node is not visible to readers anymore: lookup for
1031 * the node, and remove it (along with any other logically removed node)
1034 lookup
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->p
.reverse_hash
));
1035 dummy
= (struct cds_lfht_node
*) lookup
;
1036 _cds_lfht_gc_bucket(dummy
, node
);
1038 assert(is_removed(rcu_dereference(node
->p
.next
)));
1043 void *partition_resize_thread(void *arg
)
1045 struct partition_resize_work
*work
= arg
;
1047 work
->ht
->cds_lfht_rcu_register_thread();
1048 work
->fct(work
->ht
, work
->i
, work
->start
, work
->len
);
1049 work
->ht
->cds_lfht_rcu_unregister_thread();
1054 void partition_resize_helper(struct cds_lfht
*ht
, unsigned long i
,
1056 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
1057 unsigned long start
, unsigned long len
))
1059 unsigned long partition_len
;
1060 struct partition_resize_work
*work
;
1062 unsigned long nr_threads
;
1065 * Note: nr_cpus_mask + 1 is always power of 2.
1066 * We spawn just the number of threads we need to satisfy the minimum
1067 * partition size, up to the number of CPUs in the system.
1069 if (nr_cpus_mask
> 0) {
1070 nr_threads
= min(nr_cpus_mask
+ 1,
1071 len
>> MIN_PARTITION_PER_THREAD_ORDER
);
1075 partition_len
= len
>> get_count_order_ulong(nr_threads
);
1076 work
= calloc(nr_threads
, sizeof(*work
));
1078 for (thread
= 0; thread
< nr_threads
; thread
++) {
1079 work
[thread
].ht
= ht
;
1081 work
[thread
].len
= partition_len
;
1082 work
[thread
].start
= thread
* partition_len
;
1083 work
[thread
].fct
= fct
;
1084 ret
= pthread_create(&(work
[thread
].thread_id
), ht
->resize_attr
,
1085 partition_resize_thread
, &work
[thread
]);
1088 for (thread
= 0; thread
< nr_threads
; thread
++) {
1089 ret
= pthread_join(work
[thread
].thread_id
, NULL
);
1096 * Holding RCU read lock to protect _cds_lfht_add against memory
1097 * reclaim that could be performed by other call_rcu worker threads (ABA
1100 * When we reach a certain length, we can split this population phase over
1101 * many worker threads, based on the number of CPUs available in the system.
1102 * This should therefore take care of not having the expand lagging behind too
1103 * many concurrent insertion threads by using the scheduler's ability to
1104 * schedule dummy node population fairly with insertions.
1107 void init_table_populate_partition(struct cds_lfht
*ht
, unsigned long i
,
1108 unsigned long start
, unsigned long len
)
1112 assert(i
> ht
->min_alloc_order
);
1113 ht
->cds_lfht_rcu_read_lock();
1114 for (j
= start
; j
< start
+ len
; j
++) {
1115 struct cds_lfht_node
*new_node
=
1116 (struct cds_lfht_node
*) &ht
->t
.tbl
[i
]->nodes
[j
];
1118 dbg_printf("init populate: i %lu j %lu hash %lu\n",
1119 i
, j
, (1UL << (i
- 1)) + j
);
1120 new_node
->p
.reverse_hash
=
1121 bit_reverse_ulong((1UL << (i
- 1)) + j
);
1122 _cds_lfht_add(ht
, NULL
, 1UL << (i
- 1),
1125 ht
->cds_lfht_rcu_read_unlock();
1129 void init_table_populate(struct cds_lfht
*ht
, unsigned long i
,
1132 assert(nr_cpus_mask
!= -1);
1133 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1134 ht
->cds_lfht_rcu_thread_online();
1135 init_table_populate_partition(ht
, i
, 0, len
);
1136 ht
->cds_lfht_rcu_thread_offline();
1139 partition_resize_helper(ht
, i
, len
, init_table_populate_partition
);
1143 void init_table(struct cds_lfht
*ht
,
1144 unsigned long first_order
, unsigned long last_order
)
1148 dbg_printf("init table: first_order %lu last_order %lu\n",
1149 first_order
, last_order
);
1150 assert(first_order
> ht
->min_alloc_order
);
1151 for (i
= first_order
; i
<= last_order
; i
++) {
1154 len
= 1UL << (i
- 1);
1155 dbg_printf("init order %lu len: %lu\n", i
, len
);
1157 /* Stop expand if the resize target changes under us */
1158 if (CMM_LOAD_SHARED(ht
->t
.resize_target
) < (1UL << i
))
1161 ht
->t
.tbl
[i
] = calloc(1, len
* sizeof(struct _cds_lfht_node
));
1162 assert(ht
->t
.tbl
[i
]);
1165 * Set all dummy nodes reverse hash values for a level and
1166 * link all dummy nodes into the table.
1168 init_table_populate(ht
, i
, len
);
1171 * Update table size.
1173 cmm_smp_wmb(); /* populate data before RCU size */
1174 CMM_STORE_SHARED(ht
->t
.size
, 1UL << i
);
1176 dbg_printf("init new size: %lu\n", 1UL << i
);
1177 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1183 * Holding RCU read lock to protect _cds_lfht_remove against memory
1184 * reclaim that could be performed by other call_rcu worker threads (ABA
1186 * For a single level, we logically remove and garbage collect each node.
1188 * As a design choice, we perform logical removal and garbage collection on a
1189 * node-per-node basis to simplify this algorithm. We also assume keeping good
1190 * cache locality of the operation would overweight possible performance gain
1191 * that could be achieved by batching garbage collection for multiple levels.
1192 * However, this would have to be justified by benchmarks.
1194 * Concurrent removal and add operations are helping us perform garbage
1195 * collection of logically removed nodes. We guarantee that all logically
1196 * removed nodes have been garbage-collected (unlinked) before call_rcu is
1197 * invoked to free a hole level of dummy nodes (after a grace period).
1199 * Logical removal and garbage collection can therefore be done in batch or on a
1200 * node-per-node basis, as long as the guarantee above holds.
1202 * When we reach a certain length, we can split this removal over many worker
1203 * threads, based on the number of CPUs available in the system. This should
1204 * take care of not letting resize process lag behind too many concurrent
1205 * updater threads actively inserting into the hash table.
1208 void remove_table_partition(struct cds_lfht
*ht
, unsigned long i
,
1209 unsigned long start
, unsigned long len
)
1213 assert(i
> ht
->min_alloc_order
);
1214 ht
->cds_lfht_rcu_read_lock();
1215 for (j
= start
; j
< start
+ len
; j
++) {
1216 struct cds_lfht_node
*fini_node
=
1217 (struct cds_lfht_node
*) &ht
->t
.tbl
[i
]->nodes
[j
];
1219 dbg_printf("remove entry: i %lu j %lu hash %lu\n",
1220 i
, j
, (1UL << (i
- 1)) + j
);
1221 fini_node
->p
.reverse_hash
=
1222 bit_reverse_ulong((1UL << (i
- 1)) + j
);
1223 (void) _cds_lfht_del(ht
, 1UL << (i
- 1), fini_node
, 1);
1225 ht
->cds_lfht_rcu_read_unlock();
1229 void remove_table(struct cds_lfht
*ht
, unsigned long i
, unsigned long len
)
1232 assert(nr_cpus_mask
!= -1);
1233 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1234 ht
->cds_lfht_rcu_thread_online();
1235 remove_table_partition(ht
, i
, 0, len
);
1236 ht
->cds_lfht_rcu_thread_offline();
1239 partition_resize_helper(ht
, i
, len
, remove_table_partition
);
1243 void fini_table(struct cds_lfht
*ht
,
1244 unsigned long first_order
, unsigned long last_order
)
1247 void *free_by_rcu
= NULL
;
1249 dbg_printf("fini table: first_order %lu last_order %lu\n",
1250 first_order
, last_order
);
1251 assert(first_order
> ht
->min_alloc_order
);
1252 for (i
= last_order
; i
>= first_order
; i
--) {
1255 len
= 1UL << (i
- 1);
1256 dbg_printf("fini order %lu len: %lu\n", i
, len
);
1258 /* Stop shrink if the resize target changes under us */
1259 if (CMM_LOAD_SHARED(ht
->t
.resize_target
) > (1UL << (i
- 1)))
1262 cmm_smp_wmb(); /* populate data before RCU size */
1263 CMM_STORE_SHARED(ht
->t
.size
, 1UL << (i
- 1));
1266 * We need to wait for all add operations to reach Q.S. (and
1267 * thus use the new table for lookups) before we can start
1268 * releasing the old dummy nodes. Otherwise their lookup will
1269 * return a logically removed node as insert position.
1271 ht
->cds_lfht_synchronize_rcu();
1276 * Set "removed" flag in dummy nodes about to be removed.
1277 * Unlink all now-logically-removed dummy node pointers.
1278 * Concurrent add/remove operation are helping us doing
1281 remove_table(ht
, i
, len
);
1283 free_by_rcu
= ht
->t
.tbl
[i
];
1285 dbg_printf("fini new size: %lu\n", 1UL << i
);
1286 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1291 ht
->cds_lfht_synchronize_rcu();
1297 void cds_lfht_create_dummy(struct cds_lfht
*ht
, unsigned long size
)
1299 struct _cds_lfht_node
*prev
, *node
;
1300 unsigned long order
, len
, i
, j
;
1302 ht
->t
.tbl
[0] = calloc(1, ht
->min_alloc_size
* sizeof(struct _cds_lfht_node
));
1303 assert(ht
->t
.tbl
[0]);
1305 dbg_printf("create dummy: order %lu index %lu hash %lu\n", 0, 0, 0);
1306 ht
->t
.tbl
[0]->nodes
[0].next
= flag_dummy(get_end());
1307 ht
->t
.tbl
[0]->nodes
[0].reverse_hash
= 0;
1309 for (order
= 1; order
< get_count_order_ulong(size
) + 1; order
++) {
1310 len
= 1UL << (order
- 1);
1311 if (order
<= ht
->min_alloc_order
) {
1312 ht
->t
.tbl
[order
] = (struct rcu_level
*) (ht
->t
.tbl
[0]->nodes
+ len
);
1314 ht
->t
.tbl
[order
] = calloc(1, len
* sizeof(struct _cds_lfht_node
));
1315 assert(ht
->t
.tbl
[order
]);
1319 prev
= ht
->t
.tbl
[i
]->nodes
;
1320 for (j
= 0; j
< len
; j
++) {
1321 if (j
& (j
- 1)) { /* Between power of 2 */
1323 } else if (j
) { /* At each power of 2 */
1325 prev
= ht
->t
.tbl
[i
]->nodes
;
1328 node
= &ht
->t
.tbl
[order
]->nodes
[j
];
1329 dbg_printf("create dummy: order %lu index %lu hash %lu\n",
1331 node
->next
= prev
->next
;
1332 assert(is_dummy(node
->next
));
1333 node
->reverse_hash
= bit_reverse_ulong(j
+ len
);
1334 prev
->next
= flag_dummy((struct cds_lfht_node
*)node
);
1339 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
1340 unsigned long min_alloc_size
,
1342 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
1343 void (*func
)(struct rcu_head
*head
)),
1344 void (*cds_lfht_synchronize_rcu
)(void),
1345 void (*cds_lfht_rcu_read_lock
)(void),
1346 void (*cds_lfht_rcu_read_unlock
)(void),
1347 void (*cds_lfht_rcu_thread_offline
)(void),
1348 void (*cds_lfht_rcu_thread_online
)(void),
1349 void (*cds_lfht_rcu_register_thread
)(void),
1350 void (*cds_lfht_rcu_unregister_thread
)(void),
1351 pthread_attr_t
*attr
)
1353 struct cds_lfht
*ht
;
1354 unsigned long order
;
1356 /* min_alloc_size must be power of two */
1357 if (!min_alloc_size
|| (min_alloc_size
& (min_alloc_size
- 1)))
1359 /* init_size must be power of two */
1360 if (!init_size
|| (init_size
& (init_size
- 1)))
1362 min_alloc_size
= max(min_alloc_size
, MIN_TABLE_SIZE
);
1363 init_size
= max(init_size
, min_alloc_size
);
1364 ht
= calloc(1, sizeof(struct cds_lfht
));
1367 ht
->cds_lfht_call_rcu
= cds_lfht_call_rcu
;
1368 ht
->cds_lfht_synchronize_rcu
= cds_lfht_synchronize_rcu
;
1369 ht
->cds_lfht_rcu_read_lock
= cds_lfht_rcu_read_lock
;
1370 ht
->cds_lfht_rcu_read_unlock
= cds_lfht_rcu_read_unlock
;
1371 ht
->cds_lfht_rcu_thread_offline
= cds_lfht_rcu_thread_offline
;
1372 ht
->cds_lfht_rcu_thread_online
= cds_lfht_rcu_thread_online
;
1373 ht
->cds_lfht_rcu_register_thread
= cds_lfht_rcu_register_thread
;
1374 ht
->cds_lfht_rcu_unregister_thread
= cds_lfht_rcu_unregister_thread
;
1375 ht
->resize_attr
= attr
;
1376 alloc_split_items_count(ht
);
1377 /* this mutex should not nest in read-side C.S. */
1378 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
1379 order
= get_count_order_ulong(init_size
);
1380 ht
->t
.resize_target
= 1UL << order
;
1381 ht
->min_alloc_size
= min_alloc_size
;
1382 ht
->min_alloc_order
= get_count_order_ulong(min_alloc_size
);
1383 cds_lfht_create_dummy(ht
, 1UL << order
);
1384 ht
->t
.size
= 1UL << order
;
1388 void cds_lfht_lookup(struct cds_lfht
*ht
, cds_lfht_match_fct match
,
1389 unsigned long hash
, void *key
, struct cds_lfht_iter
*iter
)
1391 struct cds_lfht_node
*node
, *next
, *dummy_node
;
1392 struct _cds_lfht_node
*lookup
;
1393 unsigned long reverse_hash
, size
;
1395 reverse_hash
= bit_reverse_ulong(hash
);
1397 size
= rcu_dereference(ht
->t
.size
);
1398 lookup
= lookup_bucket(ht
, size
, hash
);
1399 dummy_node
= (struct cds_lfht_node
*) lookup
;
1400 /* We can always skip the dummy node initially */
1401 node
= rcu_dereference(dummy_node
->p
.next
);
1402 node
= clear_flag(node
);
1404 if (caa_unlikely(is_end(node
))) {
1408 if (caa_unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
1412 next
= rcu_dereference(node
->p
.next
);
1413 assert(node
== clear_flag(node
));
1414 if (caa_likely(!is_removed(next
))
1416 && node
->p
.reverse_hash
== reverse_hash
1417 && caa_likely(match(node
, key
))) {
1420 node
= clear_flag(next
);
1422 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
1427 void cds_lfht_next_duplicate(struct cds_lfht
*ht
, cds_lfht_match_fct match
,
1428 struct cds_lfht_iter
*iter
)
1430 struct cds_lfht_node
*node
, *next
;
1431 unsigned long reverse_hash
;
1435 reverse_hash
= node
->p
.reverse_hash
;
1438 node
= clear_flag(next
);
1441 if (caa_unlikely(is_end(node
))) {
1445 if (caa_unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
1449 next
= rcu_dereference(node
->p
.next
);
1450 if (caa_likely(!is_removed(next
))
1452 && caa_likely(match(node
->key
, key
))) {
1455 node
= clear_flag(next
);
1457 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
1462 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1464 struct cds_lfht_node
*node
, *next
;
1466 node
= clear_flag(iter
->next
);
1468 if (caa_unlikely(is_end(node
))) {
1472 next
= rcu_dereference(node
->p
.next
);
1473 if (caa_likely(!is_removed(next
))
1474 && !is_dummy(next
)) {
1477 node
= clear_flag(next
);
1479 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
1484 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1486 struct _cds_lfht_node
*lookup
;
1489 * Get next after first dummy node. The first dummy node is the
1490 * first node of the linked list.
1492 lookup
= &ht
->t
.tbl
[0]->nodes
[0];
1493 iter
->next
= lookup
->next
;
1494 cds_lfht_next(ht
, iter
);
1497 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
1498 struct cds_lfht_node
*node
)
1502 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1503 size
= rcu_dereference(ht
->t
.size
);
1504 _cds_lfht_add(ht
, NULL
, size
, node
, NULL
, 0);
1505 ht_count_add(ht
, size
, hash
);
1508 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
1509 cds_lfht_match_fct match
,
1511 struct cds_lfht_node
*node
)
1514 struct cds_lfht_iter iter
;
1516 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1517 size
= rcu_dereference(ht
->t
.size
);
1518 _cds_lfht_add(ht
, match
, size
, node
, &iter
, 0);
1519 if (iter
.node
== node
)
1520 ht_count_add(ht
, size
, hash
);
1524 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
1525 cds_lfht_match_fct match
,
1527 struct cds_lfht_node
*node
)
1530 struct cds_lfht_iter iter
;
1532 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1533 size
= rcu_dereference(ht
->t
.size
);
1535 _cds_lfht_add(ht
, match
, size
, node
, &iter
, 0);
1536 if (iter
.node
== node
) {
1537 ht_count_add(ht
, size
, hash
);
1541 if (!_cds_lfht_replace(ht
, size
, iter
.node
, iter
.next
, node
))
1546 int cds_lfht_replace(struct cds_lfht
*ht
, struct cds_lfht_iter
*old_iter
,
1547 struct cds_lfht_node
*new_node
)
1551 size
= rcu_dereference(ht
->t
.size
);
1552 return _cds_lfht_replace(ht
, size
, old_iter
->node
, old_iter
->next
,
1556 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1558 unsigned long size
, hash
;
1561 size
= rcu_dereference(ht
->t
.size
);
1562 ret
= _cds_lfht_del(ht
, size
, iter
->node
, 0);
1564 hash
= bit_reverse_ulong(iter
->node
->p
.reverse_hash
);
1565 ht_count_del(ht
, size
, hash
);
1571 int cds_lfht_delete_dummy(struct cds_lfht
*ht
)
1573 struct cds_lfht_node
*node
;
1574 struct _cds_lfht_node
*lookup
;
1575 unsigned long order
, i
, size
;
1577 /* Check that the table is empty */
1578 lookup
= &ht
->t
.tbl
[0]->nodes
[0];
1579 node
= (struct cds_lfht_node
*) lookup
;
1581 node
= clear_flag(node
)->p
.next
;
1582 if (!is_dummy(node
))
1584 assert(!is_removed(node
));
1585 } while (!is_end(node
));
1587 * size accessed without rcu_dereference because hash table is
1591 /* Internal sanity check: all nodes left should be dummy */
1592 for (order
= 0; order
< get_count_order_ulong(size
) + 1; order
++) {
1595 len
= !order
? 1 : 1UL << (order
- 1);
1596 for (i
= 0; i
< len
; i
++) {
1597 dbg_printf("delete order %lu i %lu hash %lu\n",
1599 bit_reverse_ulong(ht
->t
.tbl
[order
]->nodes
[i
].reverse_hash
));
1600 assert(is_dummy(ht
->t
.tbl
[order
]->nodes
[i
].next
));
1603 if (order
== ht
->min_alloc_order
)
1604 poison_free(ht
->t
.tbl
[0]);
1605 else if (order
> ht
->min_alloc_order
)
1606 poison_free(ht
->t
.tbl
[order
]);
1607 /* Nothing to delete for order < ht->min_alloc_order */
1613 * Should only be called when no more concurrent readers nor writers can
1614 * possibly access the table.
1616 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
)
1620 /* Wait for in-flight resize operations to complete */
1621 _CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
1622 cmm_smp_mb(); /* Store destroy before load resize */
1623 while (uatomic_read(&ht
->in_progress_resize
))
1624 poll(NULL
, 0, 100); /* wait for 100ms */
1625 ret
= cds_lfht_delete_dummy(ht
);
1628 free_split_items_count(ht
);
1630 *attr
= ht
->resize_attr
;
1635 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
1636 long *approx_before
,
1637 unsigned long *count
,
1638 unsigned long *removed
,
1641 struct cds_lfht_node
*node
, *next
;
1642 struct _cds_lfht_node
*lookup
;
1643 unsigned long nr_dummy
= 0;
1646 if (ht
->split_count
) {
1649 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1650 *approx_before
+= uatomic_read(&ht
->split_count
[i
].add
);
1651 *approx_before
-= uatomic_read(&ht
->split_count
[i
].del
);
1658 /* Count non-dummy nodes in the table */
1659 lookup
= &ht
->t
.tbl
[0]->nodes
[0];
1660 node
= (struct cds_lfht_node
*) lookup
;
1662 next
= rcu_dereference(node
->p
.next
);
1663 if (is_removed(next
)) {
1664 if (!is_dummy(next
))
1668 } else if (!is_dummy(next
))
1672 node
= clear_flag(next
);
1673 } while (!is_end(node
));
1674 dbg_printf("number of dummy nodes: %lu\n", nr_dummy
);
1676 if (ht
->split_count
) {
1679 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1680 *approx_after
+= uatomic_read(&ht
->split_count
[i
].add
);
1681 *approx_after
-= uatomic_read(&ht
->split_count
[i
].del
);
1686 /* called with resize mutex held */
1688 void _do_cds_lfht_grow(struct cds_lfht
*ht
,
1689 unsigned long old_size
, unsigned long new_size
)
1691 unsigned long old_order
, new_order
;
1693 old_order
= get_count_order_ulong(old_size
);
1694 new_order
= get_count_order_ulong(new_size
);
1695 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1696 old_size
, old_order
, new_size
, new_order
);
1697 assert(new_size
> old_size
);
1698 init_table(ht
, old_order
+ 1, new_order
);
1701 /* called with resize mutex held */
1703 void _do_cds_lfht_shrink(struct cds_lfht
*ht
,
1704 unsigned long old_size
, unsigned long new_size
)
1706 unsigned long old_order
, new_order
;
1708 new_size
= max(new_size
, ht
->min_alloc_size
);
1709 old_order
= get_count_order_ulong(old_size
);
1710 new_order
= get_count_order_ulong(new_size
);
1711 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1712 old_size
, old_order
, new_size
, new_order
);
1713 assert(new_size
< old_size
);
1715 /* Remove and unlink all dummy nodes to remove. */
1716 fini_table(ht
, new_order
+ 1, old_order
);
1720 /* called with resize mutex held */
1722 void _do_cds_lfht_resize(struct cds_lfht
*ht
)
1724 unsigned long new_size
, old_size
;
1727 * Resize table, re-do if the target size has changed under us.
1730 assert(uatomic_read(&ht
->in_progress_resize
));
1731 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1733 ht
->t
.resize_initiated
= 1;
1734 old_size
= ht
->t
.size
;
1735 new_size
= CMM_LOAD_SHARED(ht
->t
.resize_target
);
1736 if (old_size
< new_size
)
1737 _do_cds_lfht_grow(ht
, old_size
, new_size
);
1738 else if (old_size
> new_size
)
1739 _do_cds_lfht_shrink(ht
, old_size
, new_size
);
1740 ht
->t
.resize_initiated
= 0;
1741 /* write resize_initiated before read resize_target */
1743 } while (ht
->t
.size
!= CMM_LOAD_SHARED(ht
->t
.resize_target
));
1747 unsigned long resize_target_grow(struct cds_lfht
*ht
, unsigned long new_size
)
1749 return _uatomic_xchg_monotonic_increase(&ht
->t
.resize_target
, new_size
);
1753 void resize_target_update_count(struct cds_lfht
*ht
,
1754 unsigned long count
)
1756 count
= max(count
, ht
->min_alloc_size
);
1757 uatomic_set(&ht
->t
.resize_target
, count
);
1760 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
)
1762 resize_target_update_count(ht
, new_size
);
1763 CMM_STORE_SHARED(ht
->t
.resize_initiated
, 1);
1764 ht
->cds_lfht_rcu_thread_offline();
1765 pthread_mutex_lock(&ht
->resize_mutex
);
1766 _do_cds_lfht_resize(ht
);
1767 pthread_mutex_unlock(&ht
->resize_mutex
);
1768 ht
->cds_lfht_rcu_thread_online();
1772 void do_resize_cb(struct rcu_head
*head
)
1774 struct rcu_resize_work
*work
=
1775 caa_container_of(head
, struct rcu_resize_work
, head
);
1776 struct cds_lfht
*ht
= work
->ht
;
1778 ht
->cds_lfht_rcu_thread_offline();
1779 pthread_mutex_lock(&ht
->resize_mutex
);
1780 _do_cds_lfht_resize(ht
);
1781 pthread_mutex_unlock(&ht
->resize_mutex
);
1782 ht
->cds_lfht_rcu_thread_online();
1784 cmm_smp_mb(); /* finish resize before decrement */
1785 uatomic_dec(&ht
->in_progress_resize
);
1789 void __cds_lfht_resize_lazy_launch(struct cds_lfht
*ht
)
1791 struct rcu_resize_work
*work
;
1793 /* Store resize_target before read resize_initiated */
1795 if (!CMM_LOAD_SHARED(ht
->t
.resize_initiated
)) {
1796 uatomic_inc(&ht
->in_progress_resize
);
1797 cmm_smp_mb(); /* increment resize count before load destroy */
1798 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
)) {
1799 uatomic_dec(&ht
->in_progress_resize
);
1802 work
= malloc(sizeof(*work
));
1804 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1805 CMM_STORE_SHARED(ht
->t
.resize_initiated
, 1);
1810 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
)
1812 unsigned long target_size
= size
<< growth
;
1814 if (resize_target_grow(ht
, target_size
) >= target_size
)
1817 __cds_lfht_resize_lazy_launch(ht
);
1821 * We favor grow operations over shrink. A shrink operation never occurs
1822 * if a grow operation is queued for lazy execution. A grow operation
1823 * cancels any pending shrink lazy execution.
1826 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
1827 unsigned long count
)
1829 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
1831 count
= max(count
, ht
->min_alloc_size
);
1833 return; /* Already the right size, no resize needed */
1834 if (count
> size
) { /* lazy grow */
1835 if (resize_target_grow(ht
, count
) >= count
)
1837 } else { /* lazy shrink */
1841 s
= uatomic_cmpxchg(&ht
->t
.resize_target
, size
, count
);
1843 break; /* no resize needed */
1845 return; /* growing is/(was just) in progress */
1847 return; /* some other thread do shrink */
1851 __cds_lfht_resize_lazy_launch(ht
);