rculfhash: flip order of removal
[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 1
50 #define CHAIN_LEN_RESIZE_THRESHOLD 2
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 int resize_initiated;
60 struct rcu_head head;
61 struct rcu_ht_node *tbl[0];
62 };
63
64 struct rcu_ht {
65 struct rcu_table *t; /* shared */
66 ht_hash_fct hash_fct;
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));
72 };
73
74 struct rcu_resize_work {
75 struct rcu_head head;
76 struct rcu_ht *ht;
77 };
78
79 /*
80 * Algorithm to reverse bits in a word by lookup table, extended to
81 * 64-bit words.
82 * Source:
83 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
84 * Originally from Public Domain.
85 */
86
87 static const uint8_t BitReverseTable256[256] =
88 {
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)
93 };
94 #undef R2
95 #undef R4
96 #undef R6
97
98 static
99 uint8_t bit_reverse_u8(uint8_t v)
100 {
101 return BitReverseTable256[v];
102 }
103
104 static __attribute__((unused))
105 uint32_t bit_reverse_u32(uint32_t v)
106 {
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));
111 }
112
113 static __attribute__((unused))
114 uint64_t bit_reverse_u64(uint64_t v)
115 {
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));
124 }
125
126 static
127 unsigned long bit_reverse_ulong(unsigned long v)
128 {
129 #if (CAA_BITS_PER_LONG == 32)
130 return bit_reverse_u32(v);
131 #else
132 return bit_reverse_u64(v);
133 #endif
134 }
135
136 /*
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.
140 */
141 static const char LogTable256[256] =
142 {
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)
147 };
148
149 uint32_t log2_u32(uint32_t v)
150 {
151 uint32_t t, tt;
152
153 if ((tt = (v >> 16)))
154 return (t = (tt >> 8))
155 ? 24 + LogTable256[t]
156 : 16 + LogTable256[tt];
157 else
158 return (t = (v >> 8))
159 ? 8 + LogTable256[t]
160 : LogTable256[v];
161 }
162
163 static
164 void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth);
165
166 static
167 void check_resize(struct rcu_ht *ht, struct rcu_table *t,
168 uint32_t chain_len)
169 {
170 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
171 ht_resize_lazy(ht, t,
172 log2_u32(chain_len - CHAIN_LEN_TARGET - 1));
173 }
174
175 static
176 struct rcu_ht_node *clear_flag(struct rcu_ht_node *node)
177 {
178 return (struct rcu_ht_node *) (((unsigned long) node) & ~0x1);
179 }
180
181 static
182 int is_removed(struct rcu_ht_node *node)
183 {
184 return ((unsigned long) node) & 0x1;
185 }
186
187 static
188 struct rcu_ht_node *flag_removed(struct rcu_ht_node *node)
189 {
190 return (struct rcu_ht_node *) (((unsigned long) node) | 0x1);
191 }
192
193 static
194 unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
195 {
196 unsigned long old1, old2;
197
198 old1 = uatomic_read(ptr);
199 do {
200 old2 = old1;
201 if (old2 >= v)
202 return old2;
203 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
204 return v;
205 }
206
207 static
208 int _ht_add(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node,
209 int unique)
210 {
211 struct rcu_ht_node *iter_prev, *iter, *iter_prev_next, *next;
212
213 if (!t->size)
214 return 0;
215 for (;;) {
216 uint32_t chain_len = 0;
217
218 /*
219 * iter_prev points to the non-removed node prior to the
220 * insert location.
221 * iter iterates until it finds the next non-removed
222 * node.
223 */
224 iter_prev = rcu_dereference(t->tbl[node->hash & (t->size - 1)]);
225 /* We can always skip the dummy node initially */
226 iter_prev_next = next = rcu_dereference(iter_prev->next);
227 assert(iter_prev);
228 assert(iter_prev->reverse_hash <= node->reverse_hash);
229 for (;;) {
230 iter = next;
231 if (unlikely(!clear_flag(iter)))
232 break;
233 next = rcu_dereference(clear_flag(iter)->next);
234 if (unlikely(is_removed(next)))
235 continue;
236 if (unique
237 && !clear_flag(iter)->dummy
238 && !ht->compare_fct(node->key, node->key_len,
239 clear_flag(iter)->key,
240 clear_flag(iter)->key_len))
241 return -EEXIST;
242 if (clear_flag(iter)->reverse_hash > node->reverse_hash)
243 break;
244 /* Only account for identical reverse hash once */
245 if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash)
246 check_resize(ht, t, ++chain_len);
247 iter_prev = clear_flag(iter);
248 iter_prev_next = next;
249 }
250 assert(node != clear_flag(iter));
251 assert(!is_removed(iter_prev));
252 assert(iter_prev != node);
253 node->next = iter;
254 if (uatomic_cmpxchg(&iter_prev->next, iter_prev_next,
255 node) != iter_prev_next)
256 continue;
257 else
258 break;
259 }
260 return 0;
261 }
262
263 static
264 int _ht_remove(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node)
265 {
266 struct rcu_ht_node *iter_prev, *iter, *iter_prev_next, *next, *old;
267 int found;
268 int flagged = 0;
269
270 /* logically delete the node */
271 old = rcu_dereference(node->next);
272 do {
273 next = old;
274 if (is_removed(next))
275 goto end;
276 old = uatomic_cmpxchg(&node->next, next,
277 flag_removed(next));
278 } while (old != next);
279
280 /* We performed the (logical) deletion. */
281 flagged = 1;
282
283 /*
284 * Ensure that the node is not visible to readers anymore: lookup for
285 * the node, and remove it if found.
286 */
287 retry:
288 found = 0;
289 /*
290 * iter_prev points to the non-removed node prior to the remove
291 * location.
292 * next points to the non-removed node following the remove location.
293 */
294 iter_prev = rcu_dereference(t->tbl[node->hash & (t->size - 1)]);
295 /* We can always skip the dummy node initially */
296 iter_prev_next = next = rcu_dereference(iter_prev->next);
297 assert(iter_prev);
298 assert(iter_prev->reverse_hash <= node->reverse_hash);
299 for (;;) {
300 iter = next;
301 if (unlikely(!clear_flag(iter)))
302 break;
303 next = rcu_dereference(clear_flag(iter)->next);
304 if (clear_flag(iter) == node) {
305 found = 1;
306 assert(is_removed(rcu_dereference(node->next)));
307 break;
308 }
309 if (unlikely(is_removed(next)))
310 continue;
311 if (clear_flag(iter)->reverse_hash > node->reverse_hash)
312 break;
313 iter_prev = clear_flag(iter);
314 iter_prev_next = next;
315 }
316 if (!found)
317 goto end;
318 if (uatomic_cmpxchg(&iter_prev->next, iter_prev_next,
319 clear_flag(next)) != iter_prev_next)
320 goto retry;
321 end:
322 /*
323 * Only the flagging action indicated that we (and no other)
324 * removed the node from the hash.
325 */
326 if (flagged) {
327 assert(is_removed(rcu_dereference(node->next)));
328 return 0;
329 } else
330 return -ENOENT;
331 }
332
333 static
334 void init_table(struct rcu_ht *ht, struct rcu_table *t,
335 unsigned long first, unsigned long len)
336 {
337 unsigned long i, end;
338
339 end = first + len;
340 for (i = first; i < end; i++) {
341 /* Update table size when power of two */
342 if (i != 0 && !(i & (i - 1)))
343 t->size = i;
344 t->tbl[i] = calloc(1, sizeof(struct rcu_ht_node));
345 t->tbl[i]->dummy = 1;
346 t->tbl[i]->hash = i;
347 t->tbl[i]->reverse_hash = bit_reverse_ulong(i);
348 (void) _ht_add(ht, t, t->tbl[i], 0);
349 }
350 t->resize_target = t->size = end;
351 t->resize_initiated = 0;
352 }
353
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)))
360 {
361 struct rcu_ht *ht;
362
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 *)));
372 ht->t->size = 0;
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);
376 return ht;
377 }
378
379 struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key, size_t key_len)
380 {
381 struct rcu_table *t;
382 struct rcu_ht_node *node;
383 unsigned long hash, reverse_hash;
384
385 hash = ht->hash_fct(key, key_len, ht->hash_seed);
386 reverse_hash = bit_reverse_ulong(hash);
387
388 t = rcu_dereference(ht->t);
389 node = rcu_dereference(t->tbl[hash & (t->size - 1)]);
390 for (;;) {
391 if (unlikely(!node))
392 break;
393 if (unlikely(node->reverse_hash > reverse_hash)) {
394 node = NULL;
395 break;
396 }
397 if (!ht->compare_fct(node->key, node->key_len, key, key_len)) {
398 if (unlikely(is_removed(rcu_dereference(node->next))))
399 node = NULL;
400 break;
401 }
402 node = clear_flag(rcu_dereference(node->next));
403 }
404 return node;
405 }
406
407 void ht_add(struct rcu_ht *ht, struct rcu_ht_node *node)
408 {
409 struct rcu_table *t;
410
411 node->hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
412 node->reverse_hash = bit_reverse_ulong((unsigned long) node->hash);
413
414 t = rcu_dereference(ht->t);
415 (void) _ht_add(ht, t, node, 0);
416 }
417
418 int ht_add_unique(struct rcu_ht *ht, struct rcu_ht_node *node)
419 {
420 struct rcu_table *t;
421
422 node->hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
423 node->reverse_hash = bit_reverse_ulong((unsigned long) node->hash);
424
425 t = rcu_dereference(ht->t);
426 return _ht_add(ht, t, node, 1);
427 }
428
429 int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node)
430 {
431 struct rcu_table *t;
432
433 t = rcu_dereference(ht->t);
434 return _ht_remove(ht, t, node);
435 }
436
437 static
438 int ht_delete_dummy(struct rcu_ht *ht)
439 {
440 struct rcu_table *t;
441 struct rcu_ht_node *node;
442 unsigned long i;
443
444 t = ht->t;
445 /* Check that the table is empty */
446 node = t->tbl[0];
447 do {
448 if (!node->dummy)
449 return -EPERM;
450 node = node->next;
451 } while (node);
452 /* Internal sanity check: all nodes left should be dummy */
453 for (i = 0; i < t->size; i++) {
454 assert(t->tbl[i]->dummy);
455 free(t->tbl[i]);
456 }
457 return 0;
458 }
459
460 /*
461 * Should only be called when no more concurrent readers nor writers can
462 * possibly access the table.
463 */
464 int ht_destroy(struct rcu_ht *ht)
465 {
466 int ret;
467
468 ret = ht_delete_dummy(ht);
469 if (ret)
470 return ret;
471 free(ht->t);
472 free(ht);
473 return ret;
474 }
475
476 static
477 void ht_free_table_cb(struct rcu_head *head)
478 {
479 struct rcu_table *t =
480 caa_container_of(head, struct rcu_table, head);
481 free(t);
482 }
483
484 /* called with resize mutex held */
485 static
486 void _do_ht_resize(struct rcu_ht *ht)
487 {
488 unsigned long new_size, old_size;
489 struct rcu_table *new_t, *old_t;
490
491 old_t = ht->t;
492 old_size = old_t->size;
493
494 new_size = CMM_LOAD_SHARED(old_t->resize_target);
495 dbg_printf("rculfhash: resize from %lu to %lu buckets\n",
496 old_size, new_size);
497 if (old_size == new_size)
498 return;
499 new_t = malloc(sizeof(struct rcu_table)
500 + (new_size * sizeof(struct rcu_ht_node *)));
501 assert(new_size > old_size);
502 memcpy(&new_t->tbl, &old_t->tbl,
503 old_size * sizeof(struct rcu_ht_node *));
504 init_table(ht, new_t, old_size, new_size - old_size);
505 /* Changing table and size atomically wrt lookups */
506 rcu_assign_pointer(ht->t, new_t);
507 ht->ht_call_rcu(&old_t->head, ht_free_table_cb);
508 }
509
510 static
511 unsigned long resize_target_update(struct rcu_table *t,
512 int growth_order)
513 {
514 return _uatomic_max(&t->resize_target,
515 t->size << growth_order);
516 }
517
518 void ht_resize(struct rcu_ht *ht, int growth)
519 {
520 struct rcu_table *t = rcu_dereference(ht->t);
521 unsigned long target_size;
522
523 target_size = resize_target_update(t, growth);
524 if (t->size < target_size) {
525 CMM_STORE_SHARED(t->resize_initiated, 1);
526 pthread_mutex_lock(&ht->resize_mutex);
527 _do_ht_resize(ht);
528 pthread_mutex_unlock(&ht->resize_mutex);
529 }
530 }
531
532 static
533 void do_resize_cb(struct rcu_head *head)
534 {
535 struct rcu_resize_work *work =
536 caa_container_of(head, struct rcu_resize_work, head);
537 struct rcu_ht *ht = work->ht;
538
539 pthread_mutex_lock(&ht->resize_mutex);
540 _do_ht_resize(ht);
541 pthread_mutex_unlock(&ht->resize_mutex);
542 free(work);
543 }
544
545 static
546 void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth)
547 {
548 struct rcu_resize_work *work;
549 unsigned long target_size;
550
551 target_size = resize_target_update(t, growth);
552 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
553 work = malloc(sizeof(*work));
554 work->ht = ht;
555 ht->ht_call_rcu(&work->head, do_resize_cb);
556 CMM_STORE_SHARED(t->resize_initiated, 1);
557 }
558 }
This page took 0.039911 seconds and 5 git commands to generate.