4 * Userspace RCU library - Lock-Free Resizable RCU Hash Table
6 * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Based on the following articles:
25 * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
26 * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
27 * - Michael, M. M. High performance dynamic lock-free hash tables
28 * and list-based sets. In Proceedings of the fourteenth annual ACM
29 * symposium on Parallel algorithms and architectures, ACM Press,
32 * Some specificities of this Lock-Free Resizable RCU Hash Table
35 * - RCU read-side critical section allows readers to perform hash
36 * table lookups and use the returned objects safely by delaying
37 * memory reclaim of a grace period.
38 * - Add and remove operations are lock-free, and do not need to
39 * allocate memory. They need to be executed within RCU read-side
40 * critical section to ensure the objects they read are valid and to
41 * deal with the cmpxchg ABA problem.
42 * - add and add_unique operations are supported. add_unique checks if
43 * the node key already exists in the hash table. It ensures no key
45 * - The resize operation executes concurrently with add/remove/lookup.
46 * - Hash table nodes are contained within a split-ordered list. This
47 * list is ordered by incrementing reversed-bits-hash value.
48 * - An index of dummy nodes is kept. These dummy nodes are the hash
49 * table "buckets", and they are also chained together in the
50 * split-ordered list, which allows recursive expansion.
51 * - The resize operation for small tables only allows expanding the hash table.
52 * It is triggered automatically by detecting long chains in the add
54 * - The resize operation for larger tables (and available through an
55 * API) allows both expanding and shrinking the hash table.
56 * - Per-CPU Split-counters are used to keep track of the number of
57 * nodes within the hash table for automatic resize triggering.
58 * - Resize operation initiated by long chain detection is executed by a
59 * call_rcu thread, which keeps lock-freedom of add and remove.
60 * - Resize operations are protected by a mutex.
61 * - The removal operation is split in two parts: first, a "removed"
62 * flag is set in the next pointer within the node to remove. Then,
63 * a "garbage collection" is performed in the bucket containing the
64 * removed node (from the start of the bucket up to the removed node).
65 * All encountered nodes with "removed" flag set in their next
66 * pointers are removed from the linked-list. If the cmpxchg used for
67 * removal fails (due to concurrent garbage-collection or concurrent
68 * add), we retry from the beginning of the bucket. This ensures that
69 * the node with "removed" flag set is removed from the hash table
70 * (not visible to lookups anymore) before the RCU read-side critical
71 * section held across removal ends. Furthermore, this ensures that
72 * the node with "removed" flag set is removed from the linked-list
73 * before its memory is reclaimed. Only the thread which removal
74 * successfully set the "removed" flag (with a cmpxchg) into a node's
75 * next pointer is considered to have succeeded its removal (and thus
76 * owns the node to reclaim). Because we garbage-collect starting from
77 * an invariant node (the start-of-bucket dummy node) up to the
78 * "removed" node (or find a reverse-hash that is higher), we are sure
79 * that a successful traversal of the chain leads to a chain that is
80 * present in the linked-list (the start node is never removed) and
81 * that is does not contain the "removed" node anymore, even if
82 * concurrent delete/add operations are changing the structure of the
84 * - The add operation performs gargage collection of buckets if it
85 * encounters nodes with removed flag set in the bucket where it wants
86 * to add its new node. This ensures lock-freedom of add operation by
87 * helping the remover unlink nodes from the list rather than to wait
89 * - A RCU "order table" indexed by log2(hash index) is copied and
90 * expanded by the resize operation. This order table allows finding
91 * the "dummy node" tables.
92 * - There is one dummy node table per hash index order. The size of
93 * each dummy node table is half the number of hashes contained in
95 * - call_rcu is used to garbage-collect the old order table.
96 * - The per-order dummy node tables contain a compact version of the
97 * hash table nodes. These tables are invariant after they are
98 * populated into the hash table.
100 * A bit of ascii art explanation:
102 * Order index is the off-by-one compare to the actual power of 2 because
103 * we use index 0 to deal with the 0 special-case.
105 * This shows the nodes for a small table ordered by reversed bits:
117 * This shows the nodes in order of non-reversed bits, linked by
118 * reversed-bit order.
126 * | | | 3 011 110 <- |
128 * 3 -> | | | 4 100 001 | |
144 #include <urcu-call-rcu.h>
145 #include <urcu/arch.h>
146 #include <urcu/uatomic.h>
147 #include <urcu/jhash.h>
148 #include <urcu/compiler.h>
149 #include <urcu/rculfhash.h>
154 #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
156 #define dbg_printf(fmt, args...)
160 * Per-CPU split-counters lazily update the global counter each 1024
161 * addition/removal. It automatically keeps track of resize required.
162 * We use the bucket length as indicator for need to expand for small
163 * tables and machines lacking per-cpu data suppport.
165 #define COUNT_COMMIT_ORDER 10
166 #define CHAIN_LEN_TARGET 1
167 #define CHAIN_LEN_RESIZE_THRESHOLD 3
170 #define max(a, b) ((a) > (b) ? (a) : (b))
174 * The removed flag needs to be updated atomically with the pointer.
175 * The dummy flag does not require to be updated atomically with the
176 * pointer, but it is added as a pointer low bit flag to save space.
178 #define REMOVED_FLAG (1UL << 0)
179 #define DUMMY_FLAG (1UL << 1)
180 #define FLAGS_MASK ((1UL << 2) - 1)
182 struct ht_items_count
{
183 unsigned long add
, remove
;
184 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
187 struct rcu_head head
;
188 struct _cds_lfht_node nodes
[0];
192 unsigned long size
; /* always a power of 2 */
193 unsigned long resize_target
;
194 int resize_initiated
;
195 struct rcu_head head
;
196 struct rcu_level
*tbl
[0];
200 struct rcu_table
*t
; /* shared */
201 cds_lfht_hash_fct hash_fct
;
202 cds_lfht_compare_fct compare_fct
;
203 unsigned long hash_seed
;
204 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
205 unsigned int in_progress_resize
, in_progress_destroy
;
206 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
207 void (*func
)(struct rcu_head
*head
));
208 void (*cds_lfht_synchronize_rcu
)(void);
209 unsigned long count
; /* global approximate item count */
210 struct ht_items_count
*percpu_count
; /* per-cpu item count */
213 struct rcu_resize_work
{
214 struct rcu_head head
;
219 * Algorithm to reverse bits in a word by lookup table, extended to
222 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
223 * Originally from Public Domain.
226 static const uint8_t BitReverseTable256
[256] =
228 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
229 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
230 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
231 R6(0), R6(2), R6(1), R6(3)
238 uint8_t bit_reverse_u8(uint8_t v
)
240 return BitReverseTable256
[v
];
243 static __attribute__((unused
))
244 uint32_t bit_reverse_u32(uint32_t v
)
246 return ((uint32_t) bit_reverse_u8(v
) << 24) |
247 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
248 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
249 ((uint32_t) bit_reverse_u8(v
>> 24));
252 static __attribute__((unused
))
253 uint64_t bit_reverse_u64(uint64_t v
)
255 return ((uint64_t) bit_reverse_u8(v
) << 56) |
256 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
257 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
258 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
259 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
260 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
261 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
262 ((uint64_t) bit_reverse_u8(v
>> 56));
266 unsigned long bit_reverse_ulong(unsigned long v
)
268 #if (CAA_BITS_PER_LONG == 32)
269 return bit_reverse_u32(v
);
271 return bit_reverse_u64(v
);
276 * fls: returns the position of the most significant bit.
277 * Returns 0 if no bit is set, else returns the position of the most
278 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
280 #if defined(__i386) || defined(__x86_64)
282 unsigned int fls_u32(uint32_t x
)
290 : "=r" (r
) : "rm" (x
));
296 #if defined(__x86_64)
298 unsigned int fls_u64(uint64_t x
)
306 : "=r" (r
) : "rm" (x
));
313 static __attribute__((unused
))
314 unsigned int fls_u64(uint64_t x
)
321 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
325 if (!(x
& 0xFFFF000000000000ULL
)) {
329 if (!(x
& 0xFF00000000000000ULL
)) {
333 if (!(x
& 0xF000000000000000ULL
)) {
337 if (!(x
& 0xC000000000000000ULL
)) {
341 if (!(x
& 0x8000000000000000ULL
)) {
350 static __attribute__((unused
))
351 unsigned int fls_u32(uint32_t x
)
357 if (!(x
& 0xFFFF0000U
)) {
361 if (!(x
& 0xFF000000U
)) {
365 if (!(x
& 0xF0000000U
)) {
369 if (!(x
& 0xC0000000U
)) {
373 if (!(x
& 0x80000000U
)) {
381 unsigned int fls_ulong(unsigned long x
)
383 #if (CAA_BITS_PER_lONG == 32)
390 int get_count_order_u32(uint32_t x
)
394 order
= fls_u32(x
) - 1;
400 int get_count_order_ulong(unsigned long x
)
404 order
= fls_ulong(x
) - 1;
411 void cds_lfht_resize_lazy(struct cds_lfht
*ht
, struct rcu_table
*t
, int growth
);
414 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
415 * available, then we support hash table item accounting.
416 * In the unfortunate event the number of CPUs reported would be
417 * inaccurate, we use modulo arithmetic on the number of CPUs we got.
419 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
422 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, struct rcu_table
*t
,
423 unsigned long count
);
425 static long nr_cpus_mask
= -1;
428 struct ht_items_count
*alloc_per_cpu_items_count(void)
430 struct ht_items_count
*count
;
432 switch (nr_cpus_mask
) {
439 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
445 * round up number of CPUs to next power of two, so we
446 * can use & for modulo.
448 maxcpus
= 1UL << get_count_order_ulong(maxcpus
);
449 nr_cpus_mask
= maxcpus
- 1;
453 return calloc(nr_cpus_mask
+ 1, sizeof(*count
));
458 void free_per_cpu_items_count(struct ht_items_count
*count
)
468 assert(nr_cpus_mask
>= 0);
469 cpu
= sched_getcpu();
470 if (unlikely(cpu
< 0))
473 return cpu
& nr_cpus_mask
;
477 void ht_count_add(struct cds_lfht
*ht
, struct rcu_table
*t
)
479 unsigned long percpu_count
;
482 if (unlikely(!ht
->percpu_count
))
485 if (unlikely(cpu
< 0))
487 percpu_count
= uatomic_add_return(&ht
->percpu_count
[cpu
].add
, 1);
488 if (unlikely(!(percpu_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
491 dbg_printf("add percpu %lu\n", percpu_count
);
492 count
= uatomic_add_return(&ht
->count
,
493 1UL << COUNT_COMMIT_ORDER
);
495 if (!(count
& (count
- 1))) {
496 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
)
499 dbg_printf("add set global %lu\n", count
);
500 cds_lfht_resize_lazy_count(ht
, t
,
501 count
>> (CHAIN_LEN_TARGET
- 1));
507 void ht_count_remove(struct cds_lfht
*ht
, struct rcu_table
*t
)
509 unsigned long percpu_count
;
512 if (unlikely(!ht
->percpu_count
))
515 if (unlikely(cpu
< 0))
517 percpu_count
= uatomic_add_return(&ht
->percpu_count
[cpu
].remove
, -1);
518 if (unlikely(!(percpu_count
& ((1UL << COUNT_COMMIT_ORDER
) - 1)))) {
521 dbg_printf("remove percpu %lu\n", percpu_count
);
522 count
= uatomic_add_return(&ht
->count
,
523 -(1UL << COUNT_COMMIT_ORDER
));
525 if (!(count
& (count
- 1))) {
526 if ((count
>> CHAIN_LEN_RESIZE_THRESHOLD
)
529 dbg_printf("remove set global %lu\n", count
);
530 cds_lfht_resize_lazy_count(ht
, t
,
531 count
>> (CHAIN_LEN_TARGET
- 1));
536 #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
538 static const long nr_cpus_mask
= -1;
541 struct ht_items_count
*alloc_per_cpu_items_count(void)
547 void free_per_cpu_items_count(struct ht_items_count
*count
)
552 void ht_count_add(struct cds_lfht
*ht
, struct rcu_table
*t
)
557 void ht_count_remove(struct cds_lfht
*ht
, struct rcu_table
*t
)
561 #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
565 void check_resize(struct cds_lfht
*ht
, struct rcu_table
*t
,
570 count
= uatomic_read(&ht
->count
);
572 * Use bucket-local length for small table expand and for
573 * environments lacking per-cpu data support.
575 if (count
>= (1UL << COUNT_COMMIT_ORDER
))
578 dbg_printf("WARNING: large chain length: %u.\n",
580 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
581 cds_lfht_resize_lazy(ht
, t
,
582 get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
586 struct cds_lfht_node
*clear_flag(struct cds_lfht_node
*node
)
588 return (struct cds_lfht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
592 int is_removed(struct cds_lfht_node
*node
)
594 return ((unsigned long) node
) & REMOVED_FLAG
;
598 struct cds_lfht_node
*flag_removed(struct cds_lfht_node
*node
)
600 return (struct cds_lfht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
604 int is_dummy(struct cds_lfht_node
*node
)
606 return ((unsigned long) node
) & DUMMY_FLAG
;
610 struct cds_lfht_node
*flag_dummy(struct cds_lfht_node
*node
)
612 return (struct cds_lfht_node
*) (((unsigned long) node
) | DUMMY_FLAG
);
616 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
618 unsigned long old1
, old2
;
620 old1
= uatomic_read(ptr
);
625 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
630 void cds_lfht_free_table_cb(struct rcu_head
*head
)
632 struct rcu_table
*t
=
633 caa_container_of(head
, struct rcu_table
, head
);
638 void cds_lfht_free_level(struct rcu_head
*head
)
640 struct rcu_level
*l
=
641 caa_container_of(head
, struct rcu_level
, head
);
646 * Remove all logically deleted nodes from a bucket up to a certain node key.
649 void _cds_lfht_gc_bucket(struct cds_lfht_node
*dummy
, struct cds_lfht_node
*node
)
651 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_next
;
655 /* We can always skip the dummy node initially */
656 iter
= rcu_dereference(iter_prev
->p
.next
);
657 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
659 if (unlikely(!clear_flag(iter
)))
661 if (likely(clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
))
663 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
664 if (likely(is_removed(next
)))
666 iter_prev
= clear_flag(iter
);
669 assert(!is_removed(iter
));
671 new_next
= flag_dummy(clear_flag(next
));
673 new_next
= clear_flag(next
);
674 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
679 struct cds_lfht_node
*_cds_lfht_add(struct cds_lfht
*ht
, struct rcu_table
*t
,
680 struct cds_lfht_node
*node
, int unique
, int dummy
)
682 struct cds_lfht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
684 struct _cds_lfht_node
*lookup
;
685 unsigned long hash
, index
, order
;
689 node
->p
.next
= flag_dummy(NULL
);
690 return node
; /* Initial first add (head) */
692 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
694 uint32_t chain_len
= 0;
697 * iter_prev points to the non-removed node prior to the
700 index
= hash
& (t
->size
- 1);
701 order
= get_count_order_ulong(index
+ 1);
702 lookup
= &t
->tbl
[order
]->nodes
[index
& ((!order
? 0 : (1UL << (order
- 1))) - 1)];
703 iter_prev
= (struct cds_lfht_node
*) lookup
;
704 /* We can always skip the dummy node initially */
705 iter
= rcu_dereference(iter_prev
->p
.next
);
706 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
708 if (unlikely(!clear_flag(iter
)))
710 if (likely(clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
))
712 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
713 if (unlikely(is_removed(next
)))
717 && !ht
->compare_fct(node
->key
, node
->key_len
,
718 clear_flag(iter
)->key
,
719 clear_flag(iter
)->key_len
))
720 return clear_flag(iter
);
721 /* Only account for identical reverse hash once */
722 if (iter_prev
->p
.reverse_hash
!= clear_flag(iter
)->p
.reverse_hash
724 check_resize(ht
, t
, ++chain_len
);
725 iter_prev
= clear_flag(iter
);
729 assert(node
!= clear_flag(iter
));
730 assert(!is_removed(iter_prev
));
731 assert(iter_prev
!= node
);
733 node
->p
.next
= clear_flag(iter
);
735 node
->p
.next
= flag_dummy(clear_flag(iter
));
737 new_node
= flag_dummy(node
);
740 if (uatomic_cmpxchg(&iter_prev
->p
.next
, iter
,
742 continue; /* retry */
746 assert(!is_removed(iter
));
748 new_next
= flag_dummy(clear_flag(next
));
750 new_next
= clear_flag(next
);
751 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
755 /* Garbage collect logically removed nodes in the bucket */
756 index
= hash
& (t
->size
- 1);
757 order
= get_count_order_ulong(index
+ 1);
758 lookup
= &t
->tbl
[order
]->nodes
[index
& (!order
? 0 : ((1UL << (order
- 1)) - 1))];
759 dummy_node
= (struct cds_lfht_node
*) lookup
;
760 _cds_lfht_gc_bucket(dummy_node
, node
);
765 int _cds_lfht_remove(struct cds_lfht
*ht
, struct rcu_table
*t
,
766 struct cds_lfht_node
*node
, int dummy_removal
)
768 struct cds_lfht_node
*dummy
, *next
, *old
;
769 struct _cds_lfht_node
*lookup
;
771 unsigned long hash
, index
, order
;
773 /* logically delete the node */
774 old
= rcu_dereference(node
->p
.next
);
777 if (unlikely(is_removed(next
)))
780 assert(is_dummy(next
));
782 assert(!is_dummy(next
));
783 old
= uatomic_cmpxchg(&node
->p
.next
, next
,
785 } while (old
!= next
);
787 /* We performed the (logical) deletion. */
791 node
= clear_flag(node
);
794 * Ensure that the node is not visible to readers anymore: lookup for
795 * the node, and remove it (along with any other logically removed node)
798 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
800 index
= hash
& (t
->size
- 1);
801 order
= get_count_order_ulong(index
+ 1);
802 lookup
= &t
->tbl
[order
]->nodes
[index
& (!order
? 0 : ((1UL << (order
- 1)) - 1))];
803 dummy
= (struct cds_lfht_node
*) lookup
;
804 _cds_lfht_gc_bucket(dummy
, node
);
807 * Only the flagging action indicated that we (and no other)
808 * removed the node from the hash.
811 assert(is_removed(rcu_dereference(node
->p
.next
)));
818 void init_table(struct cds_lfht
*ht
, struct rcu_table
*t
,
819 unsigned long first_order
, unsigned long len_order
)
821 unsigned long i
, end_order
;
823 dbg_printf("init table: first_order %lu end_order %lu\n",
824 first_order
, first_order
+ len_order
);
825 end_order
= first_order
+ len_order
;
826 t
->size
= !first_order
? 0 : (1UL << (first_order
- 1));
827 for (i
= first_order
; i
< end_order
; i
++) {
828 unsigned long j
, len
;
830 len
= !i
? 1 : 1UL << (i
- 1);
831 dbg_printf("init order %lu len: %lu\n", i
, len
);
832 t
->tbl
[i
] = calloc(1, sizeof(struct rcu_level
)
833 + (len
* sizeof(struct _cds_lfht_node
)));
834 for (j
= 0; j
< len
; j
++) {
835 struct cds_lfht_node
*new_node
=
836 (struct cds_lfht_node
*) &t
->tbl
[i
]->nodes
[j
];
838 dbg_printf("init entry: i %lu j %lu hash %lu\n",
839 i
, j
, !i
? 0 : (1UL << (i
- 1)) + j
);
840 new_node
->p
.reverse_hash
=
841 bit_reverse_ulong(!i
? 0 : (1UL << (i
- 1)) + j
);
842 (void) _cds_lfht_add(ht
, t
, new_node
, 0, 1);
843 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
846 /* Update table size */
847 t
->size
= !i
? 1 : (1UL << i
);
848 dbg_printf("init new size: %lu\n", t
->size
);
849 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
852 t
->resize_target
= t
->size
;
853 t
->resize_initiated
= 0;
857 void fini_table(struct cds_lfht
*ht
, struct rcu_table
*t
,
858 unsigned long first_order
, unsigned long len_order
)
862 dbg_printf("fini table: first_order %lu end_order %lu\n",
863 first_order
, first_order
+ len_order
);
864 end_order
= first_order
+ len_order
;
865 assert(first_order
> 0);
866 assert(t
->size
== (1UL << (end_order
- 1)));
867 for (i
= end_order
- 1; i
>= first_order
; i
--) {
868 unsigned long j
, len
;
870 len
= !i
? 1 : 1UL << (i
- 1);
871 dbg_printf("fini order %lu len: %lu\n", i
, len
);
872 /* Update table size */
873 t
->size
= 1UL << (i
- 1);
875 for (j
= 0; j
< len
; j
++) {
876 struct cds_lfht_node
*new_node
=
877 (struct cds_lfht_node
*) &t
->tbl
[i
]->nodes
[j
];
879 dbg_printf("fini entry: i %lu j %lu hash %lu\n",
880 i
, j
, !i
? 0 : (1UL << (i
- 1)) + j
);
881 new_node
->p
.reverse_hash
=
882 bit_reverse_ulong(!i
? 0 : (1UL << (i
- 1)) + j
);
883 (void) _cds_lfht_remove(ht
, t
, new_node
, 1);
884 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
887 ht
->cds_lfht_call_rcu(&t
->tbl
[i
]->head
, cds_lfht_free_level
);
888 dbg_printf("fini new size: %lu\n", t
->size
);
889 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
892 t
->resize_target
= t
->size
;
893 t
->resize_initiated
= 0;
896 struct cds_lfht
*cds_lfht_new(cds_lfht_hash_fct hash_fct
,
897 cds_lfht_compare_fct compare_fct
,
898 unsigned long hash_seed
,
899 unsigned long init_size
,
900 void (*cds_lfht_call_rcu
)(struct rcu_head
*head
,
901 void (*func
)(struct rcu_head
*head
)),
902 void (*cds_lfht_synchronize_rcu
)(void))
907 /* init_size must be power of two */
908 if (init_size
&& (init_size
& (init_size
- 1)))
910 ht
= calloc(1, sizeof(struct cds_lfht
));
911 ht
->hash_fct
= hash_fct
;
912 ht
->compare_fct
= compare_fct
;
913 ht
->hash_seed
= hash_seed
;
914 ht
->cds_lfht_call_rcu
= cds_lfht_call_rcu
;
915 ht
->cds_lfht_synchronize_rcu
= cds_lfht_synchronize_rcu
;
916 ht
->in_progress_resize
= 0;
917 ht
->percpu_count
= alloc_per_cpu_items_count();
918 /* this mutex should not nest in read-side C.S. */
919 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
920 order
= get_count_order_ulong(max(init_size
, 1)) + 1;
921 ht
->t
= calloc(1, sizeof(struct cds_lfht
)
922 + (order
* sizeof(struct rcu_level
*)));
924 pthread_mutex_lock(&ht
->resize_mutex
);
925 init_table(ht
, ht
->t
, 0, order
);
926 pthread_mutex_unlock(&ht
->resize_mutex
);
930 struct cds_lfht_node
*cds_lfht_lookup(struct cds_lfht
*ht
, void *key
, size_t key_len
)
933 struct cds_lfht_node
*node
, *next
;
934 struct _cds_lfht_node
*lookup
;
935 unsigned long hash
, reverse_hash
, index
, order
;
937 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
938 reverse_hash
= bit_reverse_ulong(hash
);
940 t
= rcu_dereference(ht
->t
);
941 index
= hash
& (t
->size
- 1);
942 order
= get_count_order_ulong(index
+ 1);
943 lookup
= &t
->tbl
[order
]->nodes
[index
& (!order
? 0 : ((1UL << (order
- 1))) - 1)];
944 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
945 hash
, index
, order
, index
& (!order
? 0 : ((1UL << (order
- 1)) - 1)));
946 node
= (struct cds_lfht_node
*) lookup
;
950 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
954 next
= rcu_dereference(node
->p
.next
);
955 if (likely(!is_removed(next
))
957 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
960 node
= clear_flag(next
);
962 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
966 struct cds_lfht_node
*cds_lfht_next(struct cds_lfht
*ht
,
967 struct cds_lfht_node
*node
)
969 struct cds_lfht_node
*next
;
970 unsigned long reverse_hash
;
974 reverse_hash
= node
->p
.reverse_hash
;
976 key_len
= node
->key_len
;
977 next
= rcu_dereference(node
->p
.next
);
978 node
= clear_flag(next
);
983 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
987 next
= rcu_dereference(node
->p
.next
);
988 if (likely(!is_removed(next
))
990 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
993 node
= clear_flag(next
);
995 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
999 void cds_lfht_add(struct cds_lfht
*ht
, struct cds_lfht_node
*node
)
1001 struct rcu_table
*t
;
1004 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
1005 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1007 t
= rcu_dereference(ht
->t
);
1008 (void) _cds_lfht_add(ht
, t
, node
, 0, 0);
1009 ht_count_add(ht
, t
);
1012 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
1013 struct cds_lfht_node
*node
)
1015 struct rcu_table
*t
;
1017 struct cds_lfht_node
*ret
;
1019 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
1020 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
1022 t
= rcu_dereference(ht
->t
);
1023 ret
= _cds_lfht_add(ht
, t
, node
, 1, 0);
1025 ht_count_add(ht
, t
);
1029 int cds_lfht_remove(struct cds_lfht
*ht
, struct cds_lfht_node
*node
)
1031 struct rcu_table
*t
;
1034 t
= rcu_dereference(ht
->t
);
1035 ret
= _cds_lfht_remove(ht
, t
, node
, 0);
1037 ht_count_remove(ht
, t
);
1042 int cds_lfht_delete_dummy(struct cds_lfht
*ht
)
1044 struct rcu_table
*t
;
1045 struct cds_lfht_node
*node
;
1046 struct _cds_lfht_node
*lookup
;
1047 unsigned long order
, i
;
1050 /* Check that the table is empty */
1051 lookup
= &t
->tbl
[0]->nodes
[0];
1052 node
= (struct cds_lfht_node
*) lookup
;
1054 node
= clear_flag(node
)->p
.next
;
1055 if (!is_dummy(node
))
1057 assert(!is_removed(node
));
1058 } while (clear_flag(node
));
1059 /* Internal sanity check: all nodes left should be dummy */
1060 for (order
= 0; order
< get_count_order_ulong(t
->size
) + 1; order
++) {
1063 len
= !order
? 1 : 1UL << (order
- 1);
1064 for (i
= 0; i
< len
; i
++) {
1065 dbg_printf("delete order %lu i %lu hash %lu\n",
1067 bit_reverse_ulong(t
->tbl
[order
]->nodes
[i
].reverse_hash
));
1068 assert(is_dummy(t
->tbl
[order
]->nodes
[i
].next
));
1070 free(t
->tbl
[order
]);
1076 * Should only be called when no more concurrent readers nor writers can
1077 * possibly access the table.
1079 int cds_lfht_destroy(struct cds_lfht
*ht
)
1083 /* Wait for in-flight resize operations to complete */
1084 CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
1085 while (uatomic_read(&ht
->in_progress_resize
))
1086 poll(NULL
, 0, 100); /* wait for 100ms */
1087 ret
= cds_lfht_delete_dummy(ht
);
1091 free_per_cpu_items_count(ht
->percpu_count
);
1096 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
1097 unsigned long *count
,
1098 unsigned long *removed
)
1100 struct rcu_table
*t
;
1101 struct cds_lfht_node
*node
, *next
;
1102 struct _cds_lfht_node
*lookup
;
1103 unsigned long nr_dummy
= 0;
1108 t
= rcu_dereference(ht
->t
);
1109 /* Count non-dummy nodes in the table */
1110 lookup
= &t
->tbl
[0]->nodes
[0];
1111 node
= (struct cds_lfht_node
*) lookup
;
1113 next
= rcu_dereference(node
->p
.next
);
1114 if (is_removed(next
)) {
1115 assert(!is_dummy(next
));
1117 } else if (!is_dummy(next
))
1121 node
= clear_flag(next
);
1123 dbg_printf("number of dummy nodes: %lu\n", nr_dummy
);
1126 /* called with resize mutex held */
1128 void _do_cds_lfht_grow(struct cds_lfht
*ht
, struct rcu_table
*old_t
,
1129 unsigned long old_size
, unsigned long new_size
)
1131 unsigned long old_order
, new_order
;
1132 struct rcu_table
*new_t
;
1134 old_order
= get_count_order_ulong(old_size
) + 1;
1135 new_order
= get_count_order_ulong(new_size
) + 1;
1136 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1137 old_size
, old_order
, new_size
, new_order
);
1138 new_t
= malloc(sizeof(struct cds_lfht
)
1139 + (new_order
* sizeof(struct rcu_level
*)));
1140 assert(new_size
> old_size
);
1141 memcpy(&new_t
->tbl
, &old_t
->tbl
,
1142 old_order
* sizeof(struct rcu_level
*));
1143 init_table(ht
, new_t
, old_order
, new_order
- old_order
);
1144 /* Changing table and size atomically wrt lookups */
1145 rcu_assign_pointer(ht
->t
, new_t
);
1146 ht
->cds_lfht_call_rcu(&old_t
->head
, cds_lfht_free_table_cb
);
1149 /* called with resize mutex held */
1151 void _do_cds_lfht_shrink(struct cds_lfht
*ht
, struct rcu_table
*old_t
,
1152 unsigned long old_size
, unsigned long new_size
)
1154 unsigned long old_order
, new_order
;
1155 struct rcu_table
*new_t
;
1157 new_size
= max(new_size
, 1);
1158 old_order
= get_count_order_ulong(old_size
) + 1;
1159 new_order
= get_count_order_ulong(new_size
) + 1;
1160 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1161 old_size
, old_order
, new_size
, new_order
);
1162 new_t
= malloc(sizeof(struct cds_lfht
)
1163 + (new_order
* sizeof(struct rcu_level
*)));
1164 assert(new_size
< old_size
);
1165 memcpy(&new_t
->tbl
, &old_t
->tbl
,
1166 new_order
* sizeof(struct rcu_level
*));
1167 new_t
->size
= !new_order
? 1 : (1UL << (new_order
- 1));
1168 new_t
->resize_target
= new_t
->size
;
1169 new_t
->resize_initiated
= 0;
1171 /* Changing table and size atomically wrt lookups */
1172 rcu_assign_pointer(ht
->t
, new_t
);
1175 * We need to wait for all reader threads to reach Q.S. (and
1176 * thus use the new table for lookups) before we can start
1177 * releasing the old dummy nodes.
1179 ht
->cds_lfht_synchronize_rcu();
1181 /* Unlink and remove all now-unused dummy node pointers. */
1182 fini_table(ht
, old_t
, new_order
, old_order
- new_order
);
1183 ht
->cds_lfht_call_rcu(&old_t
->head
, cds_lfht_free_table_cb
);
1187 /* called with resize mutex held */
1189 void _do_cds_lfht_resize(struct cds_lfht
*ht
)
1191 unsigned long new_size
, old_size
;
1192 struct rcu_table
*old_t
;
1195 old_size
= old_t
->size
;
1196 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
1197 if (old_size
< new_size
)
1198 _do_cds_lfht_grow(ht
, old_t
, old_size
, new_size
);
1199 else if (old_size
> new_size
)
1200 _do_cds_lfht_shrink(ht
, old_t
, old_size
, new_size
);
1202 CMM_STORE_SHARED(old_t
->resize_initiated
, 0);
1206 unsigned long resize_target_update(struct rcu_table
*t
,
1209 return _uatomic_max(&t
->resize_target
,
1210 t
->size
<< growth_order
);
1214 unsigned long resize_target_update_count(struct rcu_table
*t
,
1215 unsigned long count
)
1217 count
= max(count
, 1);
1218 return uatomic_set(&t
->resize_target
, count
);
1221 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
)
1223 struct rcu_table
*t
= rcu_dereference(ht
->t
);
1224 unsigned long target_size
;
1226 target_size
= resize_target_update_count(t
, new_size
);
1227 CMM_STORE_SHARED(t
->resize_initiated
, 1);
1228 pthread_mutex_lock(&ht
->resize_mutex
);
1229 _do_cds_lfht_resize(ht
);
1230 pthread_mutex_unlock(&ht
->resize_mutex
);
1234 void do_resize_cb(struct rcu_head
*head
)
1236 struct rcu_resize_work
*work
=
1237 caa_container_of(head
, struct rcu_resize_work
, head
);
1238 struct cds_lfht
*ht
= work
->ht
;
1240 pthread_mutex_lock(&ht
->resize_mutex
);
1241 _do_cds_lfht_resize(ht
);
1242 pthread_mutex_unlock(&ht
->resize_mutex
);
1244 cmm_smp_mb(); /* finish resize before decrement */
1245 uatomic_dec(&ht
->in_progress_resize
);
1249 void cds_lfht_resize_lazy(struct cds_lfht
*ht
, struct rcu_table
*t
, int growth
)
1251 struct rcu_resize_work
*work
;
1252 unsigned long target_size
;
1254 target_size
= resize_target_update(t
, growth
);
1255 if (!CMM_LOAD_SHARED(t
->resize_initiated
) && t
->size
< target_size
) {
1256 uatomic_inc(&ht
->in_progress_resize
);
1257 cmm_smp_mb(); /* increment resize count before calling it */
1258 work
= malloc(sizeof(*work
));
1260 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1261 CMM_STORE_SHARED(t
->resize_initiated
, 1);
1265 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
1268 void cds_lfht_resize_lazy_count(struct cds_lfht
*ht
, struct rcu_table
*t
,
1269 unsigned long count
)
1271 struct rcu_resize_work
*work
;
1272 unsigned long target_size
;
1274 target_size
= resize_target_update_count(t
, count
);
1275 if (!CMM_LOAD_SHARED(t
->resize_initiated
)) {
1276 uatomic_inc(&ht
->in_progress_resize
);
1277 cmm_smp_mb(); /* increment resize count before calling it */
1278 work
= malloc(sizeof(*work
));
1280 ht
->cds_lfht_call_rcu(&work
->head
, do_resize_cb
);
1281 CMM_STORE_SHARED(t
->resize_initiated
, 1);