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
32 #include <urcu-call-rcu.h>
33 #include <urcu/arch.h>
34 #include <urcu/uatomic.h>
35 #include <urcu/jhash.h>
36 #include <urcu/compiler.h>
37 #include <urcu/rculfhash.h>
41 #define DEBUG /* Test */
44 #define dbg_printf(args...) printf(args)
46 #define dbg_printf(args...)
49 #define CHAIN_LEN_TARGET 1
50 #define CHAIN_LEN_RESIZE_THRESHOLD 2
53 #define max(a, b) ((a) > (b) ? (a) : (b))
57 * The removed flag needs to be updated atomically with the pointer.
58 * The dummy flag does not require to be updated atomically with the
59 * pointer, but it is added as a pointer low bit flag to save space.
61 #define REMOVED_FLAG (1UL << 0)
62 #define DUMMY_FLAG (1UL << 1)
63 #define FLAGS_MASK ((1UL << 2) - 1)
66 unsigned long size
; /* always a power of 2 */
67 unsigned long resize_target
;
70 struct rcu_ht_node
*tbl
[0];
74 struct rcu_table
*t
; /* shared */
76 ht_compare_fct compare_fct
;
77 unsigned long hash_seed
;
78 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
79 unsigned int in_progress_resize
;
80 void (*ht_call_rcu
)(struct rcu_head
*head
,
81 void (*func
)(struct rcu_head
*head
));
84 struct rcu_resize_work
{
90 * Algorithm to reverse bits in a word by lookup table, extended to
93 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
94 * Originally from Public Domain.
97 static const uint8_t BitReverseTable256
[256] =
99 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
100 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
101 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
102 R6(0), R6(2), R6(1), R6(3)
109 uint8_t bit_reverse_u8(uint8_t v
)
111 return BitReverseTable256
[v
];
114 static __attribute__((unused
))
115 uint32_t bit_reverse_u32(uint32_t v
)
117 return ((uint32_t) bit_reverse_u8(v
) << 24) |
118 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
119 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
120 ((uint32_t) bit_reverse_u8(v
>> 24));
123 static __attribute__((unused
))
124 uint64_t bit_reverse_u64(uint64_t v
)
126 return ((uint64_t) bit_reverse_u8(v
) << 56) |
127 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
128 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
129 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
130 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
131 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
132 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
133 ((uint64_t) bit_reverse_u8(v
>> 56));
137 unsigned long bit_reverse_ulong(unsigned long v
)
139 #if (CAA_BITS_PER_LONG == 32)
140 return bit_reverse_u32(v
);
142 return bit_reverse_u64(v
);
147 * Algorithm to find the log2 of a 32-bit unsigned integer.
148 * source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
149 * Originally from Public Domain.
151 static const char LogTable256
[256] =
153 #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
154 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
155 LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
156 LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
159 uint32_t log2_u32(uint32_t v
)
163 if ((tt
= (v
>> 16)))
164 return (t
= (tt
>> 8))
165 ? 24 + LogTable256
[t
]
166 : 16 + LogTable256
[tt
];
168 return (t
= (v
>> 8))
174 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
);
177 void check_resize(struct rcu_ht
*ht
, struct rcu_table
*t
,
180 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
181 ht_resize_lazy(ht
, t
,
182 log2_u32(chain_len
- CHAIN_LEN_TARGET
- 1));
186 struct rcu_ht_node
*clear_flag(struct rcu_ht_node
*node
)
188 return (struct rcu_ht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
192 int is_removed(struct rcu_ht_node
*node
)
194 return ((unsigned long) node
) & REMOVED_FLAG
;
198 struct rcu_ht_node
*flag_removed(struct rcu_ht_node
*node
)
200 return (struct rcu_ht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
204 int is_dummy(struct rcu_ht_node
*node
)
206 return ((unsigned long) node
) & DUMMY_FLAG
;
210 struct rcu_ht_node
*flag_dummy(struct rcu_ht_node
*node
)
212 return (struct rcu_ht_node
*) (((unsigned long) node
) | DUMMY_FLAG
);
216 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
218 unsigned long old1
, old2
;
220 old1
= uatomic_read(ptr
);
225 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
230 * Remove all logically deleted nodes from a bucket up to a certain node key.
233 void _ht_gc_bucket(struct rcu_ht_node
*dummy
, struct rcu_ht_node
*node
)
235 struct rcu_ht_node
*iter_prev
, *iter
, *next
, *new_next
;
239 /* We can always skip the dummy node initially */
240 iter
= rcu_dereference(iter_prev
->p
.next
);
241 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
243 if (unlikely(!clear_flag(iter
)))
245 if (clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
)
247 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
248 if (is_removed(next
))
250 iter_prev
= clear_flag(iter
);
253 assert(!is_removed(iter
));
255 new_next
= flag_dummy(clear_flag(next
));
257 new_next
= clear_flag(next
);
258 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
263 struct rcu_ht_node
*_ht_add(struct rcu_ht
*ht
, struct rcu_table
*t
,
264 struct rcu_ht_node
*node
, int unique
, int dummy
)
266 struct rcu_ht_node
*iter_prev
, *iter
, *next
, *new_node
, *new_next
,
272 node
->p
.next
= flag_dummy(NULL
);
273 return node
; /* Initial first add (head) */
275 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
277 uint32_t chain_len
= 0;
280 * iter_prev points to the non-removed node prior to the
283 iter_prev
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
284 /* We can always skip the dummy node initially */
285 iter
= rcu_dereference(iter_prev
->p
.next
);
286 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
288 if (unlikely(!clear_flag(iter
)))
290 if (clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
)
292 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
293 if (is_removed(next
))
297 && !ht
->compare_fct(node
->key
, node
->key_len
,
298 clear_flag(iter
)->key
,
299 clear_flag(iter
)->key_len
))
300 return clear_flag(iter
);
301 /* Only account for identical reverse hash once */
302 if (iter_prev
->p
.reverse_hash
!= clear_flag(iter
)->p
.reverse_hash
)
303 check_resize(ht
, t
, ++chain_len
);
304 iter_prev
= clear_flag(iter
);
308 assert(node
!= clear_flag(iter
));
309 assert(!is_removed(iter_prev
));
310 assert(iter_prev
!= node
);
312 node
->p
.next
= clear_flag(iter
);
314 node
->p
.next
= flag_dummy(clear_flag(iter
));
316 new_node
= flag_dummy(node
);
319 if (uatomic_cmpxchg(&iter_prev
->p
.next
, iter
,
321 continue; /* retry */
325 assert(!is_removed(iter
));
327 new_next
= flag_dummy(clear_flag(next
));
329 new_next
= clear_flag(next
);
330 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, new_next
);
334 /* Garbage collect logically removed nodes in the bucket */
335 dummy_node
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
336 _ht_gc_bucket(dummy_node
, node
);
341 int _ht_remove(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
)
343 struct rcu_ht_node
*dummy
, *next
, *old
;
347 /* logically delete the node */
348 old
= rcu_dereference(node
->p
.next
);
351 if (is_removed(next
))
353 assert(!is_dummy(next
));
354 old
= uatomic_cmpxchg(&node
->p
.next
, next
,
356 } while (old
!= next
);
358 /* We performed the (logical) deletion. */
362 * Ensure that the node is not visible to readers anymore: lookup for
363 * the node, and remove it (along with any other logically removed node)
366 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
367 dummy
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
368 _ht_gc_bucket(dummy
, node
);
371 * Only the flagging action indicated that we (and no other)
372 * removed the node from the hash.
375 assert(is_removed(rcu_dereference(node
->p
.next
)));
382 void init_table(struct rcu_ht
*ht
, struct rcu_table
*t
,
383 unsigned long first
, unsigned long len
)
385 unsigned long i
, end
;
388 for (i
= first
; i
< end
; i
++) {
389 /* Update table size when power of two */
390 if (i
!= 0 && !(i
& (i
- 1)))
392 t
->tbl
[i
] = calloc(1, sizeof(struct _rcu_ht_node
));
393 t
->tbl
[i
]->p
.reverse_hash
= bit_reverse_ulong(i
);
394 (void) _ht_add(ht
, t
, t
->tbl
[i
], 0, 1);
396 t
->resize_target
= t
->size
= end
;
397 t
->resize_initiated
= 0;
400 struct rcu_ht
*ht_new(ht_hash_fct hash_fct
,
401 ht_compare_fct compare_fct
,
402 unsigned long hash_seed
,
403 unsigned long init_size
,
404 void (*ht_call_rcu
)(struct rcu_head
*head
,
405 void (*func
)(struct rcu_head
*head
)))
409 ht
= calloc(1, sizeof(struct rcu_ht
));
410 ht
->hash_fct
= hash_fct
;
411 ht
->compare_fct
= compare_fct
;
412 ht
->hash_seed
= hash_seed
;
413 ht
->ht_call_rcu
= ht_call_rcu
;
414 ht
->in_progress_resize
= 0;
415 /* this mutex should not nest in read-side C.S. */
416 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
417 ht
->t
= calloc(1, sizeof(struct rcu_table
)
418 + (max(init_size
, 1) * sizeof(struct rcu_ht_node
*)));
420 pthread_mutex_lock(&ht
->resize_mutex
);
421 init_table(ht
, ht
->t
, 0, max(init_size
, 1));
422 pthread_mutex_unlock(&ht
->resize_mutex
);
426 struct rcu_ht_node
*ht_lookup(struct rcu_ht
*ht
, void *key
, size_t key_len
)
429 struct rcu_ht_node
*node
, *next
;
430 unsigned long hash
, reverse_hash
;
432 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
433 reverse_hash
= bit_reverse_ulong(hash
);
435 t
= rcu_dereference(ht
->t
);
436 node
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
440 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
444 next
= rcu_dereference(node
->p
.next
);
445 if (likely(!is_removed(next
))
447 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
450 node
= clear_flag(next
);
452 assert(!node
|| !is_dummy(rcu_dereference(node
->p
.next
)));
456 void ht_add(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
461 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
462 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
464 t
= rcu_dereference(ht
->t
);
465 (void) _ht_add(ht
, t
, node
, 0, 0);
468 struct rcu_ht_node
*ht_add_unique(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
473 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
474 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
476 t
= rcu_dereference(ht
->t
);
477 return _ht_add(ht
, t
, node
, 1, 0);
480 int ht_remove(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
484 t
= rcu_dereference(ht
->t
);
485 return _ht_remove(ht
, t
, node
);
489 int ht_delete_dummy(struct rcu_ht
*ht
)
492 struct rcu_ht_node
*node
;
496 /* Check that the table is empty */
499 node
= clear_flag(node
)->p
.next
;
502 assert(!is_removed(node
));
503 } while (clear_flag(node
));
504 /* Internal sanity check: all nodes left should be dummy */
505 for (i
= 0; i
< t
->size
; i
++) {
506 assert(is_dummy(t
->tbl
[i
]->p
.next
));
513 * Should only be called when no more concurrent readers nor writers can
514 * possibly access the table.
516 int ht_destroy(struct rcu_ht
*ht
)
520 /* Wait for in-flight resize operations to complete */
521 while (uatomic_read(&ht
->in_progress_resize
))
522 poll(NULL
, 0, 100); /* wait for 100ms */
523 ret
= ht_delete_dummy(ht
);
531 void ht_count_nodes(struct rcu_ht
*ht
,
532 unsigned long *count
,
533 unsigned long *removed
)
536 struct rcu_ht_node
*node
, *next
;
541 t
= rcu_dereference(ht
->t
);
542 /* Check that the table is empty */
543 node
= rcu_dereference(t
->tbl
[0]);
545 next
= rcu_dereference(node
->p
.next
);
546 if (is_removed(next
)) {
547 assert(!is_dummy(next
));
549 } else if (!is_dummy(next
))
551 node
= clear_flag(next
);
556 void ht_free_table_cb(struct rcu_head
*head
)
558 struct rcu_table
*t
=
559 caa_container_of(head
, struct rcu_table
, head
);
563 /* called with resize mutex held */
565 void _do_ht_resize(struct rcu_ht
*ht
)
567 unsigned long new_size
, old_size
;
568 struct rcu_table
*new_t
, *old_t
;
571 old_size
= old_t
->size
;
573 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
574 dbg_printf("rculfhash: resize from %lu to %lu buckets\n",
576 if (old_size
== new_size
)
578 new_t
= malloc(sizeof(struct rcu_table
)
579 + (new_size
* sizeof(struct rcu_ht_node
*)));
580 assert(new_size
> old_size
);
581 memcpy(&new_t
->tbl
, &old_t
->tbl
,
582 old_size
* sizeof(struct rcu_ht_node
*));
583 init_table(ht
, new_t
, old_size
, new_size
- old_size
);
584 /* Changing table and size atomically wrt lookups */
585 rcu_assign_pointer(ht
->t
, new_t
);
586 ht
->ht_call_rcu(&old_t
->head
, ht_free_table_cb
);
590 unsigned long resize_target_update(struct rcu_table
*t
,
593 return _uatomic_max(&t
->resize_target
,
594 t
->size
<< growth_order
);
597 void ht_resize(struct rcu_ht
*ht
, int growth
)
599 struct rcu_table
*t
= rcu_dereference(ht
->t
);
600 unsigned long target_size
;
602 target_size
= resize_target_update(t
, growth
);
603 if (t
->size
< target_size
) {
604 CMM_STORE_SHARED(t
->resize_initiated
, 1);
605 pthread_mutex_lock(&ht
->resize_mutex
);
607 pthread_mutex_unlock(&ht
->resize_mutex
);
612 void do_resize_cb(struct rcu_head
*head
)
614 struct rcu_resize_work
*work
=
615 caa_container_of(head
, struct rcu_resize_work
, head
);
616 struct rcu_ht
*ht
= work
->ht
;
618 pthread_mutex_lock(&ht
->resize_mutex
);
620 pthread_mutex_unlock(&ht
->resize_mutex
);
622 cmm_smp_mb(); /* finish resize before decrement */
623 uatomic_dec(&ht
->in_progress_resize
);
627 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
)
629 struct rcu_resize_work
*work
;
630 unsigned long target_size
;
632 target_size
= resize_target_update(t
, growth
);
633 if (!CMM_LOAD_SHARED(t
->resize_initiated
) && t
->size
< target_size
) {
634 uatomic_inc(&ht
->in_progress_resize
);
635 cmm_smp_mb(); /* increment resize count before calling it */
636 work
= malloc(sizeof(*work
));
638 ht
->ht_call_rcu(&work
->head
, do_resize_cb
);
639 CMM_STORE_SHARED(t
->resize_initiated
, 1);