4 * Userspace RCU library - Lock-Free Resizable RCU Hash Table
6 * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Based on the following articles:
26 * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
27 * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
28 * - Michael, M. M. High performance dynamic lock-free hash tables
29 * and list-based sets. In Proceedings of the fourteenth annual ACM
30 * symposium on Parallel algorithms and architectures, ACM Press,
33 * Some specificities of this Lock-Free Resizable RCU Hash Table
36 * - RCU read-side critical section allows readers to perform hash
37 * table lookups and use the returned objects safely by delaying
38 * memory reclaim of a grace period.
39 * - Add and remove operations are lock-free, and do not need to
40 * allocate memory. They need to be executed within RCU read-side
41 * critical section to ensure the objects they read are valid and to
42 * deal with the cmpxchg ABA problem.
43 * - add and add_unique operations are supported. add_unique checks if
44 * the node key already exists in the hash table. It ensures no key
46 * - The resize operation executes concurrently with add/remove/lookup.
47 * - Hash table nodes are contained within a split-ordered list. This
48 * list is ordered by incrementing reversed-bits-hash value.
49 * - An index of bucket nodes is kept. These bucket nodes are the hash
50 * table "buckets", and they are also chained together in the
51 * split-ordered list, which allows recursive expansion.
52 * - The resize operation for small tables only allows expanding the hash table.
53 * It is triggered automatically by detecting long chains in the add
55 * - The resize operation for larger tables (and available through an
56 * API) allows both expanding and shrinking the hash table.
57 * - Split-counters are used to keep track of the number of
58 * nodes within the hash table for automatic resize triggering.
59 * - Resize operation initiated by long chain detection is executed by a
60 * call_rcu thread, which keeps lock-freedom of add and remove.
61 * - Resize operations are protected by a mutex.
62 * - The removal operation is split in two parts: first, a "removed"
63 * flag is set in the next pointer within the node to remove. Then,
64 * a "garbage collection" is performed in the bucket containing the
65 * removed node (from the start of the bucket up to the removed node).
66 * All encountered nodes with "removed" flag set in their next
67 * pointers are removed from the linked-list. If the cmpxchg used for
68 * removal fails (due to concurrent garbage-collection or concurrent
69 * add), we retry from the beginning of the bucket. This ensures that
70 * the node with "removed" flag set is removed from the hash table
71 * (not visible to lookups anymore) before the RCU read-side critical
72 * section held across removal ends. Furthermore, this ensures that
73 * the node with "removed" flag set is removed from the linked-list
74 * before its memory is reclaimed. Only the thread which removal
75 * successfully set the "removed" flag (with a cmpxchg) into a node's
76 * next pointer is considered to have succeeded its removal (and thus
77 * owns the node to reclaim). Because we garbage-collect starting from
78 * an invariant node (the start-of-bucket bucket node) up to the
79 * "removed" node (or find a reverse-hash that is higher), we are sure
80 * that a successful traversal of the chain leads to a chain that is
81 * present in the linked-list (the start node is never removed) and
82 * that is does not contain the "removed" node anymore, even if
83 * concurrent delete/add operations are changing the structure of the
85 * - The add operation performs gargage collection of buckets if it
86 * encounters nodes with removed flag set in the bucket where it wants
87 * to add its new node. This ensures lock-freedom of add operation by
88 * helping the remover unlink nodes from the list rather than to wait
90 * - A RCU "order table" indexed by log2(hash index) is copied and
91 * expanded by the resize operation. This order table allows finding
92 * the "bucket node" tables.
93 * - There is one bucket node table per hash index order. The size of
94 * each bucket node table is half the number of hashes contained in
95 * this order (except for order 0).
96 * - synchronzie_rcu is used to garbage-collect the old bucket node table.
97 * - The per-order bucket node tables contain a compact version of the
98 * hash table nodes. These tables are invariant after they are
99 * populated into the hash table.
101 * Bucket node tables:
103 * hash table hash table the last all bucket node tables
104 * order size bucket node 0 1 2 3 4 5 6(index)
111 * 5 32 16 1 1 2 4 8 16
112 * 6 64 32 1 1 2 4 8 16 32
114 * When growing/shrinking, we only focus on the last bucket node table
115 * which size is (!order ? 1 : (1 << (order -1))).
117 * Example for growing/shrinking:
118 * grow hash table from order 5 to 6: init the index=6 bucket node table
119 * shrink hash table from order 6 to 5: fini the index=6 bucket node table
121 * A bit of ascii art explanation:
123 * Order index is the off-by-one compare to the actual power of 2 because
124 * we use index 0 to deal with the 0 special-case.
126 * This shows the nodes for a small table ordered by reversed bits:
138 * This shows the nodes in order of non-reversed bits, linked by
139 * reversed-bit order.
144 * 2 | | 2 010 010 <- |
145 * | | | 3 011 110 | <- |
146 * 3 -> | | | 4 100 001 | |
162 #include <urcu-call-rcu.h>
163 #include <urcu-flavor.h>
164 #include <urcu/arch.h>
165 #include <urcu/uatomic.h>
166 #include <urcu/compiler.h>
167 #include <urcu/rculfhash.h>
168 #include <rculfhash-internal.h>
173 * Split-counters lazily update the global counter each 1024
174 * addition/removal. It automatically keeps track of resize required.
175 * We use the bucket length as indicator for need to expand for small
176 * tables and machines lacking per-cpu data suppport.
178 #define COUNT_COMMIT_ORDER 10
179 #define DEFAULT_SPLIT_COUNT_MASK 0xFUL
180 #define CHAIN_LEN_TARGET 1
181 #define CHAIN_LEN_RESIZE_THRESHOLD 3
184 * Define the minimum table size.
186 #define MIN_TABLE_ORDER 0
187 #define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER)
190 * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink.
192 #define MIN_PARTITION_PER_THREAD_ORDER 12
193 #define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
196 * The removed flag needs to be updated atomically with the pointer.
197 * It indicates that no node must attach to the node scheduled for
198 * removal, and that node garbage collection must be performed.
199 * The bucket flag does not require to be updated atomically with the
200 * pointer, but it is added as a pointer low bit flag to save space.
202 #define REMOVED_FLAG (1UL << 0)
203 #define BUCKET_FLAG (1UL << 1)
204 #define FLAGS_MASK ((1UL << 2) - 1)
206 /* Value of the end pointer. Should not interact with flags. */
207 #define END_VALUE NULL
210 * ht_items_count: Split-counters counting the number of node addition
211 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
212 * is set at hash table creation.
214 * These are free-running counters, never reset to zero. They count the
215 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
216 * operations to update the global counter. We choose a power-of-2 value
217 * for the trigger to deal with 32 or 64-bit overflow of the counter.
219 struct ht_items_count
{
220 unsigned long add
, del
;
221 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
224 * rcu_resize_work: Contains arguments passed to RCU worker thread
225 * responsible for performing lazy resize.
227 struct rcu_resize_work
{
228 struct rcu_head head
;
233 * partition_resize_work: Contains arguments passed to worker threads
234 * executing the hash table resize on partitions of the hash table
235 * assigned to each processor's worker thread.
237 struct partition_resize_work
{
240 unsigned long i
, start
, len
;
241 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
242 unsigned long start
, unsigned long len
);
246 * Algorithm to reverse bits in a word by lookup table, extended to
249 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
250 * Originally from Public Domain.
253 static const uint8_t BitReverseTable256
[256] =
255 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
256 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
257 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
258 R6(0), R6(2), R6(1), R6(3)
265 uint8_t bit_reverse_u8(uint8_t v
)
267 return BitReverseTable256
[v
];
270 static __attribute__((unused
))
271 uint32_t bit_reverse_u32(uint32_t v
)
273 return ((uint32_t) bit_reverse_u8(v
) << 24) |
274 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
275 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
276 ((uint32_t) bit_reverse_u8(v
>> 24));
279 static __attribute__((unused
))
280 uint64_t bit_reverse_u64(uint64_t v
)
282 return ((uint64_t) bit_reverse_u8(v
) << 56) |
283 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
284 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
285 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
286 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
287 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
288 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
289 ((uint64_t) bit_reverse_u8(v
>> 56));
293 unsigned long bit_reverse_ulong(unsigned long v
)
295 #if (CAA_BITS_PER_LONG == 32)
296 return bit_reverse_u32(v
);
298 return bit_reverse_u64(v
);
303 * fls: returns the position of the most significant bit.
304 * Returns 0 if no bit is set, else returns the position of the most
305 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
307 #if defined(__i386) || defined(__x86_64)
309 unsigned int fls_u32(uint32_t x
)
317 : "=r" (r
) : "rm" (x
));
323 #if defined(__x86_64)
325 unsigned int fls_u64(uint64_t x
)
333 : "=r" (r
) : "rm" (x
));
340 static __attribute__((unused
))
341 unsigned int fls_u64(uint64_t x
)
348 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
352 if (!(x
& 0xFFFF000000000000ULL
)) {
356 if (!(x
& 0xFF00000000000000ULL
)) {
360 if (!(x
& 0xF000000000000000ULL
)) {
364 if (!(x
& 0xC000000000000000ULL
)) {
368 if (!(x
& 0x8000000000000000ULL
)) {
377 static __attribute__((unused
))
378 unsigned int fls_u32(uint32_t x
)
384 if (!(x
& 0xFFFF0000U
)) {
388 if (!(x
& 0xFF000000U
)) {
392 if (!(x
& 0xF0000000U
)) {
396 if (!(x
& 0xC0000000U
)) {
400 if (!(x
& 0x80000000U
)) {
408 unsigned int cds_lfht_fls_ulong(unsigned long x
)
410 #if (CAA_BITS_PER_LONG == 32)
418 * Return the minimum order for which x <= (1UL << order).
419 * Return -1 if x is 0.
421 int cds_lfht_get_count_order_u32(uint32_t x
)
426 return fls_u32(x
- 1);
430 * Return the minimum order for which x <= (1UL << order).
431 * Return -1 if x is 0.
433 int cds_lfht_get_count_order_ulong(unsigned long x
)
438 return cds_lfht_fls_ulong(x
- 1);
442 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
);
445 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
446 unsigned long count
);
448 static long nr_cpus_mask
= -1;
449 static long split_count_mask
= -1;
451 #if defined(HAVE_SYSCONF)
452 static void ht_init_nr_cpus_mask(void)
456 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
462 * round up number of CPUs to next power of two, so we
463 * can use & for modulo.
465 maxcpus
= 1UL << cds_lfht_get_count_order_ulong(maxcpus
);
466 nr_cpus_mask
= maxcpus
- 1;
468 #else /* #if defined(HAVE_SYSCONF) */
469 static void ht_init_nr_cpus_mask(void)
473 #endif /* #else #if defined(HAVE_SYSCONF) */
476 void alloc_split_items_count(struct cds_lfht
*ht
)
478 struct ht_items_count
*count
;
480 if (nr_cpus_mask
== -1) {
481 ht_init_nr_cpus_mask();
482 if (nr_cpus_mask
< 0)
483 split_count_mask
= DEFAULT_SPLIT_COUNT_MASK
;
485 split_count_mask
= nr_cpus_mask
;
488 assert(split_count_mask
>= 0);
490 if (ht
->flags
& CDS_LFHT_ACCOUNTING
) {
491 ht
->split_count
= calloc(split_count_mask
+ 1, sizeof(*count
));
492 assert(ht
->split_count
);
494 ht
->split_count
= NULL
;
499 void free_split_items_count(struct cds_lfht
*ht
)
501 poison_free(ht
->split_count
);
504 #if defined(HAVE_SCHED_GETCPU)
506 int ht_get_split_count_index(unsigned long hash
)
510 assert(split_count_mask
>= 0);
511 cpu
= sched_getcpu();
512 if (caa_unlikely(cpu
< 0))
513 return hash
& split_count_mask
;
515 return cpu
& split_count_mask
;
517 #else /* #if defined(HAVE_SCHED_GETCPU) */
519 int ht_get_split_count_index(unsigned long hash
)
521 return hash
& split_count_mask
;
523 #endif /* #else #if defined(HAVE_SCHED_GETCPU) */
526 void ht_count_add(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
528 unsigned long split_count
;
532 if (caa_unlikely(!ht
->split_count
))
534 index
= ht_get_split_count_index(hash
);
535 split_count
= uatomic_add_return(&ht
->split_count
[index
].add
, 1);
536 if (caa_likely(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))
538 /* Only if number of add multiple of 1UL << COUNT_COMMIT_ORDER */
540 dbg_printf("add split count %lu\n", split_count
);
541 count
= uatomic_add_return(&ht
->count
,
542 1UL << COUNT_COMMIT_ORDER
);
543 if (caa_likely(count
& (count
- 1)))
545 /* Only if global count is power of 2 */
547 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) < size
)
549 dbg_printf("add set global %ld\n", count
);
550 cds_lfht_resize_lazy_count(ht
, size
,
551 count
>> (CHAIN_LEN_TARGET
- 1));
555 void ht_count_del(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
557 unsigned long split_count
;
561 if (caa_unlikely(!ht
->split_count
))
563 index
= ht_get_split_count_index(hash
);
564 split_count
= uatomic_add_return(&ht
->split_count
[index
].del
, 1);
565 if (caa_likely(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))
567 /* Only if number of deletes multiple of 1UL << COUNT_COMMIT_ORDER */
569 dbg_printf("del split count %lu\n", split_count
);
570 count
= uatomic_add_return(&ht
->count
,
571 -(1UL << COUNT_COMMIT_ORDER
));
572 if (caa_likely(count
& (count
- 1)))
574 /* Only if global count is power of 2 */
576 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) >= size
)
578 dbg_printf("del set global %ld\n", count
);
580 * Don't shrink table if the number of nodes is below a
583 if (count
< (1UL << COUNT_COMMIT_ORDER
) * (split_count_mask
+ 1))
585 cds_lfht_resize_lazy_count(ht
, size
,
586 count
>> (CHAIN_LEN_TARGET
- 1));
590 void check_resize(struct cds_lfht
*ht
, unsigned long size
, uint32_t chain_len
)
594 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
596 count
= uatomic_read(&ht
->count
);
598 * Use bucket-local length for small table expand and for
599 * environments lacking per-cpu data support.
601 if (count
>= (1UL << COUNT_COMMIT_ORDER
))
604 dbg_printf("WARNING: large chain length: %u.\n",
606 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
607 cds_lfht_resize_lazy_grow(ht
, size
,
608 cds_lfht_get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
612 struct cds_lfht_node
*clear_flag(struct cds_lfht_node
*node
)
614 return (struct cds_lfht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
618 int is_removed(struct cds_lfht_node
*node
)
620 return ((unsigned long) node
) & REMOVED_FLAG
;
624 struct cds_lfht_node
*flag_removed(struct cds_lfht_node
*node
)
626 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
630 int is_bucket(struct cds_lfht_node
*node
)
632 return ((unsigned long) node
) & BUCKET_FLAG
;
636 struct cds_lfht_node
*flag_bucket(struct cds_lfht_node
*node
)
638 return (struct cds_lfht_node
*) (((unsigned long) node
) | BUCKET_FLAG
);
642 struct cds_lfht_node
*get_end(void)
644 return (struct cds_lfht_node
*) END_VALUE
;
648 int is_end(struct cds_lfht_node
*node
)
650 return clear_flag(node
) == (struct cds_lfht_node
*) END_VALUE
;
654 unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr
,
657 unsigned long old1
, old2
;
659 old1
= uatomic_read(ptr
);
664 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
669 void cds_lfht_alloc_bucket_table(struct cds_lfht
*ht
, unsigned long order
)
671 return ht
->mm
->alloc_bucket_table(ht
, order
);
675 * cds_lfht_free_bucket_table() should be called with decreasing order.
676 * When cds_lfht_free_bucket_table(0) is called, it means the whole
680 void cds_lfht_free_bucket_table(struct cds_lfht
*ht
, unsigned long order
)
682 return ht
->mm
->free_bucket_table(ht
, order
);
686 struct cds_lfht_node
*bucket_at(struct cds_lfht
*ht
, unsigned long index
)
688 return ht
->bucket_at(ht
, index
);
692 struct cds_lfht_node
*lookup_bucket(struct cds_lfht
*ht
, unsigned long size
,
696 return bucket_at(ht
, hash
& (size
- 1));
700 * Remove all logically deleted nodes from a bucket up to a certain node key.
703 void _cds_lfht_gc_bucket(struct cds_lfht_node
*bucket
, struct cds_lfht_node
*node
)
705 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_next
;
707 assert(!is_bucket(bucket
));
708 assert(!is_removed(bucket
));
709 assert(!is_bucket(node
));
710 assert(!is_removed(node
));
713 /* We can always skip the bucket node initially */
714 iter
= rcu_dereference(iter_prev
->next
);
715 assert(!is_removed(iter
));
716 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
718 * We should never be called with bucket (start of chain)
719 * and logically removed node (end of path compression
720 * marker) being the actual same node. This would be a
721 * bug in the algorithm implementation.
723 assert(bucket
!= node
);
725 if (caa_unlikely(is_end(iter
)))
727 if (caa_likely(clear_flag(iter
)->reverse_hash
> node
->reverse_hash
))
729 next
= rcu_dereference(clear_flag(iter
)->next
);
730 if (caa_likely(is_removed(next
)))
732 iter_prev
= clear_flag(iter
);
735 assert(!is_removed(iter
));
737 new_next
= flag_bucket(clear_flag(next
));
739 new_next
= clear_flag(next
);
740 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, new_next
);
745 int _cds_lfht_replace(struct cds_lfht
*ht
, unsigned long size
,
746 struct cds_lfht_node
*old_node
,
747 struct cds_lfht_node
*old_next
,
748 struct cds_lfht_node
*new_node
)
750 struct cds_lfht_node
*bucket
, *ret_next
;
752 if (!old_node
) /* Return -ENOENT if asked to replace NULL node */
755 assert(!is_removed(old_node
));
756 assert(!is_bucket(old_node
));
757 assert(!is_removed(new_node
));
758 assert(!is_bucket(new_node
));
759 assert(new_node
!= old_node
);
761 /* Insert after node to be replaced */
762 if (is_removed(old_next
)) {
764 * Too late, the old node has been removed under us
765 * between lookup and replace. Fail.
769 assert(!is_bucket(old_next
));
770 assert(new_node
!= clear_flag(old_next
));
771 new_node
->next
= clear_flag(old_next
);
773 * Here is the whole trick for lock-free replace: we add
774 * the replacement node _after_ the node we want to
775 * replace by atomically setting its next pointer at the
776 * same time we set its removal flag. Given that
777 * the lookups/get next use an iterator aware of the
778 * next pointer, they will either skip the old node due
779 * to the removal flag and see the new node, or use
780 * the old node, but will not see the new one.
782 ret_next
= uatomic_cmpxchg(&old_node
->next
,
783 old_next
, flag_removed(new_node
));
784 if (ret_next
== old_next
)
785 break; /* We performed the replacement. */
790 * Ensure that the old node is not visible to readers anymore:
791 * lookup for the node, and remove it (along with any other
792 * logically removed node) if found.
794 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(old_node
->reverse_hash
));
795 _cds_lfht_gc_bucket(bucket
, new_node
);
797 assert(is_removed(rcu_dereference(old_node
->next
)));
802 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
803 * mode. A NULL unique_ret allows creation of duplicate keys.
806 void _cds_lfht_add(struct cds_lfht
*ht
,
807 cds_lfht_match_fct match
,
810 struct cds_lfht_node
*node
,
811 struct cds_lfht_iter
*unique_ret
,
814 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
816 struct cds_lfht_node
*bucket
;
818 assert(!is_bucket(node
));
819 assert(!is_removed(node
));
820 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->reverse_hash
));
822 uint32_t chain_len
= 0;
825 * iter_prev points to the non-removed node prior to the
829 /* We can always skip the bucket node initially */
830 iter
= rcu_dereference(iter_prev
->next
);
831 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
833 if (caa_unlikely(is_end(iter
)))
835 if (caa_likely(clear_flag(iter
)->reverse_hash
> node
->reverse_hash
))
838 /* bucket node is the first node of the identical-hash-value chain */
839 if (bucket_flag
&& clear_flag(iter
)->reverse_hash
== node
->reverse_hash
)
842 next
= rcu_dereference(clear_flag(iter
)->next
);
843 if (caa_unlikely(is_removed(next
)))
849 && clear_flag(iter
)->reverse_hash
== node
->reverse_hash
) {
850 struct cds_lfht_iter d_iter
= { .node
= node
, .next
= iter
, };
853 * uniquely adding inserts the node as the first
854 * node of the identical-hash-value node chain.
856 * This semantic ensures no duplicated keys
857 * should ever be observable in the table
858 * (including observe one node by one node
859 * by forward iterations)
861 cds_lfht_next_duplicate(ht
, match
, key
, &d_iter
);
865 *unique_ret
= d_iter
;
869 /* Only account for identical reverse hash once */
870 if (iter_prev
->reverse_hash
!= clear_flag(iter
)->reverse_hash
872 check_resize(ht
, size
, ++chain_len
);
873 iter_prev
= clear_flag(iter
);
878 assert(node
!= clear_flag(iter
));
879 assert(!is_removed(iter_prev
));
880 assert(!is_removed(iter
));
881 assert(iter_prev
!= node
);
883 node
->next
= clear_flag(iter
);
885 node
->next
= flag_bucket(clear_flag(iter
));
887 new_node
= flag_bucket(node
);
890 if (uatomic_cmpxchg(&iter_prev
->next
, iter
,
892 continue; /* retry */
899 assert(!is_removed(iter
));
901 new_next
= flag_bucket(clear_flag(next
));
903 new_next
= clear_flag(next
);
904 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, new_next
);
909 unique_ret
->node
= return_node
;
910 /* unique_ret->next left unset, never used. */
915 int _cds_lfht_del(struct cds_lfht
*ht
, unsigned long size
,
916 struct cds_lfht_node
*node
,
919 struct cds_lfht_node
*bucket
, *next
, *old
;
921 if (!node
) /* Return -ENOENT if asked to delete NULL node */
924 /* logically delete the node */
925 assert(!is_bucket(node
));
926 assert(!is_removed(node
));
927 old
= rcu_dereference(node
->next
);
929 struct cds_lfht_node
*new_next
;
932 if (caa_unlikely(is_removed(next
)))
935 assert(is_bucket(next
));
937 assert(!is_bucket(next
));
938 new_next
= flag_removed(next
);
939 old
= uatomic_cmpxchg(&node
->next
, next
, new_next
);
940 } while (old
!= next
);
941 /* We performed the (logical) deletion. */
944 * Ensure that the node is not visible to readers anymore: lookup for
945 * the node, and remove it (along with any other logically removed node)
948 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->reverse_hash
));
949 _cds_lfht_gc_bucket(bucket
, node
);
951 assert(is_removed(rcu_dereference(node
->next
)));
956 void *partition_resize_thread(void *arg
)
958 struct partition_resize_work
*work
= arg
;
960 work
->ht
->flavor
->register_thread();
961 work
->fct(work
->ht
, work
->i
, work
->start
, work
->len
);
962 work
->ht
->flavor
->unregister_thread();
967 void partition_resize_helper(struct cds_lfht
*ht
, unsigned long i
,
969 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
970 unsigned long start
, unsigned long len
))
972 unsigned long partition_len
;
973 struct partition_resize_work
*work
;
975 unsigned long nr_threads
;
978 * Note: nr_cpus_mask + 1 is always power of 2.
979 * We spawn just the number of threads we need to satisfy the minimum
980 * partition size, up to the number of CPUs in the system.
982 if (nr_cpus_mask
> 0) {
983 nr_threads
= min(nr_cpus_mask
+ 1,
984 len
>> MIN_PARTITION_PER_THREAD_ORDER
);
988 partition_len
= len
>> cds_lfht_get_count_order_ulong(nr_threads
);
989 work
= calloc(nr_threads
, sizeof(*work
));
991 for (thread
= 0; thread
< nr_threads
; thread
++) {
992 work
[thread
].ht
= ht
;
994 work
[thread
].len
= partition_len
;
995 work
[thread
].start
= thread
* partition_len
;
996 work
[thread
].fct
= fct
;
997 ret
= pthread_create(&(work
[thread
].thread_id
), ht
->resize_attr
,
998 partition_resize_thread
, &work
[thread
]);
1001 for (thread
= 0; thread
< nr_threads
; thread
++) {
1002 ret
= pthread_join(work
[thread
].thread_id
, NULL
);
1009 * Holding RCU read lock to protect _cds_lfht_add against memory
1010 * reclaim that could be performed by other call_rcu worker threads (ABA
1013 * When we reach a certain length, we can split this population phase over
1014 * many worker threads, based on the number of CPUs available in the system.
1015 * This should therefore take care of not having the expand lagging behind too
1016 * many concurrent insertion threads by using the scheduler's ability to
1017 * schedule bucket node population fairly with insertions.
1020 void init_table_populate_partition(struct cds_lfht
*ht
, unsigned long i
,
1021 unsigned long start
, unsigned long len
)
1023 unsigned long j
, size
= 1UL << (i
- 1);
1025 assert(i
> MIN_TABLE_ORDER
);
1026 ht
->flavor
->read_lock();
1027 for (j
= size
+ start
; j
< size
+ start
+ len
; j
++) {
1028 struct cds_lfht_node
*new_node
= bucket_at(ht
, j
);
1030 assert(j
>= size
&& j
< (size
<< 1));
1031 dbg_printf("init populate: order %lu index %lu hash %lu\n",
1033 new_node
->reverse_hash
= bit_reverse_ulong(j
);
1034 _cds_lfht_add(ht
, NULL
, NULL
, size
, new_node
, NULL
, 1);
1036 ht
->flavor
->read_unlock();
1040 void init_table_populate(struct cds_lfht
*ht
, unsigned long i
,
1043 assert(nr_cpus_mask
!= -1);
1044 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1045 ht
->flavor
->thread_online();
1046 init_table_populate_partition(ht
, i
, 0, len
);
1047 ht
->flavor
->thread_offline();
1050 partition_resize_helper(ht
, i
, len
, init_table_populate_partition
);
1054 void init_table(struct cds_lfht
*ht
,
1055 unsigned long first_order
, unsigned long last_order
)
1059 dbg_printf("init table: first_order %lu last_order %lu\n",
1060 first_order
, last_order
);
1061 assert(first_order
> MIN_TABLE_ORDER
);
1062 for (i
= first_order
; i
<= last_order
; i
++) {
1065 len
= 1UL << (i
- 1);
1066 dbg_printf("init order %lu len: %lu\n", i
, len
);
1068 /* Stop expand if the resize target changes under us */
1069 if (CMM_LOAD_SHARED(ht
->resize_target
) < (1UL << i
))
1072 cds_lfht_alloc_bucket_table(ht
, i
);
1075 * Set all bucket nodes reverse hash values for a level and
1076 * link all bucket nodes into the table.
1078 init_table_populate(ht
, i
, len
);
1081 * Update table size.
1083 cmm_smp_wmb(); /* populate data before RCU size */
1084 CMM_STORE_SHARED(ht
->size
, 1UL << i
);
1086 dbg_printf("init new size: %lu\n", 1UL << i
);
1087 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1093 * Holding RCU read lock to protect _cds_lfht_remove against memory
1094 * reclaim that could be performed by other call_rcu worker threads (ABA
1096 * For a single level, we logically remove and garbage collect each node.
1098 * As a design choice, we perform logical removal and garbage collection on a
1099 * node-per-node basis to simplify this algorithm. We also assume keeping good
1100 * cache locality of the operation would overweight possible performance gain
1101 * that could be achieved by batching garbage collection for multiple levels.
1102 * However, this would have to be justified by benchmarks.
1104 * Concurrent removal and add operations are helping us perform garbage
1105 * collection of logically removed nodes. We guarantee that all logically
1106 * removed nodes have been garbage-collected (unlinked) before call_rcu is
1107 * invoked to free a hole level of bucket nodes (after a grace period).
1109 * Logical removal and garbage collection can therefore be done in batch or on a
1110 * node-per-node basis, as long as the guarantee above holds.
1112 * When we reach a certain length, we can split this removal over many worker
1113 * threads, based on the number of CPUs available in the system. This should
1114 * take care of not letting resize process lag behind too many concurrent
1115 * updater threads actively inserting into the hash table.
1118 void remove_table_partition(struct cds_lfht
*ht
, unsigned long i
,
1119 unsigned long start
, unsigned long len
)
1121 unsigned long j
, size
= 1UL << (i
- 1);
1123 assert(i
> MIN_TABLE_ORDER
);
1124 ht
->flavor
->read_lock();
1125 for (j
= size
+ start
; j
< size
+ start
+ len
; j
++) {
1126 struct cds_lfht_node
*fini_bucket
= bucket_at(ht
, j
);
1127 struct cds_lfht_node
*parent_bucket
= bucket_at(ht
, j
- size
);
1129 assert(j
>= size
&& j
< (size
<< 1));
1130 dbg_printf("remove entry: order %lu index %lu hash %lu\n",
1132 /* Set the REMOVED_FLAG to freeze the ->next for gc */
1133 uatomic_or(&fini_bucket
->next
, REMOVED_FLAG
);
1134 _cds_lfht_gc_bucket(parent_bucket
, fini_bucket
);
1136 ht
->flavor
->read_unlock();
1140 void remove_table(struct cds_lfht
*ht
, unsigned long i
, unsigned long len
)
1143 assert(nr_cpus_mask
!= -1);
1144 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1145 ht
->flavor
->thread_online();
1146 remove_table_partition(ht
, i
, 0, len
);
1147 ht
->flavor
->thread_offline();
1150 partition_resize_helper(ht
, i
, len
, remove_table_partition
);
1154 * fini_table() is never called for first_order == 0, which is why
1155 * free_by_rcu_order == 0 can be used as criterion to know if free must
1159 void fini_table(struct cds_lfht
*ht
,
1160 unsigned long first_order
, unsigned long last_order
)
1163 unsigned long free_by_rcu_order
= 0;
1165 dbg_printf("fini table: first_order %lu last_order %lu\n",
1166 first_order
, last_order
);
1167 assert(first_order
> MIN_TABLE_ORDER
);
1168 for (i
= last_order
; i
>= first_order
; i
--) {
1171 len
= 1UL << (i
- 1);
1172 dbg_printf("fini order %lu len: %lu\n", i
, len
);
1174 /* Stop shrink if the resize target changes under us */
1175 if (CMM_LOAD_SHARED(ht
->resize_target
) > (1UL << (i
- 1)))
1178 cmm_smp_wmb(); /* populate data before RCU size */
1179 CMM_STORE_SHARED(ht
->size
, 1UL << (i
- 1));
1182 * We need to wait for all add operations to reach Q.S. (and
1183 * thus use the new table for lookups) before we can start
1184 * releasing the old bucket nodes. Otherwise their lookup will
1185 * return a logically removed node as insert position.
1187 ht
->flavor
->update_synchronize_rcu();
1188 if (free_by_rcu_order
)
1189 cds_lfht_free_bucket_table(ht
, free_by_rcu_order
);
1192 * Set "removed" flag in bucket nodes about to be removed.
1193 * Unlink all now-logically-removed bucket node pointers.
1194 * Concurrent add/remove operation are helping us doing
1197 remove_table(ht
, i
, len
);
1199 free_by_rcu_order
= i
;
1201 dbg_printf("fini new size: %lu\n", 1UL << i
);
1202 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1206 if (free_by_rcu_order
) {
1207 ht
->flavor
->update_synchronize_rcu();
1208 cds_lfht_free_bucket_table(ht
, free_by_rcu_order
);
1213 void cds_lfht_create_bucket(struct cds_lfht
*ht
, unsigned long size
)
1215 struct cds_lfht_node
*prev
, *node
;
1216 unsigned long order
, len
, i
;
1218 cds_lfht_alloc_bucket_table(ht
, 0);
1220 dbg_printf("create bucket: order 0 index 0 hash 0\n");
1221 node
= bucket_at(ht
, 0);
1222 node
->next
= flag_bucket(get_end());
1223 node
->reverse_hash
= 0;
1225 for (order
= 1; order
< cds_lfht_get_count_order_ulong(size
) + 1; order
++) {
1226 len
= 1UL << (order
- 1);
1227 cds_lfht_alloc_bucket_table(ht
, order
);
1229 for (i
= 0; i
< len
; i
++) {
1231 * Now, we are trying to init the node with the
1232 * hash=(len+i) (which is also a bucket with the
1233 * index=(len+i)) and insert it into the hash table,
1234 * so this node has to be inserted after the bucket
1235 * with the index=(len+i)&(len-1)=i. And because there
1236 * is no other non-bucket node nor bucket node with
1237 * larger index/hash inserted, so the bucket node
1238 * being inserted should be inserted directly linked
1239 * after the bucket node with index=i.
1241 prev
= bucket_at(ht
, i
);
1242 node
= bucket_at(ht
, len
+ i
);
1244 dbg_printf("create bucket: order %lu index %lu hash %lu\n",
1245 order
, len
+ i
, len
+ i
);
1246 node
->reverse_hash
= bit_reverse_ulong(len
+ i
);
1248 /* insert after prev */
1249 assert(is_bucket(prev
->next
));
1250 node
->next
= prev
->next
;
1251 prev
->next
= flag_bucket(node
);
1256 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
1257 unsigned long min_nr_alloc_buckets
,
1258 unsigned long max_nr_buckets
,
1260 const struct cds_lfht_mm_type
*mm
,
1261 const struct rcu_flavor_struct
*flavor
,
1262 pthread_attr_t
*attr
)
1264 struct cds_lfht
*ht
;
1265 unsigned long order
;
1267 /* min_nr_alloc_buckets must be power of two */
1268 if (!min_nr_alloc_buckets
|| (min_nr_alloc_buckets
& (min_nr_alloc_buckets
- 1)))
1271 /* init_size must be power of two */
1272 if (!init_size
|| (init_size
& (init_size
- 1)))
1276 * Memory management plugin default.
1279 if (CAA_BITS_PER_LONG
> 32
1281 && max_nr_buckets
<= (1ULL << 32)) {
1283 * For 64-bit architectures, with max number of
1284 * buckets small enough not to use the entire
1285 * 64-bit memory mapping space (and allowing a
1286 * fair number of hash table instances), use the
1287 * mmap allocator, which is faster than the
1290 mm
= &cds_lfht_mm_mmap
;
1293 * The fallback is to use the order allocator.
1295 mm
= &cds_lfht_mm_order
;
1299 /* max_nr_buckets == 0 for order based mm means infinite */
1300 if (mm
== &cds_lfht_mm_order
&& !max_nr_buckets
)
1301 max_nr_buckets
= 1UL << (MAX_TABLE_ORDER
- 1);
1303 /* max_nr_buckets must be power of two */
1304 if (!max_nr_buckets
|| (max_nr_buckets
& (max_nr_buckets
- 1)))
1307 min_nr_alloc_buckets
= max(min_nr_alloc_buckets
, MIN_TABLE_SIZE
);
1308 init_size
= max(init_size
, MIN_TABLE_SIZE
);
1309 max_nr_buckets
= max(max_nr_buckets
, min_nr_alloc_buckets
);
1310 init_size
= min(init_size
, max_nr_buckets
);
1312 ht
= mm
->alloc_cds_lfht(min_nr_alloc_buckets
, max_nr_buckets
);
1314 assert(ht
->mm
== mm
);
1315 assert(ht
->bucket_at
== mm
->bucket_at
);
1318 ht
->flavor
= flavor
;
1319 ht
->resize_attr
= attr
;
1320 alloc_split_items_count(ht
);
1321 /* this mutex should not nest in read-side C.S. */
1322 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
1323 order
= cds_lfht_get_count_order_ulong(init_size
);
1324 ht
->resize_target
= 1UL << order
;
1325 cds_lfht_create_bucket(ht
, 1UL << order
);
1326 ht
->size
= 1UL << order
;
1330 void cds_lfht_lookup(struct cds_lfht
*ht
, unsigned long hash
,
1331 cds_lfht_match_fct match
, const void *key
,
1332 struct cds_lfht_iter
*iter
)
1334 struct cds_lfht_node
*node
, *next
, *bucket
;
1335 unsigned long reverse_hash
, size
;
1337 reverse_hash
= bit_reverse_ulong(hash
);
1339 size
= rcu_dereference(ht
->size
);
1340 bucket
= lookup_bucket(ht
, size
, hash
);
1341 /* We can always skip the bucket node initially */
1342 node
= rcu_dereference(bucket
->next
);
1343 node
= clear_flag(node
);
1345 if (caa_unlikely(is_end(node
))) {
1349 if (caa_unlikely(node
->reverse_hash
> reverse_hash
)) {
1353 next
= rcu_dereference(node
->next
);
1354 assert(node
== clear_flag(node
));
1355 if (caa_likely(!is_removed(next
))
1357 && node
->reverse_hash
== reverse_hash
1358 && caa_likely(match(node
, key
))) {
1361 node
= clear_flag(next
);
1363 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1368 void cds_lfht_next_duplicate(struct cds_lfht
*ht
, cds_lfht_match_fct match
,
1369 const void *key
, struct cds_lfht_iter
*iter
)
1371 struct cds_lfht_node
*node
, *next
;
1372 unsigned long reverse_hash
;
1375 reverse_hash
= node
->reverse_hash
;
1377 node
= clear_flag(next
);
1380 if (caa_unlikely(is_end(node
))) {
1384 if (caa_unlikely(node
->reverse_hash
> reverse_hash
)) {
1388 next
= rcu_dereference(node
->next
);
1389 if (caa_likely(!is_removed(next
))
1391 && caa_likely(match(node
, key
))) {
1394 node
= clear_flag(next
);
1396 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1401 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1403 struct cds_lfht_node
*node
, *next
;
1405 node
= clear_flag(iter
->next
);
1407 if (caa_unlikely(is_end(node
))) {
1411 next
= rcu_dereference(node
->next
);
1412 if (caa_likely(!is_removed(next
))
1413 && !is_bucket(next
)) {
1416 node
= clear_flag(next
);
1418 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1423 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1426 * Get next after first bucket node. The first bucket node is the
1427 * first node of the linked list.
1429 iter
->next
= bucket_at(ht
, 0)->next
;
1430 cds_lfht_next(ht
, iter
);
1433 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
1434 struct cds_lfht_node
*node
)
1438 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1439 size
= rcu_dereference(ht
->size
);
1440 _cds_lfht_add(ht
, NULL
, NULL
, size
, node
, NULL
, 0);
1441 ht_count_add(ht
, size
, hash
);
1444 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
1446 cds_lfht_match_fct match
,
1448 struct cds_lfht_node
*node
)
1451 struct cds_lfht_iter iter
;
1453 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1454 size
= rcu_dereference(ht
->size
);
1455 _cds_lfht_add(ht
, match
, key
, size
, node
, &iter
, 0);
1456 if (iter
.node
== node
)
1457 ht_count_add(ht
, size
, hash
);
1461 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
1463 cds_lfht_match_fct match
,
1465 struct cds_lfht_node
*node
)
1468 struct cds_lfht_iter iter
;
1470 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1471 size
= rcu_dereference(ht
->size
);
1473 _cds_lfht_add(ht
, match
, key
, size
, node
, &iter
, 0);
1474 if (iter
.node
== node
) {
1475 ht_count_add(ht
, size
, hash
);
1479 if (!_cds_lfht_replace(ht
, size
, iter
.node
, iter
.next
, node
))
1484 int cds_lfht_replace(struct cds_lfht
*ht
, struct cds_lfht_iter
*old_iter
,
1485 struct cds_lfht_node
*new_node
)
1489 size
= rcu_dereference(ht
->size
);
1490 return _cds_lfht_replace(ht
, size
, old_iter
->node
, old_iter
->next
,
1494 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1496 unsigned long size
, hash
;
1499 size
= rcu_dereference(ht
->size
);
1500 ret
= _cds_lfht_del(ht
, size
, iter
->node
, 0);
1502 hash
= bit_reverse_ulong(iter
->node
->reverse_hash
);
1503 ht_count_del(ht
, size
, hash
);
1509 int cds_lfht_delete_bucket(struct cds_lfht
*ht
)
1511 struct cds_lfht_node
*node
;
1512 unsigned long order
, i
, size
;
1514 /* Check that the table is empty */
1515 node
= bucket_at(ht
, 0);
1517 node
= clear_flag(node
)->next
;
1518 if (!is_bucket(node
))
1520 assert(!is_removed(node
));
1521 } while (!is_end(node
));
1523 * size accessed without rcu_dereference because hash table is
1527 /* Internal sanity check: all nodes left should be bucket */
1528 for (i
= 0; i
< size
; i
++) {
1529 node
= bucket_at(ht
, i
);
1530 dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n",
1531 i
, i
, bit_reverse_ulong(node
->reverse_hash
));
1532 assert(is_bucket(node
->next
));
1535 for (order
= cds_lfht_get_count_order_ulong(size
); (long)order
>= 0; order
--)
1536 cds_lfht_free_bucket_table(ht
, order
);
1542 * Should only be called when no more concurrent readers nor writers can
1543 * possibly access the table.
1545 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
)
1549 /* Wait for in-flight resize operations to complete */
1550 _CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
1551 cmm_smp_mb(); /* Store destroy before load resize */
1552 while (uatomic_read(&ht
->in_progress_resize
))
1553 poll(NULL
, 0, 100); /* wait for 100ms */
1554 ret
= cds_lfht_delete_bucket(ht
);
1557 free_split_items_count(ht
);
1559 *attr
= ht
->resize_attr
;
1564 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
1565 long *approx_before
,
1566 unsigned long *count
,
1567 unsigned long *removed
,
1570 struct cds_lfht_node
*node
, *next
;
1571 unsigned long nr_bucket
= 0;
1574 if (ht
->split_count
) {
1577 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1578 *approx_before
+= uatomic_read(&ht
->split_count
[i
].add
);
1579 *approx_before
-= uatomic_read(&ht
->split_count
[i
].del
);
1586 /* Count non-bucket nodes in the table */
1587 node
= bucket_at(ht
, 0);
1589 next
= rcu_dereference(node
->next
);
1590 if (is_removed(next
)) {
1591 if (!is_bucket(next
))
1595 } else if (!is_bucket(next
))
1599 node
= clear_flag(next
);
1600 } while (!is_end(node
));
1601 dbg_printf("number of bucket nodes: %lu\n", nr_bucket
);
1603 if (ht
->split_count
) {
1606 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1607 *approx_after
+= uatomic_read(&ht
->split_count
[i
].add
);
1608 *approx_after
-= uatomic_read(&ht
->split_count
[i
].del
);
1613 /* called with resize mutex held */
1615 void _do_cds_lfht_grow(struct cds_lfht
*ht
,
1616 unsigned long old_size
, unsigned long new_size
)
1618 unsigned long old_order
, new_order
;
1620 old_order
= cds_lfht_get_count_order_ulong(old_size
);
1621 new_order
= cds_lfht_get_count_order_ulong(new_size
);
1622 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1623 old_size
, old_order
, new_size
, new_order
);
1624 assert(new_size
> old_size
);
1625 init_table(ht
, old_order
+ 1, new_order
);
1628 /* called with resize mutex held */
1630 void _do_cds_lfht_shrink(struct cds_lfht
*ht
,
1631 unsigned long old_size
, unsigned long new_size
)
1633 unsigned long old_order
, new_order
;
1635 new_size
= max(new_size
, MIN_TABLE_SIZE
);
1636 old_order
= cds_lfht_get_count_order_ulong(old_size
);
1637 new_order
= cds_lfht_get_count_order_ulong(new_size
);
1638 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1639 old_size
, old_order
, new_size
, new_order
);
1640 assert(new_size
< old_size
);
1642 /* Remove and unlink all bucket nodes to remove. */
1643 fini_table(ht
, new_order
+ 1, old_order
);
1647 /* called with resize mutex held */
1649 void _do_cds_lfht_resize(struct cds_lfht
*ht
)
1651 unsigned long new_size
, old_size
;
1654 * Resize table, re-do if the target size has changed under us.
1657 assert(uatomic_read(&ht
->in_progress_resize
));
1658 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1660 ht
->resize_initiated
= 1;
1661 old_size
= ht
->size
;
1662 new_size
= CMM_LOAD_SHARED(ht
->resize_target
);
1663 if (old_size
< new_size
)
1664 _do_cds_lfht_grow(ht
, old_size
, new_size
);
1665 else if (old_size
> new_size
)
1666 _do_cds_lfht_shrink(ht
, old_size
, new_size
);
1667 ht
->resize_initiated
= 0;
1668 /* write resize_initiated before read resize_target */
1670 } while (ht
->size
!= CMM_LOAD_SHARED(ht
->resize_target
));
1674 unsigned long resize_target_grow(struct cds_lfht
*ht
, unsigned long new_size
)
1676 return _uatomic_xchg_monotonic_increase(&ht
->resize_target
, new_size
);
1680 void resize_target_update_count(struct cds_lfht
*ht
,
1681 unsigned long count
)
1683 count
= max(count
, MIN_TABLE_SIZE
);
1684 count
= min(count
, ht
->max_nr_buckets
);
1685 uatomic_set(&ht
->resize_target
, count
);
1688 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
)
1690 resize_target_update_count(ht
, new_size
);
1691 CMM_STORE_SHARED(ht
->resize_initiated
, 1);
1692 ht
->flavor
->thread_offline();
1693 pthread_mutex_lock(&ht
->resize_mutex
);
1694 _do_cds_lfht_resize(ht
);
1695 pthread_mutex_unlock(&ht
->resize_mutex
);
1696 ht
->flavor
->thread_online();
1700 void do_resize_cb(struct rcu_head
*head
)
1702 struct rcu_resize_work
*work
=
1703 caa_container_of(head
, struct rcu_resize_work
, head
);
1704 struct cds_lfht
*ht
= work
->ht
;
1706 ht
->flavor
->thread_offline();
1707 pthread_mutex_lock(&ht
->resize_mutex
);
1708 _do_cds_lfht_resize(ht
);
1709 pthread_mutex_unlock(&ht
->resize_mutex
);
1710 ht
->flavor
->thread_online();
1712 cmm_smp_mb(); /* finish resize before decrement */
1713 uatomic_dec(&ht
->in_progress_resize
);
1717 void __cds_lfht_resize_lazy_launch(struct cds_lfht
*ht
)
1719 struct rcu_resize_work
*work
;
1721 /* Store resize_target before read resize_initiated */
1723 if (!CMM_LOAD_SHARED(ht
->resize_initiated
)) {
1724 uatomic_inc(&ht
->in_progress_resize
);
1725 cmm_smp_mb(); /* increment resize count before load destroy */
1726 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
)) {
1727 uatomic_dec(&ht
->in_progress_resize
);
1730 work
= malloc(sizeof(*work
));
1732 ht
->flavor
->update_call_rcu(&work
->head
, do_resize_cb
);
1733 CMM_STORE_SHARED(ht
->resize_initiated
, 1);
1738 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
)
1740 unsigned long target_size
= size
<< growth
;
1742 target_size
= min(target_size
, ht
->max_nr_buckets
);
1743 if (resize_target_grow(ht
, target_size
) >= target_size
)
1746 __cds_lfht_resize_lazy_launch(ht
);
1750 * We favor grow operations over shrink. A shrink operation never occurs
1751 * if a grow operation is queued for lazy execution. A grow operation
1752 * cancels any pending shrink lazy execution.
1755 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
1756 unsigned long count
)
1758 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
1760 count
= max(count
, MIN_TABLE_SIZE
);
1761 count
= min(count
, ht
->max_nr_buckets
);
1763 return; /* Already the right size, no resize needed */
1764 if (count
> size
) { /* lazy grow */
1765 if (resize_target_grow(ht
, count
) >= count
)
1767 } else { /* lazy shrink */
1771 s
= uatomic_cmpxchg(&ht
->resize_target
, size
, count
);
1773 break; /* no resize needed */
1775 return; /* growing is/(was just) in progress */
1777 return; /* some other thread do shrink */
1781 __cds_lfht_resize_lazy_launch(ht
);