rculfhash: Update API documentation
[urcu.git] / rculfhash.c
CommitLineData
5e28c532 1/*
abc490a1
MD
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
5e28c532
MD
21 */
22
e753ff5a
MD
23/*
24 * Based on the following articles:
25 * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
26 * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
27 * - Michael, M. M. High performance dynamic lock-free hash tables
28 * and list-based sets. In Proceedings of the fourteenth annual ACM
29 * symposium on Parallel algorithms and architectures, ACM Press,
30 * (2002), 73-82.
31 *
32 * Some specificities of this Lock-Free Expandable RCU Hash Table
33 * implementation:
34 *
35 * - RCU read-side critical section allows readers to perform hash
36 * table lookups and use the returned objects safely by delaying
37 * memory reclaim of a grace period.
38 * - Add and remove operations are lock-free, and do not need to
39 * allocate memory. They need to be executed within RCU read-side
40 * critical section to ensure the objects they read are valid and to
41 * deal with the cmpxchg ABA problem.
42 * - add and add_unique operations are supported. add_unique checks if
43 * the node key already exists in the hash table. It ensures no key
44 * duplicata exists.
45 * - The resize operation executes concurrently with add/remove/lookup.
46 * - Hash table nodes are contained within a split-ordered list. This
47 * list is ordered by incrementing reversed-bits-hash value.
48 * - An index of dummy nodes is kept. These dummy nodes are the hash
49 * table "buckets", and they are also chained together in the
50 * split-ordered list, which allows recursive expansion.
51 * - The resize operation only allows expanding the hash table.
52 * It is triggered either through an API call or automatically by
53 * detecting long chains in the add operation.
54 * - Resize operation initiated by long chain detection is executed by a
55 * call_rcu thread, which keeps lock-freedom of add and remove.
56 * - Resize operations are protected by a mutex.
57 * - The removal operation is split in two parts: first, a "removed"
58 * flag is set in the next pointer within the node to remove. Then,
59 * a "garbage collection" is performed in the bucket containing the
60 * removed node (from the start of the bucket up to the removed node).
61 * All encountered nodes with "removed" flag set in their next
62 * pointers are removed from the linked-list. If the cmpxchg used for
63 * removal fails (due to concurrent garbage-collection or concurrent
64 * add), we retry from the beginning of the bucket. This ensures that
65 * the node with "removed" flag set is removed from the hash table
66 * (not visible to lookups anymore) before the RCU read-side critical
67 * section held across removal ends. Furthermore, this ensures that
68 * the node with "removed" flag set is removed from the linked-list
69 * before its memory is reclaimed. Only the thread which removal
70 * successfully set the "removed" flag (with a cmpxchg) into a node's
71 * next pointer is considered to have succeeded its removal (and thus
72 * owns the node to reclaim). Because we garbage-collect starting from
73 * an invariant node (the start-of-bucket dummy node) up to the
74 * "removed" node (or find a reverse-hash that is higher), we are sure
75 * that a successful traversal of the chain leads to a chain that is
76 * present in the linked-list (the start node is never removed) and
77 * that is does not contain the "removed" node anymore, even if
78 * concurrent delete/add operations are changing the structure of the
79 * list concurrently.
29e669f6
MD
80 * - The add operation performs gargage collection of buckets if it
81 * encounters nodes with removed flag set in the bucket where it wants
82 * to add its new node. This ensures lock-freedom of add operation by
83 * helping the remover unlink nodes from the list rather than to wait
84 * for it do to so.
e753ff5a
MD
85 * - A RCU "order table" indexed by log2(hash index) is copied and
86 * expanded by the resize operation. This order table allows finding
87 * the "dummy node" tables.
88 * - There is one dummy node table per hash index order. The size of
89 * each dummy node table is half the number of hashes contained in
90 * this order.
91 * - call_rcu is used to garbage-collect the old order table.
92 * - The per-order dummy node tables contain a compact version of the
93 * hash table nodes. These tables are invariant after they are
94 * populated into the hash table.
95 */
96
2ed95849
MD
97#define _LGPL_SOURCE
98#include <stdlib.h>
e0ba718a
MD
99#include <errno.h>
100#include <assert.h>
101#include <stdio.h>
abc490a1 102#include <stdint.h>
f000907d 103#include <string.h>
e0ba718a 104
2ed95849 105#include <urcu.h>
abc490a1 106#include <urcu-call-rcu.h>
a42cc659
MD
107#include <urcu/arch.h>
108#include <urcu/uatomic.h>
674f7a69 109#include <urcu/jhash.h>
a42cc659 110#include <urcu/compiler.h>
abc490a1 111#include <urcu/rculfhash.h>
5e28c532 112#include <stdio.h>
464a1ec9 113#include <pthread.h>
44395fb7 114
f9830efd 115#ifdef DEBUG
f0c29ed7 116#define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
f9830efd 117#else
e753ff5a 118#define dbg_printf(fmt, args...)
f9830efd
MD
119#endif
120
01370f0b
MD
121#define CHAIN_LEN_TARGET 4
122#define CHAIN_LEN_RESIZE_THRESHOLD 8
2ed95849 123
abc490a1
MD
124#ifndef max
125#define max(a, b) ((a) > (b) ? (a) : (b))
126#endif
2ed95849 127
d95bd160
MD
128/*
129 * The removed flag needs to be updated atomically with the pointer.
130 * The dummy flag does not require to be updated atomically with the
131 * pointer, but it is added as a pointer low bit flag to save space.
132 */
d37166c6 133#define REMOVED_FLAG (1UL << 0)
f5596c94
MD
134#define DUMMY_FLAG (1UL << 1)
135#define FLAGS_MASK ((1UL << 2) - 1)
d37166c6 136
395270b6 137struct rcu_table {
abc490a1 138 unsigned long size; /* always a power of 2 */
f9830efd 139 unsigned long resize_target;
11519af6 140 int resize_initiated;
abc490a1 141 struct rcu_head head;
14044b37 142 struct _cds_lfht_node *tbl[0];
395270b6
MD
143};
144
14044b37 145struct cds_lfht {
395270b6 146 struct rcu_table *t; /* shared */
14044b37
MD
147 cds_lfht_hash_fct hash_fct;
148 cds_lfht_compare_fct compare_fct;
732ad076 149 unsigned long hash_seed;
464a1ec9 150 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
33c7c748 151 unsigned int in_progress_resize, in_progress_destroy;
14044b37 152 void (*cds_lfht_call_rcu)(struct rcu_head *head,
abc490a1 153 void (*func)(struct rcu_head *head));
2ed95849
MD
154};
155
abc490a1
MD
156struct rcu_resize_work {
157 struct rcu_head head;
14044b37 158 struct cds_lfht *ht;
abc490a1 159};
2ed95849 160
abc490a1
MD
161/*
162 * Algorithm to reverse bits in a word by lookup table, extended to
163 * 64-bit words.
f9830efd 164 * Source:
abc490a1 165 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
f9830efd 166 * Originally from Public Domain.
abc490a1
MD
167 */
168
169static const uint8_t BitReverseTable256[256] =
2ed95849 170{
abc490a1
MD
171#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
172#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
173#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
174 R6(0), R6(2), R6(1), R6(3)
175};
176#undef R2
177#undef R4
178#undef R6
2ed95849 179
abc490a1
MD
180static
181uint8_t bit_reverse_u8(uint8_t v)
182{
183 return BitReverseTable256[v];
184}
ab7d5fc6 185
abc490a1
MD
186static __attribute__((unused))
187uint32_t bit_reverse_u32(uint32_t v)
188{
189 return ((uint32_t) bit_reverse_u8(v) << 24) |
190 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
191 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
192 ((uint32_t) bit_reverse_u8(v >> 24));
2ed95849
MD
193}
194
abc490a1
MD
195static __attribute__((unused))
196uint64_t bit_reverse_u64(uint64_t v)
2ed95849 197{
abc490a1
MD
198 return ((uint64_t) bit_reverse_u8(v) << 56) |
199 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
200 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
201 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
202 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
203 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
204 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
205 ((uint64_t) bit_reverse_u8(v >> 56));
206}
207
208static
209unsigned long bit_reverse_ulong(unsigned long v)
210{
211#if (CAA_BITS_PER_LONG == 32)
212 return bit_reverse_u32(v);
213#else
214 return bit_reverse_u64(v);
215#endif
216}
217
f9830efd 218/*
24365af7
MD
219 * fls: returns the position of the most significant bit.
220 * Returns 0 if no bit is set, else returns the position of the most
221 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
f9830efd 222 */
24365af7
MD
223#if defined(__i386) || defined(__x86_64)
224static inline
225unsigned int fls_u32(uint32_t x)
f9830efd 226{
24365af7
MD
227 int r;
228
229 asm("bsrl %1,%0\n\t"
230 "jnz 1f\n\t"
231 "movl $-1,%0\n\t"
232 "1:\n\t"
233 : "=r" (r) : "rm" (x));
234 return r + 1;
235}
236#define HAS_FLS_U32
237#endif
238
239#if defined(__x86_64)
240static inline
241unsigned int fls_u64(uint64_t x)
242{
243 long r;
244
245 asm("bsrq %1,%0\n\t"
246 "jnz 1f\n\t"
247 "movq $-1,%0\n\t"
248 "1:\n\t"
249 : "=r" (r) : "rm" (x));
250 return r + 1;
251}
252#define HAS_FLS_U64
253#endif
254
255#ifndef HAS_FLS_U64
256static __attribute__((unused))
257unsigned int fls_u64(uint64_t x)
258{
259 unsigned int r = 64;
260
261 if (!x)
262 return 0;
263
264 if (!(x & 0xFFFFFFFF00000000ULL)) {
265 x <<= 32;
266 r -= 32;
267 }
268 if (!(x & 0xFFFF000000000000ULL)) {
269 x <<= 16;
270 r -= 16;
271 }
272 if (!(x & 0xFF00000000000000ULL)) {
273 x <<= 8;
274 r -= 8;
275 }
276 if (!(x & 0xF000000000000000ULL)) {
277 x <<= 4;
278 r -= 4;
279 }
280 if (!(x & 0xC000000000000000ULL)) {
281 x <<= 2;
282 r -= 2;
283 }
284 if (!(x & 0x8000000000000000ULL)) {
285 x <<= 1;
286 r -= 1;
287 }
288 return r;
289}
290#endif
291
292#ifndef HAS_FLS_U32
293static __attribute__((unused))
294unsigned int fls_u32(uint32_t x)
295{
296 unsigned int r = 32;
f9830efd 297
24365af7
MD
298 if (!x)
299 return 0;
300 if (!(x & 0xFFFF0000U)) {
301 x <<= 16;
302 r -= 16;
303 }
304 if (!(x & 0xFF000000U)) {
305 x <<= 8;
306 r -= 8;
307 }
308 if (!(x & 0xF0000000U)) {
309 x <<= 4;
310 r -= 4;
311 }
312 if (!(x & 0xC0000000U)) {
313 x <<= 2;
314 r -= 2;
315 }
316 if (!(x & 0x80000000U)) {
317 x <<= 1;
318 r -= 1;
319 }
320 return r;
321}
322#endif
323
324unsigned int fls_ulong(unsigned long x)
f9830efd 325{
24365af7
MD
326#if (CAA_BITS_PER_lONG == 32)
327 return fls_u32(x);
328#else
329 return fls_u64(x);
330#endif
331}
f9830efd 332
24365af7
MD
333int get_count_order_u32(uint32_t x)
334{
335 int order;
336
337 order = fls_u32(x) - 1;
338 if (x & (x - 1))
339 order++;
340 return order;
341}
342
343int get_count_order_ulong(unsigned long x)
344{
345 int order;
346
347 order = fls_ulong(x) - 1;
348 if (x & (x - 1))
349 order++;
350 return order;
f9830efd
MD
351}
352
353static
14044b37 354void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth);
f9830efd
MD
355
356static
14044b37 357void check_resize(struct cds_lfht *ht, struct rcu_table *t,
f9830efd
MD
358 uint32_t chain_len)
359{
24365af7 360 if (chain_len > 100)
f0c29ed7 361 dbg_printf("WARNING: large chain length: %u.\n",
24365af7 362 chain_len);
3390d470 363 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
14044b37 364 cds_lfht_resize_lazy(ht, t,
01370f0b 365 get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
f9830efd
MD
366}
367
abc490a1 368static
14044b37 369struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
abc490a1 370{
14044b37 371 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
abc490a1
MD
372}
373
374static
14044b37 375int is_removed(struct cds_lfht_node *node)
abc490a1 376{
d37166c6 377 return ((unsigned long) node) & REMOVED_FLAG;
abc490a1
MD
378}
379
380static
14044b37 381struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
abc490a1 382{
14044b37 383 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
abc490a1
MD
384}
385
f5596c94 386static
14044b37 387int is_dummy(struct cds_lfht_node *node)
f5596c94
MD
388{
389 return ((unsigned long) node) & DUMMY_FLAG;
390}
391
392static
14044b37 393struct cds_lfht_node *flag_dummy(struct cds_lfht_node *node)
f5596c94 394{
14044b37 395 return (struct cds_lfht_node *) (((unsigned long) node) | DUMMY_FLAG);
f5596c94
MD
396}
397
abc490a1 398static
f9830efd 399unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
abc490a1
MD
400{
401 unsigned long old1, old2;
402
403 old1 = uatomic_read(ptr);
404 do {
405 old2 = old1;
406 if (old2 >= v)
f9830efd 407 return old2;
abc490a1 408 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
f9830efd 409 return v;
abc490a1
MD
410}
411
273399de
MD
412/*
413 * Remove all logically deleted nodes from a bucket up to a certain node key.
414 */
415static
14044b37 416void _cds_lfht_gc_bucket(struct cds_lfht_node *dummy, struct cds_lfht_node *node)
273399de 417{
14044b37 418 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
273399de
MD
419
420 for (;;) {
421 iter_prev = dummy;
422 /* We can always skip the dummy node initially */
cc4fcb10
MD
423 iter = rcu_dereference(iter_prev->p.next);
424 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
273399de 425 for (;;) {
a2974903 426 if (unlikely(!clear_flag(iter)))
479c8a32 427 return;
76412f24 428 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
273399de 429 return;
cc4fcb10 430 next = rcu_dereference(clear_flag(iter)->p.next);
76412f24 431 if (likely(is_removed(next)))
273399de 432 break;
b453eae1 433 iter_prev = clear_flag(iter);
273399de
MD
434 iter = next;
435 }
436 assert(!is_removed(iter));
f5596c94
MD
437 if (is_dummy(iter))
438 new_next = flag_dummy(clear_flag(next));
439 else
440 new_next = clear_flag(next);
441 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
273399de
MD
442 }
443}
444
abc490a1 445static
14044b37
MD
446struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, struct rcu_table *t,
447 struct cds_lfht_node *node, int unique, int dummy)
abc490a1 448{
14044b37 449 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
f5596c94 450 *dummy_node;
14044b37 451 struct _cds_lfht_node *lookup;
24365af7 452 unsigned long hash, index, order;
abc490a1 453
18117871 454 if (!t->size) {
f5596c94
MD
455 assert(dummy);
456 node->p.next = flag_dummy(NULL);
18117871
MD
457 return node; /* Initial first add (head) */
458 }
cc4fcb10 459 hash = bit_reverse_ulong(node->p.reverse_hash);
abc490a1 460 for (;;) {
f9830efd 461 uint32_t chain_len = 0;
abc490a1 462
11519af6
MD
463 /*
464 * iter_prev points to the non-removed node prior to the
465 * insert location.
11519af6 466 */
24365af7
MD
467 index = hash & (t->size - 1);
468 order = get_count_order_ulong(index + 1);
469 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
14044b37 470 iter_prev = (struct cds_lfht_node *) lookup;
11519af6 471 /* We can always skip the dummy node initially */
cc4fcb10
MD
472 iter = rcu_dereference(iter_prev->p.next);
473 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
abc490a1 474 for (;;) {
a2974903 475 if (unlikely(!clear_flag(iter)))
273399de 476 goto insert;
76412f24 477 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
273399de 478 goto insert;
cc4fcb10 479 next = rcu_dereference(clear_flag(iter)->p.next);
76412f24 480 if (unlikely(is_removed(next)))
9dba85be 481 goto gc_node;
e43f23f8 482 if (unique
1b81fe1a 483 && !is_dummy(next)
e43f23f8
MD
484 && !ht->compare_fct(node->key, node->key_len,
485 clear_flag(iter)->key,
486 clear_flag(iter)->key_len))
18117871 487 return clear_flag(iter);
11519af6 488 /* Only account for identical reverse hash once */
24365af7
MD
489 if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash
490 && !is_dummy(next))
11519af6
MD
491 check_resize(ht, t, ++chain_len);
492 iter_prev = clear_flag(iter);
273399de 493 iter = next;
abc490a1 494 }
273399de 495 insert:
7ec59d3b 496 assert(node != clear_flag(iter));
11519af6 497 assert(!is_removed(iter_prev));
f000907d 498 assert(iter_prev != node);
f5596c94 499 if (!dummy)
1b81fe1a 500 node->p.next = clear_flag(iter);
f5596c94 501 else
1b81fe1a 502 node->p.next = flag_dummy(clear_flag(iter));
f5596c94
MD
503 if (is_dummy(iter))
504 new_node = flag_dummy(node);
505 else
506 new_node = node;
cc4fcb10 507 if (uatomic_cmpxchg(&iter_prev->p.next, iter,
f5596c94 508 new_node) != iter)
273399de 509 continue; /* retry */
11519af6 510 else
273399de 511 goto gc_end;
9dba85be
MD
512 gc_node:
513 assert(!is_removed(iter));
f5596c94
MD
514 if (is_dummy(iter))
515 new_next = flag_dummy(clear_flag(next));
516 else
517 new_next = clear_flag(next);
518 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
273399de 519 /* retry */
464a1ec9 520 }
273399de
MD
521gc_end:
522 /* Garbage collect logically removed nodes in the bucket */
24365af7
MD
523 index = hash & (t->size - 1);
524 order = get_count_order_ulong(index + 1);
525 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
14044b37
MD
526 dummy_node = (struct cds_lfht_node *) lookup;
527 _cds_lfht_gc_bucket(dummy_node, node);
18117871 528 return node;
abc490a1 529}
464a1ec9 530
abc490a1 531static
14044b37
MD
532int _cds_lfht_remove(struct cds_lfht *ht, struct rcu_table *t,
533 struct cds_lfht_node *node)
abc490a1 534{
14044b37
MD
535 struct cds_lfht_node *dummy, *next, *old;
536 struct _cds_lfht_node *lookup;
abc490a1 537 int flagged = 0;
24365af7 538 unsigned long hash, index, order;
5e28c532 539
7ec59d3b 540 /* logically delete the node */
cc4fcb10 541 old = rcu_dereference(node->p.next);
7ec59d3b
MD
542 do {
543 next = old;
76412f24 544 if (unlikely(is_removed(next)))
7ec59d3b 545 goto end;
1b81fe1a 546 assert(!is_dummy(next));
cc4fcb10 547 old = uatomic_cmpxchg(&node->p.next, next,
7ec59d3b
MD
548 flag_removed(next));
549 } while (old != next);
550
551 /* We performed the (logical) deletion. */
552 flagged = 1;
553
554 /*
555 * Ensure that the node is not visible to readers anymore: lookup for
273399de
MD
556 * the node, and remove it (along with any other logically removed node)
557 * if found.
11519af6 558 */
cc4fcb10 559 hash = bit_reverse_ulong(node->p.reverse_hash);
24365af7
MD
560 index = hash & (t->size - 1);
561 order = get_count_order_ulong(index + 1);
562 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
14044b37
MD
563 dummy = (struct cds_lfht_node *) lookup;
564 _cds_lfht_gc_bucket(dummy, node);
2ed95849 565end:
11519af6
MD
566 /*
567 * Only the flagging action indicated that we (and no other)
568 * removed the node from the hash.
569 */
7ec59d3b 570 if (flagged) {
cc4fcb10 571 assert(is_removed(rcu_dereference(node->p.next)));
11519af6 572 return 0;
7ec59d3b 573 } else
11519af6 574 return -ENOENT;
abc490a1 575}
2ed95849 576
abc490a1 577static
14044b37 578void init_table(struct cds_lfht *ht, struct rcu_table *t,
24365af7
MD
579 unsigned long first_order, unsigned long len_order)
580{
581 unsigned long i, end_order;
582
f0c29ed7 583 dbg_printf("init table: first_order %lu end_order %lu\n",
24365af7
MD
584 first_order, first_order + len_order);
585 end_order = first_order + len_order;
586 t->size = !first_order ? 0 : (1UL << (first_order - 1));
587 for (i = first_order; i < end_order; i++) {
588 unsigned long j, len;
589
590 len = !i ? 1 : 1UL << (i - 1);
f0c29ed7 591 dbg_printf("init order %lu len: %lu\n", i, len);
14044b37 592 t->tbl[i] = calloc(len, sizeof(struct _cds_lfht_node));
24365af7 593 for (j = 0; j < len; j++) {
f0c29ed7 594 dbg_printf("init entry: i %lu j %lu hash %lu\n",
24365af7 595 i, j, !i ? 0 : (1UL << (i - 1)) + j);
14044b37
MD
596 struct cds_lfht_node *new_node =
597 (struct cds_lfht_node *) &t->tbl[i][j];
24365af7
MD
598 new_node->p.reverse_hash =
599 bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j);
14044b37 600 (void) _cds_lfht_add(ht, t, new_node, 0, 1);
33c7c748
MD
601 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
602 break;
24365af7
MD
603 }
604 /* Update table size */
605 t->size = !i ? 1 : (1UL << i);
f0c29ed7 606 dbg_printf("init new size: %lu\n", t->size);
33c7c748
MD
607 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
608 break;
abc490a1 609 }
24365af7 610 t->resize_target = t->size;
11519af6 611 t->resize_initiated = 0;
2ed95849
MD
612}
613
14044b37
MD
614struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
615 cds_lfht_compare_fct compare_fct,
616 unsigned long hash_seed,
617 unsigned long init_size,
618 void (*cds_lfht_call_rcu)(struct rcu_head *head,
619 void (*func)(struct rcu_head *head)))
abc490a1 620{
14044b37 621 struct cds_lfht *ht;
24365af7 622 unsigned long order;
abc490a1 623
8129be4e
MD
624 /* init_size must be power of two */
625 if (init_size & (init_size - 1))
626 return NULL;
14044b37 627 ht = calloc(1, sizeof(struct cds_lfht));
abc490a1 628 ht->hash_fct = hash_fct;
732ad076
MD
629 ht->compare_fct = compare_fct;
630 ht->hash_seed = hash_seed;
14044b37 631 ht->cds_lfht_call_rcu = cds_lfht_call_rcu;
848d4088 632 ht->in_progress_resize = 0;
abc490a1
MD
633 /* this mutex should not nest in read-side C.S. */
634 pthread_mutex_init(&ht->resize_mutex, NULL);
24365af7 635 order = get_count_order_ulong(max(init_size, 1)) + 1;
14044b37
MD
636 ht->t = calloc(1, sizeof(struct cds_lfht)
637 + (order * sizeof(struct _cds_lfht_node *)));
abc490a1 638 ht->t->size = 0;
f000907d 639 pthread_mutex_lock(&ht->resize_mutex);
24365af7 640 init_table(ht, ht->t, 0, order);
f000907d 641 pthread_mutex_unlock(&ht->resize_mutex);
abc490a1
MD
642 return ht;
643}
644
14044b37 645struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len)
2ed95849 646{
395270b6 647 struct rcu_table *t;
14044b37
MD
648 struct cds_lfht_node *node, *next;
649 struct _cds_lfht_node *lookup;
24365af7 650 unsigned long hash, reverse_hash, index, order;
2ed95849 651
732ad076 652 hash = ht->hash_fct(key, key_len, ht->hash_seed);
abc490a1 653 reverse_hash = bit_reverse_ulong(hash);
464a1ec9 654
395270b6 655 t = rcu_dereference(ht->t);
24365af7
MD
656 index = hash & (t->size - 1);
657 order = get_count_order_ulong(index + 1);
658 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
f0c29ed7 659 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
24365af7 660 hash, index, order, index & ((1UL << (order - 1)) - 1));
14044b37 661 node = (struct cds_lfht_node *) lookup;
2ed95849 662 for (;;) {
abc490a1
MD
663 if (unlikely(!node))
664 break;
cc4fcb10 665 if (unlikely(node->p.reverse_hash > reverse_hash)) {
abc490a1
MD
666 node = NULL;
667 break;
2ed95849 668 }
1b81fe1a
MD
669 next = rcu_dereference(node->p.next);
670 if (likely(!is_removed(next))
671 && !is_dummy(next)
49c2e2d6 672 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
273399de 673 break;
2ed95849 674 }
1b81fe1a 675 node = clear_flag(next);
2ed95849 676 }
1b81fe1a 677 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
abc490a1
MD
678 return node;
679}
e0ba718a 680
14044b37 681void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node)
abc490a1
MD
682{
683 struct rcu_table *t;
49c2e2d6 684 unsigned long hash;
ab7d5fc6 685
49c2e2d6 686 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
cc4fcb10 687 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
2ed95849 688
abc490a1 689 t = rcu_dereference(ht->t);
14044b37 690 (void) _cds_lfht_add(ht, t, node, 0, 0);
3eca1b8c
MD
691}
692
14044b37
MD
693struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
694 struct cds_lfht_node *node)
3eca1b8c
MD
695{
696 struct rcu_table *t;
49c2e2d6 697 unsigned long hash;
3eca1b8c 698
49c2e2d6 699 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
cc4fcb10 700 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
3eca1b8c
MD
701
702 t = rcu_dereference(ht->t);
14044b37 703 return _cds_lfht_add(ht, t, node, 1, 0);
2ed95849
MD
704}
705
14044b37 706int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node)
2ed95849 707{
abc490a1
MD
708 struct rcu_table *t;
709
710 t = rcu_dereference(ht->t);
14044b37 711 return _cds_lfht_remove(ht, t, node);
2ed95849 712}
ab7d5fc6 713
abc490a1 714static
14044b37 715int cds_lfht_delete_dummy(struct cds_lfht *ht)
674f7a69 716{
395270b6 717 struct rcu_table *t;
14044b37
MD
718 struct cds_lfht_node *node;
719 struct _cds_lfht_node *lookup;
24365af7 720 unsigned long order, i;
674f7a69 721
abc490a1
MD
722 t = ht->t;
723 /* Check that the table is empty */
24365af7 724 lookup = &t->tbl[0][0];
14044b37 725 node = (struct cds_lfht_node *) lookup;
abc490a1 726 do {
1b81fe1a
MD
727 node = clear_flag(node)->p.next;
728 if (!is_dummy(node))
abc490a1 729 return -EPERM;
273399de 730 assert(!is_removed(node));
a2974903 731 } while (clear_flag(node));
abc490a1 732 /* Internal sanity check: all nodes left should be dummy */
24365af7
MD
733 for (order = 0; order < get_count_order_ulong(t->size) + 1; order++) {
734 unsigned long len;
735
736 len = !order ? 1 : 1UL << (order - 1);
737 for (i = 0; i < len; i++) {
f0c29ed7 738 dbg_printf("delete order %lu i %lu hash %lu\n",
24365af7
MD
739 order, i,
740 bit_reverse_ulong(t->tbl[order][i].reverse_hash));
741 assert(is_dummy(t->tbl[order][i].next));
742 }
743 free(t->tbl[order]);
674f7a69 744 }
abc490a1 745 return 0;
674f7a69
MD
746}
747
748/*
749 * Should only be called when no more concurrent readers nor writers can
750 * possibly access the table.
751 */
14044b37 752int cds_lfht_destroy(struct cds_lfht *ht)
674f7a69 753{
5e28c532
MD
754 int ret;
755
848d4088 756 /* Wait for in-flight resize operations to complete */
33c7c748 757 CMM_STORE_SHARED(ht->in_progress_destroy, 1);
848d4088
MD
758 while (uatomic_read(&ht->in_progress_resize))
759 poll(NULL, 0, 100); /* wait for 100ms */
14044b37 760 ret = cds_lfht_delete_dummy(ht);
abc490a1
MD
761 if (ret)
762 return ret;
395270b6 763 free(ht->t);
674f7a69 764 free(ht);
5e28c532 765 return ret;
674f7a69
MD
766}
767
14044b37 768void cds_lfht_count_nodes(struct cds_lfht *ht,
273399de
MD
769 unsigned long *count,
770 unsigned long *removed)
771{
772 struct rcu_table *t;
14044b37
MD
773 struct cds_lfht_node *node, *next;
774 struct _cds_lfht_node *lookup;
24365af7 775 unsigned long nr_dummy = 0;
273399de
MD
776
777 *count = 0;
778 *removed = 0;
779
780 t = rcu_dereference(ht->t);
24365af7
MD
781 /* Count non-dummy nodes in the table */
782 lookup = &t->tbl[0][0];
14044b37 783 node = (struct cds_lfht_node *) lookup;
273399de 784 do {
cc4fcb10 785 next = rcu_dereference(node->p.next);
273399de 786 if (is_removed(next)) {
1b81fe1a 787 assert(!is_dummy(next));
273399de 788 (*removed)++;
1b81fe1a 789 } else if (!is_dummy(next))
273399de 790 (*count)++;
24365af7
MD
791 else
792 (nr_dummy)++;
273399de
MD
793 node = clear_flag(next);
794 } while (node);
f0c29ed7 795 dbg_printf("number of dummy nodes: %lu\n", nr_dummy);
273399de
MD
796}
797
abc490a1 798static
14044b37 799void cds_lfht_free_table_cb(struct rcu_head *head)
abc490a1
MD
800{
801 struct rcu_table *t =
802 caa_container_of(head, struct rcu_table, head);
803 free(t);
804}
805
806/* called with resize mutex held */
807static
14044b37 808void _do_cds_lfht_resize(struct cds_lfht *ht)
464a1ec9 809{
24365af7 810 unsigned long new_size, old_size, old_order, new_order;
395270b6 811 struct rcu_table *new_t, *old_t;
464a1ec9 812
395270b6
MD
813 old_t = ht->t;
814 old_size = old_t->size;
24365af7 815 old_order = get_count_order_ulong(old_size) + 1;
464a1ec9 816
f9830efd 817 new_size = CMM_LOAD_SHARED(old_t->resize_target);
abc490a1 818 if (old_size == new_size)
464a1ec9 819 return;
24365af7 820 new_order = get_count_order_ulong(new_size) + 1;
f0c29ed7 821 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
df03fab8 822 old_size, old_order, new_size, new_order);
14044b37
MD
823 new_t = malloc(sizeof(struct cds_lfht)
824 + (new_order * sizeof(struct _cds_lfht_node *)));
f000907d
MD
825 assert(new_size > old_size);
826 memcpy(&new_t->tbl, &old_t->tbl,
14044b37 827 old_order * sizeof(struct _cds_lfht_node *));
24365af7 828 init_table(ht, new_t, old_order, new_order - old_order);
f000907d
MD
829 /* Changing table and size atomically wrt lookups */
830 rcu_assign_pointer(ht->t, new_t);
14044b37 831 ht->cds_lfht_call_rcu(&old_t->head, cds_lfht_free_table_cb);
464a1ec9
MD
832}
833
abc490a1 834static
f9830efd
MD
835unsigned long resize_target_update(struct rcu_table *t,
836 int growth_order)
464a1ec9 837{
f9830efd
MD
838 return _uatomic_max(&t->resize_target,
839 t->size << growth_order);
464a1ec9
MD
840}
841
14044b37 842void cds_lfht_resize(struct cds_lfht *ht, int growth)
464a1ec9 843{
f9830efd
MD
844 struct rcu_table *t = rcu_dereference(ht->t);
845 unsigned long target_size;
846
f0c29ed7
MD
847 if (growth < 0) {
848 /*
849 * Silently refuse to shrink hash table. (not supported)
850 */
851 dbg_printf("shrinking hash table not supported.\n");
852 return;
853 }
854
f9830efd
MD
855 target_size = resize_target_update(t, growth);
856 if (t->size < target_size) {
11519af6 857 CMM_STORE_SHARED(t->resize_initiated, 1);
f9830efd 858 pthread_mutex_lock(&ht->resize_mutex);
14044b37 859 _do_cds_lfht_resize(ht);
f9830efd
MD
860 pthread_mutex_unlock(&ht->resize_mutex);
861 }
abc490a1 862}
464a1ec9 863
abc490a1
MD
864static
865void do_resize_cb(struct rcu_head *head)
866{
867 struct rcu_resize_work *work =
868 caa_container_of(head, struct rcu_resize_work, head);
14044b37 869 struct cds_lfht *ht = work->ht;
abc490a1
MD
870
871 pthread_mutex_lock(&ht->resize_mutex);
14044b37 872 _do_cds_lfht_resize(ht);
abc490a1
MD
873 pthread_mutex_unlock(&ht->resize_mutex);
874 free(work);
848d4088
MD
875 cmm_smp_mb(); /* finish resize before decrement */
876 uatomic_dec(&ht->in_progress_resize);
464a1ec9
MD
877}
878
abc490a1 879static
14044b37 880void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth)
ab7d5fc6 881{
abc490a1 882 struct rcu_resize_work *work;
f9830efd 883 unsigned long target_size;
abc490a1 884
f9830efd 885 target_size = resize_target_update(t, growth);
11519af6 886 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
848d4088
MD
887 uatomic_inc(&ht->in_progress_resize);
888 cmm_smp_mb(); /* increment resize count before calling it */
f9830efd
MD
889 work = malloc(sizeof(*work));
890 work->ht = ht;
14044b37 891 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
11519af6 892 CMM_STORE_SHARED(t->resize_initiated, 1);
f9830efd 893 }
ab7d5fc6 894}
This page took 0.068946 seconds and 4 git commands to generate.