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 BUCKET_SIZE_RESIZE_THRESHOLD 4
52 #define max(a, b) ((a) > (b) ? (a) : (b))
56 unsigned long size
; /* always a power of 2 */
57 unsigned long resize_target
;
59 struct rcu_ht_node
*tbl
[0];
63 struct rcu_table
*t
; /* shared */
65 ht_compare_fct compare_fct
;
66 unsigned long hash_seed
;
67 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
68 void (*ht_call_rcu
)(struct rcu_head
*head
,
69 void (*func
)(struct rcu_head
*head
));
72 struct rcu_resize_work
{
78 * Algorithm to reverse bits in a word by lookup table, extended to
81 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
82 * Originally from Public Domain.
85 static const uint8_t BitReverseTable256
[256] =
87 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
88 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
89 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
90 R6(0), R6(2), R6(1), R6(3)
97 uint8_t bit_reverse_u8(uint8_t v
)
99 return BitReverseTable256
[v
];
102 static __attribute__((unused
))
103 uint32_t bit_reverse_u32(uint32_t v
)
105 return ((uint32_t) bit_reverse_u8(v
) << 24) |
106 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
107 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
108 ((uint32_t) bit_reverse_u8(v
>> 24));
111 static __attribute__((unused
))
112 uint64_t bit_reverse_u64(uint64_t v
)
114 return ((uint64_t) bit_reverse_u8(v
) << 56) |
115 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
116 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
117 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
118 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
119 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
120 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
121 ((uint64_t) bit_reverse_u8(v
>> 56));
125 unsigned long bit_reverse_ulong(unsigned long v
)
127 #if (CAA_BITS_PER_LONG == 32)
128 return bit_reverse_u32(v
);
130 return bit_reverse_u64(v
);
135 * Algorithm to find the log2 of a 32-bit unsigned integer.
136 * source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
137 * Originally from Public Domain.
139 static const char LogTable256
[256] =
141 #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
142 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
143 LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
144 LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
147 uint32_t log2_u32(uint32_t v
)
151 if ((tt
= (v
>> 16)))
152 return (t
= (tt
>> 8))
153 ? 24 + LogTable256
[t
]
154 : 16 + LogTable256
[tt
];
156 return (t
= (v
>> 8))
162 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
);
165 void check_resize(struct rcu_ht
*ht
, struct rcu_table
*t
,
168 if (chain_len
>= BUCKET_SIZE_RESIZE_THRESHOLD
)
169 ht_resize_lazy(ht
, t
, log2_u32(chain_len
));
173 struct rcu_ht_node
*clear_flag(struct rcu_ht_node
*node
)
175 return (struct rcu_ht_node
*) (((unsigned long) node
) & ~0x1);
179 int is_removed(struct rcu_ht_node
*node
)
181 return ((unsigned long) node
) & 0x1;
185 struct rcu_ht_node
*flag_removed(struct rcu_ht_node
*node
)
187 return (struct rcu_ht_node
*) (((unsigned long) node
) | 0x1);
191 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
193 unsigned long old1
, old2
;
195 old1
= uatomic_read(ptr
);
200 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
205 void _ht_add(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
)
207 struct rcu_ht_node
*iter_prev
= NULL
, *iter
= NULL
;
212 uint32_t chain_len
= 0;
214 iter_prev
= rcu_dereference(t
->tbl
[node
->hash
& (t
->size
- 1)]);
216 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
218 iter
= clear_flag(rcu_dereference(iter_prev
->next
));
221 if (iter
->reverse_hash
< node
->reverse_hash
)
224 check_resize(ht
, t
, ++chain_len
);
226 /* add in iter_prev->next */
227 if (is_removed(iter
))
229 assert(node
!= iter
);
231 assert(iter_prev
!= node
);
232 if (uatomic_cmpxchg(&iter_prev
->next
, iter
, node
) != iter
)
239 int _ht_remove(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
)
241 struct rcu_ht_node
*iter_prev
, *iter
, *next
, *old
;
242 unsigned long chain_len
;
249 iter_prev
= rcu_dereference(t
->tbl
[node
->hash
& (t
->size
- 1)]);
251 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
253 iter
= clear_flag(rcu_dereference(iter_prev
->next
));
256 if (iter
->reverse_hash
< node
->reverse_hash
)
268 next
= rcu_dereference(iter
->next
);
270 if (is_removed(next
)) {
274 /* set deletion flag */
275 if ((old
= uatomic_cmpxchg(&iter
->next
, next
,
276 flag_removed(next
))) != next
) {
277 if (old
== flag_removed(next
)) {
287 * Remove the element from the list. Retry if there has been a
288 * concurrent add (there cannot be a concurrent delete, because
289 * we won the deletion flag cmpxchg).
291 if (uatomic_cmpxchg(&iter_prev
->next
, iter
, clear_flag(next
)) != iter
)
298 void init_table(struct rcu_ht
*ht
, struct rcu_table
*t
,
299 unsigned long first
, unsigned long len
)
301 unsigned long i
, end
;
304 for (i
= first
; i
< end
; i
++) {
305 /* Update table size when power of two */
306 if (i
!= 0 && !(i
& (i
- 1)))
308 t
->tbl
[i
] = calloc(1, sizeof(struct rcu_ht_node
));
309 t
->tbl
[i
]->dummy
= 1;
311 t
->tbl
[i
]->reverse_hash
= bit_reverse_ulong(i
);
312 _ht_add(ht
, t
, t
->tbl
[i
]);
314 t
->resize_target
= t
->size
= end
;
317 struct rcu_ht
*ht_new(ht_hash_fct hash_fct
,
318 ht_compare_fct compare_fct
,
319 unsigned long hash_seed
,
320 unsigned long init_size
,
321 void (*ht_call_rcu
)(struct rcu_head
*head
,
322 void (*func
)(struct rcu_head
*head
)))
326 ht
= calloc(1, sizeof(struct rcu_ht
));
327 ht
->hash_fct
= hash_fct
;
328 ht
->compare_fct
= compare_fct
;
329 ht
->hash_seed
= hash_seed
;
330 ht
->ht_call_rcu
= ht_call_rcu
;
331 /* this mutex should not nest in read-side C.S. */
332 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
333 ht
->t
= calloc(1, sizeof(struct rcu_table
)
334 + (max(init_size
, 1) * sizeof(struct rcu_ht_node
*)));
336 pthread_mutex_lock(&ht
->resize_mutex
);
337 init_table(ht
, ht
->t
, 0, max(init_size
, 1));
338 pthread_mutex_unlock(&ht
->resize_mutex
);
342 struct rcu_ht_node
*ht_lookup(struct rcu_ht
*ht
, void *key
, size_t key_len
)
345 struct rcu_ht_node
*node
;
346 unsigned long hash
, reverse_hash
;
348 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
349 reverse_hash
= bit_reverse_ulong(hash
);
351 t
= rcu_dereference(ht
->t
);
352 node
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
356 if (node
->reverse_hash
> reverse_hash
) {
360 if (!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
)) {
361 if (is_removed(rcu_dereference(node
->next
)))
365 node
= clear_flag(rcu_dereference(node
->next
));
370 void ht_add(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
374 node
->hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
375 node
->reverse_hash
= bit_reverse_ulong((unsigned long) node
->hash
);
377 t
= rcu_dereference(ht
->t
);
378 _ht_add(ht
, t
, node
);
381 int ht_remove(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
385 t
= rcu_dereference(ht
->t
);
386 return _ht_remove(ht
, t
, node
);
390 int ht_delete_dummy(struct rcu_ht
*ht
)
393 struct rcu_ht_node
*node
;
397 /* Check that the table is empty */
404 /* Internal sanity check: all nodes left should be dummy */
405 for (i
= 0; i
< t
->size
; i
++) {
406 assert(t
->tbl
[i
]->dummy
);
413 * Should only be called when no more concurrent readers nor writers can
414 * possibly access the table.
416 int ht_destroy(struct rcu_ht
*ht
)
420 ret
= ht_delete_dummy(ht
);
429 void ht_free_table_cb(struct rcu_head
*head
)
431 struct rcu_table
*t
=
432 caa_container_of(head
, struct rcu_table
, head
);
436 /* called with resize mutex held */
438 void _do_ht_resize(struct rcu_ht
*ht
)
440 unsigned long new_size
, old_size
;
441 struct rcu_table
*new_t
, *old_t
;
444 old_size
= old_t
->size
;
446 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
447 dbg_printf("rculfhash: resize from %lu to %lu buckets\n",
449 if (old_size
== new_size
)
451 new_t
= malloc(sizeof(struct rcu_table
)
452 + (new_size
* sizeof(struct rcu_ht_node
*)));
453 assert(new_size
> old_size
);
454 memcpy(&new_t
->tbl
, &old_t
->tbl
,
455 old_size
* sizeof(struct rcu_ht_node
*));
456 init_table(ht
, new_t
, old_size
, new_size
- old_size
);
457 /* Changing table and size atomically wrt lookups */
458 rcu_assign_pointer(ht
->t
, new_t
);
459 ht
->ht_call_rcu(&old_t
->head
, ht_free_table_cb
);
463 unsigned long resize_target_update(struct rcu_table
*t
,
466 return _uatomic_max(&t
->resize_target
,
467 t
->size
<< growth_order
);
470 void ht_resize(struct rcu_ht
*ht
, int growth
)
472 struct rcu_table
*t
= rcu_dereference(ht
->t
);
473 unsigned long target_size
;
475 target_size
= resize_target_update(t
, growth
);
476 if (t
->size
< target_size
) {
477 pthread_mutex_lock(&ht
->resize_mutex
);
479 pthread_mutex_unlock(&ht
->resize_mutex
);
484 void do_resize_cb(struct rcu_head
*head
)
486 struct rcu_resize_work
*work
=
487 caa_container_of(head
, struct rcu_resize_work
, head
);
488 struct rcu_ht
*ht
= work
->ht
;
490 pthread_mutex_lock(&ht
->resize_mutex
);
492 pthread_mutex_unlock(&ht
->resize_mutex
);
497 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
)
499 struct rcu_resize_work
*work
;
500 unsigned long target_size
;
502 target_size
= resize_target_update(t
, growth
);
503 if (t
->size
< target_size
) {
504 work
= malloc(sizeof(*work
));
506 ht
->ht_call_rcu(&work
->head
, do_resize_cb
);