4 * Userspace RCU library - Lock-Free Expandable 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 Expandable 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 only allows expanding the hash table.
52 * It is triggered either through an API call or automatically by
53 * detecting long chains in the add operation.
54 * - Resize operation initiated by long chain detection is executed by a
55 * call_rcu thread, which keeps lock-freedom of add and remove.
56 * - Resize operations are protected by a mutex.
57 * - The removal operation is split in two parts: first, a "removed"
58 * flag is set in the next pointer within the node to remove. Then,
59 * a "garbage collection" is performed in the bucket containing the
60 * removed node (from the start of the bucket up to the removed node).
61 * All encountered nodes with "removed" flag set in their next
62 * pointers are removed from the linked-list. If the cmpxchg used for
63 * removal fails (due to concurrent garbage-collection or concurrent
64 * add), we retry from the beginning of the bucket. This ensures that
65 * the node with "removed" flag set is removed from the hash table
66 * (not visible to lookups anymore) before the RCU read-side critical
67 * section held across removal ends. Furthermore, this ensures that
68 * the node with "removed" flag set is removed from the linked-list
69 * before its memory is reclaimed. Only the thread which removal
70 * successfully set the "removed" flag (with a cmpxchg) into a node's
71 * next pointer is considered to have succeeded its removal (and thus
72 * owns the node to reclaim). Because we garbage-collect starting from
73 * an invariant node (the start-of-bucket dummy node) up to the
74 * "removed" node (or find a reverse-hash that is higher), we are sure
75 * that a successful traversal of the chain leads to a chain that is
76 * present in the linked-list (the start node is never removed) and
77 * that is does not contain the "removed" node anymore, even if
78 * concurrent delete/add operations are changing the structure of the
80 * - A RCU "order table" indexed by log2(hash index) is copied and
81 * expanded by the resize operation. This order table allows finding
82 * the "dummy node" tables.
83 * - There is one dummy node table per hash index order. The size of
84 * each dummy node table is half the number of hashes contained in
86 * - call_rcu is used to garbage-collect the old order table.
87 * - The per-order dummy node tables contain a compact version of the
88 * hash table nodes. These tables are invariant after they are
89 * populated into the hash table.
101 #include <urcu-call-rcu.h>
102 #include <urcu/arch.h>
103 #include <urcu/uatomic.h>
104 #include <urcu/jhash.h>
105 #include <urcu/compiler.h>
106 #include <urcu/rculfhash.h>
111 #define dbg_printf(fmt, args...) printf(fmt, ## args)
113 #define dbg_printf(fmt, args...)
116 #define CHAIN_LEN_TARGET 4
117 #define CHAIN_LEN_RESIZE_THRESHOLD 8
120 #define max(a, b) ((a) > (b) ? (a) : (b))
124 * The removed flag needs to be updated atomically with the pointer.
125 * The dummy flag does not require to be updated atomically with the
126 * pointer, but it is added as a pointer low bit flag to save space.
128 #define REMOVED_FLAG (1UL << 0)
129 #define DUMMY_FLAG (1UL << 1)
130 #define FLAGS_MASK ((1UL << 2) - 1)
133 unsigned long size
; /* always a power of 2 */
134 unsigned long resize_target
;
135 int resize_initiated
;
136 struct rcu_head head
;
137 struct _rcu_ht_node
*tbl
[0];
141 struct rcu_table
*t
; /* shared */
142 ht_hash_fct hash_fct
;
143 ht_compare_fct compare_fct
;
144 unsigned long hash_seed
;
145 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
146 unsigned int in_progress_resize
, in_progress_destroy
;
147 void (*ht_call_rcu
)(struct rcu_head
*head
,
148 void (*func
)(struct rcu_head
*head
));
151 struct rcu_resize_work
{
152 struct rcu_head head
;
157 * Algorithm to reverse bits in a word by lookup table, extended to
160 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
161 * Originally from Public Domain.
164 static const uint8_t BitReverseTable256
[256] =
166 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
167 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
168 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
169 R6(0), R6(2), R6(1), R6(3)
176 uint8_t bit_reverse_u8(uint8_t v
)
178 return BitReverseTable256
[v
];
181 static __attribute__((unused
))
182 uint32_t bit_reverse_u32(uint32_t v
)
184 return ((uint32_t) bit_reverse_u8(v
) << 24) |
185 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
186 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
187 ((uint32_t) bit_reverse_u8(v
>> 24));
190 static __attribute__((unused
))
191 uint64_t bit_reverse_u64(uint64_t v
)
193 return ((uint64_t) bit_reverse_u8(v
) << 56) |
194 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
195 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
196 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
197 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
198 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
199 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
200 ((uint64_t) bit_reverse_u8(v
>> 56));
204 unsigned long bit_reverse_ulong(unsigned long v
)
206 #if (CAA_BITS_PER_LONG == 32)
207 return bit_reverse_u32(v
);
209 return bit_reverse_u64(v
);
214 * fls: returns the position of the most significant bit.
215 * Returns 0 if no bit is set, else returns the position of the most
216 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
218 #if defined(__i386) || defined(__x86_64)
220 unsigned int fls_u32(uint32_t x
)
228 : "=r" (r
) : "rm" (x
));
234 #if defined(__x86_64)
236 unsigned int fls_u64(uint64_t x
)
244 : "=r" (r
) : "rm" (x
));
251 static __attribute__((unused
))
252 unsigned int fls_u64(uint64_t x
)
259 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
263 if (!(x
& 0xFFFF000000000000ULL
)) {
267 if (!(x
& 0xFF00000000000000ULL
)) {
271 if (!(x
& 0xF000000000000000ULL
)) {
275 if (!(x
& 0xC000000000000000ULL
)) {
279 if (!(x
& 0x8000000000000000ULL
)) {
288 static __attribute__((unused
))
289 unsigned int fls_u32(uint32_t x
)
295 if (!(x
& 0xFFFF0000U
)) {
299 if (!(x
& 0xFF000000U
)) {
303 if (!(x
& 0xF0000000U
)) {
307 if (!(x
& 0xC0000000U
)) {
311 if (!(x
& 0x80000000U
)) {
319 unsigned int fls_ulong(unsigned long x
)
321 #if (CAA_BITS_PER_lONG == 32)
328 int get_count_order_u32(uint32_t x
)
332 order
= fls_u32(x
) - 1;
338 int get_count_order_ulong(unsigned long x
)
342 order
= fls_ulong(x
) - 1;
349 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
);
352 void check_resize(struct rcu_ht
*ht
, struct rcu_table
*t
,
356 dbg_printf("rculfhash: WARNING: large chain length: %u.\n",
358 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
359 ht_resize_lazy(ht
, t
,
360 get_count_order_u32(chain_len
- (CHAIN_LEN_TARGET
- 1)));
364 struct rcu_ht_node
*clear_flag(struct rcu_ht_node
*node
)
366 return (struct rcu_ht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
370 int is_removed(struct rcu_ht_node
*node
)
372 return ((unsigned long) node
) & REMOVED_FLAG
;
376 struct rcu_ht_node
*flag_removed(struct rcu_ht_node
*node
)
378 return (struct rcu_ht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
382 int is_dummy(struct rcu_ht_node
*node
)
384 return ((unsigned long) node
) & DUMMY_FLAG
;
388 struct rcu_ht_node
*flag_dummy(struct rcu_ht_node
*node
)
390 return (struct rcu_ht_node
*) (((unsigned long) node
) | DUMMY_FLAG
);
394 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
396 unsigned long old1
, old2
;
398 old1
= uatomic_read(ptr
);
403 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
408 * Remove all logically deleted nodes from a bucket up to a certain node key.
411 void _ht_gc_bucket(struct rcu_ht_node
*dummy
, struct rcu_ht_node
*node
)
413 struct rcu_ht_node
*iter_prev
, *iter
, *next
, *new_next
;
417 /* We can always skip the dummy node initially */
418 iter
= rcu_dereference(iter_prev
->p
.next
);
419 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
421 if (unlikely(!clear_flag(iter
)))
423 if (clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
)
425 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
426 if (is_removed(next
))
428 iter_prev
= clear_flag(iter
);
431 assert(!is_removed(iter
));
433 new_next
= flag_dummy(clear_flag(next
));
435 new_next
= clear_flag(next
);
436 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
441 struct rcu_ht_node
*_ht_add(struct rcu_ht
*ht
, struct rcu_table
*t
,
442 struct rcu_ht_node
*node
, int unique
, int dummy
)
444 struct rcu_ht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
446 struct _rcu_ht_node
*lookup
;
447 unsigned long hash
, index
, order
;
451 node
->p
.next
= flag_dummy(NULL
);
452 return node
; /* Initial first add (head) */
454 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
456 uint32_t chain_len
= 0;
459 * iter_prev points to the non-removed node prior to the
462 index
= hash
& (t
->size
- 1);
463 order
= get_count_order_ulong(index
+ 1);
464 lookup
= &t
->tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
465 iter_prev
= (struct rcu_ht_node
*) lookup
;
466 /* We can always skip the dummy node initially */
467 iter
= rcu_dereference(iter_prev
->p
.next
);
468 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
470 if (unlikely(!clear_flag(iter
)))
472 if (clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
)
474 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
475 if (is_removed(next
))
479 && !ht
->compare_fct(node
->key
, node
->key_len
,
480 clear_flag(iter
)->key
,
481 clear_flag(iter
)->key_len
))
482 return clear_flag(iter
);
483 /* Only account for identical reverse hash once */
484 if (iter_prev
->p
.reverse_hash
!= clear_flag(iter
)->p
.reverse_hash
486 check_resize(ht
, t
, ++chain_len
);
487 iter_prev
= clear_flag(iter
);
491 assert(node
!= clear_flag(iter
));
492 assert(!is_removed(iter_prev
));
493 assert(iter_prev
!= node
);
495 node
->p
.next
= clear_flag(iter
);
497 node
->p
.next
= flag_dummy(clear_flag(iter
));
499 new_node
= flag_dummy(node
);
502 if (uatomic_cmpxchg(&iter_prev
->p
.next
, iter
,
504 continue; /* retry */
508 assert(!is_removed(iter
));
510 new_next
= flag_dummy(clear_flag(next
));
512 new_next
= clear_flag(next
);
513 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
517 /* Garbage collect logically removed nodes in the bucket */
518 index
= hash
& (t
->size
- 1);
519 order
= get_count_order_ulong(index
+ 1);
520 lookup
= &t
->tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
521 dummy_node
= (struct rcu_ht_node
*) lookup
;
522 _ht_gc_bucket(dummy_node
, node
);
527 int _ht_remove(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
)
529 struct rcu_ht_node
*dummy
, *next
, *old
;
530 struct _rcu_ht_node
*lookup
;
532 unsigned long hash
, index
, order
;
534 /* logically delete the node */
535 old
= rcu_dereference(node
->p
.next
);
538 if (is_removed(next
))
540 assert(!is_dummy(next
));
541 old
= uatomic_cmpxchg(&node
->p
.next
, next
,
543 } while (old
!= next
);
545 /* We performed the (logical) deletion. */
549 * Ensure that the node is not visible to readers anymore: lookup for
550 * the node, and remove it (along with any other logically removed node)
553 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
554 index
= hash
& (t
->size
- 1);
555 order
= get_count_order_ulong(index
+ 1);
556 lookup
= &t
->tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
557 dummy
= (struct rcu_ht_node
*) lookup
;
558 _ht_gc_bucket(dummy
, node
);
561 * Only the flagging action indicated that we (and no other)
562 * removed the node from the hash.
565 assert(is_removed(rcu_dereference(node
->p
.next
)));
572 void init_table(struct rcu_ht
*ht
, struct rcu_table
*t
,
573 unsigned long first_order
, unsigned long len_order
)
575 unsigned long i
, end_order
;
577 dbg_printf("rculfhash: init table: first_order %lu end_order %lu\n",
578 first_order
, first_order
+ len_order
);
579 end_order
= first_order
+ len_order
;
580 t
->size
= !first_order
? 0 : (1UL << (first_order
- 1));
581 for (i
= first_order
; i
< end_order
; i
++) {
582 unsigned long j
, len
;
584 len
= !i
? 1 : 1UL << (i
- 1);
585 dbg_printf("rculfhash: init order %lu len: %lu\n", i
, len
);
586 t
->tbl
[i
] = calloc(len
, sizeof(struct _rcu_ht_node
));
587 for (j
= 0; j
< len
; j
++) {
588 dbg_printf("rculfhash: init entry: i %lu j %lu hash %lu\n",
589 i
, j
, !i
? 0 : (1UL << (i
- 1)) + j
);
590 struct rcu_ht_node
*new_node
=
591 (struct rcu_ht_node
*) &t
->tbl
[i
][j
];
592 new_node
->p
.reverse_hash
=
593 bit_reverse_ulong(!i
? 0 : (1UL << (i
- 1)) + j
);
594 (void) _ht_add(ht
, t
, new_node
, 0, 1);
595 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
598 /* Update table size */
599 t
->size
= !i
? 1 : (1UL << i
);
600 dbg_printf("rculfhash: init new size: %lu\n", t
->size
);
601 if (CMM_LOAD_SHARED(ht
->in_progress_destroy
))
604 t
->resize_target
= t
->size
;
605 t
->resize_initiated
= 0;
608 struct rcu_ht
*ht_new(ht_hash_fct hash_fct
,
609 ht_compare_fct compare_fct
,
610 unsigned long hash_seed
,
611 unsigned long init_size
,
612 void (*ht_call_rcu
)(struct rcu_head
*head
,
613 void (*func
)(struct rcu_head
*head
)))
618 ht
= calloc(1, sizeof(struct rcu_ht
));
619 ht
->hash_fct
= hash_fct
;
620 ht
->compare_fct
= compare_fct
;
621 ht
->hash_seed
= hash_seed
;
622 ht
->ht_call_rcu
= ht_call_rcu
;
623 ht
->in_progress_resize
= 0;
624 /* this mutex should not nest in read-side C.S. */
625 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
626 order
= get_count_order_ulong(max(init_size
, 1)) + 1;
627 ht
->t
= calloc(1, sizeof(struct rcu_table
)
628 + (order
* sizeof(struct _rcu_ht_node
*)));
630 pthread_mutex_lock(&ht
->resize_mutex
);
631 init_table(ht
, ht
->t
, 0, order
);
632 pthread_mutex_unlock(&ht
->resize_mutex
);
636 struct rcu_ht_node
*ht_lookup(struct rcu_ht
*ht
, void *key
, size_t key_len
)
639 struct rcu_ht_node
*node
, *next
;
640 struct _rcu_ht_node
*lookup
;
641 unsigned long hash
, reverse_hash
, index
, order
;
643 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
644 reverse_hash
= bit_reverse_ulong(hash
);
646 t
= rcu_dereference(ht
->t
);
647 index
= hash
& (t
->size
- 1);
648 order
= get_count_order_ulong(index
+ 1);
649 lookup
= &t
->tbl
[order
][index
& ((1UL << (order
- 1)) - 1)];
650 dbg_printf("rculfhash: lookup hash %lu index %lu order %lu aridx %lu\n",
651 hash
, index
, order
, index
& ((1UL << (order
- 1)) - 1));
652 node
= (struct rcu_ht_node
*) lookup
;
656 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
660 next
= rcu_dereference(node
->p
.next
);
661 if (likely(!is_removed(next
))
663 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
666 node
= clear_flag(next
);
668 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
672 void ht_add(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
677 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
678 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
680 t
= rcu_dereference(ht
->t
);
681 (void) _ht_add(ht
, t
, node
, 0, 0);
684 struct rcu_ht_node
*ht_add_unique(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
689 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
690 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
692 t
= rcu_dereference(ht
->t
);
693 return _ht_add(ht
, t
, node
, 1, 0);
696 int ht_remove(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
700 t
= rcu_dereference(ht
->t
);
701 return _ht_remove(ht
, t
, node
);
705 int ht_delete_dummy(struct rcu_ht
*ht
)
708 struct rcu_ht_node
*node
;
709 struct _rcu_ht_node
*lookup
;
710 unsigned long order
, i
;
713 /* Check that the table is empty */
714 lookup
= &t
->tbl
[0][0];
715 node
= (struct rcu_ht_node
*) lookup
;
717 node
= clear_flag(node
)->p
.next
;
720 assert(!is_removed(node
));
721 } while (clear_flag(node
));
722 /* Internal sanity check: all nodes left should be dummy */
723 for (order
= 0; order
< get_count_order_ulong(t
->size
) + 1; order
++) {
726 len
= !order
? 1 : 1UL << (order
- 1);
727 for (i
= 0; i
< len
; i
++) {
728 dbg_printf("rculfhash: delete order %lu i %lu hash %lu\n",
730 bit_reverse_ulong(t
->tbl
[order
][i
].reverse_hash
));
731 assert(is_dummy(t
->tbl
[order
][i
].next
));
739 * Should only be called when no more concurrent readers nor writers can
740 * possibly access the table.
742 int ht_destroy(struct rcu_ht
*ht
)
746 /* Wait for in-flight resize operations to complete */
747 CMM_STORE_SHARED(ht
->in_progress_destroy
, 1);
748 while (uatomic_read(&ht
->in_progress_resize
))
749 poll(NULL
, 0, 100); /* wait for 100ms */
750 ret
= ht_delete_dummy(ht
);
758 void ht_count_nodes(struct rcu_ht
*ht
,
759 unsigned long *count
,
760 unsigned long *removed
)
763 struct rcu_ht_node
*node
, *next
;
764 struct _rcu_ht_node
*lookup
;
765 unsigned long nr_dummy
= 0;
770 t
= rcu_dereference(ht
->t
);
771 /* Count non-dummy nodes in the table */
772 lookup
= &t
->tbl
[0][0];
773 node
= (struct rcu_ht_node
*) lookup
;
775 next
= rcu_dereference(node
->p
.next
);
776 if (is_removed(next
)) {
777 assert(!is_dummy(next
));
779 } else if (!is_dummy(next
))
783 node
= clear_flag(next
);
785 dbg_printf("rculfhash: number of dummy nodes: %lu\n", nr_dummy
);
789 void ht_free_table_cb(struct rcu_head
*head
)
791 struct rcu_table
*t
=
792 caa_container_of(head
, struct rcu_table
, head
);
796 /* called with resize mutex held */
798 void _do_ht_resize(struct rcu_ht
*ht
)
800 unsigned long new_size
, old_size
, old_order
, new_order
;
801 struct rcu_table
*new_t
, *old_t
;
804 old_size
= old_t
->size
;
805 old_order
= get_count_order_ulong(old_size
) + 1;
807 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
808 if (old_size
== new_size
)
810 new_order
= get_count_order_ulong(new_size
) + 1;
811 printf("rculfhash: resize from %lu (order %lu) to %lu (order %lu) buckets\n",
812 old_size
, old_order
, new_size
, new_order
);
813 new_t
= malloc(sizeof(struct rcu_table
)
814 + (new_order
* sizeof(struct _rcu_ht_node
*)));
815 assert(new_size
> old_size
);
816 memcpy(&new_t
->tbl
, &old_t
->tbl
,
817 old_order
* sizeof(struct _rcu_ht_node
*));
818 init_table(ht
, new_t
, old_order
, new_order
- old_order
);
819 /* Changing table and size atomically wrt lookups */
820 rcu_assign_pointer(ht
->t
, new_t
);
821 ht
->ht_call_rcu(&old_t
->head
, ht_free_table_cb
);
825 unsigned long resize_target_update(struct rcu_table
*t
,
828 return _uatomic_max(&t
->resize_target
,
829 t
->size
<< growth_order
);
832 void ht_resize(struct rcu_ht
*ht
, int growth
)
834 struct rcu_table
*t
= rcu_dereference(ht
->t
);
835 unsigned long target_size
;
837 target_size
= resize_target_update(t
, growth
);
838 if (t
->size
< target_size
) {
839 CMM_STORE_SHARED(t
->resize_initiated
, 1);
840 pthread_mutex_lock(&ht
->resize_mutex
);
842 pthread_mutex_unlock(&ht
->resize_mutex
);
847 void do_resize_cb(struct rcu_head
*head
)
849 struct rcu_resize_work
*work
=
850 caa_container_of(head
, struct rcu_resize_work
, head
);
851 struct rcu_ht
*ht
= work
->ht
;
853 pthread_mutex_lock(&ht
->resize_mutex
);
855 pthread_mutex_unlock(&ht
->resize_mutex
);
857 cmm_smp_mb(); /* finish resize before decrement */
858 uatomic_dec(&ht
->in_progress_resize
);
862 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
)
864 struct rcu_resize_work
*work
;
865 unsigned long target_size
;
867 target_size
= resize_target_update(t
, growth
);
868 if (!CMM_LOAD_SHARED(t
->resize_initiated
) && t
->size
< target_size
) {
869 uatomic_inc(&ht
->in_progress_resize
);
870 cmm_smp_mb(); /* increment resize count before calling it */
871 work
= malloc(sizeof(*work
));
873 ht
->ht_call_rcu(&work
->head
, do_resize_cb
);
874 CMM_STORE_SHARED(t
->resize_initiated
, 1);