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_ORDER 0
191 #define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER)
193 #if (CAA_BITS_PER_LONG == 32)
194 #define MAX_TABLE_ORDER 32
196 #define MAX_TABLE_ORDER 64
200 * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink.
202 #define MIN_PARTITION_PER_THREAD_ORDER 12
203 #define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
206 #define min(a, b) ((a) < (b) ? (a) : (b))
210 #define max(a, b) ((a) > (b) ? (a) : (b))
214 * The removed flag needs to be updated atomically with the pointer.
215 * It indicates that no node must attach to the node scheduled for
216 * removal, and that node garbage collection must be performed.
217 * The bucket flag does not require to be updated atomically with the
218 * pointer, but it is added as a pointer low bit flag to save space.
220 #define REMOVED_FLAG (1UL << 0)
221 #define BUCKET_FLAG (1UL << 1)
222 #define FLAGS_MASK ((1UL << 2) - 1)
224 /* Value of the end pointer. Should not interact with flags. */
225 #define END_VALUE NULL
228 * ht_items_count: Split-counters counting the number of node addition
229 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
230 * is set at hash table creation.
232 * These are free-running counters, never reset to zero. They count the
233 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
234 * operations to update the global counter. We choose a power-of-2 value
235 * for the trigger to deal with 32 or 64-bit overflow of the counter.
237 struct ht_items_count
{
238 unsigned long add
, del
;
239 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
242 * rcu_table: Contains the size and desired new size if a resize
243 * operation is in progress, as well as the statically-sized array of
244 * bucket table pointers.
247 unsigned long size
; /* always a power of 2, shared (RCU) */
248 unsigned long resize_target
;
249 int resize_initiated
;
252 * Contains the per order-index-level bucket node table. The size
253 * of each bucket node table is half the number of hashes contained
254 * in this order (except for order 0). The minimum allocation size
255 * parameter allows combining the bucket node arrays of the lowermost
256 * levels to improve cache locality for small index orders.
258 struct cds_lfht_node
*tbl
[MAX_TABLE_ORDER
];
262 * cds_lfht: Top-level data structure representing a lock-free hash
263 * table. Defined in the implementation file to make it be an opaque
268 unsigned long min_alloc_buckets_order
;
269 unsigned long min_nr_alloc_buckets
;
270 unsigned long max_nr_buckets
;
273 * We need to put the work threads offline (QSBR) when taking this
274 * mutex, because we use synchronize_rcu within this mutex critical
275 * section, which waits on read-side critical sections, and could
276 * therefore cause grace-period deadlock if we hold off RCU G.P.
279 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
280 unsigned int in_progress_resize
, in_progress_destroy
;
281 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
282 void (*func
)(struct rcu_head
*head
));
283 void (*cds_lfht_synchronize_rcu
)(void);
284 void (*cds_lfht_rcu_read_lock
)(void);
285 void (*cds_lfht_rcu_read_unlock
)(void);
286 void (*cds_lfht_rcu_thread_offline
)(void);
287 void (*cds_lfht_rcu_thread_online
)(void);
288 void (*cds_lfht_rcu_register_thread
)(void);
289 void (*cds_lfht_rcu_unregister_thread
)(void);
290 pthread_attr_t
*resize_attr
; /* Resize threads attributes */
291 long count
; /* global approximate item count */
292 struct ht_items_count
*split_count
; /* split item count */
296 * rcu_resize_work: Contains arguments passed to RCU worker thread
297 * responsible for performing lazy resize.
299 struct rcu_resize_work
{
300 struct rcu_head head
;
305 * partition_resize_work: Contains arguments passed to worker threads
306 * executing the hash table resize on partitions of the hash table
307 * assigned to each processor's worker thread.
309 struct partition_resize_work
{
312 unsigned long i
, start
, len
;
313 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
314 unsigned long start
, unsigned long len
);
318 void _cds_lfht_add(struct cds_lfht
*ht
,
319 cds_lfht_match_fct match
,
322 struct cds_lfht_node
*node
,
323 struct cds_lfht_iter
*unique_ret
,
327 * Algorithm to reverse bits in a word by lookup table, extended to
330 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
331 * Originally from Public Domain.
334 static const uint8_t BitReverseTable256
[256] =
336 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
337 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
338 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
339 R6(0), R6(2), R6(1), R6(3)
346 uint8_t bit_reverse_u8(uint8_t v
)
348 return BitReverseTable256
[v
];
351 static __attribute__((unused
))
352 uint32_t bit_reverse_u32(uint32_t v
)
354 return ((uint32_t) bit_reverse_u8(v
) << 24) |
355 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
356 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
357 ((uint32_t) bit_reverse_u8(v
>> 24));
360 static __attribute__((unused
))
361 uint64_t bit_reverse_u64(uint64_t v
)
363 return ((uint64_t) bit_reverse_u8(v
) << 56) |
364 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
365 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
366 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
367 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
368 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
369 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
370 ((uint64_t) bit_reverse_u8(v
>> 56));
374 unsigned long bit_reverse_ulong(unsigned long v
)
376 #if (CAA_BITS_PER_LONG == 32)
377 return bit_reverse_u32(v
);
379 return bit_reverse_u64(v
);
384 * fls: returns the position of the most significant bit.
385 * Returns 0 if no bit is set, else returns the position of the most
386 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
388 #if defined(__i386) || defined(__x86_64)
390 unsigned int fls_u32(uint32_t x
)
398 : "=r" (r
) : "rm" (x
));
404 #if defined(__x86_64)
406 unsigned int fls_u64(uint64_t x
)
414 : "=r" (r
) : "rm" (x
));
421 static __attribute__((unused
))
422 unsigned int fls_u64(uint64_t x
)
429 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
433 if (!(x
& 0xFFFF000000000000ULL
)) {
437 if (!(x
& 0xFF00000000000000ULL
)) {
441 if (!(x
& 0xF000000000000000ULL
)) {
445 if (!(x
& 0xC000000000000000ULL
)) {
449 if (!(x
& 0x8000000000000000ULL
)) {
458 static __attribute__((unused
))
459 unsigned int fls_u32(uint32_t x
)
465 if (!(x
& 0xFFFF0000U
)) {
469 if (!(x
& 0xFF000000U
)) {
473 if (!(x
& 0xF0000000U
)) {
477 if (!(x
& 0xC0000000U
)) {
481 if (!(x
& 0x80000000U
)) {
489 unsigned int fls_ulong(unsigned long x
)
491 #if (CAA_BITS_PER_LONG == 32)
499 * Return the minimum order for which x <= (1UL << order).
500 * Return -1 if x is 0.
502 int get_count_order_u32(uint32_t x
)
507 return fls_u32(x
- 1);
511 * Return the minimum order for which x <= (1UL << order).
512 * Return -1 if x is 0.
514 int get_count_order_ulong(unsigned long x
)
519 return fls_ulong(x
- 1);
523 #define poison_free(ptr) \
526 memset(ptr, 0x42, sizeof(*(ptr))); \
531 #define poison_free(ptr) free(ptr)
535 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
);
538 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
539 unsigned long count
);
541 static long nr_cpus_mask
= -1;
542 static long split_count_mask
= -1;
544 #if defined(HAVE_SYSCONF)
545 static void ht_init_nr_cpus_mask(void)
549 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
555 * round up number of CPUs to next power of two, so we
556 * can use & for modulo.
558 maxcpus
= 1UL << get_count_order_ulong(maxcpus
);
559 nr_cpus_mask
= maxcpus
- 1;
561 #else /* #if defined(HAVE_SYSCONF) */
562 static void ht_init_nr_cpus_mask(void)
566 #endif /* #else #if defined(HAVE_SYSCONF) */
569 void alloc_split_items_count(struct cds_lfht
*ht
)
571 struct ht_items_count
*count
;
573 if (nr_cpus_mask
== -1) {
574 ht_init_nr_cpus_mask();
575 if (nr_cpus_mask
< 0)
576 split_count_mask
= DEFAULT_SPLIT_COUNT_MASK
;
578 split_count_mask
= nr_cpus_mask
;
581 assert(split_count_mask
>= 0);
583 if (ht
->flags
& CDS_LFHT_ACCOUNTING
) {
584 ht
->split_count
= calloc(split_count_mask
+ 1, sizeof(*count
));
585 assert(ht
->split_count
);
587 ht
->split_count
= NULL
;
592 void free_split_items_count(struct cds_lfht
*ht
)
594 poison_free(ht
->split_count
);
597 #if defined(HAVE_SCHED_GETCPU)
599 int ht_get_split_count_index(unsigned long hash
)
603 assert(split_count_mask
>= 0);
604 cpu
= sched_getcpu();
605 if (caa_unlikely(cpu
< 0))
606 return hash
& split_count_mask
;
608 return cpu
& split_count_mask
;
610 #else /* #if defined(HAVE_SCHED_GETCPU) */
612 int ht_get_split_count_index(unsigned long hash
)
614 return hash
& split_count_mask
;
616 #endif /* #else #if defined(HAVE_SCHED_GETCPU) */
619 void ht_count_add(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
621 unsigned long split_count
;
624 if (caa_unlikely(!ht
->split_count
))
626 index
= ht_get_split_count_index(hash
);
627 split_count
= uatomic_add_return(&ht
->split_count
[index
].add
, 1);
628 if (caa_unlikely(!(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
631 dbg_printf("add split count %lu\n", split_count
);
632 count
= uatomic_add_return(&ht
->count
,
633 1UL << COUNT_COMMIT_ORDER
);
635 if (!(count
& (count
- 1))) {
636 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) < size
)
638 dbg_printf("add set global %ld\n", count
);
639 cds_lfht_resize_lazy_count(ht
, size
,
640 count
>> (CHAIN_LEN_TARGET
- 1));
646 void ht_count_del(struct cds_lfht
*ht
, unsigned long size
, unsigned long hash
)
648 unsigned long split_count
;
651 if (caa_unlikely(!ht
->split_count
))
653 index
= ht_get_split_count_index(hash
);
654 split_count
= uatomic_add_return(&ht
->split_count
[index
].del
, 1);
655 if (caa_unlikely(!(split_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
658 dbg_printf("del split count %lu\n", split_count
);
659 count
= uatomic_add_return(&ht
->count
,
660 -(1UL << COUNT_COMMIT_ORDER
));
662 if (!(count
& (count
- 1))) {
663 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
) >= size
)
665 dbg_printf("del set global %ld\n", count
);
667 * Don't shrink table if the number of nodes is below a
670 if (count
< (1UL << COUNT_COMMIT_ORDER
) * (split_count_mask
+ 1))
672 cds_lfht_resize_lazy_count(ht
, size
,
673 count
>> (CHAIN_LEN_TARGET
- 1));
679 void check_resize(struct cds_lfht
*ht
, unsigned long size
, uint32_t chain_len
)
683 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
685 count
= uatomic_read(&ht
->count
);
687 * Use bucket-local length for small table expand and for
688 * environments lacking per-cpu data support.
690 if (count
>= (1UL << COUNT_COMMIT_ORDER
))
693 dbg_printf("WARNING: large chain length: %u.\n",
695 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
696 cds_lfht_resize_lazy_grow(ht
, size
,
697 get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
701 struct cds_lfht_node
*clear_flag(struct cds_lfht_node
*node
)
703 return (struct cds_lfht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
707 int is_removed(struct cds_lfht_node
*node
)
709 return ((unsigned long) node
) & REMOVED_FLAG
;
713 struct cds_lfht_node
*flag_removed(struct cds_lfht_node
*node
)
715 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
719 int is_bucket(struct cds_lfht_node
*node
)
721 return ((unsigned long) node
) & BUCKET_FLAG
;
725 struct cds_lfht_node
*flag_bucket(struct cds_lfht_node
*node
)
727 return (struct cds_lfht_node
*) (((unsigned long) node
) | BUCKET_FLAG
);
731 struct cds_lfht_node
*get_end(void)
733 return (struct cds_lfht_node
*) END_VALUE
;
737 int is_end(struct cds_lfht_node
*node
)
739 return clear_flag(node
) == (struct cds_lfht_node
*) END_VALUE
;
743 unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr
,
746 unsigned long old1
, old2
;
748 old1
= uatomic_read(ptr
);
753 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
758 void cds_lfht_alloc_bucket_table(struct cds_lfht
*ht
, unsigned long order
)
761 ht
->t
.tbl
[0] = calloc(ht
->min_nr_alloc_buckets
,
762 sizeof(struct cds_lfht_node
));
763 assert(ht
->t
.tbl
[0]);
764 } else if (order
> ht
->min_alloc_buckets_order
) {
765 ht
->t
.tbl
[order
] = calloc(1UL << (order
-1),
766 sizeof(struct cds_lfht_node
));
767 assert(ht
->t
.tbl
[order
]);
769 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
773 * cds_lfht_free_bucket_table() should be called with decreasing order.
774 * When cds_lfht_free_bucket_table(0) is called, it means the whole
778 void cds_lfht_free_bucket_table(struct cds_lfht
*ht
, unsigned long order
)
781 poison_free(ht
->t
.tbl
[0]);
782 else if (order
> ht
->min_alloc_buckets_order
)
783 poison_free(ht
->t
.tbl
[order
]);
784 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
788 struct cds_lfht_node
*bucket_at(struct cds_lfht
*ht
, unsigned long index
)
792 if ((__builtin_constant_p(index
) && index
== 0)
793 || index
< ht
->min_nr_alloc_buckets
) {
794 dbg_printf("bucket index %lu order 0 aridx 0\n", index
);
795 return &ht
->t
.tbl
[0][index
];
798 * equivalent to get_count_order_ulong(index + 1), but optimizes
799 * away the non-existing 0 special-case for
800 * get_count_order_ulong.
802 order
= fls_ulong(index
);
803 dbg_printf("bucket index %lu order %lu aridx %lu\n",
804 index
, order
, index
& ((1UL << (order
- 1)) - 1));
805 return &ht
->t
.tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
809 struct cds_lfht_node
*lookup_bucket(struct cds_lfht
*ht
, unsigned long size
,
813 return bucket_at(ht
, hash
& (size
- 1));
817 * Remove all logically deleted nodes from a bucket up to a certain node key.
820 void _cds_lfht_gc_bucket(struct cds_lfht_node
*bucket
, struct cds_lfht_node
*node
)
822 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_next
;
824 assert(!is_bucket(bucket
));
825 assert(!is_removed(bucket
));
826 assert(!is_bucket(node
));
827 assert(!is_removed(node
));
830 /* We can always skip the bucket node initially */
831 iter
= rcu_dereference(iter_prev
->next
);
832 assert(!is_removed(iter
));
833 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
835 * We should never be called with bucket (start of chain)
836 * and logically removed node (end of path compression
837 * marker) being the actual same node. This would be a
838 * bug in the algorithm implementation.
840 assert(bucket
!= node
);
842 if (caa_unlikely(is_end(iter
)))
844 if (caa_likely(clear_flag(iter
)->reverse_hash
> node
->reverse_hash
))
846 next
= rcu_dereference(clear_flag(iter
)->next
);
847 if (caa_likely(is_removed(next
)))
849 iter_prev
= clear_flag(iter
);
852 assert(!is_removed(iter
));
854 new_next
= flag_bucket(clear_flag(next
));
856 new_next
= clear_flag(next
);
857 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, new_next
);
863 int _cds_lfht_replace(struct cds_lfht
*ht
, unsigned long size
,
864 struct cds_lfht_node
*old_node
,
865 struct cds_lfht_node
*old_next
,
866 struct cds_lfht_node
*new_node
)
868 struct cds_lfht_node
*bucket
, *ret_next
;
870 if (!old_node
) /* Return -ENOENT if asked to replace NULL node */
873 assert(!is_removed(old_node
));
874 assert(!is_bucket(old_node
));
875 assert(!is_removed(new_node
));
876 assert(!is_bucket(new_node
));
877 assert(new_node
!= old_node
);
879 /* Insert after node to be replaced */
880 if (is_removed(old_next
)) {
882 * Too late, the old node has been removed under us
883 * between lookup and replace. Fail.
887 assert(!is_bucket(old_next
));
888 assert(new_node
!= clear_flag(old_next
));
889 new_node
->next
= clear_flag(old_next
);
891 * Here is the whole trick for lock-free replace: we add
892 * the replacement node _after_ the node we want to
893 * replace by atomically setting its next pointer at the
894 * same time we set its removal flag. Given that
895 * the lookups/get next use an iterator aware of the
896 * next pointer, they will either skip the old node due
897 * to the removal flag and see the new node, or use
898 * the old node, but will not see the new one.
900 ret_next
= uatomic_cmpxchg(&old_node
->next
,
901 old_next
, flag_removed(new_node
));
902 if (ret_next
== old_next
)
903 break; /* We performed the replacement. */
908 * Ensure that the old node is not visible to readers anymore:
909 * lookup for the node, and remove it (along with any other
910 * logically removed node) if found.
912 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(old_node
->reverse_hash
));
913 _cds_lfht_gc_bucket(bucket
, new_node
);
915 assert(is_removed(rcu_dereference(old_node
->next
)));
920 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
921 * mode. A NULL unique_ret allows creation of duplicate keys.
924 void _cds_lfht_add(struct cds_lfht
*ht
,
925 cds_lfht_match_fct match
,
928 struct cds_lfht_node
*node
,
929 struct cds_lfht_iter
*unique_ret
,
932 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
934 struct cds_lfht_node
*bucket
;
936 assert(!is_bucket(node
));
937 assert(!is_removed(node
));
938 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->reverse_hash
));
940 uint32_t chain_len
= 0;
943 * iter_prev points to the non-removed node prior to the
947 /* We can always skip the bucket node initially */
948 iter
= rcu_dereference(iter_prev
->next
);
949 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
951 if (caa_unlikely(is_end(iter
)))
953 if (caa_likely(clear_flag(iter
)->reverse_hash
> node
->reverse_hash
))
956 /* bucket node is the first node of the identical-hash-value chain */
957 if (bucket_flag
&& clear_flag(iter
)->reverse_hash
== node
->reverse_hash
)
960 next
= rcu_dereference(clear_flag(iter
)->next
);
961 if (caa_unlikely(is_removed(next
)))
967 && clear_flag(iter
)->reverse_hash
== node
->reverse_hash
) {
968 struct cds_lfht_iter d_iter
= { .node
= node
, .next
= iter
, };
971 * uniquely adding inserts the node as the first
972 * node of the identical-hash-value node chain.
974 * This semantic ensures no duplicated keys
975 * should ever be observable in the table
976 * (including observe one node by one node
977 * by forward iterations)
979 cds_lfht_next_duplicate(ht
, match
, key
, &d_iter
);
983 *unique_ret
= d_iter
;
987 /* Only account for identical reverse hash once */
988 if (iter_prev
->reverse_hash
!= clear_flag(iter
)->reverse_hash
990 check_resize(ht
, size
, ++chain_len
);
991 iter_prev
= clear_flag(iter
);
996 assert(node
!= clear_flag(iter
));
997 assert(!is_removed(iter_prev
));
998 assert(!is_removed(iter
));
999 assert(iter_prev
!= node
);
1001 node
->next
= clear_flag(iter
);
1003 node
->next
= flag_bucket(clear_flag(iter
));
1004 if (is_bucket(iter
))
1005 new_node
= flag_bucket(node
);
1008 if (uatomic_cmpxchg(&iter_prev
->next
, iter
,
1009 new_node
) != iter
) {
1010 continue; /* retry */
1017 assert(!is_removed(iter
));
1018 if (is_bucket(iter
))
1019 new_next
= flag_bucket(clear_flag(next
));
1021 new_next
= clear_flag(next
);
1022 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, new_next
);
1027 unique_ret
->node
= return_node
;
1028 /* unique_ret->next left unset, never used. */
1033 int _cds_lfht_del(struct cds_lfht
*ht
, unsigned long size
,
1034 struct cds_lfht_node
*node
,
1037 struct cds_lfht_node
*bucket
, *next
, *old
;
1039 if (!node
) /* Return -ENOENT if asked to delete NULL node */
1042 /* logically delete the node */
1043 assert(!is_bucket(node
));
1044 assert(!is_removed(node
));
1045 old
= rcu_dereference(node
->next
);
1047 struct cds_lfht_node
*new_next
;
1050 if (caa_unlikely(is_removed(next
)))
1053 assert(is_bucket(next
));
1055 assert(!is_bucket(next
));
1056 new_next
= flag_removed(next
);
1057 old
= uatomic_cmpxchg(&node
->next
, next
, new_next
);
1058 } while (old
!= next
);
1059 /* We performed the (logical) deletion. */
1062 * Ensure that the node is not visible to readers anymore: lookup for
1063 * the node, and remove it (along with any other logically removed node)
1066 bucket
= lookup_bucket(ht
, size
, bit_reverse_ulong(node
->reverse_hash
));
1067 _cds_lfht_gc_bucket(bucket
, node
);
1069 assert(is_removed(rcu_dereference(node
->next
)));
1074 void *partition_resize_thread(void *arg
)
1076 struct partition_resize_work
*work
= arg
;
1078 work
->ht
->cds_lfht_rcu_register_thread();
1079 work
->fct(work
->ht
, work
->i
, work
->start
, work
->len
);
1080 work
->ht
->cds_lfht_rcu_unregister_thread();
1085 void partition_resize_helper(struct cds_lfht
*ht
, unsigned long i
,
1087 void (*fct
)(struct cds_lfht
*ht
, unsigned long i
,
1088 unsigned long start
, unsigned long len
))
1090 unsigned long partition_len
;
1091 struct partition_resize_work
*work
;
1093 unsigned long nr_threads
;
1096 * Note: nr_cpus_mask + 1 is always power of 2.
1097 * We spawn just the number of threads we need to satisfy the minimum
1098 * partition size, up to the number of CPUs in the system.
1100 if (nr_cpus_mask
> 0) {
1101 nr_threads
= min(nr_cpus_mask
+ 1,
1102 len
>> MIN_PARTITION_PER_THREAD_ORDER
);
1106 partition_len
= len
>> get_count_order_ulong(nr_threads
);
1107 work
= calloc(nr_threads
, sizeof(*work
));
1109 for (thread
= 0; thread
< nr_threads
; thread
++) {
1110 work
[thread
].ht
= ht
;
1112 work
[thread
].len
= partition_len
;
1113 work
[thread
].start
= thread
* partition_len
;
1114 work
[thread
].fct
= fct
;
1115 ret
= pthread_create(&(work
[thread
].thread_id
), ht
->resize_attr
,
1116 partition_resize_thread
, &work
[thread
]);
1119 for (thread
= 0; thread
< nr_threads
; thread
++) {
1120 ret
= pthread_join(work
[thread
].thread_id
, NULL
);
1127 * Holding RCU read lock to protect _cds_lfht_add against memory
1128 * reclaim that could be performed by other call_rcu worker threads (ABA
1131 * When we reach a certain length, we can split this population phase over
1132 * many worker threads, based on the number of CPUs available in the system.
1133 * This should therefore take care of not having the expand lagging behind too
1134 * many concurrent insertion threads by using the scheduler's ability to
1135 * schedule bucket node population fairly with insertions.
1138 void init_table_populate_partition(struct cds_lfht
*ht
, unsigned long i
,
1139 unsigned long start
, unsigned long len
)
1141 unsigned long j
, size
= 1UL << (i
- 1);
1143 assert(i
> MIN_TABLE_ORDER
);
1144 ht
->cds_lfht_rcu_read_lock();
1145 for (j
= size
+ start
; j
< size
+ start
+ len
; j
++) {
1146 struct cds_lfht_node
*new_node
= bucket_at(ht
, j
);
1148 assert(j
>= size
&& j
< (size
<< 1));
1149 dbg_printf("init populate: order %lu index %lu hash %lu\n",
1151 new_node
->reverse_hash
= bit_reverse_ulong(j
);
1152 _cds_lfht_add(ht
, NULL
, NULL
, size
, new_node
, NULL
, 1);
1154 ht
->cds_lfht_rcu_read_unlock();
1158 void init_table_populate(struct cds_lfht
*ht
, unsigned long i
,
1161 assert(nr_cpus_mask
!= -1);
1162 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1163 ht
->cds_lfht_rcu_thread_online();
1164 init_table_populate_partition(ht
, i
, 0, len
);
1165 ht
->cds_lfht_rcu_thread_offline();
1168 partition_resize_helper(ht
, i
, len
, init_table_populate_partition
);
1172 void init_table(struct cds_lfht
*ht
,
1173 unsigned long first_order
, unsigned long last_order
)
1177 dbg_printf("init table: first_order %lu last_order %lu\n",
1178 first_order
, last_order
);
1179 assert(first_order
> MIN_TABLE_ORDER
);
1180 for (i
= first_order
; i
<= last_order
; i
++) {
1183 len
= 1UL << (i
- 1);
1184 dbg_printf("init order %lu len: %lu\n", i
, len
);
1186 /* Stop expand if the resize target changes under us */
1187 if (CMM_LOAD_SHARED(ht
->t
.resize_target
) < (1UL << i
))
1190 cds_lfht_alloc_bucket_table(ht
, i
);
1193 * Set all bucket nodes reverse hash values for a level and
1194 * link all bucket nodes into the table.
1196 init_table_populate(ht
, i
, len
);
1199 * Update table size.
1201 cmm_smp_wmb(); /* populate data before RCU size */
1202 CMM_STORE_SHARED(ht
->t
.size
, 1UL << i
);
1204 dbg_printf("init new size: %lu\n", 1UL << i
);
1205 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1211 * Holding RCU read lock to protect _cds_lfht_remove against memory
1212 * reclaim that could be performed by other call_rcu worker threads (ABA
1214 * For a single level, we logically remove and garbage collect each node.
1216 * As a design choice, we perform logical removal and garbage collection on a
1217 * node-per-node basis to simplify this algorithm. We also assume keeping good
1218 * cache locality of the operation would overweight possible performance gain
1219 * that could be achieved by batching garbage collection for multiple levels.
1220 * However, this would have to be justified by benchmarks.
1222 * Concurrent removal and add operations are helping us perform garbage
1223 * collection of logically removed nodes. We guarantee that all logically
1224 * removed nodes have been garbage-collected (unlinked) before call_rcu is
1225 * invoked to free a hole level of bucket nodes (after a grace period).
1227 * Logical removal and garbage collection can therefore be done in batch or on a
1228 * node-per-node basis, as long as the guarantee above holds.
1230 * When we reach a certain length, we can split this removal over many worker
1231 * threads, based on the number of CPUs available in the system. This should
1232 * take care of not letting resize process lag behind too many concurrent
1233 * updater threads actively inserting into the hash table.
1236 void remove_table_partition(struct cds_lfht
*ht
, unsigned long i
,
1237 unsigned long start
, unsigned long len
)
1239 unsigned long j
, size
= 1UL << (i
- 1);
1241 assert(i
> MIN_TABLE_ORDER
);
1242 ht
->cds_lfht_rcu_read_lock();
1243 for (j
= size
+ start
; j
< size
+ start
+ len
; j
++) {
1244 struct cds_lfht_node
*fini_node
= bucket_at(ht
, j
);
1246 assert(j
>= size
&& j
< (size
<< 1));
1247 dbg_printf("remove entry: order %lu index %lu hash %lu\n",
1249 fini_node
->reverse_hash
= bit_reverse_ulong(j
);
1250 (void) _cds_lfht_del(ht
, size
, fini_node
, 1);
1252 ht
->cds_lfht_rcu_read_unlock();
1256 void remove_table(struct cds_lfht
*ht
, unsigned long i
, unsigned long len
)
1259 assert(nr_cpus_mask
!= -1);
1260 if (nr_cpus_mask
< 0 || len
< 2 * MIN_PARTITION_PER_THREAD
) {
1261 ht
->cds_lfht_rcu_thread_online();
1262 remove_table_partition(ht
, i
, 0, len
);
1263 ht
->cds_lfht_rcu_thread_offline();
1266 partition_resize_helper(ht
, i
, len
, remove_table_partition
);
1270 void fini_table(struct cds_lfht
*ht
,
1271 unsigned long first_order
, unsigned long last_order
)
1274 unsigned long free_by_rcu_order
= 0;
1276 dbg_printf("fini table: first_order %lu last_order %lu\n",
1277 first_order
, last_order
);
1278 assert(first_order
> MIN_TABLE_ORDER
);
1279 for (i
= last_order
; i
>= first_order
; i
--) {
1282 len
= 1UL << (i
- 1);
1283 dbg_printf("fini order %lu len: %lu\n", i
, len
);
1285 /* Stop shrink if the resize target changes under us */
1286 if (CMM_LOAD_SHARED(ht
->t
.resize_target
) > (1UL << (i
- 1)))
1289 cmm_smp_wmb(); /* populate data before RCU size */
1290 CMM_STORE_SHARED(ht
->t
.size
, 1UL << (i
- 1));
1293 * We need to wait for all add operations to reach Q.S. (and
1294 * thus use the new table for lookups) before we can start
1295 * releasing the old bucket nodes. Otherwise their lookup will
1296 * return a logically removed node as insert position.
1298 ht
->cds_lfht_synchronize_rcu();
1299 if (free_by_rcu_order
)
1300 cds_lfht_free_bucket_table(ht
, free_by_rcu_order
);
1303 * Set "removed" flag in bucket nodes about to be removed.
1304 * Unlink all now-logically-removed bucket node pointers.
1305 * Concurrent add/remove operation are helping us doing
1308 remove_table(ht
, i
, len
);
1310 free_by_rcu_order
= i
;
1312 dbg_printf("fini new size: %lu\n", 1UL << i
);
1313 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1317 if (free_by_rcu_order
) {
1318 ht
->cds_lfht_synchronize_rcu();
1319 cds_lfht_free_bucket_table(ht
, free_by_rcu_order
);
1324 void cds_lfht_create_bucket(struct cds_lfht
*ht
, unsigned long size
)
1326 struct cds_lfht_node
*prev
, *node
;
1327 unsigned long order
, len
, i
;
1329 cds_lfht_alloc_bucket_table(ht
, 0);
1331 dbg_printf("create bucket: order 0 index 0 hash 0\n");
1332 node
= bucket_at(ht
, 0);
1333 node
->next
= flag_bucket(get_end());
1334 node
->reverse_hash
= 0;
1336 for (order
= 1; order
< get_count_order_ulong(size
) + 1; order
++) {
1337 len
= 1UL << (order
- 1);
1338 cds_lfht_alloc_bucket_table(ht
, order
);
1340 for (i
= 0; i
< len
; i
++) {
1342 * Now, we are trying to init the node with the
1343 * hash=(len+i) (which is also a bucket with the
1344 * index=(len+i)) and insert it into the hash table,
1345 * so this node has to be inserted after the bucket
1346 * with the index=(len+i)&(len-1)=i. And because there
1347 * is no other non-bucket node nor bucket node with
1348 * larger index/hash inserted, so the bucket node
1349 * being inserted should be inserted directly linked
1350 * after the bucket node with index=i.
1352 prev
= bucket_at(ht
, i
);
1353 node
= bucket_at(ht
, len
+ i
);
1355 dbg_printf("create bucket: order %lu index %lu hash %lu\n",
1356 order
, len
+ i
, len
+ i
);
1357 node
->reverse_hash
= bit_reverse_ulong(len
+ i
);
1359 /* insert after prev */
1360 assert(is_bucket(prev
->next
));
1361 node
->next
= prev
->next
;
1362 prev
->next
= flag_bucket(node
);
1367 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
1368 unsigned long min_nr_alloc_buckets
,
1369 unsigned long max_nr_buckets
,
1371 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
1372 void (*func
)(struct rcu_head
*head
)),
1373 void (*cds_lfht_synchronize_rcu
)(void),
1374 void (*cds_lfht_rcu_read_lock
)(void),
1375 void (*cds_lfht_rcu_read_unlock
)(void),
1376 void (*cds_lfht_rcu_thread_offline
)(void),
1377 void (*cds_lfht_rcu_thread_online
)(void),
1378 void (*cds_lfht_rcu_register_thread
)(void),
1379 void (*cds_lfht_rcu_unregister_thread
)(void),
1380 pthread_attr_t
*attr
)
1382 struct cds_lfht
*ht
;
1383 unsigned long order
;
1385 /* min_nr_alloc_buckets must be power of two */
1386 if (!min_nr_alloc_buckets
|| (min_nr_alloc_buckets
& (min_nr_alloc_buckets
- 1)))
1389 /* init_size must be power of two */
1390 if (!init_size
|| (init_size
& (init_size
- 1)))
1393 if (!max_nr_buckets
)
1394 max_nr_buckets
= 1UL << (MAX_TABLE_ORDER
- 1);
1396 /* max_nr_buckets must be power of two */
1397 if (!max_nr_buckets
|| (max_nr_buckets
& (max_nr_buckets
- 1)))
1400 min_nr_alloc_buckets
= max(min_nr_alloc_buckets
, MIN_TABLE_SIZE
);
1401 init_size
= max(init_size
, MIN_TABLE_SIZE
);
1402 max_nr_buckets
= max(max_nr_buckets
, min_nr_alloc_buckets
);
1403 init_size
= min(init_size
, max_nr_buckets
);
1404 ht
= calloc(1, sizeof(struct cds_lfht
));
1407 ht
->cds_lfht_call_rcu
= cds_lfht_call_rcu
;
1408 ht
->cds_lfht_synchronize_rcu
= cds_lfht_synchronize_rcu
;
1409 ht
->cds_lfht_rcu_read_lock
= cds_lfht_rcu_read_lock
;
1410 ht
->cds_lfht_rcu_read_unlock
= cds_lfht_rcu_read_unlock
;
1411 ht
->cds_lfht_rcu_thread_offline
= cds_lfht_rcu_thread_offline
;
1412 ht
->cds_lfht_rcu_thread_online
= cds_lfht_rcu_thread_online
;
1413 ht
->cds_lfht_rcu_register_thread
= cds_lfht_rcu_register_thread
;
1414 ht
->cds_lfht_rcu_unregister_thread
= cds_lfht_rcu_unregister_thread
;
1415 ht
->resize_attr
= attr
;
1416 alloc_split_items_count(ht
);
1417 /* this mutex should not nest in read-side C.S. */
1418 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
1419 order
= get_count_order_ulong(init_size
);
1420 ht
->t
.resize_target
= 1UL << order
;
1421 ht
->min_nr_alloc_buckets
= min_nr_alloc_buckets
;
1422 ht
->min_alloc_buckets_order
= get_count_order_ulong(min_nr_alloc_buckets
);
1423 ht
->max_nr_buckets
= max_nr_buckets
;
1424 cds_lfht_create_bucket(ht
, 1UL << order
);
1425 ht
->t
.size
= 1UL << order
;
1429 void cds_lfht_lookup(struct cds_lfht
*ht
, unsigned long hash
,
1430 cds_lfht_match_fct match
, const void *key
,
1431 struct cds_lfht_iter
*iter
)
1433 struct cds_lfht_node
*node
, *next
, *bucket
;
1434 unsigned long reverse_hash
, size
;
1436 reverse_hash
= bit_reverse_ulong(hash
);
1438 size
= rcu_dereference(ht
->t
.size
);
1439 bucket
= lookup_bucket(ht
, size
, hash
);
1440 /* We can always skip the bucket node initially */
1441 node
= rcu_dereference(bucket
->next
);
1442 node
= clear_flag(node
);
1444 if (caa_unlikely(is_end(node
))) {
1448 if (caa_unlikely(node
->reverse_hash
> reverse_hash
)) {
1452 next
= rcu_dereference(node
->next
);
1453 assert(node
== clear_flag(node
));
1454 if (caa_likely(!is_removed(next
))
1456 && node
->reverse_hash
== reverse_hash
1457 && caa_likely(match(node
, key
))) {
1460 node
= clear_flag(next
);
1462 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1467 void cds_lfht_next_duplicate(struct cds_lfht
*ht
, cds_lfht_match_fct match
,
1468 const void *key
, struct cds_lfht_iter
*iter
)
1470 struct cds_lfht_node
*node
, *next
;
1471 unsigned long reverse_hash
;
1474 reverse_hash
= node
->reverse_hash
;
1476 node
= clear_flag(next
);
1479 if (caa_unlikely(is_end(node
))) {
1483 if (caa_unlikely(node
->reverse_hash
> reverse_hash
)) {
1487 next
= rcu_dereference(node
->next
);
1488 if (caa_likely(!is_removed(next
))
1490 && caa_likely(match(node
, key
))) {
1493 node
= clear_flag(next
);
1495 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1500 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1502 struct cds_lfht_node
*node
, *next
;
1504 node
= clear_flag(iter
->next
);
1506 if (caa_unlikely(is_end(node
))) {
1510 next
= rcu_dereference(node
->next
);
1511 if (caa_likely(!is_removed(next
))
1512 && !is_bucket(next
)) {
1515 node
= clear_flag(next
);
1517 assert(!node
|| !is_bucket(rcu_dereference(node
->next
)));
1522 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1525 * Get next after first bucket node. The first bucket node is the
1526 * first node of the linked list.
1528 iter
->next
= bucket_at(ht
, 0)->next
;
1529 cds_lfht_next(ht
, iter
);
1532 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
1533 struct cds_lfht_node
*node
)
1537 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1538 size
= rcu_dereference(ht
->t
.size
);
1539 _cds_lfht_add(ht
, NULL
, NULL
, size
, node
, NULL
, 0);
1540 ht_count_add(ht
, size
, hash
);
1543 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
1545 cds_lfht_match_fct match
,
1547 struct cds_lfht_node
*node
)
1550 struct cds_lfht_iter iter
;
1552 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1553 size
= rcu_dereference(ht
->t
.size
);
1554 _cds_lfht_add(ht
, match
, key
, size
, node
, &iter
, 0);
1555 if (iter
.node
== node
)
1556 ht_count_add(ht
, size
, hash
);
1560 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
1562 cds_lfht_match_fct match
,
1564 struct cds_lfht_node
*node
)
1567 struct cds_lfht_iter iter
;
1569 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1570 size
= rcu_dereference(ht
->t
.size
);
1572 _cds_lfht_add(ht
, match
, key
, size
, node
, &iter
, 0);
1573 if (iter
.node
== node
) {
1574 ht_count_add(ht
, size
, hash
);
1578 if (!_cds_lfht_replace(ht
, size
, iter
.node
, iter
.next
, node
))
1583 int cds_lfht_replace(struct cds_lfht
*ht
, struct cds_lfht_iter
*old_iter
,
1584 struct cds_lfht_node
*new_node
)
1588 size
= rcu_dereference(ht
->t
.size
);
1589 return _cds_lfht_replace(ht
, size
, old_iter
->node
, old_iter
->next
,
1593 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
)
1595 unsigned long size
, hash
;
1598 size
= rcu_dereference(ht
->t
.size
);
1599 ret
= _cds_lfht_del(ht
, size
, iter
->node
, 0);
1601 hash
= bit_reverse_ulong(iter
->node
->reverse_hash
);
1602 ht_count_del(ht
, size
, hash
);
1608 int cds_lfht_delete_bucket(struct cds_lfht
*ht
)
1610 struct cds_lfht_node
*node
;
1611 unsigned long order
, i
, size
;
1613 /* Check that the table is empty */
1614 node
= bucket_at(ht
, 0);
1616 node
= clear_flag(node
)->next
;
1617 if (!is_bucket(node
))
1619 assert(!is_removed(node
));
1620 } while (!is_end(node
));
1622 * size accessed without rcu_dereference because hash table is
1626 /* Internal sanity check: all nodes left should be bucket */
1627 for (i
= 0; i
< size
; i
++) {
1628 node
= bucket_at(ht
, i
);
1629 dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n",
1630 i
, i
, bit_reverse_ulong(node
->reverse_hash
));
1631 assert(is_bucket(node
->next
));
1634 for (order
= get_count_order_ulong(size
); (long)order
>= 0; order
--)
1635 cds_lfht_free_bucket_table(ht
, order
);
1641 * Should only be called when no more concurrent readers nor writers can
1642 * possibly access the table.
1644 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
)
1648 /* Wait for in-flight resize operations to complete */
1649 _CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
1650 cmm_smp_mb(); /* Store destroy before load resize */
1651 while (uatomic_read(&ht
->in_progress_resize
))
1652 poll(NULL
, 0, 100); /* wait for 100ms */
1653 ret
= cds_lfht_delete_bucket(ht
);
1656 free_split_items_count(ht
);
1658 *attr
= ht
->resize_attr
;
1663 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
1664 long *approx_before
,
1665 unsigned long *count
,
1666 unsigned long *removed
,
1669 struct cds_lfht_node
*node
, *next
;
1670 unsigned long nr_bucket
= 0;
1673 if (ht
->split_count
) {
1676 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1677 *approx_before
+= uatomic_read(&ht
->split_count
[i
].add
);
1678 *approx_before
-= uatomic_read(&ht
->split_count
[i
].del
);
1685 /* Count non-bucket nodes in the table */
1686 node
= bucket_at(ht
, 0);
1688 next
= rcu_dereference(node
->next
);
1689 if (is_removed(next
)) {
1690 if (!is_bucket(next
))
1694 } else if (!is_bucket(next
))
1698 node
= clear_flag(next
);
1699 } while (!is_end(node
));
1700 dbg_printf("number of bucket nodes: %lu\n", nr_bucket
);
1702 if (ht
->split_count
) {
1705 for (i
= 0; i
< split_count_mask
+ 1; i
++) {
1706 *approx_after
+= uatomic_read(&ht
->split_count
[i
].add
);
1707 *approx_after
-= uatomic_read(&ht
->split_count
[i
].del
);
1712 /* called with resize mutex held */
1714 void _do_cds_lfht_grow(struct cds_lfht
*ht
,
1715 unsigned long old_size
, unsigned long new_size
)
1717 unsigned long old_order
, new_order
;
1719 old_order
= get_count_order_ulong(old_size
);
1720 new_order
= get_count_order_ulong(new_size
);
1721 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1722 old_size
, old_order
, new_size
, new_order
);
1723 assert(new_size
> old_size
);
1724 init_table(ht
, old_order
+ 1, new_order
);
1727 /* called with resize mutex held */
1729 void _do_cds_lfht_shrink(struct cds_lfht
*ht
,
1730 unsigned long old_size
, unsigned long new_size
)
1732 unsigned long old_order
, new_order
;
1734 new_size
= max(new_size
, MIN_TABLE_SIZE
);
1735 old_order
= get_count_order_ulong(old_size
);
1736 new_order
= get_count_order_ulong(new_size
);
1737 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1738 old_size
, old_order
, new_size
, new_order
);
1739 assert(new_size
< old_size
);
1741 /* Remove and unlink all bucket nodes to remove. */
1742 fini_table(ht
, new_order
+ 1, old_order
);
1746 /* called with resize mutex held */
1748 void _do_cds_lfht_resize(struct cds_lfht
*ht
)
1750 unsigned long new_size
, old_size
;
1753 * Resize table, re-do if the target size has changed under us.
1756 assert(uatomic_read(&ht
->in_progress_resize
));
1757 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
1759 ht
->t
.resize_initiated
= 1;
1760 old_size
= ht
->t
.size
;
1761 new_size
= CMM_LOAD_SHARED(ht
->t
.resize_target
);
1762 if (old_size
< new_size
)
1763 _do_cds_lfht_grow(ht
, old_size
, new_size
);
1764 else if (old_size
> new_size
)
1765 _do_cds_lfht_shrink(ht
, old_size
, new_size
);
1766 ht
->t
.resize_initiated
= 0;
1767 /* write resize_initiated before read resize_target */
1769 } while (ht
->t
.size
!= CMM_LOAD_SHARED(ht
->t
.resize_target
));
1773 unsigned long resize_target_grow(struct cds_lfht
*ht
, unsigned long new_size
)
1775 return _uatomic_xchg_monotonic_increase(&ht
->t
.resize_target
, new_size
);
1779 void resize_target_update_count(struct cds_lfht
*ht
,
1780 unsigned long count
)
1782 count
= max(count
, MIN_TABLE_SIZE
);
1783 count
= min(count
, ht
->max_nr_buckets
);
1784 uatomic_set(&ht
->t
.resize_target
, count
);
1787 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
)
1789 resize_target_update_count(ht
, new_size
);
1790 CMM_STORE_SHARED(ht
->t
.resize_initiated
, 1);
1791 ht
->cds_lfht_rcu_thread_offline();
1792 pthread_mutex_lock(&ht
->resize_mutex
);
1793 _do_cds_lfht_resize(ht
);
1794 pthread_mutex_unlock(&ht
->resize_mutex
);
1795 ht
->cds_lfht_rcu_thread_online();
1799 void do_resize_cb(struct rcu_head
*head
)
1801 struct rcu_resize_work
*work
=
1802 caa_container_of(head
, struct rcu_resize_work
, head
);
1803 struct cds_lfht
*ht
= work
->ht
;
1805 ht
->cds_lfht_rcu_thread_offline();
1806 pthread_mutex_lock(&ht
->resize_mutex
);
1807 _do_cds_lfht_resize(ht
);
1808 pthread_mutex_unlock(&ht
->resize_mutex
);
1809 ht
->cds_lfht_rcu_thread_online();
1811 cmm_smp_mb(); /* finish resize before decrement */
1812 uatomic_dec(&ht
->in_progress_resize
);
1816 void __cds_lfht_resize_lazy_launch(struct cds_lfht
*ht
)
1818 struct rcu_resize_work
*work
;
1820 /* Store resize_target before read resize_initiated */
1822 if (!CMM_LOAD_SHARED(ht
->t
.resize_initiated
)) {
1823 uatomic_inc(&ht
->in_progress_resize
);
1824 cmm_smp_mb(); /* increment resize count before load destroy */
1825 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
)) {
1826 uatomic_dec(&ht
->in_progress_resize
);
1829 work
= malloc(sizeof(*work
));
1831 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1832 CMM_STORE_SHARED(ht
->t
.resize_initiated
, 1);
1837 void cds_lfht_resize_lazy_grow(struct cds_lfht
*ht
, unsigned long size
, int growth
)
1839 unsigned long target_size
= size
<< growth
;
1841 target_size
= min(target_size
, ht
->max_nr_buckets
);
1842 if (resize_target_grow(ht
, target_size
) >= target_size
)
1845 __cds_lfht_resize_lazy_launch(ht
);
1849 * We favor grow operations over shrink. A shrink operation never occurs
1850 * if a grow operation is queued for lazy execution. A grow operation
1851 * cancels any pending shrink lazy execution.
1854 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, unsigned long size
,
1855 unsigned long count
)
1857 if (!(ht
->flags
& CDS_LFHT_AUTO_RESIZE
))
1859 count
= max(count
, MIN_TABLE_SIZE
);
1860 count
= min(count
, ht
->max_nr_buckets
);
1862 return; /* Already the right size, no resize needed */
1863 if (count
> size
) { /* lazy grow */
1864 if (resize_target_grow(ht
, count
) >= count
)
1866 } else { /* lazy shrink */
1870 s
= uatomic_cmpxchg(&ht
->t
.resize_target
, size
, count
);
1872 break; /* no resize needed */
1874 return; /* growing is/(was just) in progress */
1876 return; /* some other thread do shrink */
1880 __cds_lfht_resize_lazy_launch(ht
);