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))
56 #define REMOVED_FLAG (1UL << 0)
57 #define FLAGS_MASK ((1UL << 1) - 1)
60 unsigned long size
; /* always a power of 2 */
61 unsigned long resize_target
;
64 struct rcu_ht_node
*tbl
[0];
68 struct rcu_table
*t
; /* shared */
70 ht_compare_fct compare_fct
;
71 unsigned long hash_seed
;
72 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
73 unsigned int in_progress_resize
;
74 void (*ht_call_rcu
)(struct rcu_head
*head
,
75 void (*func
)(struct rcu_head
*head
));
78 struct rcu_resize_work
{
84 * Algorithm to reverse bits in a word by lookup table, extended to
87 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
88 * Originally from Public Domain.
91 static const uint8_t BitReverseTable256
[256] =
93 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
94 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
95 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
96 R6(0), R6(2), R6(1), R6(3)
103 uint8_t bit_reverse_u8(uint8_t v
)
105 return BitReverseTable256
[v
];
108 static __attribute__((unused
))
109 uint32_t bit_reverse_u32(uint32_t v
)
111 return ((uint32_t) bit_reverse_u8(v
) << 24) |
112 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
113 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
114 ((uint32_t) bit_reverse_u8(v
>> 24));
117 static __attribute__((unused
))
118 uint64_t bit_reverse_u64(uint64_t v
)
120 return ((uint64_t) bit_reverse_u8(v
) << 56) |
121 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
122 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
123 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
124 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
125 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
126 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
127 ((uint64_t) bit_reverse_u8(v
>> 56));
131 unsigned long bit_reverse_ulong(unsigned long v
)
133 #if (CAA_BITS_PER_LONG == 32)
134 return bit_reverse_u32(v
);
136 return bit_reverse_u64(v
);
141 * Algorithm to find the log2 of a 32-bit unsigned integer.
142 * source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
143 * Originally from Public Domain.
145 static const char LogTable256
[256] =
147 #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
148 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
149 LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
150 LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
153 uint32_t log2_u32(uint32_t v
)
157 if ((tt
= (v
>> 16)))
158 return (t
= (tt
>> 8))
159 ? 24 + LogTable256
[t
]
160 : 16 + LogTable256
[tt
];
162 return (t
= (v
>> 8))
168 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
);
171 void check_resize(struct rcu_ht
*ht
, struct rcu_table
*t
,
174 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
175 ht_resize_lazy(ht
, t
,
176 log2_u32(chain_len
- CHAIN_LEN_TARGET
- 1));
180 struct rcu_ht_node
*clear_flag(struct rcu_ht_node
*node
)
182 return (struct rcu_ht_node
*) (((unsigned long) node
) & ~FLAGS_MASK
);
186 int is_removed(struct rcu_ht_node
*node
)
188 return ((unsigned long) node
) & REMOVED_FLAG
;
192 struct rcu_ht_node
*flag_removed(struct rcu_ht_node
*node
)
194 return (struct rcu_ht_node
*) (((unsigned long) node
) | REMOVED_FLAG
);
198 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
200 unsigned long old1
, old2
;
202 old1
= uatomic_read(ptr
);
207 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
212 * Remove all logically deleted nodes from a bucket up to a certain node key.
215 void _ht_gc_bucket(struct rcu_ht_node
*dummy
, struct rcu_ht_node
*node
)
217 struct rcu_ht_node
*iter_prev
, *iter
, *next
;
221 /* We can always skip the dummy node initially */
222 iter
= rcu_dereference(iter_prev
->p
.next
);
223 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
225 if (unlikely(!clear_flag(iter
)))
227 if (clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
)
229 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
230 if (is_removed(next
))
235 assert(!is_removed(iter
));
236 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, clear_flag(next
));
241 struct rcu_ht_node
*_ht_add(struct rcu_ht
*ht
, struct rcu_table
*t
,
242 struct rcu_ht_node
*node
, int unique
)
244 struct rcu_ht_node
*iter_prev
, *dummy
, *iter
, *next
;
248 assert(node
->p
.dummy
);
249 return node
; /* Initial first add (head) */
251 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
253 uint32_t chain_len
= 0;
256 * iter_prev points to the non-removed node prior to the
259 iter_prev
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
260 /* We can always skip the dummy node initially */
261 iter
= rcu_dereference(iter_prev
->p
.next
);
262 assert(iter_prev
->p
.reverse_hash
<= node
->p
.reverse_hash
);
264 if (unlikely(!clear_flag(iter
)))
266 if (clear_flag(iter
)->p
.reverse_hash
> node
->p
.reverse_hash
)
268 next
= rcu_dereference(clear_flag(iter
)->p
.next
);
269 if (is_removed(next
))
272 && !clear_flag(iter
)->p
.dummy
273 && !ht
->compare_fct(node
->key
, node
->key_len
,
274 clear_flag(iter
)->key
,
275 clear_flag(iter
)->key_len
))
276 return clear_flag(iter
);
277 /* Only account for identical reverse hash once */
278 if (iter_prev
->p
.reverse_hash
!= clear_flag(iter
)->p
.reverse_hash
)
279 check_resize(ht
, t
, ++chain_len
);
280 iter_prev
= clear_flag(iter
);
284 assert(node
!= clear_flag(iter
));
285 assert(!is_removed(iter_prev
));
286 assert(iter_prev
!= node
);
288 if (uatomic_cmpxchg(&iter_prev
->p
.next
, iter
,
290 continue; /* retry */
294 assert(!is_removed(iter
));
295 (void) uatomic_cmpxchg(&iter_prev
->p
.next
, iter
, clear_flag(next
));
299 /* Garbage collect logically removed nodes in the bucket */
300 dummy
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
301 _ht_gc_bucket(dummy
, node
);
306 int _ht_remove(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
)
308 struct rcu_ht_node
*dummy
, *next
, *old
;
312 /* logically delete the node */
313 old
= rcu_dereference(node
->p
.next
);
316 if (is_removed(next
))
318 assert(!node
->p
.dummy
);
319 old
= uatomic_cmpxchg(&node
->p
.next
, next
,
321 } while (old
!= next
);
323 /* We performed the (logical) deletion. */
327 * Ensure that the node is not visible to readers anymore: lookup for
328 * the node, and remove it (along with any other logically removed node)
331 hash
= bit_reverse_ulong(node
->p
.reverse_hash
);
332 dummy
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
333 _ht_gc_bucket(dummy
, node
);
336 * Only the flagging action indicated that we (and no other)
337 * removed the node from the hash.
340 assert(is_removed(rcu_dereference(node
->p
.next
)));
347 void init_table(struct rcu_ht
*ht
, struct rcu_table
*t
,
348 unsigned long first
, unsigned long len
)
350 unsigned long i
, end
;
353 for (i
= first
; i
< end
; i
++) {
354 /* Update table size when power of two */
355 if (i
!= 0 && !(i
& (i
- 1)))
357 t
->tbl
[i
] = calloc(1, sizeof(struct _rcu_ht_node
));
358 t
->tbl
[i
]->p
.dummy
= 1;
359 t
->tbl
[i
]->p
.reverse_hash
= bit_reverse_ulong(i
);
360 (void) _ht_add(ht
, t
, t
->tbl
[i
], 0);
362 t
->resize_target
= t
->size
= end
;
363 t
->resize_initiated
= 0;
366 struct rcu_ht
*ht_new(ht_hash_fct hash_fct
,
367 ht_compare_fct compare_fct
,
368 unsigned long hash_seed
,
369 unsigned long init_size
,
370 void (*ht_call_rcu
)(struct rcu_head
*head
,
371 void (*func
)(struct rcu_head
*head
)))
375 ht
= calloc(1, sizeof(struct rcu_ht
));
376 ht
->hash_fct
= hash_fct
;
377 ht
->compare_fct
= compare_fct
;
378 ht
->hash_seed
= hash_seed
;
379 ht
->ht_call_rcu
= ht_call_rcu
;
380 ht
->in_progress_resize
= 0;
381 /* this mutex should not nest in read-side C.S. */
382 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
383 ht
->t
= calloc(1, sizeof(struct rcu_table
)
384 + (max(init_size
, 1) * sizeof(struct rcu_ht_node
*)));
386 pthread_mutex_lock(&ht
->resize_mutex
);
387 init_table(ht
, ht
->t
, 0, max(init_size
, 1));
388 pthread_mutex_unlock(&ht
->resize_mutex
);
392 struct rcu_ht_node
*ht_lookup(struct rcu_ht
*ht
, void *key
, size_t key_len
)
395 struct rcu_ht_node
*node
;
396 unsigned long hash
, reverse_hash
;
398 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
399 reverse_hash
= bit_reverse_ulong(hash
);
401 t
= rcu_dereference(ht
->t
);
402 node
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
406 if (unlikely(node
->p
.reverse_hash
> reverse_hash
)) {
410 if (likely(!is_removed(rcu_dereference(node
->p
.next
)))
412 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
415 node
= clear_flag(rcu_dereference(node
->p
.next
));
417 assert(!node
|| !node
->p
.dummy
);
421 void ht_add(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
426 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
427 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
429 t
= rcu_dereference(ht
->t
);
430 (void) _ht_add(ht
, t
, node
, 0);
433 struct rcu_ht_node
*ht_add_unique(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
438 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
439 node
->p
.reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
441 t
= rcu_dereference(ht
->t
);
442 return _ht_add(ht
, t
, node
, 1);
445 int ht_remove(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
449 t
= rcu_dereference(ht
->t
);
450 return _ht_remove(ht
, t
, node
);
454 int ht_delete_dummy(struct rcu_ht
*ht
)
457 struct rcu_ht_node
*node
;
461 /* Check that the table is empty */
467 assert(!is_removed(node
));
468 } while (clear_flag(node
));
469 /* Internal sanity check: all nodes left should be dummy */
470 for (i
= 0; i
< t
->size
; i
++) {
471 assert(t
->tbl
[i
]->p
.dummy
);
478 * Should only be called when no more concurrent readers nor writers can
479 * possibly access the table.
481 int ht_destroy(struct rcu_ht
*ht
)
485 /* Wait for in-flight resize operations to complete */
486 while (uatomic_read(&ht
->in_progress_resize
))
487 poll(NULL
, 0, 100); /* wait for 100ms */
488 ret
= ht_delete_dummy(ht
);
496 void ht_count_nodes(struct rcu_ht
*ht
,
497 unsigned long *count
,
498 unsigned long *removed
)
501 struct rcu_ht_node
*node
, *next
;
506 t
= rcu_dereference(ht
->t
);
507 /* Check that the table is empty */
508 node
= rcu_dereference(t
->tbl
[0]);
510 next
= rcu_dereference(node
->p
.next
);
511 if (is_removed(next
)) {
512 assert(!node
->p
.dummy
);
514 } else if (!node
->p
.dummy
)
516 node
= clear_flag(next
);
521 void ht_free_table_cb(struct rcu_head
*head
)
523 struct rcu_table
*t
=
524 caa_container_of(head
, struct rcu_table
, head
);
528 /* called with resize mutex held */
530 void _do_ht_resize(struct rcu_ht
*ht
)
532 unsigned long new_size
, old_size
;
533 struct rcu_table
*new_t
, *old_t
;
536 old_size
= old_t
->size
;
538 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
539 dbg_printf("rculfhash: resize from %lu to %lu buckets\n",
541 if (old_size
== new_size
)
543 new_t
= malloc(sizeof(struct rcu_table
)
544 + (new_size
* sizeof(struct rcu_ht_node
*)));
545 assert(new_size
> old_size
);
546 memcpy(&new_t
->tbl
, &old_t
->tbl
,
547 old_size
* sizeof(struct rcu_ht_node
*));
548 init_table(ht
, new_t
, old_size
, new_size
- old_size
);
549 /* Changing table and size atomically wrt lookups */
550 rcu_assign_pointer(ht
->t
, new_t
);
551 ht
->ht_call_rcu(&old_t
->head
, ht_free_table_cb
);
555 unsigned long resize_target_update(struct rcu_table
*t
,
558 return _uatomic_max(&t
->resize_target
,
559 t
->size
<< growth_order
);
562 void ht_resize(struct rcu_ht
*ht
, int growth
)
564 struct rcu_table
*t
= rcu_dereference(ht
->t
);
565 unsigned long target_size
;
567 target_size
= resize_target_update(t
, growth
);
568 if (t
->size
< target_size
) {
569 CMM_STORE_SHARED(t
->resize_initiated
, 1);
570 pthread_mutex_lock(&ht
->resize_mutex
);
572 pthread_mutex_unlock(&ht
->resize_mutex
);
577 void do_resize_cb(struct rcu_head
*head
)
579 struct rcu_resize_work
*work
=
580 caa_container_of(head
, struct rcu_resize_work
, head
);
581 struct rcu_ht
*ht
= work
->ht
;
583 pthread_mutex_lock(&ht
->resize_mutex
);
585 pthread_mutex_unlock(&ht
->resize_mutex
);
587 cmm_smp_mb(); /* finish resize before decrement */
588 uatomic_dec(&ht
->in_progress_resize
);
592 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
)
594 struct rcu_resize_work
*work
;
595 unsigned long target_size
;
597 target_size
= resize_target_update(t
, growth
);
598 if (!CMM_LOAD_SHARED(t
->resize_initiated
) && t
->size
< target_size
) {
599 uatomic_inc(&ht
->in_progress_resize
);
600 cmm_smp_mb(); /* increment resize count before calling it */
601 work
= malloc(sizeof(*work
));
603 ht
->ht_call_rcu(&work
->head
, do_resize_cb
);
604 CMM_STORE_SHARED(t
->resize_initiated
, 1);