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/arch.h>
164 #include <urcu/uatomic.h>
165 #include <urcu/compiler.h>
166 #include <urcu/rculfhash.h>
171 #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
173 #define dbg_printf(fmt, args...)
177 * Split-counters lazily update the global counter each 1024
178 * addition/removal. It automatically keeps track of resize required.
179 * We use the bucket length as indicator for need to expand for small
180 * tables and machines lacking per-cpu data suppport.
182 #define COUNT_COMMIT_ORDER 10
183 #define DEFAULT_SPLIT_COUNT_MASK 0xFUL
184 #define CHAIN_LEN_TARGET 1
185 #define CHAIN_LEN_RESIZE_THRESHOLD 3
188 * Define the minimum table size.
190 #define MIN_TABLE_SIZE 1
192 #if (CAA_BITS_PER_LONG == 32)
193 #define MAX_TABLE_ORDER 32
195 #define MAX_TABLE_ORDER 64
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 #define min(a, b) ((a) < (b) ? (a) : (b))
209 #define max(a, b) ((a) > (b) ? (a) : (b))
213 * The removed flag needs to be updated atomically with the pointer.
214 * It indicates that no node must attach to the node scheduled for
215 * removal, and that node garbage collection must be performed.
216 * The bucket flag does not require to be updated atomically with the
217 * pointer, but it is added as a pointer low bit flag to save space.
219 #define REMOVED_FLAG (1UL << 0)
220 #define BUCKET_FLAG (1UL << 1)
221 #define FLAGS_MASK ((1UL << 2) - 1)
223 /* Value of the end pointer. Should not interact with flags. */
224 #define END_VALUE NULL
227 * ht_items_count: Split-counters counting the number of node addition
228 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
229 * is set at hash table creation.
231 * These are free-running counters, never reset to zero. They count the
232 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
233 * operations to update the global counter. We choose a power-of-2 value
234 * for the trigger to deal with 32 or 64-bit overflow of the counter.
236 struct ht_items_count
{
237 unsigned long add
, del
;
238 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
241 * rcu_level: Contains the per order-index-level bucket node table. The
242 * size of each bucket node table is half the number of hashes contained
243 * in this order (except for order 0). The minimum allocation size
244 * parameter allows combining the bucket node arrays of the lowermost
245 * levels to improve cache locality for small index orders.
248 /* Note: manually update allocation length when adding a field */
249 struct cds_lfht_node nodes
[0];
253 * rcu_table: Contains the size and desired new size if a resize
254 * operation is in progress, as well as the statically-sized array of
255 * rcu_level pointers.
258 unsigned long size
; /* always a power of 2, shared (RCU) */
259 unsigned long resize_target
;
260 int resize_initiated
;
261 struct rcu_level
*tbl
[MAX_TABLE_ORDER
];
265 * cds_lfht: Top-level data structure representing a lock-free hash
266 * table. Defined in the implementation file to make it be an opaque
271 unsigned long min_alloc_order
;
272 unsigned long min_alloc_size
;
275 * We need to put the work threads offline (QSBR) when taking this
276 * mutex, because we use synchronize_rcu within this mutex critical
277 * section, which waits on read-side critical sections, and could
278 * therefore cause grace-period deadlock if we hold off RCU G.P.
281 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
282 unsigned int in_progress_resize
, in_progress_destroy
;
283 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
284 void (*func
)(struct rcu_head
*head
));
285 void (*cds_lfht_synchronize_rcu
)(void);
286 void (*cds_lfht_rcu_read_lock
)(void);
287 void (*cds_lfht_rcu_read_unlock
)(void);
288 void (*cds_lfht_rcu_thread_offline
)(void);
289 void (*cds_lfht_rcu_thread_online
)(void);
290 void (*cds_lfht_rcu_register_thread
)(void);
291 void (*cds_lfht_rcu_unregister_thread
)(void);
292 pthread_attr_t
*resize_attr
; /* Resize threads attributes */
293 long count
; /* global approximate item count */
294 struct ht_items_count
*split_count
; /* split item count */
298 * rcu_resize_work: Contains arguments passed to RCU worker thread
299 * responsible for performing lazy resize.
301 struct rcu_resize_work
{
302 struct rcu_head head
;
307 * partition_resize_work: Contains arguments passed to worker threads
308 * executing the hash table resize on partitions of the hash table
309 * assigned to each processor's worker thread.
311 struct partition_resize_work
{
314 unsigned long i
, start
, len
;
315 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
316 unsigned long start
, unsigned long len
);
320 void _cds_lfht_add(struct cds_lfht
*ht
,
321 cds_lfht_match_fct match
,
324 struct cds_lfht_node
*node
,
325 struct cds_lfht_iter
*unique_ret
,
329 * Algorithm to reverse bits in a word by lookup table, extended to
332 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
333 * Originally from Public Domain.
336 static const uint8_t BitReverseTable256
[256] =
338 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
339 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
340 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
341 R6(0), R6(2), R6(1), R6(3)
348 uint8_t bit_reverse_u8(uint8_t v
)
350 return BitReverseTable256
[v
];
353 static __attribute__((unused
))
354 uint32_t bit_reverse_u32(uint32_t v
)
356 return ((uint32_t) bit_reverse_u8(v
) << 24) |
357 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
358 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
359 ((uint32_t) bit_reverse_u8(v
>> 24));
362 static __attribute__((unused
))
363 uint64_t bit_reverse_u64(uint64_t v
)
365 return ((uint64_t) bit_reverse_u8(v
) << 56) |
366 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
367 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
368 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
369 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
370 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
371 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
372 ((uint64_t) bit_reverse_u8(v
>> 56));
376 unsigned long bit_reverse_ulong(unsigned long v
)
378 #if (CAA_BITS_PER_LONG == 32)
379 return bit_reverse_u32(v
);
381 return bit_reverse_u64(v
);
386 * fls: returns the position of the most significant bit.
387 * Returns 0 if no bit is set, else returns the position of the most
388 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
390 #if defined(__i386) || defined(__x86_64)
392 unsigned int fls_u32(uint32_t x
)
400 : "=r" (r
) : "rm" (x
));
406 #if defined(__x86_64)
408 unsigned int fls_u64(uint64_t x
)
416 : "=r" (r
) : "rm" (x
));
423 static __attribute__((unused
))
424 unsigned int fls_u64(uint64_t x
)
431 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
435 if (!(x
& 0xFFFF000000000000ULL
)) {
439 if (!(x
& 0xFF00000000000000ULL
)) {
443 if (!(x
& 0xF000000000000000ULL
)) {
447 if (!(x
& 0xC000000000000000ULL
)) {
451 if (!(x
& 0x8000000000000000ULL
)) {
460 static __attribute__((unused
))
461 unsigned int fls_u32(uint32_t x
)
467 if (!(x
& 0xFFFF0000U
)) {
471 if (!(x
& 0xFF000000U
)) {
475 if (!(x
& 0xF0000000U
)) {
479 if (!(x
& 0xC0000000U
)) {
483 if (!(x
& 0x80000000U
)) {
491 unsigned int fls_ulong(unsigned long x
)
493 #if (CAA_BITS_PER_LONG == 32)
501 * Return the minimum order for which x <= (1UL << order).
502 * Return -1 if x is 0.
504 int get_count_order_u32(uint32_t x
)
509 return fls_u32(x
- 1);
513 * Return the minimum order for which x <= (1UL << order).
514 * Return -1 if x is 0.
516 int get_count_order_ulong(unsigned long x
)
521 return fls_ulong(x
- 1);
525 #define poison_free(ptr) \
528 memset(ptr, 0x42, sizeof(*(ptr))); \
533 #define poison_free(ptr) free(ptr)
537 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
);
540 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
541 unsigned long count
);
543 static long nr_cpus_mask
= -1;
544 static long split_count_mask
= -1;
546 #if defined(HAVE_SYSCONF)
547 static void ht_init_nr_cpus_mask(void)
551 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
557 * round up number of CPUs to next power of two, so we
558 * can use & for modulo.
560 maxcpus
= 1UL << get_count_order_ulong(maxcpus
);
561 nr_cpus_mask
= maxcpus
- 1;
563 #else /* #if defined(HAVE_SYSCONF) */
564 static void ht_init_nr_cpus_mask(void)
568 #endif /* #else #if defined(HAVE_SYSCONF) */
571 void alloc_split_items_count(struct cds_lfht
*ht
)
573 struct ht_items_count
*count
;
575 if (nr_cpus_mask
== -1) {
576 ht_init_nr_cpus_mask();
577 if (nr_cpus_mask
< 0)
578 split_count_mask
= DEFAULT_SPLIT_COUNT_MASK
;
580 split_count_mask
= nr_cpus_mask
;
583 assert(split_count_mask
>= 0);
585 if (ht
->flags
& CDS_LFHT_ACCOUNTING
) {
586 ht
->split_count
= calloc(split_count_mask
+ 1, sizeof(*count
));
587 assert(ht
->split_count
);
589 ht
->split_count
= NULL
;
594 void free_split_items_count(struct cds_lfht
*ht
)
596 poison_free(ht
->split_count
);
599 #if defined(HAVE_SCHED_GETCPU)
601 int ht_get_split_count_index(unsigned long hash
)
605 assert(split_count_mask
>= 0);
606 cpu
= sched_getcpu();
607 if (caa_unlikely(cpu
< 0))
608 return hash
& split_count_mask
;
610 return cpu
& split_count_mask
;
612 #else /* #if defined(HAVE_SCHED_GETCPU) */
614 int ht_get_split_count_index(unsigned long hash
)
616 return hash
& split_count_mask
;
618 #endif /* #else #if defined(HAVE_SCHED_GETCPU) */
621 void ht_count_add(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
623 unsigned long split_count
;
626 if (caa_unlikely(!ht
->split_count
))
628 index
= ht_get_split_count_index(hash
);
629 split_count
= uatomic_add_return(&ht
->split_count
[index
].add
, 1);
630 if (caa_unlikely(!(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
633 dbg_printf("add split count %lu\n", split_count
);
634 count
= uatomic_add_return(&ht
->count
,
635 1UL << COUNT_COMMIT_ORDER
);
637 if (!(count
& (count
- 1))) {
638 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) < size
)
640 dbg_printf("add set global %ld\n", count
);
641 cds_lfht_resize_lazy_count(ht
, size
,
642 count
>> (CHAIN_LEN_TARGET
- 1));
648 void ht_count_del(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
650 unsigned long split_count
;
653 if (caa_unlikely(!ht
->split_count
))
655 index
= ht_get_split_count_index(hash
);
656 split_count
= uatomic_add_return(&ht
->split_count
[index
].del
, 1);
657 if (caa_unlikely(!(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
660 dbg_printf("del split count %lu\n", split_count
);
661 count
= uatomic_add_return(&ht
->count
,
662 -(1UL << COUNT_COMMIT_ORDER
));
664 if (!(count
& (count
- 1))) {
665 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) >= size
)
667 dbg_printf("del set global %ld\n", count
);
669 * Don't shrink table if the number of nodes is below a
672 if (count
< (1UL << COUNT_COMMIT_ORDER
) * (split_count_mask
+ 1))
674 cds_lfht_resize_lazy_count(ht
, size
,
675 count
>> (CHAIN_LEN_TARGET
- 1));
681 void check_resize(struct cds_lfht
*ht
, unsigned long size
, uint32_t chain_len
)
685 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
687 count
= uatomic_read(&ht
->count
);
689 * Use bucket-local length for small table expand and for
690 * environments lacking per-cpu data support.
692 if (count
>= (1UL << COUNT_COMMIT_ORDER
))
695 dbg_printf("WARNING: large chain length: %u.\n",
697 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
698 cds_lfht_resize_lazy_grow(ht
, size
,
699 get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
703 struct cds_lfht_node
*clear_flag(struct cds_lfht_node
*node
)
705 return (struct cds_lfht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
709 int is_removed(struct cds_lfht_node
*node
)
711 return ((unsigned long) node
) & REMOVED_FLAG
;
715 struct cds_lfht_node
*flag_removed(struct cds_lfht_node
*node
)
717 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
721 int is_bucket(struct cds_lfht_node
*node
)
723 return ((unsigned long) node
) & BUCKET_FLAG
;
727 struct cds_lfht_node
*flag_bucket(struct cds_lfht_node
*node
)
729 return (struct cds_lfht_node
*) (((unsigned long) node
) | BUCKET_FLAG
);
733 struct cds_lfht_node
*get_end(void)
735 return (struct cds_lfht_node
*) END_VALUE
;
739 int is_end(struct cds_lfht_node
*node
)
741 return clear_flag(node
) == (struct cds_lfht_node
*) END_VALUE
;
745 unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr
,
748 unsigned long old1
, old2
;
750 old1
= uatomic_read(ptr
);
755 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
760 struct cds_lfht_node
*bucket_at(struct cds_lfht
*ht
, unsigned long index
)
764 if ((__builtin_constant_p(index
) && index
== 0)
765 || index
< ht
->min_alloc_size
) {
766 dbg_printf("bucket index %lu order 0 aridx 0\n", index
);
767 return &ht
->t
.tbl
[0]->nodes
[index
];
770 * equivalent to get_count_order_ulong(index + 1), but optimizes
771 * away the non-existing 0 special-case for
772 * get_count_order_ulong.
774 order
= fls_ulong(index
);
775 dbg_printf("bucket index %lu order %lu aridx %lu\n",
776 index
, order
, index
& ((1UL << (order
- 1)) - 1));
777 return &ht
->t
.tbl
[order
]->nodes
[index
& ((1UL << (order
- 1)) - 1)];
781 struct cds_lfht_node
*lookup_bucket(struct cds_lfht
*ht
, unsigned long size
,
785 return bucket_at(ht
, hash
& (size
- 1));
789 * Remove all logically deleted nodes from a bucket up to a certain node key.
792 void _cds_lfht_gc_bucket(struct cds_lfht_node
*bucket
, struct cds_lfht_node
*node
)
794 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_next
;
796 assert(!is_bucket(bucket
));
797 assert(!is_removed(bucket
));
798 assert(!is_bucket(node
));
799 assert(!is_removed(node
));
802 /* We can always skip the bucket node initially */
803 iter
= rcu_dereference(iter_prev
->next
);
804 assert(!is_removed(iter
));
805 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
807 * We should never be called with bucket (start of chain)
808 * and logically removed node (end of path compression
809 * marker) being the actual same node. This would be a
810 * bug in the algorithm implementation.
812 assert(bucket
!= node
);
814 if (caa_unlikely(is_end(iter
)))
816 if (caa_likely(clear_flag(iter
)->reverse_hash
> node
->reverse_hash
))
818 next
= rcu_dereference(clear_flag(iter
)->next
);
819 if (caa_likely(is_removed(next
)))
821 iter_prev
= clear_flag(iter
);
824 assert(!is_removed(iter
));
826 new_next
= flag_bucket(clear_flag(next
));
828 new_next
= clear_flag(next
);
829 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, new_next
);
835 int _cds_lfht_replace(struct cds_lfht
*ht
, unsigned long size
,
836 struct cds_lfht_node
*old_node
,
837 struct cds_lfht_node
*old_next
,
838 struct cds_lfht_node
*new_node
)
840 struct cds_lfht_node
*bucket
, *ret_next
;
842 if (!old_node
) /* Return -ENOENT if asked to replace NULL node */
845 assert(!is_removed(old_node
));
846 assert(!is_bucket(old_node
));
847 assert(!is_removed(new_node
));
848 assert(!is_bucket(new_node
));
849 assert(new_node
!= old_node
);
851 /* Insert after node to be replaced */
852 if (is_removed(old_next
)) {
854 * Too late, the old node has been removed under us
855 * between lookup and replace. Fail.
859 assert(!is_bucket(old_next
));
860 assert(new_node
!= clear_flag(old_next
));
861 new_node
->next
= clear_flag(old_next
);
863 * Here is the whole trick for lock-free replace: we add
864 * the replacement node _after_ the node we want to
865 * replace by atomically setting its next pointer at the
866 * same time we set its removal flag. Given that
867 * the lookups/get next use an iterator aware of the
868 * next pointer, they will either skip the old node due
869 * to the removal flag and see the new node, or use
870 * the old node, but will not see the new one.
872 ret_next
= uatomic_cmpxchg(&old_node
->next
,
873 old_next
, flag_removed(new_node
));
874 if (ret_next
== old_next
)
875 break; /* We performed the replacement. */
880 * Ensure that the old node is not visible to readers anymore:
881 * lookup for the node, and remove it (along with any other
882 * logically removed node) if found.
884 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(old_node
->reverse_hash
));
885 _cds_lfht_gc_bucket(bucket
, new_node
);
887 assert(is_removed(rcu_dereference(old_node
->next
)));
892 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
893 * mode. A NULL unique_ret allows creation of duplicate keys.
896 void _cds_lfht_add(struct cds_lfht
*ht
,
897 cds_lfht_match_fct match
,
900 struct cds_lfht_node
*node
,
901 struct cds_lfht_iter
*unique_ret
,
904 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
906 struct cds_lfht_node
*bucket
;
908 assert(!is_bucket(node
));
909 assert(!is_removed(node
));
910 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->reverse_hash
));
912 uint32_t chain_len
= 0;
915 * iter_prev points to the non-removed node prior to the
919 /* We can always skip the bucket node initially */
920 iter
= rcu_dereference(iter_prev
->next
);
921 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
923 if (caa_unlikely(is_end(iter
)))
925 if (caa_likely(clear_flag(iter
)->reverse_hash
> node
->reverse_hash
))
928 /* bucket node is the first node of the identical-hash-value chain */
929 if (bucket_flag
&& clear_flag(iter
)->reverse_hash
== node
->reverse_hash
)
932 next
= rcu_dereference(clear_flag(iter
)->next
);
933 if (caa_unlikely(is_removed(next
)))
939 && clear_flag(iter
)->reverse_hash
== node
->reverse_hash
) {
940 struct cds_lfht_iter d_iter
= { .node
= node
, .next
= iter
, };
943 * uniquely adding inserts the node as the first
944 * node of the identical-hash-value node chain.
946 * This semantic ensures no duplicated keys
947 * should ever be observable in the table
948 * (including observe one node by one node
949 * by forward iterations)
951 cds_lfht_next_duplicate(ht
, match
, key
, &d_iter
);
955 *unique_ret
= d_iter
;
959 /* Only account for identical reverse hash once */
960 if (iter_prev
->reverse_hash
!= clear_flag(iter
)->reverse_hash
962 check_resize(ht
, size
, ++chain_len
);
963 iter_prev
= clear_flag(iter
);
968 assert(node
!= clear_flag(iter
));
969 assert(!is_removed(iter_prev
));
970 assert(!is_removed(iter
));
971 assert(iter_prev
!= node
);
973 node
->next
= clear_flag(iter
);
975 node
->next
= flag_bucket(clear_flag(iter
));
977 new_node
= flag_bucket(node
);
980 if (uatomic_cmpxchg(&iter_prev
->next
, iter
,
982 continue; /* retry */
989 assert(!is_removed(iter
));
991 new_next
= flag_bucket(clear_flag(next
));
993 new_next
= clear_flag(next
);
994 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, new_next
);
999 unique_ret
->node
= return_node
;
1000 /* unique_ret->next left unset, never used. */
1005 int _cds_lfht_del(struct cds_lfht
*ht
, unsigned long size
,
1006 struct cds_lfht_node
*node
,
1009 struct cds_lfht_node
*bucket
, *next
, *old
;
1011 if (!node
) /* Return -ENOENT if asked to delete NULL node */
1014 /* logically delete the node */
1015 assert(!is_bucket(node
));
1016 assert(!is_removed(node
));
1017 old
= rcu_dereference(node
->next
);
1019 struct cds_lfht_node
*new_next
;
1022 if (caa_unlikely(is_removed(next
)))
1025 assert(is_bucket(next
));
1027 assert(!is_bucket(next
));
1028 new_next
= flag_removed(next
);
1029 old
= uatomic_cmpxchg(&node
->next
, next
, new_next
);
1030 } while (old
!= next
);
1031 /* We performed the (logical) deletion. */
1034 * Ensure that the node is not visible to readers anymore: lookup for
1035 * the node, and remove it (along with any other logically removed node)
1038 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->reverse_hash
));
1039 _cds_lfht_gc_bucket(bucket
, node
);
1041 assert(is_removed(rcu_dereference(node
->next
)));
1046 void *partition_resize_thread(void *arg
)
1048 struct partition_resize_work
*work
= arg
;
1050 work
->ht
->cds_lfht_rcu_register_thread();
1051 work
->fct(work
->ht
, work
->i
, work
->start
, work
->len
);
1052 work
->ht
->cds_lfht_rcu_unregister_thread();
1057 void partition_resize_helper(struct cds_lfht
*ht
, unsigned long i
,
1059 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
1060 unsigned long start
, unsigned long len
))
1062 unsigned long partition_len
;
1063 struct partition_resize_work
*work
;
1065 unsigned long nr_threads
;
1068 * Note: nr_cpus_mask + 1 is always power of 2.
1069 * We spawn just the number of threads we need to satisfy the minimum
1070 * partition size, up to the number of CPUs in the system.
1072 if (nr_cpus_mask
> 0) {
1073 nr_threads
= min(nr_cpus_mask
+ 1,
1074 len
>> MIN_PARTITION_PER_THREAD_ORDER
);
1078 partition_len
= len
>> get_count_order_ulong(nr_threads
);
1079 work
= calloc(nr_threads
, sizeof(*work
));
1081 for (thread
= 0; thread
< nr_threads
; thread
++) {
1082 work
[thread
].ht
= ht
;
1084 work
[thread
].len
= partition_len
;
1085 work
[thread
].start
= thread
* partition_len
;
1086 work
[thread
].fct
= fct
;
1087 ret
= pthread_create(&(work
[thread
].thread_id
), ht
->resize_attr
,
1088 partition_resize_thread
, &work
[thread
]);
1091 for (thread
= 0; thread
< nr_threads
; thread
++) {
1092 ret
= pthread_join(work
[thread
].thread_id
, NULL
);
1099 * Holding RCU read lock to protect _cds_lfht_add against memory
1100 * reclaim that could be performed by other call_rcu worker threads (ABA
1103 * When we reach a certain length, we can split this population phase over
1104 * many worker threads, based on the number of CPUs available in the system.
1105 * This should therefore take care of not having the expand lagging behind too
1106 * many concurrent insertion threads by using the scheduler's ability to
1107 * schedule bucket node population fairly with insertions.
1110 void init_table_populate_partition(struct cds_lfht
*ht
, unsigned long i
,
1111 unsigned long start
, unsigned long len
)
1113 unsigned long j
, size
= 1UL << (i
- 1);
1115 assert(i
> ht
->min_alloc_order
);
1116 ht
->cds_lfht_rcu_read_lock();
1117 for (j
= size
+ start
; j
< size
+ start
+ len
; j
++) {
1118 struct cds_lfht_node
*new_node
= bucket_at(ht
, j
);
1120 assert(j
>= size
&& j
< (size
<< 1));
1121 dbg_printf("init populate: order %lu index %lu hash %lu\n",
1123 new_node
->reverse_hash
= bit_reverse_ulong(j
);
1124 _cds_lfht_add(ht
, NULL
, NULL
, size
, new_node
, NULL
, 1);
1126 ht
->cds_lfht_rcu_read_unlock();
1130 void init_table_populate(struct cds_lfht
*ht
, unsigned long i
,
1133 assert(nr_cpus_mask
!= -1);
1134 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1135 ht
->cds_lfht_rcu_thread_online();
1136 init_table_populate_partition(ht
, i
, 0, len
);
1137 ht
->cds_lfht_rcu_thread_offline();
1140 partition_resize_helper(ht
, i
, len
, init_table_populate_partition
);
1144 void init_table(struct cds_lfht
*ht
,
1145 unsigned long first_order
, unsigned long last_order
)
1149 dbg_printf("init table: first_order %lu last_order %lu\n",
1150 first_order
, last_order
);
1151 assert(first_order
> ht
->min_alloc_order
);
1152 for (i
= first_order
; i
<= last_order
; i
++) {
1155 len
= 1UL << (i
- 1);
1156 dbg_printf("init order %lu len: %lu\n", i
, len
);
1158 /* Stop expand if the resize target changes under us */
1159 if (CMM_LOAD_SHARED(ht
->t
.resize_target
) < (1UL << i
))
1162 ht
->t
.tbl
[i
] = calloc(1, len
* sizeof(struct cds_lfht_node
));
1163 assert(ht
->t
.tbl
[i
]);
1166 * Set all bucket nodes reverse hash values for a level and
1167 * link all bucket nodes into the table.
1169 init_table_populate(ht
, i
, len
);
1172 * Update table size.
1174 cmm_smp_wmb(); /* populate data before RCU size */
1175 CMM_STORE_SHARED(ht
->t
.size
, 1UL << i
);
1177 dbg_printf("init new size: %lu\n", 1UL << i
);
1178 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1184 * Holding RCU read lock to protect _cds_lfht_remove against memory
1185 * reclaim that could be performed by other call_rcu worker threads (ABA
1187 * For a single level, we logically remove and garbage collect each node.
1189 * As a design choice, we perform logical removal and garbage collection on a
1190 * node-per-node basis to simplify this algorithm. We also assume keeping good
1191 * cache locality of the operation would overweight possible performance gain
1192 * that could be achieved by batching garbage collection for multiple levels.
1193 * However, this would have to be justified by benchmarks.
1195 * Concurrent removal and add operations are helping us perform garbage
1196 * collection of logically removed nodes. We guarantee that all logically
1197 * removed nodes have been garbage-collected (unlinked) before call_rcu is
1198 * invoked to free a hole level of bucket nodes (after a grace period).
1200 * Logical removal and garbage collection can therefore be done in batch or on a
1201 * node-per-node basis, as long as the guarantee above holds.
1203 * When we reach a certain length, we can split this removal over many worker
1204 * threads, based on the number of CPUs available in the system. This should
1205 * take care of not letting resize process lag behind too many concurrent
1206 * updater threads actively inserting into the hash table.
1209 void remove_table_partition(struct cds_lfht
*ht
, unsigned long i
,
1210 unsigned long start
, unsigned long len
)
1212 unsigned long j
, size
= 1UL << (i
- 1);
1214 assert(i
> ht
->min_alloc_order
);
1215 ht
->cds_lfht_rcu_read_lock();
1216 for (j
= size
+ start
; j
< size
+ start
+ len
; j
++) {
1217 struct cds_lfht_node
*fini_node
= bucket_at(ht
, j
);
1219 assert(j
>= size
&& j
< (size
<< 1));
1220 dbg_printf("remove entry: order %lu index %lu hash %lu\n",
1222 fini_node
->reverse_hash
= bit_reverse_ulong(j
);
1223 (void) _cds_lfht_del(ht
, size
, fini_node
, 1);
1225 ht
->cds_lfht_rcu_read_unlock();
1229 void remove_table(struct cds_lfht
*ht
, unsigned long i
, unsigned long len
)
1232 assert(nr_cpus_mask
!= -1);
1233 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1234 ht
->cds_lfht_rcu_thread_online();
1235 remove_table_partition(ht
, i
, 0, len
);
1236 ht
->cds_lfht_rcu_thread_offline();
1239 partition_resize_helper(ht
, i
, len
, remove_table_partition
);
1243 void fini_table(struct cds_lfht
*ht
,
1244 unsigned long first_order
, unsigned long last_order
)
1247 void *free_by_rcu
= NULL
;
1249 dbg_printf("fini table: first_order %lu last_order %lu\n",
1250 first_order
, last_order
);
1251 assert(first_order
> ht
->min_alloc_order
);
1252 for (i
= last_order
; i
>= first_order
; i
--) {
1255 len
= 1UL << (i
- 1);
1256 dbg_printf("fini order %lu len: %lu\n", i
, len
);
1258 /* Stop shrink if the resize target changes under us */
1259 if (CMM_LOAD_SHARED(ht
->t
.resize_target
) > (1UL << (i
- 1)))
1262 cmm_smp_wmb(); /* populate data before RCU size */
1263 CMM_STORE_SHARED(ht
->t
.size
, 1UL << (i
- 1));
1266 * We need to wait for all add operations to reach Q.S. (and
1267 * thus use the new table for lookups) before we can start
1268 * releasing the old bucket nodes. Otherwise their lookup will
1269 * return a logically removed node as insert position.
1271 ht
->cds_lfht_synchronize_rcu();
1276 * Set "removed" flag in bucket nodes about to be removed.
1277 * Unlink all now-logically-removed bucket node pointers.
1278 * Concurrent add/remove operation are helping us doing
1281 remove_table(ht
, i
, len
);
1283 free_by_rcu
= ht
->t
.tbl
[i
];
1285 dbg_printf("fini new size: %lu\n", 1UL << i
);
1286 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1291 ht
->cds_lfht_synchronize_rcu();
1297 void cds_lfht_create_bucket(struct cds_lfht
*ht
, unsigned long size
)
1299 struct cds_lfht_node
*prev
, *node
;
1300 unsigned long order
, len
, i
;
1302 ht
->t
.tbl
[0] = calloc(1, ht
->min_alloc_size
* sizeof(struct cds_lfht_node
));
1303 assert(ht
->t
.tbl
[0]);
1305 dbg_printf("create bucket: order 0 index 0 hash 0\n");
1306 node
= bucket_at(ht
, 0);
1307 node
->next
= flag_bucket(get_end());
1308 node
->reverse_hash
= 0;
1310 for (order
= 1; order
< get_count_order_ulong(size
) + 1; order
++) {
1311 len
= 1UL << (order
- 1);
1312 if (order
<= ht
->min_alloc_order
) {
1313 ht
->t
.tbl
[order
] = (struct rcu_level
*) (ht
->t
.tbl
[0]->nodes
+ len
);
1315 ht
->t
.tbl
[order
] = calloc(1, len
* sizeof(struct cds_lfht_node
));
1316 assert(ht
->t
.tbl
[order
]);
1319 for (i
= 0; i
< len
; i
++) {
1321 * Now, we are trying to init the node with the
1322 * hash=(len+i) (which is also a bucket with the
1323 * index=(len+i)) and insert it into the hash table,
1324 * so this node has to be inserted after the bucket
1325 * with the index=(len+i)&(len-1)=i. And because there
1326 * is no other non-bucket node nor bucket node with
1327 * larger index/hash inserted, so the bucket node
1328 * being inserted should be inserted directly linked
1329 * after the bucket node with index=i.
1331 prev
= bucket_at(ht
, i
);
1332 node
= bucket_at(ht
, len
+ i
);
1334 dbg_printf("create bucket: order %lu index %lu hash %lu\n",
1335 order
, len
+ i
, len
+ i
);
1336 node
->reverse_hash
= bit_reverse_ulong(len
+ i
);
1338 /* insert after prev */
1339 assert(is_bucket(prev
->next
));
1340 node
->next
= prev
->next
;
1341 prev
->next
= flag_bucket(node
);
1346 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
1347 unsigned long min_alloc_size
,
1349 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
1350 void (*func
)(struct rcu_head
*head
)),
1351 void (*cds_lfht_synchronize_rcu
)(void),
1352 void (*cds_lfht_rcu_read_lock
)(void),
1353 void (*cds_lfht_rcu_read_unlock
)(void),
1354 void (*cds_lfht_rcu_thread_offline
)(void),
1355 void (*cds_lfht_rcu_thread_online
)(void),
1356 void (*cds_lfht_rcu_register_thread
)(void),
1357 void (*cds_lfht_rcu_unregister_thread
)(void),
1358 pthread_attr_t
*attr
)
1360 struct cds_lfht
*ht
;
1361 unsigned long order
;
1363 /* min_alloc_size must be power of two */
1364 if (!min_alloc_size
|| (min_alloc_size
& (min_alloc_size
- 1)))
1366 /* init_size must be power of two */
1367 if (!init_size
|| (init_size
& (init_size
- 1)))
1369 min_alloc_size
= max(min_alloc_size
, MIN_TABLE_SIZE
);
1370 init_size
= max(init_size
, min_alloc_size
);
1371 ht
= calloc(1, sizeof(struct cds_lfht
));
1374 ht
->cds_lfht_call_rcu
= cds_lfht_call_rcu
;
1375 ht
->cds_lfht_synchronize_rcu
= cds_lfht_synchronize_rcu
;
1376 ht
->cds_lfht_rcu_read_lock
= cds_lfht_rcu_read_lock
;
1377 ht
->cds_lfht_rcu_read_unlock
= cds_lfht_rcu_read_unlock
;
1378 ht
->cds_lfht_rcu_thread_offline
= cds_lfht_rcu_thread_offline
;
1379 ht
->cds_lfht_rcu_thread_online
= cds_lfht_rcu_thread_online
;
1380 ht
->cds_lfht_rcu_register_thread
= cds_lfht_rcu_register_thread
;
1381 ht
->cds_lfht_rcu_unregister_thread
= cds_lfht_rcu_unregister_thread
;
1382 ht
->resize_attr
= attr
;
1383 alloc_split_items_count(ht
);
1384 /* this mutex should not nest in read-side C.S. */
1385 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
1386 order
= get_count_order_ulong(init_size
);
1387 ht
->t
.resize_target
= 1UL << order
;
1388 ht
->min_alloc_size
= min_alloc_size
;
1389 ht
->min_alloc_order
= get_count_order_ulong(min_alloc_size
);
1390 cds_lfht_create_bucket(ht
, 1UL << order
);
1391 ht
->t
.size
= 1UL << order
;
1395 void cds_lfht_lookup(struct cds_lfht
*ht
, unsigned long hash
,
1396 cds_lfht_match_fct match
, const void *key
,
1397 struct cds_lfht_iter
*iter
)
1399 struct cds_lfht_node
*node
, *next
, *bucket
;
1400 unsigned long reverse_hash
, size
;
1402 reverse_hash
= bit_reverse_ulong(hash
);
1404 size
= rcu_dereference(ht
->t
.size
);
1405 bucket
= lookup_bucket(ht
, size
, hash
);
1406 /* We can always skip the bucket node initially */
1407 node
= rcu_dereference(bucket
->next
);
1408 node
= clear_flag(node
);
1410 if (caa_unlikely(is_end(node
))) {
1414 if (caa_unlikely(node
->reverse_hash
> reverse_hash
)) {
1418 next
= rcu_dereference(node
->next
);
1419 assert(node
== clear_flag(node
));
1420 if (caa_likely(!is_removed(next
))
1422 && node
->reverse_hash
== reverse_hash
1423 && caa_likely(match(node
, key
))) {
1426 node
= clear_flag(next
);
1428 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1433 void cds_lfht_next_duplicate(struct cds_lfht
*ht
, cds_lfht_match_fct match
,
1434 const void *key
, struct cds_lfht_iter
*iter
)
1436 struct cds_lfht_node
*node
, *next
;
1437 unsigned long reverse_hash
;
1440 reverse_hash
= node
->reverse_hash
;
1442 node
= clear_flag(next
);
1445 if (caa_unlikely(is_end(node
))) {
1449 if (caa_unlikely(node
->reverse_hash
> reverse_hash
)) {
1453 next
= rcu_dereference(node
->next
);
1454 if (caa_likely(!is_removed(next
))
1456 && caa_likely(match(node
, key
))) {
1459 node
= clear_flag(next
);
1461 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1466 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1468 struct cds_lfht_node
*node
, *next
;
1470 node
= clear_flag(iter
->next
);
1472 if (caa_unlikely(is_end(node
))) {
1476 next
= rcu_dereference(node
->next
);
1477 if (caa_likely(!is_removed(next
))
1478 && !is_bucket(next
)) {
1481 node
= clear_flag(next
);
1483 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1488 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1491 * Get next after first bucket node. The first bucket node is the
1492 * first node of the linked list.
1494 iter
->next
= bucket_at(ht
, 0)->next
;
1495 cds_lfht_next(ht
, iter
);
1498 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
1499 struct cds_lfht_node
*node
)
1503 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1504 size
= rcu_dereference(ht
->t
.size
);
1505 _cds_lfht_add(ht
, NULL
, NULL
, size
, node
, NULL
, 0);
1506 ht_count_add(ht
, size
, hash
);
1509 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
1511 cds_lfht_match_fct match
,
1513 struct cds_lfht_node
*node
)
1516 struct cds_lfht_iter iter
;
1518 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1519 size
= rcu_dereference(ht
->t
.size
);
1520 _cds_lfht_add(ht
, match
, key
, size
, node
, &iter
, 0);
1521 if (iter
.node
== node
)
1522 ht_count_add(ht
, size
, hash
);
1526 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
1528 cds_lfht_match_fct match
,
1530 struct cds_lfht_node
*node
)
1533 struct cds_lfht_iter iter
;
1535 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1536 size
= rcu_dereference(ht
->t
.size
);
1538 _cds_lfht_add(ht
, match
, key
, size
, node
, &iter
, 0);
1539 if (iter
.node
== node
) {
1540 ht_count_add(ht
, size
, hash
);
1544 if (!_cds_lfht_replace(ht
, size
, iter
.node
, iter
.next
, node
))
1549 int cds_lfht_replace(struct cds_lfht
*ht
, struct cds_lfht_iter
*old_iter
,
1550 struct cds_lfht_node
*new_node
)
1554 size
= rcu_dereference(ht
->t
.size
);
1555 return _cds_lfht_replace(ht
, size
, old_iter
->node
, old_iter
->next
,
1559 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1561 unsigned long size
, hash
;
1564 size
= rcu_dereference(ht
->t
.size
);
1565 ret
= _cds_lfht_del(ht
, size
, iter
->node
, 0);
1567 hash
= bit_reverse_ulong(iter
->node
->reverse_hash
);
1568 ht_count_del(ht
, size
, hash
);
1574 int cds_lfht_delete_bucket(struct cds_lfht
*ht
)
1576 struct cds_lfht_node
*node
;
1577 unsigned long order
, i
, size
;
1579 /* Check that the table is empty */
1580 node
= bucket_at(ht
, 0);
1582 node
= clear_flag(node
)->next
;
1583 if (!is_bucket(node
))
1585 assert(!is_removed(node
));
1586 } while (!is_end(node
));
1588 * size accessed without rcu_dereference because hash table is
1592 /* Internal sanity check: all nodes left should be bucket */
1593 for (order
= 0; order
< get_count_order_ulong(size
) + 1; order
++) {
1596 len
= !order
? 1 : 1UL << (order
- 1);
1597 for (i
= 0; i
< len
; i
++) {
1598 dbg_printf("delete order %lu i %lu hash %lu\n",
1600 bit_reverse_ulong(ht
->t
.tbl
[order
]->nodes
[i
].reverse_hash
));
1601 assert(is_bucket(ht
->t
.tbl
[order
]->nodes
[i
].next
));
1604 if (order
== ht
->min_alloc_order
)
1605 poison_free(ht
->t
.tbl
[0]);
1606 else if (order
> ht
->min_alloc_order
)
1607 poison_free(ht
->t
.tbl
[order
]);
1608 /* Nothing to delete for order < ht->min_alloc_order */
1614 * Should only be called when no more concurrent readers nor writers can
1615 * possibly access the table.
1617 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
)
1621 /* Wait for in-flight resize operations to complete */
1622 _CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
1623 cmm_smp_mb(); /* Store destroy before load resize */
1624 while (uatomic_read(&ht
->in_progress_resize
))
1625 poll(NULL
, 0, 100); /* wait for 100ms */
1626 ret
= cds_lfht_delete_bucket(ht
);
1629 free_split_items_count(ht
);
1631 *attr
= ht
->resize_attr
;
1636 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
1637 long *approx_before
,
1638 unsigned long *count
,
1639 unsigned long *removed
,
1642 struct cds_lfht_node
*node
, *next
;
1643 unsigned long nr_bucket
= 0;
1646 if (ht
->split_count
) {
1649 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1650 *approx_before
+= uatomic_read(&ht
->split_count
[i
].add
);
1651 *approx_before
-= uatomic_read(&ht
->split_count
[i
].del
);
1658 /* Count non-bucket nodes in the table */
1659 node
= bucket_at(ht
, 0);
1661 next
= rcu_dereference(node
->next
);
1662 if (is_removed(next
)) {
1663 if (!is_bucket(next
))
1667 } else if (!is_bucket(next
))
1671 node
= clear_flag(next
);
1672 } while (!is_end(node
));
1673 dbg_printf("number of bucket nodes: %lu\n", nr_bucket
);
1675 if (ht
->split_count
) {
1678 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1679 *approx_after
+= uatomic_read(&ht
->split_count
[i
].add
);
1680 *approx_after
-= uatomic_read(&ht
->split_count
[i
].del
);
1685 /* called with resize mutex held */
1687 void _do_cds_lfht_grow(struct cds_lfht
*ht
,
1688 unsigned long old_size
, unsigned long new_size
)
1690 unsigned long old_order
, new_order
;
1692 old_order
= get_count_order_ulong(old_size
);
1693 new_order
= get_count_order_ulong(new_size
);
1694 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1695 old_size
, old_order
, new_size
, new_order
);
1696 assert(new_size
> old_size
);
1697 init_table(ht
, old_order
+ 1, new_order
);
1700 /* called with resize mutex held */
1702 void _do_cds_lfht_shrink(struct cds_lfht
*ht
,
1703 unsigned long old_size
, unsigned long new_size
)
1705 unsigned long old_order
, new_order
;
1707 new_size
= max(new_size
, ht
->min_alloc_size
);
1708 old_order
= get_count_order_ulong(old_size
);
1709 new_order
= get_count_order_ulong(new_size
);
1710 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1711 old_size
, old_order
, new_size
, new_order
);
1712 assert(new_size
< old_size
);
1714 /* Remove and unlink all bucket nodes to remove. */
1715 fini_table(ht
, new_order
+ 1, old_order
);
1719 /* called with resize mutex held */
1721 void _do_cds_lfht_resize(struct cds_lfht
*ht
)
1723 unsigned long new_size
, old_size
;
1726 * Resize table, re-do if the target size has changed under us.
1729 assert(uatomic_read(&ht
->in_progress_resize
));
1730 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1732 ht
->t
.resize_initiated
= 1;
1733 old_size
= ht
->t
.size
;
1734 new_size
= CMM_LOAD_SHARED(ht
->t
.resize_target
);
1735 if (old_size
< new_size
)
1736 _do_cds_lfht_grow(ht
, old_size
, new_size
);
1737 else if (old_size
> new_size
)
1738 _do_cds_lfht_shrink(ht
, old_size
, new_size
);
1739 ht
->t
.resize_initiated
= 0;
1740 /* write resize_initiated before read resize_target */
1742 } while (ht
->t
.size
!= CMM_LOAD_SHARED(ht
->t
.resize_target
));
1746 unsigned long resize_target_grow(struct cds_lfht
*ht
, unsigned long new_size
)
1748 return _uatomic_xchg_monotonic_increase(&ht
->t
.resize_target
, new_size
);
1752 void resize_target_update_count(struct cds_lfht
*ht
,
1753 unsigned long count
)
1755 count
= max(count
, ht
->min_alloc_size
);
1756 uatomic_set(&ht
->t
.resize_target
, count
);
1759 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
)
1761 resize_target_update_count(ht
, new_size
);
1762 CMM_STORE_SHARED(ht
->t
.resize_initiated
, 1);
1763 ht
->cds_lfht_rcu_thread_offline();
1764 pthread_mutex_lock(&ht
->resize_mutex
);
1765 _do_cds_lfht_resize(ht
);
1766 pthread_mutex_unlock(&ht
->resize_mutex
);
1767 ht
->cds_lfht_rcu_thread_online();
1771 void do_resize_cb(struct rcu_head
*head
)
1773 struct rcu_resize_work
*work
=
1774 caa_container_of(head
, struct rcu_resize_work
, head
);
1775 struct cds_lfht
*ht
= work
->ht
;
1777 ht
->cds_lfht_rcu_thread_offline();
1778 pthread_mutex_lock(&ht
->resize_mutex
);
1779 _do_cds_lfht_resize(ht
);
1780 pthread_mutex_unlock(&ht
->resize_mutex
);
1781 ht
->cds_lfht_rcu_thread_online();
1783 cmm_smp_mb(); /* finish resize before decrement */
1784 uatomic_dec(&ht
->in_progress_resize
);
1788 void __cds_lfht_resize_lazy_launch(struct cds_lfht
*ht
)
1790 struct rcu_resize_work
*work
;
1792 /* Store resize_target before read resize_initiated */
1794 if (!CMM_LOAD_SHARED(ht
->t
.resize_initiated
)) {
1795 uatomic_inc(&ht
->in_progress_resize
);
1796 cmm_smp_mb(); /* increment resize count before load destroy */
1797 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
)) {
1798 uatomic_dec(&ht
->in_progress_resize
);
1801 work
= malloc(sizeof(*work
));
1803 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1804 CMM_STORE_SHARED(ht
->t
.resize_initiated
, 1);
1809 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
)
1811 unsigned long target_size
= size
<< growth
;
1813 if (resize_target_grow(ht
, target_size
) >= target_size
)
1816 __cds_lfht_resize_lazy_launch(ht
);
1820 * We favor grow operations over shrink. A shrink operation never occurs
1821 * if a grow operation is queued for lazy execution. A grow operation
1822 * cancels any pending shrink lazy execution.
1825 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
1826 unsigned long count
)
1828 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
1830 count
= max(count
, ht
->min_alloc_size
);
1832 return; /* Already the right size, no resize needed */
1833 if (count
> size
) { /* lazy grow */
1834 if (resize_target_grow(ht
, count
) >= count
)
1836 } else { /* lazy shrink */
1840 s
= uatomic_cmpxchg(&ht
->t
.resize_target
, size
, count
);
1842 break; /* no resize needed */
1844 return; /* growing is/(was just) in progress */
1846 return; /* some other thread do shrink */
1850 __cds_lfht_resize_lazy_launch(ht
);