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 | |
161 #include <urcu-call-rcu.h>
162 #include <urcu/arch.h>
163 #include <urcu/uatomic.h>
164 #include <urcu/compiler.h>
168 #include "rculfhash.h"
169 #include "rculfhash-internal.h"
170 #include "urcu-flavor.h"
173 * We need to lock pthread exit, which deadlocks __nptl_setxid in the
175 * This work-around will be allowed to be removed when runas.c gets
176 * changed to do an exec() before issuing seteuid/setegid.
177 * See http://sourceware.org/bugzilla/show_bug.cgi?id=10184 for details.
179 pthread_mutex_t lttng_libc_state_lock
= PTHREAD_MUTEX_INITIALIZER
;
182 * Split-counters lazily update the global counter each 1024
183 * addition/removal. It automatically keeps track of resize required.
184 * We use the bucket length as indicator for need to expand for small
185 * tables and machines lacking per-cpu data suppport.
187 #define COUNT_COMMIT_ORDER 10
188 #define DEFAULT_SPLIT_COUNT_MASK 0xFUL
189 #define CHAIN_LEN_TARGET 1
190 #define CHAIN_LEN_RESIZE_THRESHOLD 3
193 * Define the minimum table size.
195 #define MIN_TABLE_ORDER 0
196 #define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER)
199 * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink.
201 #define MIN_PARTITION_PER_THREAD_ORDER 12
202 #define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
205 * The removed flag needs to be updated atomically with the pointer.
206 * It indicates that no node must attach to the node scheduled for
207 * removal, and that node garbage collection must be performed.
208 * The bucket flag does not require to be updated atomically with the
209 * pointer, but it is added as a pointer low bit flag to save space.
211 #define REMOVED_FLAG (1UL << 0)
212 #define BUCKET_FLAG (1UL << 1)
213 #define REMOVAL_OWNER_FLAG (1UL << 2)
214 #define FLAGS_MASK ((1UL << 3) - 1)
216 /* Value of the end pointer. Should not interact with flags. */
217 #define END_VALUE NULL
219 DEFINE_RCU_FLAVOR(rcu_flavor
);
222 * ht_items_count: Split-counters counting the number of node addition
223 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
224 * is set at hash table creation.
226 * These are free-running counters, never reset to zero. They count the
227 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
228 * operations to update the global counter. We choose a power-of-2 value
229 * for the trigger to deal with 32 or 64-bit overflow of the counter.
231 struct ht_items_count
{
232 unsigned long add
, del
;
233 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
236 * rcu_resize_work: Contains arguments passed to RCU worker thread
237 * responsible for performing lazy resize.
239 struct rcu_resize_work
{
240 struct rcu_head head
;
245 * partition_resize_work: Contains arguments passed to worker threads
246 * executing the hash table resize on partitions of the hash table
247 * assigned to each processor's worker thread.
249 struct partition_resize_work
{
252 unsigned long i
, start
, len
;
253 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
254 unsigned long start
, unsigned long len
);
258 * Algorithm to reverse bits in a word by lookup table, extended to
261 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
262 * Originally from Public Domain.
265 static const uint8_t BitReverseTable256
[256] =
267 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
268 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
269 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
270 R6(0), R6(2), R6(1), R6(3)
277 uint8_t bit_reverse_u8(uint8_t v
)
279 return BitReverseTable256
[v
];
282 static __attribute__((unused
))
283 uint32_t bit_reverse_u32(uint32_t v
)
285 return ((uint32_t) bit_reverse_u8(v
) << 24) |
286 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
287 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
288 ((uint32_t) bit_reverse_u8(v
>> 24));
291 static __attribute__((unused
))
292 uint64_t bit_reverse_u64(uint64_t v
)
294 return ((uint64_t) bit_reverse_u8(v
) << 56) |
295 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
296 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
297 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
298 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
299 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
300 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
301 ((uint64_t) bit_reverse_u8(v
>> 56));
305 unsigned long bit_reverse_ulong(unsigned long v
)
307 #if (CAA_BITS_PER_LONG == 32)
308 return bit_reverse_u32(v
);
310 return bit_reverse_u64(v
);
315 * fls: returns the position of the most significant bit.
316 * Returns 0 if no bit is set, else returns the position of the most
317 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
319 #if defined(__i386) || defined(__x86_64)
321 unsigned int fls_u32(uint32_t x
)
329 : "=r" (r
) : "rm" (x
));
335 #if defined(__x86_64)
337 unsigned int fls_u64(uint64_t x
)
345 : "=r" (r
) : "rm" (x
));
352 static __attribute__((unused
))
353 unsigned int fls_u64(uint64_t x
)
360 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
364 if (!(x
& 0xFFFF000000000000ULL
)) {
368 if (!(x
& 0xFF00000000000000ULL
)) {
372 if (!(x
& 0xF000000000000000ULL
)) {
376 if (!(x
& 0xC000000000000000ULL
)) {
380 if (!(x
& 0x8000000000000000ULL
)) {
389 static __attribute__((unused
))
390 unsigned int fls_u32(uint32_t x
)
396 if (!(x
& 0xFFFF0000U
)) {
400 if (!(x
& 0xFF000000U
)) {
404 if (!(x
& 0xF0000000U
)) {
408 if (!(x
& 0xC0000000U
)) {
412 if (!(x
& 0x80000000U
)) {
420 unsigned int cds_lfht_fls_ulong(unsigned long x
)
422 #if (CAA_BITS_PER_LONG == 32)
430 * Return the minimum order for which x <= (1UL << order).
431 * Return -1 if x is 0.
433 int cds_lfht_get_count_order_u32(uint32_t x
)
438 return fls_u32(x
- 1);
442 * Return the minimum order for which x <= (1UL << order).
443 * Return -1 if x is 0.
445 int cds_lfht_get_count_order_ulong(unsigned long x
)
450 return cds_lfht_fls_ulong(x
- 1);
454 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
);
457 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
458 unsigned long count
);
460 static long nr_cpus_mask
= -1;
461 static long split_count_mask
= -1;
463 #if defined(HAVE_SYSCONF)
464 static void ht_init_nr_cpus_mask(void)
468 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
474 * round up number of CPUs to next power of two, so we
475 * can use & for modulo.
477 maxcpus
= 1UL << cds_lfht_get_count_order_ulong(maxcpus
);
478 nr_cpus_mask
= maxcpus
- 1;
480 #else /* #if defined(HAVE_SYSCONF) */
481 static void ht_init_nr_cpus_mask(void)
485 #endif /* #else #if defined(HAVE_SYSCONF) */
488 void alloc_split_items_count(struct cds_lfht
*ht
)
490 struct ht_items_count
*count
;
492 if (nr_cpus_mask
== -1) {
493 ht_init_nr_cpus_mask();
494 if (nr_cpus_mask
< 0)
495 split_count_mask
= DEFAULT_SPLIT_COUNT_MASK
;
497 split_count_mask
= nr_cpus_mask
;
500 assert(split_count_mask
>= 0);
502 if (ht
->flags
& CDS_LFHT_ACCOUNTING
) {
503 ht
->split_count
= calloc(split_count_mask
+ 1, sizeof(*count
));
504 assert(ht
->split_count
);
506 ht
->split_count
= NULL
;
511 void free_split_items_count(struct cds_lfht
*ht
)
513 poison_free(ht
->split_count
);
516 #if defined(HAVE_SCHED_GETCPU)
518 int ht_get_split_count_index(unsigned long hash
)
522 assert(split_count_mask
>= 0);
523 cpu
= sched_getcpu();
524 if (caa_unlikely(cpu
< 0))
525 return hash
& split_count_mask
;
527 return cpu
& split_count_mask
;
529 #else /* #if defined(HAVE_SCHED_GETCPU) */
531 int ht_get_split_count_index(unsigned long hash
)
533 return hash
& split_count_mask
;
535 #endif /* #else #if defined(HAVE_SCHED_GETCPU) */
538 void ht_count_add(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
540 unsigned long split_count
;
544 if (caa_unlikely(!ht
->split_count
))
546 index
= ht_get_split_count_index(hash
);
547 split_count
= uatomic_add_return(&ht
->split_count
[index
].add
, 1);
548 if (caa_likely(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))
550 /* Only if number of add multiple of 1UL << COUNT_COMMIT_ORDER */
552 dbg_printf("add split count %lu\n", split_count
);
553 count
= uatomic_add_return(&ht
->count
,
554 1UL << COUNT_COMMIT_ORDER
);
555 if (caa_likely(count
& (count
- 1)))
557 /* Only if global count is power of 2 */
559 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) < size
)
561 dbg_printf("add set global %ld\n", count
);
562 cds_lfht_resize_lazy_count(ht
, size
,
563 count
>> (CHAIN_LEN_TARGET
- 1));
567 void ht_count_del(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
569 unsigned long split_count
;
573 if (caa_unlikely(!ht
->split_count
))
575 index
= ht_get_split_count_index(hash
);
576 split_count
= uatomic_add_return(&ht
->split_count
[index
].del
, 1);
577 if (caa_likely(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))
579 /* Only if number of deletes multiple of 1UL << COUNT_COMMIT_ORDER */
581 dbg_printf("del split count %lu\n", split_count
);
582 count
= uatomic_add_return(&ht
->count
,
583 -(1UL << COUNT_COMMIT_ORDER
));
584 if (caa_likely(count
& (count
- 1)))
586 /* Only if global count is power of 2 */
588 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) >= size
)
590 dbg_printf("del set global %ld\n", count
);
592 * Don't shrink table if the number of nodes is below a
595 if (count
< (1UL << COUNT_COMMIT_ORDER
) * (split_count_mask
+ 1))
597 cds_lfht_resize_lazy_count(ht
, size
,
598 count
>> (CHAIN_LEN_TARGET
- 1));
602 void check_resize(struct cds_lfht
*ht
, unsigned long size
, uint32_t chain_len
)
606 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
608 count
= uatomic_read(&ht
->count
);
610 * Use bucket-local length for small table expand and for
611 * environments lacking per-cpu data support.
613 if (count
>= (1UL << COUNT_COMMIT_ORDER
))
616 dbg_printf("WARNING: large chain length: %u.\n",
618 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
619 cds_lfht_resize_lazy_grow(ht
, size
,
620 cds_lfht_get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
624 struct cds_lfht_node
*clear_flag(struct cds_lfht_node
*node
)
626 return (struct cds_lfht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
630 int is_removed(struct cds_lfht_node
*node
)
632 return ((unsigned long) node
) & REMOVED_FLAG
;
636 struct cds_lfht_node
*flag_removed(struct cds_lfht_node
*node
)
638 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
642 int is_bucket(struct cds_lfht_node
*node
)
644 return ((unsigned long) node
) & BUCKET_FLAG
;
648 struct cds_lfht_node
*flag_bucket(struct cds_lfht_node
*node
)
650 return (struct cds_lfht_node
*) (((unsigned long) node
) | BUCKET_FLAG
);
654 int is_removal_owner(struct cds_lfht_node
*node
)
656 return ((unsigned long) node
) & REMOVAL_OWNER_FLAG
;
660 struct cds_lfht_node
*flag_removal_owner(struct cds_lfht_node
*node
)
662 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVAL_OWNER_FLAG
);
666 struct cds_lfht_node
*get_end(void)
668 return (struct cds_lfht_node
*) END_VALUE
;
672 int is_end(struct cds_lfht_node
*node
)
674 return clear_flag(node
) == (struct cds_lfht_node
*) END_VALUE
;
678 unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr
,
681 unsigned long old1
, old2
;
683 old1
= uatomic_read(ptr
);
688 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
693 void cds_lfht_alloc_bucket_table(struct cds_lfht
*ht
, unsigned long order
)
695 return ht
->mm
->alloc_bucket_table(ht
, order
);
699 * cds_lfht_free_bucket_table() should be called with decreasing order.
700 * When cds_lfht_free_bucket_table(0) is called, it means the whole
704 void cds_lfht_free_bucket_table(struct cds_lfht
*ht
, unsigned long order
)
706 return ht
->mm
->free_bucket_table(ht
, order
);
710 struct cds_lfht_node
*bucket_at(struct cds_lfht
*ht
, unsigned long index
)
712 return ht
->bucket_at(ht
, index
);
716 struct cds_lfht_node
*lookup_bucket(struct cds_lfht
*ht
, unsigned long size
,
720 return bucket_at(ht
, hash
& (size
- 1));
724 * Remove all logically deleted nodes from a bucket up to a certain node key.
727 void _cds_lfht_gc_bucket(struct cds_lfht_node
*bucket
, struct cds_lfht_node
*node
)
729 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_next
;
731 assert(!is_bucket(bucket
));
732 assert(!is_removed(bucket
));
733 assert(!is_bucket(node
));
734 assert(!is_removed(node
));
737 /* We can always skip the bucket node initially */
738 iter
= rcu_dereference(iter_prev
->next
);
739 assert(!is_removed(iter
));
740 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
742 * We should never be called with bucket (start of chain)
743 * and logically removed node (end of path compression
744 * marker) being the actual same node. This would be a
745 * bug in the algorithm implementation.
747 assert(bucket
!= node
);
749 if (caa_unlikely(is_end(iter
)))
751 if (caa_likely(clear_flag(iter
)->reverse_hash
> node
->reverse_hash
))
753 next
= rcu_dereference(clear_flag(iter
)->next
);
754 if (caa_likely(is_removed(next
)))
756 iter_prev
= clear_flag(iter
);
759 assert(!is_removed(iter
));
761 new_next
= flag_bucket(clear_flag(next
));
763 new_next
= clear_flag(next
);
764 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, new_next
);
769 int _cds_lfht_replace(struct cds_lfht
*ht
, unsigned long size
,
770 struct cds_lfht_node
*old_node
,
771 struct cds_lfht_node
*old_next
,
772 struct cds_lfht_node
*new_node
)
774 struct cds_lfht_node
*bucket
, *ret_next
;
776 if (!old_node
) /* Return -ENOENT if asked to replace NULL node */
779 assert(!is_removed(old_node
));
780 assert(!is_bucket(old_node
));
781 assert(!is_removed(new_node
));
782 assert(!is_bucket(new_node
));
783 assert(new_node
!= old_node
);
785 /* Insert after node to be replaced */
786 if (is_removed(old_next
)) {
788 * Too late, the old node has been removed under us
789 * between lookup and replace. Fail.
793 assert(old_next
== clear_flag(old_next
));
794 assert(new_node
!= old_next
);
795 new_node
->next
= old_next
;
797 * Here is the whole trick for lock-free replace: we add
798 * the replacement node _after_ the node we want to
799 * replace by atomically setting its next pointer at the
800 * same time we set its removal flag. Given that
801 * the lookups/get next use an iterator aware of the
802 * next pointer, they will either skip the old node due
803 * to the removal flag and see the new node, or use
804 * the old node, but will not see the new one.
805 * This is a replacement of a node with another node
806 * that has the same value: we are therefore not
807 * removing a value from the hash table.
809 ret_next
= uatomic_cmpxchg(&old_node
->next
,
810 old_next
, flag_removed(new_node
));
811 if (ret_next
== old_next
)
812 break; /* We performed the replacement. */
817 * Ensure that the old node is not visible to readers anymore:
818 * lookup for the node, and remove it (along with any other
819 * logically removed node) if found.
821 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(old_node
->reverse_hash
));
822 _cds_lfht_gc_bucket(bucket
, new_node
);
824 assert(is_removed(rcu_dereference(old_node
->next
)));
829 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
830 * mode. A NULL unique_ret allows creation of duplicate keys.
833 void _cds_lfht_add(struct cds_lfht
*ht
,
835 cds_lfht_match_fct match
,
838 struct cds_lfht_node
*node
,
839 struct cds_lfht_iter
*unique_ret
,
842 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
844 struct cds_lfht_node
*bucket
;
846 assert(!is_bucket(node
));
847 assert(!is_removed(node
));
848 bucket
= lookup_bucket(ht
, size
, hash
);
850 uint32_t chain_len
= 0;
853 * iter_prev points to the non-removed node prior to the
857 /* We can always skip the bucket node initially */
858 iter
= rcu_dereference(iter_prev
->next
);
859 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
861 if (caa_unlikely(is_end(iter
)))
863 if (caa_likely(clear_flag(iter
)->reverse_hash
> node
->reverse_hash
))
866 /* bucket node is the first node of the identical-hash-value chain */
867 if (bucket_flag
&& clear_flag(iter
)->reverse_hash
== node
->reverse_hash
)
870 next
= rcu_dereference(clear_flag(iter
)->next
);
871 if (caa_unlikely(is_removed(next
)))
877 && clear_flag(iter
)->reverse_hash
== node
->reverse_hash
) {
878 struct cds_lfht_iter d_iter
= { .node
= node
, .next
= iter
, };
881 * uniquely adding inserts the node as the first
882 * node of the identical-hash-value node chain.
884 * This semantic ensures no duplicated keys
885 * should ever be observable in the table
886 * (including observe one node by one node
887 * by forward iterations)
889 cds_lfht_next_duplicate(ht
, match
, key
, &d_iter
);
893 *unique_ret
= d_iter
;
897 /* Only account for identical reverse hash once */
898 if (iter_prev
->reverse_hash
!= clear_flag(iter
)->reverse_hash
900 check_resize(ht
, size
, ++chain_len
);
901 iter_prev
= clear_flag(iter
);
906 assert(node
!= clear_flag(iter
));
907 assert(!is_removed(iter_prev
));
908 assert(!is_removed(iter
));
909 assert(iter_prev
!= node
);
911 node
->next
= clear_flag(iter
);
913 node
->next
= flag_bucket(clear_flag(iter
));
915 new_node
= flag_bucket(node
);
918 if (uatomic_cmpxchg(&iter_prev
->next
, iter
,
920 continue; /* retry */
927 assert(!is_removed(iter
));
929 new_next
= flag_bucket(clear_flag(next
));
931 new_next
= clear_flag(next
);
932 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, new_next
);
937 unique_ret
->node
= return_node
;
938 /* unique_ret->next left unset, never used. */
943 int _cds_lfht_del(struct cds_lfht
*ht
, unsigned long size
,
944 struct cds_lfht_node
*node
)
946 struct cds_lfht_node
*bucket
, *next
;
948 if (!node
) /* Return -ENOENT if asked to delete NULL node */
951 /* logically delete the node */
952 assert(!is_bucket(node
));
953 assert(!is_removed(node
));
954 assert(!is_removal_owner(node
));
957 * We are first checking if the node had previously been
958 * logically removed (this check is not atomic with setting the
959 * logical removal flag). Return -ENOENT if the node had
960 * previously been removed.
962 next
= rcu_dereference(node
->next
);
963 if (caa_unlikely(is_removed(next
)))
965 assert(!is_bucket(next
));
967 * We set the REMOVED_FLAG unconditionally. Note that there may
968 * be more than one concurrent thread setting this flag.
969 * Knowing which wins the race will be known after the garbage
970 * collection phase, stay tuned!
972 uatomic_or(&node
->next
, REMOVED_FLAG
);
973 /* We performed the (logical) deletion. */
976 * Ensure that the node is not visible to readers anymore: lookup for
977 * the node, and remove it (along with any other logically removed node)
980 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->reverse_hash
));
981 _cds_lfht_gc_bucket(bucket
, node
);
983 assert(is_removed(rcu_dereference(node
->next
)));
985 * Last phase: atomically exchange node->next with a version
986 * having "REMOVAL_OWNER_FLAG" set. If the returned node->next
987 * pointer did _not_ have "REMOVAL_OWNER_FLAG" set, we now own
988 * the node and win the removal race.
989 * It is interesting to note that all "add" paths are forbidden
990 * to change the next pointer starting from the point where the
991 * REMOVED_FLAG is set, so here using a read, followed by a
992 * xchg() suffice to guarantee that the xchg() will ever only
993 * set the "REMOVAL_OWNER_FLAG" (or change nothing if the flag
996 if (!is_removal_owner(uatomic_xchg(&node
->next
,
997 flag_removal_owner(node
->next
))))
1004 void *partition_resize_thread(void *arg
)
1006 struct partition_resize_work
*work
= arg
;
1008 work
->ht
->flavor
->register_thread();
1009 work
->fct(work
->ht
, work
->i
, work
->start
, work
->len
);
1010 work
->ht
->flavor
->unregister_thread();
1015 void partition_resize_helper(struct cds_lfht
*ht
, unsigned long i
,
1017 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
1018 unsigned long start
, unsigned long len
))
1020 unsigned long partition_len
;
1021 struct partition_resize_work
*work
;
1023 unsigned long nr_threads
;
1026 * Note: nr_cpus_mask + 1 is always power of 2.
1027 * We spawn just the number of threads we need to satisfy the minimum
1028 * partition size, up to the number of CPUs in the system.
1030 if (nr_cpus_mask
> 0) {
1031 nr_threads
= min(nr_cpus_mask
+ 1,
1032 len
>> MIN_PARTITION_PER_THREAD_ORDER
);
1036 partition_len
= len
>> cds_lfht_get_count_order_ulong(nr_threads
);
1037 work
= calloc(nr_threads
, sizeof(*work
));
1039 pthread_mutex_lock(<tng_libc_state_lock
);
1040 for (thread
= 0; thread
< nr_threads
; thread
++) {
1041 work
[thread
].ht
= ht
;
1043 work
[thread
].len
= partition_len
;
1044 work
[thread
].start
= thread
* partition_len
;
1045 work
[thread
].fct
= fct
;
1046 ret
= pthread_create(&(work
[thread
].thread_id
), ht
->resize_attr
,
1047 partition_resize_thread
, &work
[thread
]);
1050 for (thread
= 0; thread
< nr_threads
; thread
++) {
1051 ret
= pthread_join(work
[thread
].thread_id
, NULL
);
1054 pthread_mutex_unlock(<tng_libc_state_lock
);
1059 * Holding RCU read lock to protect _cds_lfht_add against memory
1060 * reclaim that could be performed by other call_rcu worker threads (ABA
1063 * When we reach a certain length, we can split this population phase over
1064 * many worker threads, based on the number of CPUs available in the system.
1065 * This should therefore take care of not having the expand lagging behind too
1066 * many concurrent insertion threads by using the scheduler's ability to
1067 * schedule bucket node population fairly with insertions.
1070 void init_table_populate_partition(struct cds_lfht
*ht
, unsigned long i
,
1071 unsigned long start
, unsigned long len
)
1073 unsigned long j
, size
= 1UL << (i
- 1);
1075 assert(i
> MIN_TABLE_ORDER
);
1076 ht
->flavor
->read_lock();
1077 for (j
= size
+ start
; j
< size
+ start
+ len
; j
++) {
1078 struct cds_lfht_node
*new_node
= bucket_at(ht
, j
);
1080 assert(j
>= size
&& j
< (size
<< 1));
1081 dbg_printf("init populate: order %lu index %lu hash %lu\n",
1083 new_node
->reverse_hash
= bit_reverse_ulong(j
);
1084 _cds_lfht_add(ht
, j
, NULL
, NULL
, size
, new_node
, NULL
, 1);
1086 ht
->flavor
->read_unlock();
1090 void init_table_populate(struct cds_lfht
*ht
, unsigned long i
,
1093 assert(nr_cpus_mask
!= -1);
1094 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1095 ht
->flavor
->thread_online();
1096 init_table_populate_partition(ht
, i
, 0, len
);
1097 ht
->flavor
->thread_offline();
1100 partition_resize_helper(ht
, i
, len
, init_table_populate_partition
);
1104 void init_table(struct cds_lfht
*ht
,
1105 unsigned long first_order
, unsigned long last_order
)
1109 dbg_printf("init table: first_order %lu last_order %lu\n",
1110 first_order
, last_order
);
1111 assert(first_order
> MIN_TABLE_ORDER
);
1112 for (i
= first_order
; i
<= last_order
; i
++) {
1115 len
= 1UL << (i
- 1);
1116 dbg_printf("init order %lu len: %lu\n", i
, len
);
1118 /* Stop expand if the resize target changes under us */
1119 if (CMM_LOAD_SHARED(ht
->resize_target
) < (1UL << i
))
1122 cds_lfht_alloc_bucket_table(ht
, i
);
1125 * Set all bucket nodes reverse hash values for a level and
1126 * link all bucket nodes into the table.
1128 init_table_populate(ht
, i
, len
);
1131 * Update table size.
1133 cmm_smp_wmb(); /* populate data before RCU size */
1134 CMM_STORE_SHARED(ht
->size
, 1UL << i
);
1136 dbg_printf("init new size: %lu\n", 1UL << i
);
1137 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1143 * Holding RCU read lock to protect _cds_lfht_remove against memory
1144 * reclaim that could be performed by other call_rcu worker threads (ABA
1146 * For a single level, we logically remove and garbage collect each node.
1148 * As a design choice, we perform logical removal and garbage collection on a
1149 * node-per-node basis to simplify this algorithm. We also assume keeping good
1150 * cache locality of the operation would overweight possible performance gain
1151 * that could be achieved by batching garbage collection for multiple levels.
1152 * However, this would have to be justified by benchmarks.
1154 * Concurrent removal and add operations are helping us perform garbage
1155 * collection of logically removed nodes. We guarantee that all logically
1156 * removed nodes have been garbage-collected (unlinked) before call_rcu is
1157 * invoked to free a hole level of bucket nodes (after a grace period).
1159 * Logical removal and garbage collection can therefore be done in batch or on a
1160 * node-per-node basis, as long as the guarantee above holds.
1162 * When we reach a certain length, we can split this removal over many worker
1163 * threads, based on the number of CPUs available in the system. This should
1164 * take care of not letting resize process lag behind too many concurrent
1165 * updater threads actively inserting into the hash table.
1168 void remove_table_partition(struct cds_lfht
*ht
, unsigned long i
,
1169 unsigned long start
, unsigned long len
)
1171 unsigned long j
, size
= 1UL << (i
- 1);
1173 assert(i
> MIN_TABLE_ORDER
);
1174 ht
->flavor
->read_lock();
1175 for (j
= size
+ start
; j
< size
+ start
+ len
; j
++) {
1176 struct cds_lfht_node
*fini_bucket
= bucket_at(ht
, j
);
1177 struct cds_lfht_node
*parent_bucket
= bucket_at(ht
, j
- size
);
1179 assert(j
>= size
&& j
< (size
<< 1));
1180 dbg_printf("remove entry: order %lu index %lu hash %lu\n",
1182 /* Set the REMOVED_FLAG to freeze the ->next for gc */
1183 uatomic_or(&fini_bucket
->next
, REMOVED_FLAG
);
1184 _cds_lfht_gc_bucket(parent_bucket
, fini_bucket
);
1186 ht
->flavor
->read_unlock();
1190 void remove_table(struct cds_lfht
*ht
, unsigned long i
, unsigned long len
)
1193 assert(nr_cpus_mask
!= -1);
1194 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1195 ht
->flavor
->thread_online();
1196 remove_table_partition(ht
, i
, 0, len
);
1197 ht
->flavor
->thread_offline();
1200 partition_resize_helper(ht
, i
, len
, remove_table_partition
);
1204 * fini_table() is never called for first_order == 0, which is why
1205 * free_by_rcu_order == 0 can be used as criterion to know if free must
1209 void fini_table(struct cds_lfht
*ht
,
1210 unsigned long first_order
, unsigned long last_order
)
1213 unsigned long free_by_rcu_order
= 0;
1215 dbg_printf("fini table: first_order %lu last_order %lu\n",
1216 first_order
, last_order
);
1217 assert(first_order
> MIN_TABLE_ORDER
);
1218 for (i
= last_order
; i
>= first_order
; i
--) {
1221 len
= 1UL << (i
- 1);
1222 dbg_printf("fini order %lu len: %lu\n", i
, len
);
1224 /* Stop shrink if the resize target changes under us */
1225 if (CMM_LOAD_SHARED(ht
->resize_target
) > (1UL << (i
- 1)))
1228 cmm_smp_wmb(); /* populate data before RCU size */
1229 CMM_STORE_SHARED(ht
->size
, 1UL << (i
- 1));
1232 * We need to wait for all add operations to reach Q.S. (and
1233 * thus use the new table for lookups) before we can start
1234 * releasing the old bucket nodes. Otherwise their lookup will
1235 * return a logically removed node as insert position.
1237 ht
->flavor
->update_synchronize_rcu();
1238 if (free_by_rcu_order
)
1239 cds_lfht_free_bucket_table(ht
, free_by_rcu_order
);
1242 * Set "removed" flag in bucket nodes about to be removed.
1243 * Unlink all now-logically-removed bucket node pointers.
1244 * Concurrent add/remove operation are helping us doing
1247 remove_table(ht
, i
, len
);
1249 free_by_rcu_order
= i
;
1251 dbg_printf("fini new size: %lu\n", 1UL << i
);
1252 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1256 if (free_by_rcu_order
) {
1257 ht
->flavor
->update_synchronize_rcu();
1258 cds_lfht_free_bucket_table(ht
, free_by_rcu_order
);
1263 void cds_lfht_create_bucket(struct cds_lfht
*ht
, unsigned long size
)
1265 struct cds_lfht_node
*prev
, *node
;
1266 unsigned long order
, len
, i
;
1268 cds_lfht_alloc_bucket_table(ht
, 0);
1270 dbg_printf("create bucket: order 0 index 0 hash 0\n");
1271 node
= bucket_at(ht
, 0);
1272 node
->next
= flag_bucket(get_end());
1273 node
->reverse_hash
= 0;
1275 for (order
= 1; order
< cds_lfht_get_count_order_ulong(size
) + 1; order
++) {
1276 len
= 1UL << (order
- 1);
1277 cds_lfht_alloc_bucket_table(ht
, order
);
1279 for (i
= 0; i
< len
; i
++) {
1281 * Now, we are trying to init the node with the
1282 * hash=(len+i) (which is also a bucket with the
1283 * index=(len+i)) and insert it into the hash table,
1284 * so this node has to be inserted after the bucket
1285 * with the index=(len+i)&(len-1)=i. And because there
1286 * is no other non-bucket node nor bucket node with
1287 * larger index/hash inserted, so the bucket node
1288 * being inserted should be inserted directly linked
1289 * after the bucket node with index=i.
1291 prev
= bucket_at(ht
, i
);
1292 node
= bucket_at(ht
, len
+ i
);
1294 dbg_printf("create bucket: order %lu index %lu hash %lu\n",
1295 order
, len
+ i
, len
+ i
);
1296 node
->reverse_hash
= bit_reverse_ulong(len
+ i
);
1298 /* insert after prev */
1299 assert(is_bucket(prev
->next
));
1300 node
->next
= prev
->next
;
1301 prev
->next
= flag_bucket(node
);
1306 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
1307 unsigned long min_nr_alloc_buckets
,
1308 unsigned long max_nr_buckets
,
1310 const struct cds_lfht_mm_type
*mm
,
1311 const struct rcu_flavor_struct
*flavor
,
1312 pthread_attr_t
*attr
)
1314 struct cds_lfht
*ht
;
1315 unsigned long order
;
1317 /* min_nr_alloc_buckets must be power of two */
1318 if (!min_nr_alloc_buckets
|| (min_nr_alloc_buckets
& (min_nr_alloc_buckets
- 1)))
1321 /* init_size must be power of two */
1322 if (!init_size
|| (init_size
& (init_size
- 1)))
1326 * Memory management plugin default.
1329 if (CAA_BITS_PER_LONG
> 32
1331 && max_nr_buckets
<= (1ULL << 32)) {
1333 * For 64-bit architectures, with max number of
1334 * buckets small enough not to use the entire
1335 * 64-bit memory mapping space (and allowing a
1336 * fair number of hash table instances), use the
1337 * mmap allocator, which is faster than the
1340 mm
= &cds_lfht_mm_mmap
;
1343 * The fallback is to use the order allocator.
1345 mm
= &cds_lfht_mm_order
;
1349 /* max_nr_buckets == 0 for order based mm means infinite */
1350 if (mm
== &cds_lfht_mm_order
&& !max_nr_buckets
)
1351 max_nr_buckets
= 1UL << (MAX_TABLE_ORDER
- 1);
1353 /* max_nr_buckets must be power of two */
1354 if (!max_nr_buckets
|| (max_nr_buckets
& (max_nr_buckets
- 1)))
1357 min_nr_alloc_buckets
= max(min_nr_alloc_buckets
, MIN_TABLE_SIZE
);
1358 init_size
= max(init_size
, MIN_TABLE_SIZE
);
1359 max_nr_buckets
= max(max_nr_buckets
, min_nr_alloc_buckets
);
1360 init_size
= min(init_size
, max_nr_buckets
);
1362 ht
= mm
->alloc_cds_lfht(min_nr_alloc_buckets
, max_nr_buckets
);
1364 assert(ht
->mm
== mm
);
1365 assert(ht
->bucket_at
== mm
->bucket_at
);
1368 ht
->flavor
= flavor
;
1369 ht
->resize_attr
= attr
;
1370 alloc_split_items_count(ht
);
1371 /* this mutex should not nest in read-side C.S. */
1372 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
1373 order
= cds_lfht_get_count_order_ulong(init_size
);
1374 ht
->resize_target
= 1UL << order
;
1375 cds_lfht_create_bucket(ht
, 1UL << order
);
1376 ht
->size
= 1UL << order
;
1380 void cds_lfht_lookup(struct cds_lfht
*ht
, unsigned long hash
,
1381 cds_lfht_match_fct match
, const void *key
,
1382 struct cds_lfht_iter
*iter
)
1384 struct cds_lfht_node
*node
, *next
, *bucket
;
1385 unsigned long reverse_hash
, size
;
1387 reverse_hash
= bit_reverse_ulong(hash
);
1389 size
= rcu_dereference(ht
->size
);
1390 bucket
= lookup_bucket(ht
, size
, hash
);
1391 /* We can always skip the bucket node initially */
1392 node
= rcu_dereference(bucket
->next
);
1393 node
= clear_flag(node
);
1395 if (caa_unlikely(is_end(node
))) {
1399 if (caa_unlikely(node
->reverse_hash
> reverse_hash
)) {
1403 next
= rcu_dereference(node
->next
);
1404 assert(node
== clear_flag(node
));
1405 if (caa_likely(!is_removed(next
))
1407 && node
->reverse_hash
== reverse_hash
1408 && caa_likely(match(node
, key
))) {
1411 node
= clear_flag(next
);
1413 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1418 void cds_lfht_next_duplicate(struct cds_lfht
*ht
, cds_lfht_match_fct match
,
1419 const void *key
, struct cds_lfht_iter
*iter
)
1421 struct cds_lfht_node
*node
, *next
;
1422 unsigned long reverse_hash
;
1425 reverse_hash
= node
->reverse_hash
;
1427 node
= clear_flag(next
);
1430 if (caa_unlikely(is_end(node
))) {
1434 if (caa_unlikely(node
->reverse_hash
> reverse_hash
)) {
1438 next
= rcu_dereference(node
->next
);
1439 if (caa_likely(!is_removed(next
))
1441 && caa_likely(match(node
, key
))) {
1444 node
= clear_flag(next
);
1446 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1451 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1453 struct cds_lfht_node
*node
, *next
;
1455 node
= clear_flag(iter
->next
);
1457 if (caa_unlikely(is_end(node
))) {
1461 next
= rcu_dereference(node
->next
);
1462 if (caa_likely(!is_removed(next
))
1463 && !is_bucket(next
)) {
1466 node
= clear_flag(next
);
1468 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1473 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1476 * Get next after first bucket node. The first bucket node is the
1477 * first node of the linked list.
1479 iter
->next
= bucket_at(ht
, 0)->next
;
1480 cds_lfht_next(ht
, iter
);
1483 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
1484 struct cds_lfht_node
*node
)
1488 node
->reverse_hash
= bit_reverse_ulong(hash
);
1489 size
= rcu_dereference(ht
->size
);
1490 _cds_lfht_add(ht
, hash
, NULL
, NULL
, size
, node
, NULL
, 0);
1491 ht_count_add(ht
, size
, hash
);
1494 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
1496 cds_lfht_match_fct match
,
1498 struct cds_lfht_node
*node
)
1501 struct cds_lfht_iter iter
;
1503 node
->reverse_hash
= bit_reverse_ulong(hash
);
1504 size
= rcu_dereference(ht
->size
);
1505 _cds_lfht_add(ht
, hash
, match
, key
, size
, node
, &iter
, 0);
1506 if (iter
.node
== node
)
1507 ht_count_add(ht
, size
, hash
);
1511 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
1513 cds_lfht_match_fct match
,
1515 struct cds_lfht_node
*node
)
1518 struct cds_lfht_iter iter
;
1520 node
->reverse_hash
= bit_reverse_ulong(hash
);
1521 size
= rcu_dereference(ht
->size
);
1523 _cds_lfht_add(ht
, hash
, match
, key
, size
, node
, &iter
, 0);
1524 if (iter
.node
== node
) {
1525 ht_count_add(ht
, size
, hash
);
1529 if (!_cds_lfht_replace(ht
, size
, iter
.node
, iter
.next
, node
))
1534 int cds_lfht_replace(struct cds_lfht
*ht
,
1535 struct cds_lfht_iter
*old_iter
,
1537 cds_lfht_match_fct match
,
1539 struct cds_lfht_node
*new_node
)
1543 new_node
->reverse_hash
= bit_reverse_ulong(hash
);
1544 if (!old_iter
->node
)
1546 if (caa_unlikely(old_iter
->node
->reverse_hash
!= new_node
->reverse_hash
))
1548 if (caa_unlikely(!match(old_iter
->node
, key
)))
1550 size
= rcu_dereference(ht
->size
);
1551 return _cds_lfht_replace(ht
, size
, old_iter
->node
, old_iter
->next
,
1555 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_node
*node
)
1557 unsigned long size
, hash
;
1560 size
= rcu_dereference(ht
->size
);
1561 ret
= _cds_lfht_del(ht
, size
, node
);
1563 hash
= bit_reverse_ulong(node
->reverse_hash
);
1564 ht_count_del(ht
, size
, hash
);
1570 int cds_lfht_delete_bucket(struct cds_lfht
*ht
)
1572 struct cds_lfht_node
*node
;
1573 unsigned long order
, i
, size
;
1575 /* Check that the table is empty */
1576 node
= bucket_at(ht
, 0);
1578 node
= clear_flag(node
)->next
;
1579 if (!is_bucket(node
))
1581 assert(!is_removed(node
));
1582 } while (!is_end(node
));
1584 * size accessed without rcu_dereference because hash table is
1588 /* Internal sanity check: all nodes left should be bucket */
1589 for (i
= 0; i
< size
; i
++) {
1590 node
= bucket_at(ht
, i
);
1591 dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n",
1592 i
, i
, bit_reverse_ulong(node
->reverse_hash
));
1593 assert(is_bucket(node
->next
));
1596 for (order
= cds_lfht_get_count_order_ulong(size
); (long)order
>= 0; order
--)
1597 cds_lfht_free_bucket_table(ht
, order
);
1603 * Should only be called when no more concurrent readers nor writers can
1604 * possibly access the table.
1606 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
)
1610 /* Wait for in-flight resize operations to complete */
1611 _CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
1612 cmm_smp_mb(); /* Store destroy before load resize */
1613 while (uatomic_read(&ht
->in_progress_resize
))
1614 poll(NULL
, 0, 100); /* wait for 100ms */
1615 ret
= cds_lfht_delete_bucket(ht
);
1618 free_split_items_count(ht
);
1620 *attr
= ht
->resize_attr
;
1625 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
1626 long *approx_before
,
1627 unsigned long *count
,
1630 struct cds_lfht_node
*node
, *next
;
1631 unsigned long nr_bucket
= 0, nr_removed
= 0;
1634 if (ht
->split_count
) {
1637 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1638 *approx_before
+= uatomic_read(&ht
->split_count
[i
].add
);
1639 *approx_before
-= uatomic_read(&ht
->split_count
[i
].del
);
1645 /* Count non-bucket nodes in the table */
1646 node
= bucket_at(ht
, 0);
1648 next
= rcu_dereference(node
->next
);
1649 if (is_removed(next
)) {
1650 if (!is_bucket(next
))
1654 } else if (!is_bucket(next
))
1658 node
= clear_flag(next
);
1659 } while (!is_end(node
));
1660 dbg_printf("number of logically removed nodes: %lu\n", nr_removed
);
1661 dbg_printf("number of bucket nodes: %lu\n", nr_bucket
);
1663 if (ht
->split_count
) {
1666 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1667 *approx_after
+= uatomic_read(&ht
->split_count
[i
].add
);
1668 *approx_after
-= uatomic_read(&ht
->split_count
[i
].del
);
1673 /* called with resize mutex held */
1675 void _do_cds_lfht_grow(struct cds_lfht
*ht
,
1676 unsigned long old_size
, unsigned long new_size
)
1678 unsigned long old_order
, new_order
;
1680 old_order
= cds_lfht_get_count_order_ulong(old_size
);
1681 new_order
= cds_lfht_get_count_order_ulong(new_size
);
1682 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1683 old_size
, old_order
, new_size
, new_order
);
1684 assert(new_size
> old_size
);
1685 init_table(ht
, old_order
+ 1, new_order
);
1688 /* called with resize mutex held */
1690 void _do_cds_lfht_shrink(struct cds_lfht
*ht
,
1691 unsigned long old_size
, unsigned long new_size
)
1693 unsigned long old_order
, new_order
;
1695 new_size
= max(new_size
, MIN_TABLE_SIZE
);
1696 old_order
= cds_lfht_get_count_order_ulong(old_size
);
1697 new_order
= cds_lfht_get_count_order_ulong(new_size
);
1698 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1699 old_size
, old_order
, new_size
, new_order
);
1700 assert(new_size
< old_size
);
1702 /* Remove and unlink all bucket nodes to remove. */
1703 fini_table(ht
, new_order
+ 1, old_order
);
1707 /* called with resize mutex held */
1709 void _do_cds_lfht_resize(struct cds_lfht
*ht
)
1711 unsigned long new_size
, old_size
;
1714 * Resize table, re-do if the target size has changed under us.
1717 assert(uatomic_read(&ht
->in_progress_resize
));
1718 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1720 ht
->resize_initiated
= 1;
1721 old_size
= ht
->size
;
1722 new_size
= CMM_LOAD_SHARED(ht
->resize_target
);
1723 if (old_size
< new_size
)
1724 _do_cds_lfht_grow(ht
, old_size
, new_size
);
1725 else if (old_size
> new_size
)
1726 _do_cds_lfht_shrink(ht
, old_size
, new_size
);
1727 ht
->resize_initiated
= 0;
1728 /* write resize_initiated before read resize_target */
1730 } while (ht
->size
!= CMM_LOAD_SHARED(ht
->resize_target
));
1734 unsigned long resize_target_grow(struct cds_lfht
*ht
, unsigned long new_size
)
1736 return _uatomic_xchg_monotonic_increase(&ht
->resize_target
, new_size
);
1740 void resize_target_update_count(struct cds_lfht
*ht
,
1741 unsigned long count
)
1743 count
= max(count
, MIN_TABLE_SIZE
);
1744 count
= min(count
, ht
->max_nr_buckets
);
1745 uatomic_set(&ht
->resize_target
, count
);
1748 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
)
1750 resize_target_update_count(ht
, new_size
);
1751 CMM_STORE_SHARED(ht
->resize_initiated
, 1);
1752 ht
->flavor
->thread_offline();
1753 pthread_mutex_lock(&ht
->resize_mutex
);
1754 _do_cds_lfht_resize(ht
);
1755 pthread_mutex_unlock(&ht
->resize_mutex
);
1756 ht
->flavor
->thread_online();
1760 void do_resize_cb(struct rcu_head
*head
)
1762 struct rcu_resize_work
*work
=
1763 caa_container_of(head
, struct rcu_resize_work
, head
);
1764 struct cds_lfht
*ht
= work
->ht
;
1766 ht
->flavor
->thread_offline();
1767 pthread_mutex_lock(&ht
->resize_mutex
);
1768 _do_cds_lfht_resize(ht
);
1769 pthread_mutex_unlock(&ht
->resize_mutex
);
1770 ht
->flavor
->thread_online();
1772 cmm_smp_mb(); /* finish resize before decrement */
1773 uatomic_dec(&ht
->in_progress_resize
);
1777 void __cds_lfht_resize_lazy_launch(struct cds_lfht
*ht
)
1779 struct rcu_resize_work
*work
;
1781 /* Store resize_target before read resize_initiated */
1783 if (!CMM_LOAD_SHARED(ht
->resize_initiated
)) {
1784 uatomic_inc(&ht
->in_progress_resize
);
1785 cmm_smp_mb(); /* increment resize count before load destroy */
1786 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
)) {
1787 uatomic_dec(&ht
->in_progress_resize
);
1790 work
= malloc(sizeof(*work
));
1792 ht
->flavor
->update_call_rcu(&work
->head
, do_resize_cb
);
1793 CMM_STORE_SHARED(ht
->resize_initiated
, 1);
1798 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
)
1800 unsigned long target_size
= size
<< growth
;
1802 target_size
= min(target_size
, ht
->max_nr_buckets
);
1803 if (resize_target_grow(ht
, target_size
) >= target_size
)
1806 __cds_lfht_resize_lazy_launch(ht
);
1810 * We favor grow operations over shrink. A shrink operation never occurs
1811 * if a grow operation is queued for lazy execution. A grow operation
1812 * cancels any pending shrink lazy execution.
1815 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
1816 unsigned long count
)
1818 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
1820 count
= max(count
, MIN_TABLE_SIZE
);
1821 count
= min(count
, ht
->max_nr_buckets
);
1823 return; /* Already the right size, no resize needed */
1824 if (count
> size
) { /* lazy grow */
1825 if (resize_target_grow(ht
, count
) >= count
)
1827 } else { /* lazy shrink */
1831 s
= uatomic_cmpxchg(&ht
->resize_target
, size
, count
);
1833 break; /* no resize needed */
1835 return; /* growing is/(was just) in progress */
1837 return; /* some other thread do shrink */
1841 __cds_lfht_resize_lazy_launch(ht
);