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 4
50 #define CHAIN_LEN_RESIZE_THRESHOLD 16
53 #define max(a, b) ((a) > (b) ? (a) : (b))
57 unsigned long size
; /* always a power of 2 */
58 unsigned long resize_target
;
60 struct rcu_ht_node
*tbl
[0];
64 struct rcu_table
*t
; /* shared */
66 ht_compare_fct compare_fct
;
67 unsigned long hash_seed
;
68 pthread_mutex_t resize_mutex
; /* resize mutex: add/del mutex */
69 void (*ht_call_rcu
)(struct rcu_head
*head
,
70 void (*func
)(struct rcu_head
*head
));
73 struct rcu_resize_work
{
79 * Algorithm to reverse bits in a word by lookup table, extended to
82 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
83 * Originally from Public Domain.
86 static const uint8_t BitReverseTable256
[256] =
88 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
89 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
90 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
91 R6(0), R6(2), R6(1), R6(3)
98 uint8_t bit_reverse_u8(uint8_t v
)
100 return BitReverseTable256
[v
];
103 static __attribute__((unused
))
104 uint32_t bit_reverse_u32(uint32_t v
)
106 return ((uint32_t) bit_reverse_u8(v
) << 24) |
107 ((uint32_t) bit_reverse_u8(v
>> 8) << 16) |
108 ((uint32_t) bit_reverse_u8(v
>> 16) << 8) |
109 ((uint32_t) bit_reverse_u8(v
>> 24));
112 static __attribute__((unused
))
113 uint64_t bit_reverse_u64(uint64_t v
)
115 return ((uint64_t) bit_reverse_u8(v
) << 56) |
116 ((uint64_t) bit_reverse_u8(v
>> 8) << 48) |
117 ((uint64_t) bit_reverse_u8(v
>> 16) << 40) |
118 ((uint64_t) bit_reverse_u8(v
>> 24) << 32) |
119 ((uint64_t) bit_reverse_u8(v
>> 32) << 24) |
120 ((uint64_t) bit_reverse_u8(v
>> 40) << 16) |
121 ((uint64_t) bit_reverse_u8(v
>> 48) << 8) |
122 ((uint64_t) bit_reverse_u8(v
>> 56));
126 unsigned long bit_reverse_ulong(unsigned long v
)
128 #if (CAA_BITS_PER_LONG == 32)
129 return bit_reverse_u32(v
);
131 return bit_reverse_u64(v
);
136 * Algorithm to find the log2 of a 32-bit unsigned integer.
137 * source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
138 * Originally from Public Domain.
140 static const char LogTable256
[256] =
142 #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
143 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
144 LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
145 LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
148 uint32_t log2_u32(uint32_t v
)
152 if ((tt
= (v
>> 16)))
153 return (t
= (tt
>> 8))
154 ? 24 + LogTable256
[t
]
155 : 16 + LogTable256
[tt
];
157 return (t
= (v
>> 8))
163 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
);
166 void check_resize(struct rcu_ht
*ht
, struct rcu_table
*t
,
169 if (chain_len
>= CHAIN_LEN_RESIZE_THRESHOLD
)
170 ht_resize_lazy(ht
, t
,
171 log2_u32(chain_len
- CHAIN_LEN_TARGET
));
175 struct rcu_ht_node
*clear_flag(struct rcu_ht_node
*node
)
177 return (struct rcu_ht_node
*) (((unsigned long) node
) & ~0x1);
181 int is_removed(struct rcu_ht_node
*node
)
183 return ((unsigned long) node
) & 0x1;
187 struct rcu_ht_node
*flag_removed(struct rcu_ht_node
*node
)
189 return (struct rcu_ht_node
*) (((unsigned long) node
) | 0x1);
193 unsigned long _uatomic_max(unsigned long *ptr
, unsigned long v
)
195 unsigned long old1
, old2
;
197 old1
= uatomic_read(ptr
);
202 } while ((old1
= uatomic_cmpxchg(ptr
, old2
, v
)) != old2
);
207 void _ht_add(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
)
209 struct rcu_ht_node
*iter_prev
= NULL
, *iter
= NULL
;
214 uint32_t chain_len
= 0;
216 iter_prev
= rcu_dereference(t
->tbl
[node
->hash
& (t
->size
- 1)]);
218 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
220 iter
= clear_flag(rcu_dereference(iter_prev
->next
));
223 if (iter
->reverse_hash
> node
->reverse_hash
)
226 check_resize(ht
, t
, ++chain_len
);
229 * add in iter_prev->next: TODO: check for helping
230 * delete, for lock-freedom...
232 if (is_removed(iter
))
234 assert(node
!= iter
);
236 assert(iter_prev
!= node
);
237 if (uatomic_cmpxchg(&iter_prev
->next
, iter
, node
) != iter
)
244 int _ht_remove(struct rcu_ht
*ht
, struct rcu_table
*t
, struct rcu_ht_node
*node
)
246 struct rcu_ht_node
*iter_prev
, *iter
, *next
, *old
;
247 unsigned long chain_len
;
254 iter_prev
= rcu_dereference(t
->tbl
[node
->hash
& (t
->size
- 1)]);
256 assert(iter_prev
->reverse_hash
<= node
->reverse_hash
);
258 iter
= clear_flag(rcu_dereference(iter_prev
->next
));
261 if (unlikely(iter
->reverse_hash
> node
->reverse_hash
))
273 next
= rcu_dereference(iter
->next
);
275 if (is_removed(next
)) {
279 /* set deletion flag */
280 if ((old
= uatomic_cmpxchg(&iter
->next
, next
,
281 flag_removed(next
))) != next
) {
282 if (old
== flag_removed(next
)) {
292 * Remove the element from the list.
293 * Retry if there has been a concurrent add before us.
294 * Retry if the prev node has been deleted.
295 * There cannot be a concurrent delete for our position, because
296 * we won the deletion flag cmpxchg.
297 * If there is a concurrent add after us, our deletion flag
298 * makes it busy-loop (FIXME: not lock-free).
300 if (uatomic_cmpxchg(&iter_prev
->next
, iter
, clear_flag(next
)) != iter
)
307 void init_table(struct rcu_ht
*ht
, struct rcu_table
*t
,
308 unsigned long first
, unsigned long len
)
310 unsigned long i
, end
;
313 for (i
= first
; i
< end
; i
++) {
314 /* Update table size when power of two */
315 if (i
!= 0 && !(i
& (i
- 1)))
317 t
->tbl
[i
] = calloc(1, sizeof(struct rcu_ht_node
));
318 t
->tbl
[i
]->dummy
= 1;
320 t
->tbl
[i
]->reverse_hash
= bit_reverse_ulong(i
);
321 _ht_add(ht
, t
, t
->tbl
[i
]);
323 t
->resize_target
= t
->size
= end
;
326 struct rcu_ht
*ht_new(ht_hash_fct hash_fct
,
327 ht_compare_fct compare_fct
,
328 unsigned long hash_seed
,
329 unsigned long init_size
,
330 void (*ht_call_rcu
)(struct rcu_head
*head
,
331 void (*func
)(struct rcu_head
*head
)))
335 ht
= calloc(1, sizeof(struct rcu_ht
));
336 ht
->hash_fct
= hash_fct
;
337 ht
->compare_fct
= compare_fct
;
338 ht
->hash_seed
= hash_seed
;
339 ht
->ht_call_rcu
= ht_call_rcu
;
340 /* this mutex should not nest in read-side C.S. */
341 pthread_mutex_init(&ht
->resize_mutex
, NULL
);
342 ht
->t
= calloc(1, sizeof(struct rcu_table
)
343 + (max(init_size
, 1) * sizeof(struct rcu_ht_node
*)));
345 pthread_mutex_lock(&ht
->resize_mutex
);
346 init_table(ht
, ht
->t
, 0, max(init_size
, 1));
347 pthread_mutex_unlock(&ht
->resize_mutex
);
351 struct rcu_ht_node
*ht_lookup(struct rcu_ht
*ht
, void *key
, size_t key_len
)
354 struct rcu_ht_node
*node
;
355 unsigned long hash
, reverse_hash
;
357 hash
= ht
->hash_fct(key
, key_len
, ht
->hash_seed
);
358 reverse_hash
= bit_reverse_ulong(hash
);
360 t
= rcu_dereference(ht
->t
);
361 node
= rcu_dereference(t
->tbl
[hash
& (t
->size
- 1)]);
365 if (unlikely(node
->reverse_hash
> reverse_hash
)) {
369 if (!ht
->compare_fct(node
->key
, node
->key_len
, key
, key_len
)) {
370 if (unlikely(is_removed(rcu_dereference(node
->next
))))
374 node
= clear_flag(rcu_dereference(node
->next
));
379 void ht_add(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
383 node
->hash
= ht
->hash_fct(node
->key
, node
->key_len
, ht
->hash_seed
);
384 node
->reverse_hash
= bit_reverse_ulong((unsigned long) node
->hash
);
386 t
= rcu_dereference(ht
->t
);
387 _ht_add(ht
, t
, node
);
390 int ht_remove(struct rcu_ht
*ht
, struct rcu_ht_node
*node
)
394 t
= rcu_dereference(ht
->t
);
395 return _ht_remove(ht
, t
, node
);
399 int ht_delete_dummy(struct rcu_ht
*ht
)
402 struct rcu_ht_node
*node
;
406 /* Check that the table is empty */
413 /* Internal sanity check: all nodes left should be dummy */
414 for (i
= 0; i
< t
->size
; i
++) {
415 assert(t
->tbl
[i
]->dummy
);
422 * Should only be called when no more concurrent readers nor writers can
423 * possibly access the table.
425 int ht_destroy(struct rcu_ht
*ht
)
429 ret
= ht_delete_dummy(ht
);
438 void ht_free_table_cb(struct rcu_head
*head
)
440 struct rcu_table
*t
=
441 caa_container_of(head
, struct rcu_table
, head
);
445 /* called with resize mutex held */
447 void _do_ht_resize(struct rcu_ht
*ht
)
449 unsigned long new_size
, old_size
;
450 struct rcu_table
*new_t
, *old_t
;
453 old_size
= old_t
->size
;
455 new_size
= CMM_LOAD_SHARED(old_t
->resize_target
);
456 dbg_printf("rculfhash: resize from %lu to %lu buckets\n",
458 if (old_size
== new_size
)
460 new_t
= malloc(sizeof(struct rcu_table
)
461 + (new_size
* sizeof(struct rcu_ht_node
*)));
462 assert(new_size
> old_size
);
463 memcpy(&new_t
->tbl
, &old_t
->tbl
,
464 old_size
* sizeof(struct rcu_ht_node
*));
465 init_table(ht
, new_t
, old_size
, new_size
- old_size
);
466 /* Changing table and size atomically wrt lookups */
467 rcu_assign_pointer(ht
->t
, new_t
);
468 ht
->ht_call_rcu(&old_t
->head
, ht_free_table_cb
);
472 unsigned long resize_target_update(struct rcu_table
*t
,
475 return _uatomic_max(&t
->resize_target
,
476 t
->size
<< growth_order
);
479 void ht_resize(struct rcu_ht
*ht
, int growth
)
481 struct rcu_table
*t
= rcu_dereference(ht
->t
);
482 unsigned long target_size
;
484 target_size
= resize_target_update(t
, growth
);
485 if (t
->size
< target_size
) {
486 pthread_mutex_lock(&ht
->resize_mutex
);
488 pthread_mutex_unlock(&ht
->resize_mutex
);
493 void do_resize_cb(struct rcu_head
*head
)
495 struct rcu_resize_work
*work
=
496 caa_container_of(head
, struct rcu_resize_work
, head
);
497 struct rcu_ht
*ht
= work
->ht
;
499 pthread_mutex_lock(&ht
->resize_mutex
);
501 pthread_mutex_unlock(&ht
->resize_mutex
);
506 void ht_resize_lazy(struct rcu_ht
*ht
, struct rcu_table
*t
, int growth
)
508 struct rcu_resize_work
*work
;
509 unsigned long target_size
;
511 target_size
= resize_target_update(t
, growth
);
512 if (t
->size
< target_size
) {
513 work
= malloc(sizeof(*work
));
515 ht
->ht_call_rcu(&work
->head
, do_resize_cb
);