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 unsigned long size
; /* always a power of 2 */
58 unsigned long resize_target
;
61 struct rcu_ht_node
*tbl
[0];
65 struct rcu_table
*t
; /* shared */
67 ht_compare_fct compare_fct
;
68 unsigned long hash_seed
;
69 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
70 void (*ht_call_rcu
)(struct rcu_head
*head
,
71 void (*func
)(struct rcu_head
*head
));
74 struct rcu_resize_work
{
80 * Algorithm to reverse bits in a word by lookup table, extended to
83 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
84 * Originally from Public Domain.
87 static const uint8_t BitReverseTable256
[256] =
89 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
90 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
91 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
92 R6(0), R6(2), R6(1), R6(3)
99 uint8_t bit_reverse_u8(uint8_t v
)
101 return BitReverseTable256
[v
];
104 static __attribute__((unused
))
105 uint32_t bit_reverse_u32(uint32_t v
)
107 return ((uint32_t) bit_reverse_u8(v
) << 24) |
108 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
109 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
110 ((uint32_t) bit_reverse_u8(v
>> 24));
113 static __attribute__((unused
))
114 uint64_t bit_reverse_u64(uint64_t v
)
116 return ((uint64_t) bit_reverse_u8(v
) << 56) |
117 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
118 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
119 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
120 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
121 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
122 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
123 ((uint64_t) bit_reverse_u8(v
>> 56));
127 unsigned long bit_reverse_ulong(unsigned long v
)
129 #if (CAA_BITS_PER_LONG == 32)
130 return bit_reverse_u32(v
);
132 return bit_reverse_u64(v
);
137 * Algorithm to find the log2 of a 32-bit unsigned integer.
138 * source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
139 * Originally from Public Domain.
141 static const char LogTable256
[256] =
143 #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
144 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
145 LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
146 LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
149 uint32_t log2_u32(uint32_t v
)
153 if ((tt
= (v
>> 16)))
154 return (t
= (tt
>> 8))
155 ? 24 + LogTable256
[t
]
156 : 16 + LogTable256
[tt
];
158 return (t
= (v
>> 8))
164 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
);
167 void check_resize(struct rcu_ht
*ht
, struct rcu_table
*t
,
170 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
171 ht_resize_lazy(ht
, t
,
172 log2_u32(chain_len
- CHAIN_LEN_TARGET
- 1));
176 struct rcu_ht_node
*clear_flag(struct rcu_ht_node
*node
)
178 return (struct rcu_ht_node
*) (((unsigned long) node
) & ~0x1);
182 int is_removed(struct rcu_ht_node
*node
)
184 return ((unsigned long) node
) & 0x1;
188 struct rcu_ht_node
*flag_removed(struct rcu_ht_node
*node
)
190 return (struct rcu_ht_node
*) (((unsigned long) node
) | 0x1);
194 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
196 unsigned long old1
, old2
;
198 old1
= uatomic_read(ptr
);
203 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
208 * Remove all logically deleted nodes from a bucket up to a certain node key.
211 void _ht_gc_bucket(struct rcu_ht_node
*dummy
, struct rcu_ht_node
*node
)
213 struct rcu_ht_node
*iter_prev
, *iter
, *next
;
217 /* We can always skip the dummy node initially */
218 iter
= rcu_dereference(iter_prev
->next
);
219 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
223 if (clear_flag(iter
)->reverse_hash
> node
->reverse_hash
)
225 next
= rcu_dereference(clear_flag(iter
)->next
);
226 if (is_removed(next
))
231 assert(!is_removed(iter
));
232 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, clear_flag(next
));
237 int _ht_add(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
,
240 struct rcu_ht_node
*iter_prev
, *dummy
, *iter
, *next
;
245 hash
= bit_reverse_ulong(node
->reverse_hash
);
247 uint32_t chain_len
= 0;
250 * iter_prev points to the non-removed node prior to the
253 iter_prev
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
254 /* We can always skip the dummy node initially */
255 iter
= rcu_dereference(iter_prev
->next
);
256 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
260 if (clear_flag(iter
)->reverse_hash
> node
->reverse_hash
)
262 next
= rcu_dereference(clear_flag(iter
)->next
);
263 if (is_removed(next
))
265 /* Only account for identical reverse hash once */
266 if (iter_prev
->reverse_hash
!= clear_flag(iter
)->reverse_hash
)
267 check_resize(ht
, t
, ++chain_len
);
268 iter_prev
= clear_flag(iter
);
272 assert(node
!= clear_flag(iter
));
273 assert(!is_removed(iter_prev
));
274 assert(iter_prev
!= node
);
276 if (uatomic_cmpxchg(&iter_prev
->next
, iter
,
278 continue; /* retry */
282 assert(!is_removed(iter
));
283 (void) uatomic_cmpxchg(&iter_prev
->next
, iter
, clear_flag(next
));
287 /* Garbage collect logically removed nodes in the bucket */
288 dummy
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
289 _ht_gc_bucket(dummy
, node
);
294 int _ht_remove(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
)
296 struct rcu_ht_node
*dummy
, *next
, *old
;
300 /* logically delete the node */
301 old
= rcu_dereference(node
->next
);
304 if (is_removed(next
))
306 assert(!node
->dummy
);
307 old
= uatomic_cmpxchg(&node
->next
, next
,
309 } while (old
!= next
);
311 /* We performed the (logical) deletion. */
315 * Ensure that the node is not visible to readers anymore: lookup for
316 * the node, and remove it (along with any other logically removed node)
319 hash
= bit_reverse_ulong(node
->reverse_hash
);
320 dummy
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
321 _ht_gc_bucket(dummy
, node
);
324 * Only the flagging action indicated that we (and no other)
325 * removed the node from the hash.
328 assert(is_removed(rcu_dereference(node
->next
)));
335 void init_table(struct rcu_ht
*ht
, struct rcu_table
*t
,
336 unsigned long first
, unsigned long len
)
338 unsigned long i
, end
;
341 for (i
= first
; i
< end
; i
++) {
342 /* Update table size when power of two */
343 if (i
!= 0 && !(i
& (i
- 1)))
345 t
->tbl
[i
] = calloc(1, sizeof(struct rcu_ht_node
));
346 t
->tbl
[i
]->dummy
= 1;
347 t
->tbl
[i
]->reverse_hash
= bit_reverse_ulong(i
);
348 (void) _ht_add(ht
, t
, t
->tbl
[i
], 0);
350 t
->resize_target
= t
->size
= end
;
351 t
->resize_initiated
= 0;
354 struct rcu_ht
*ht_new(ht_hash_fct hash_fct
,
355 ht_compare_fct compare_fct
,
356 unsigned long hash_seed
,
357 unsigned long init_size
,
358 void (*ht_call_rcu
)(struct rcu_head
*head
,
359 void (*func
)(struct rcu_head
*head
)))
363 ht
= calloc(1, sizeof(struct rcu_ht
));
364 ht
->hash_fct
= hash_fct
;
365 ht
->compare_fct
= compare_fct
;
366 ht
->hash_seed
= hash_seed
;
367 ht
->ht_call_rcu
= ht_call_rcu
;
368 /* this mutex should not nest in read-side C.S. */
369 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
370 ht
->t
= calloc(1, sizeof(struct rcu_table
)
371 + (max(init_size
, 1) * sizeof(struct rcu_ht_node
*)));
373 pthread_mutex_lock(&ht
->resize_mutex
);
374 init_table(ht
, ht
->t
, 0, max(init_size
, 1));
375 pthread_mutex_unlock(&ht
->resize_mutex
);
379 struct rcu_ht_node
*ht_lookup(struct rcu_ht
*ht
, void *key
, size_t key_len
)
382 struct rcu_ht_node
*node
;
383 unsigned long hash
, reverse_hash
;
385 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
386 reverse_hash
= bit_reverse_ulong(hash
);
388 t
= rcu_dereference(ht
->t
);
389 node
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
393 if (unlikely(node
->reverse_hash
> reverse_hash
)) {
397 if (likely(!is_removed(rcu_dereference(node
->next
)))
399 && likely(!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
))) {
402 node
= clear_flag(rcu_dereference(node
->next
));
404 assert(!node
|| !node
->dummy
);
408 void ht_add(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
413 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
414 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
416 t
= rcu_dereference(ht
->t
);
417 (void) _ht_add(ht
, t
, node
, 0);
420 int ht_add_unique(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
425 hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
426 node
->reverse_hash
= bit_reverse_ulong((unsigned long) hash
);
428 t
= rcu_dereference(ht
->t
);
429 return _ht_add(ht
, t
, node
, 1);
432 int ht_remove(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
436 t
= rcu_dereference(ht
->t
);
437 return _ht_remove(ht
, t
, node
);
441 int ht_delete_dummy(struct rcu_ht
*ht
)
444 struct rcu_ht_node
*node
;
448 /* Check that the table is empty */
454 assert(!is_removed(node
));
456 /* Internal sanity check: all nodes left should be dummy */
457 for (i
= 0; i
< t
->size
; i
++) {
458 assert(t
->tbl
[i
]->dummy
);
465 * Should only be called when no more concurrent readers nor writers can
466 * possibly access the table.
468 int ht_destroy(struct rcu_ht
*ht
)
472 ret
= ht_delete_dummy(ht
);
480 void ht_count_nodes(struct rcu_ht
*ht
,
481 unsigned long *count
,
482 unsigned long *removed
)
485 struct rcu_ht_node
*node
, *next
;
490 t
= rcu_dereference(ht
->t
);
491 /* Check that the table is empty */
492 node
= rcu_dereference(t
->tbl
[0]);
494 next
= rcu_dereference(node
->next
);
495 if (is_removed(next
)) {
496 assert(!node
->dummy
);
498 } else if (!node
->dummy
)
500 node
= clear_flag(next
);
505 void ht_free_table_cb(struct rcu_head
*head
)
507 struct rcu_table
*t
=
508 caa_container_of(head
, struct rcu_table
, head
);
512 /* called with resize mutex held */
514 void _do_ht_resize(struct rcu_ht
*ht
)
516 unsigned long new_size
, old_size
;
517 struct rcu_table
*new_t
, *old_t
;
520 old_size
= old_t
->size
;
522 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
523 dbg_printf("rculfhash: resize from %lu to %lu buckets\n",
525 if (old_size
== new_size
)
527 new_t
= malloc(sizeof(struct rcu_table
)
528 + (new_size
* sizeof(struct rcu_ht_node
*)));
529 assert(new_size
> old_size
);
530 memcpy(&new_t
->tbl
, &old_t
->tbl
,
531 old_size
* sizeof(struct rcu_ht_node
*));
532 init_table(ht
, new_t
, old_size
, new_size
- old_size
);
533 /* Changing table and size atomically wrt lookups */
534 rcu_assign_pointer(ht
->t
, new_t
);
535 ht
->ht_call_rcu(&old_t
->head
, ht_free_table_cb
);
539 unsigned long resize_target_update(struct rcu_table
*t
,
542 return _uatomic_max(&t
->resize_target
,
543 t
->size
<< growth_order
);
546 void ht_resize(struct rcu_ht
*ht
, int growth
)
548 struct rcu_table
*t
= rcu_dereference(ht
->t
);
549 unsigned long target_size
;
551 target_size
= resize_target_update(t
, growth
);
552 if (t
->size
< target_size
) {
553 CMM_STORE_SHARED(t
->resize_initiated
, 1);
554 pthread_mutex_lock(&ht
->resize_mutex
);
556 pthread_mutex_unlock(&ht
->resize_mutex
);
561 void do_resize_cb(struct rcu_head
*head
)
563 struct rcu_resize_work
*work
=
564 caa_container_of(head
, struct rcu_resize_work
, head
);
565 struct rcu_ht
*ht
= work
->ht
;
567 pthread_mutex_lock(&ht
->resize_mutex
);
569 pthread_mutex_unlock(&ht
->resize_mutex
);
574 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
)
576 struct rcu_resize_work
*work
;
577 unsigned long target_size
;
579 target_size
= resize_target_update(t
, growth
);
580 if (!CMM_LOAD_SHARED(t
->resize_initiated
) && t
->size
< target_size
) {
581 work
= malloc(sizeof(*work
));
583 ht
->ht_call_rcu(&work
->head
, do_resize_cb
);
584 CMM_STORE_SHARED(t
->resize_initiated
, 1);