Fix reverse hash comparison
[urcu.git] / rculfhash.c
1 /*
2 * rculfhash.c
3 *
4 * Userspace RCU library - Lock-Free Expandable RCU Hash Table
5 *
6 * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #define _LGPL_SOURCE
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <string.h>
30
31 #include <urcu.h>
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>
38 #include <stdio.h>
39 #include <pthread.h>
40
41 #define DEBUG /* Test */
42
43 #ifdef DEBUG
44 #define dbg_printf(args...) printf(args)
45 #else
46 #define dbg_printf(args...)
47 #endif
48
49 #define CHAIN_LEN_TARGET 4
50 #define CHAIN_LEN_RESIZE_THRESHOLD 16
51
52 #ifndef max
53 #define max(a, b) ((a) > (b) ? (a) : (b))
54 #endif
55
56 struct rcu_table {
57 unsigned long size; /* always a power of 2 */
58 unsigned long resize_target;
59 struct rcu_head head;
60 struct rcu_ht_node *tbl[0];
61 };
62
63 struct rcu_ht {
64 struct rcu_table *t; /* shared */
65 ht_hash_fct hash_fct;
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));
71 };
72
73 struct rcu_resize_work {
74 struct rcu_head head;
75 struct rcu_ht *ht;
76 };
77
78 /*
79 * Algorithm to reverse bits in a word by lookup table, extended to
80 * 64-bit words.
81 * Source:
82 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
83 * Originally from Public Domain.
84 */
85
86 static const uint8_t BitReverseTable256[256] =
87 {
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)
92 };
93 #undef R2
94 #undef R4
95 #undef R6
96
97 static
98 uint8_t bit_reverse_u8(uint8_t v)
99 {
100 return BitReverseTable256[v];
101 }
102
103 static __attribute__((unused))
104 uint32_t bit_reverse_u32(uint32_t v)
105 {
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));
110 }
111
112 static __attribute__((unused))
113 uint64_t bit_reverse_u64(uint64_t v)
114 {
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));
123 }
124
125 static
126 unsigned long bit_reverse_ulong(unsigned long v)
127 {
128 #if (CAA_BITS_PER_LONG == 32)
129 return bit_reverse_u32(v);
130 #else
131 return bit_reverse_u64(v);
132 #endif
133 }
134
135 /*
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.
139 */
140 static const char LogTable256[256] =
141 {
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)
146 };
147
148 uint32_t log2_u32(uint32_t v)
149 {
150 uint32_t t, tt;
151
152 if ((tt = (v >> 16)))
153 return (t = (tt >> 8))
154 ? 24 + LogTable256[t]
155 : 16 + LogTable256[tt];
156 else
157 return (t = (v >> 8))
158 ? 8 + LogTable256[t]
159 : LogTable256[v];
160 }
161
162 static
163 void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth);
164
165 static
166 void check_resize(struct rcu_ht *ht, struct rcu_table *t,
167 uint32_t chain_len)
168 {
169 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
170 ht_resize_lazy(ht, t,
171 log2_u32(chain_len - CHAIN_LEN_TARGET));
172 }
173
174 static
175 struct rcu_ht_node *clear_flag(struct rcu_ht_node *node)
176 {
177 return (struct rcu_ht_node *) (((unsigned long) node) & ~0x1);
178 }
179
180 static
181 int is_removed(struct rcu_ht_node *node)
182 {
183 return ((unsigned long) node) & 0x1;
184 }
185
186 static
187 struct rcu_ht_node *flag_removed(struct rcu_ht_node *node)
188 {
189 return (struct rcu_ht_node *) (((unsigned long) node) | 0x1);
190 }
191
192 static
193 unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
194 {
195 unsigned long old1, old2;
196
197 old1 = uatomic_read(ptr);
198 do {
199 old2 = old1;
200 if (old2 >= v)
201 return old2;
202 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
203 return v;
204 }
205
206 static
207 void _ht_add(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node)
208 {
209 struct rcu_ht_node *iter_prev = NULL, *iter = NULL;
210
211 if (!t->size)
212 return;
213 for (;;) {
214 uint32_t chain_len = 0;
215
216 iter_prev = rcu_dereference(t->tbl[node->hash & (t->size - 1)]);
217 assert(iter_prev);
218 assert(iter_prev->reverse_hash <= node->reverse_hash);
219 for (;;) {
220 iter = clear_flag(rcu_dereference(iter_prev->next));
221 if (unlikely(!iter))
222 break;
223 if (iter->reverse_hash > node->reverse_hash)
224 break;
225 iter_prev = iter;
226 check_resize(ht, t, ++chain_len);
227 }
228 /* add in iter_prev->next */
229 if (is_removed(iter))
230 continue;
231 assert(node != iter);
232 node->next = iter;
233 assert(iter_prev != node);
234 if (uatomic_cmpxchg(&iter_prev->next, iter, node) != iter)
235 continue;
236 break;
237 }
238 }
239
240 static
241 int _ht_remove(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node)
242 {
243 struct rcu_ht_node *iter_prev, *iter, *next, *old;
244 unsigned long chain_len;
245 int found, ret = 0;
246 int flagged = 0;
247
248 retry:
249 chain_len = 0;
250 found = 0;
251 iter_prev = rcu_dereference(t->tbl[node->hash & (t->size - 1)]);
252 assert(iter_prev);
253 assert(iter_prev->reverse_hash <= node->reverse_hash);
254 for (;;) {
255 iter = clear_flag(rcu_dereference(iter_prev->next));
256 if (unlikely(!iter))
257 break;
258 if (unlikely(iter->reverse_hash > node->reverse_hash))
259 break;
260 if (iter == node) {
261 found = 1;
262 break;
263 }
264 iter_prev = iter;
265 }
266 if (!found) {
267 ret = -ENOENT;
268 goto end;
269 }
270 next = rcu_dereference(iter->next);
271 if (!flagged) {
272 if (is_removed(next)) {
273 ret = -ENOENT;
274 goto end;
275 }
276 /* set deletion flag */
277 if ((old = uatomic_cmpxchg(&iter->next, next,
278 flag_removed(next))) != next) {
279 if (old == flag_removed(next)) {
280 ret = -ENOENT;
281 goto end;
282 } else {
283 goto retry;
284 }
285 }
286 flagged = 1;
287 }
288 /*
289 * Remove the element from the list. Retry if there has been a
290 * concurrent add (there cannot be a concurrent delete, because
291 * we won the deletion flag cmpxchg).
292 */
293 if (uatomic_cmpxchg(&iter_prev->next, iter, clear_flag(next)) != iter)
294 goto retry;
295 end:
296 return ret;
297 }
298
299 static
300 void init_table(struct rcu_ht *ht, struct rcu_table *t,
301 unsigned long first, unsigned long len)
302 {
303 unsigned long i, end;
304
305 end = first + len;
306 for (i = first; i < end; i++) {
307 /* Update table size when power of two */
308 if (i != 0 && !(i & (i - 1)))
309 t->size = i;
310 t->tbl[i] = calloc(1, sizeof(struct rcu_ht_node));
311 t->tbl[i]->dummy = 1;
312 t->tbl[i]->hash = i;
313 t->tbl[i]->reverse_hash = bit_reverse_ulong(i);
314 _ht_add(ht, t, t->tbl[i]);
315 }
316 t->resize_target = t->size = end;
317 }
318
319 struct rcu_ht *ht_new(ht_hash_fct hash_fct,
320 ht_compare_fct compare_fct,
321 unsigned long hash_seed,
322 unsigned long init_size,
323 void (*ht_call_rcu)(struct rcu_head *head,
324 void (*func)(struct rcu_head *head)))
325 {
326 struct rcu_ht *ht;
327
328 ht = calloc(1, sizeof(struct rcu_ht));
329 ht->hash_fct = hash_fct;
330 ht->compare_fct = compare_fct;
331 ht->hash_seed = hash_seed;
332 ht->ht_call_rcu = ht_call_rcu;
333 /* this mutex should not nest in read-side C.S. */
334 pthread_mutex_init(&ht->resize_mutex, NULL);
335 ht->t = calloc(1, sizeof(struct rcu_table)
336 + (max(init_size, 1) * sizeof(struct rcu_ht_node *)));
337 ht->t->size = 0;
338 pthread_mutex_lock(&ht->resize_mutex);
339 init_table(ht, ht->t, 0, max(init_size, 1));
340 pthread_mutex_unlock(&ht->resize_mutex);
341 return ht;
342 }
343
344 struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key, size_t key_len)
345 {
346 struct rcu_table *t;
347 struct rcu_ht_node *node;
348 unsigned long hash, reverse_hash;
349
350 hash = ht->hash_fct(key, key_len, ht->hash_seed);
351 reverse_hash = bit_reverse_ulong(hash);
352
353 t = rcu_dereference(ht->t);
354 node = rcu_dereference(t->tbl[hash & (t->size - 1)]);
355 for (;;) {
356 if (unlikely(!node))
357 break;
358 if (unlikely(node->reverse_hash > reverse_hash)) {
359 node = NULL;
360 break;
361 }
362 if (!ht->compare_fct(node->key, node->key_len, key, key_len)) {
363 if (unlikely(is_removed(rcu_dereference(node->next))))
364 node = NULL;
365 break;
366 }
367 node = clear_flag(rcu_dereference(node->next));
368 }
369 return node;
370 }
371
372 void ht_add(struct rcu_ht *ht, struct rcu_ht_node *node)
373 {
374 struct rcu_table *t;
375
376 node->hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
377 node->reverse_hash = bit_reverse_ulong((unsigned long) node->hash);
378
379 t = rcu_dereference(ht->t);
380 _ht_add(ht, t, node);
381 }
382
383 int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node)
384 {
385 struct rcu_table *t;
386
387 t = rcu_dereference(ht->t);
388 return _ht_remove(ht, t, node);
389 }
390
391 static
392 int ht_delete_dummy(struct rcu_ht *ht)
393 {
394 struct rcu_table *t;
395 struct rcu_ht_node *node;
396 unsigned long i;
397
398 t = ht->t;
399 /* Check that the table is empty */
400 node = t->tbl[0];
401 do {
402 if (!node->dummy)
403 return -EPERM;
404 node = node->next;
405 } while (node);
406 /* Internal sanity check: all nodes left should be dummy */
407 for (i = 0; i < t->size; i++) {
408 assert(t->tbl[i]->dummy);
409 free(t->tbl[i]);
410 }
411 return 0;
412 }
413
414 /*
415 * Should only be called when no more concurrent readers nor writers can
416 * possibly access the table.
417 */
418 int ht_destroy(struct rcu_ht *ht)
419 {
420 int ret;
421
422 ret = ht_delete_dummy(ht);
423 if (ret)
424 return ret;
425 free(ht->t);
426 free(ht);
427 return ret;
428 }
429
430 static
431 void ht_free_table_cb(struct rcu_head *head)
432 {
433 struct rcu_table *t =
434 caa_container_of(head, struct rcu_table, head);
435 free(t);
436 }
437
438 /* called with resize mutex held */
439 static
440 void _do_ht_resize(struct rcu_ht *ht)
441 {
442 unsigned long new_size, old_size;
443 struct rcu_table *new_t, *old_t;
444
445 old_t = ht->t;
446 old_size = old_t->size;
447
448 new_size = CMM_LOAD_SHARED(old_t->resize_target);
449 dbg_printf("rculfhash: resize from %lu to %lu buckets\n",
450 old_size, new_size);
451 if (old_size == new_size)
452 return;
453 new_t = malloc(sizeof(struct rcu_table)
454 + (new_size * sizeof(struct rcu_ht_node *)));
455 assert(new_size > old_size);
456 memcpy(&new_t->tbl, &old_t->tbl,
457 old_size * sizeof(struct rcu_ht_node *));
458 init_table(ht, new_t, old_size, new_size - old_size);
459 /* Changing table and size atomically wrt lookups */
460 rcu_assign_pointer(ht->t, new_t);
461 ht->ht_call_rcu(&old_t->head, ht_free_table_cb);
462 }
463
464 static
465 unsigned long resize_target_update(struct rcu_table *t,
466 int growth_order)
467 {
468 return _uatomic_max(&t->resize_target,
469 t->size << growth_order);
470 }
471
472 void ht_resize(struct rcu_ht *ht, int growth)
473 {
474 struct rcu_table *t = rcu_dereference(ht->t);
475 unsigned long target_size;
476
477 target_size = resize_target_update(t, growth);
478 if (t->size < target_size) {
479 pthread_mutex_lock(&ht->resize_mutex);
480 _do_ht_resize(ht);
481 pthread_mutex_unlock(&ht->resize_mutex);
482 }
483 }
484
485 static
486 void do_resize_cb(struct rcu_head *head)
487 {
488 struct rcu_resize_work *work =
489 caa_container_of(head, struct rcu_resize_work, head);
490 struct rcu_ht *ht = work->ht;
491
492 pthread_mutex_lock(&ht->resize_mutex);
493 _do_ht_resize(ht);
494 pthread_mutex_unlock(&ht->resize_mutex);
495 free(work);
496 }
497
498 static
499 void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth)
500 {
501 struct rcu_resize_work *work;
502 unsigned long target_size;
503
504 target_size = resize_target_update(t, growth);
505 if (t->size < target_size) {
506 work = malloc(sizeof(*work));
507 work->ht = ht;
508 ht->ht_call_rcu(&work->head, do_resize_cb);
509 }
510 }
This page took 0.039225 seconds and 5 git commands to generate.