Commit | Line | Data |
---|---|---|
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 | |
124cf177 MD |
115 | #ifndef CDS_HT_NOATOMIC |
116 | # define test_cmpxchg(ptr, old, _new) uatomic_cmpxchg(ptr, old, _new) | |
117 | #else | |
118 | # define test_cmpxchg(ptr, old, _new) \ | |
119 | ({ \ | |
120 | __typeof__(*(ptr)) __readptr = *(ptr); \ | |
121 | (__readptr == (old)) ? ({ \ | |
122 | *(ptr) = (_new); \ | |
123 | (old); \ | |
124 | }) : __readptr; \ | |
125 | }) | |
126 | #endif | |
127 | ||
f9830efd | 128 | #ifdef DEBUG |
f0c29ed7 | 129 | #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args) |
f9830efd | 130 | #else |
e753ff5a | 131 | #define dbg_printf(fmt, args...) |
f9830efd MD |
132 | #endif |
133 | ||
01370f0b MD |
134 | #define CHAIN_LEN_TARGET 4 |
135 | #define CHAIN_LEN_RESIZE_THRESHOLD 8 | |
2ed95849 | 136 | |
abc490a1 MD |
137 | #ifndef max |
138 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
139 | #endif | |
2ed95849 | 140 | |
d95bd160 MD |
141 | /* |
142 | * The removed flag needs to be updated atomically with the pointer. | |
143 | * The dummy flag does not require to be updated atomically with the | |
144 | * pointer, but it is added as a pointer low bit flag to save space. | |
145 | */ | |
d37166c6 | 146 | #define REMOVED_FLAG (1UL << 0) |
f5596c94 MD |
147 | #define DUMMY_FLAG (1UL << 1) |
148 | #define FLAGS_MASK ((1UL << 2) - 1) | |
d37166c6 | 149 | |
395270b6 | 150 | struct rcu_table { |
abc490a1 | 151 | unsigned long size; /* always a power of 2 */ |
f9830efd | 152 | unsigned long resize_target; |
11519af6 | 153 | int resize_initiated; |
abc490a1 | 154 | struct rcu_head head; |
14044b37 | 155 | struct _cds_lfht_node *tbl[0]; |
395270b6 MD |
156 | }; |
157 | ||
14044b37 | 158 | struct cds_lfht { |
395270b6 | 159 | struct rcu_table *t; /* shared */ |
14044b37 MD |
160 | cds_lfht_hash_fct hash_fct; |
161 | cds_lfht_compare_fct compare_fct; | |
732ad076 | 162 | unsigned long hash_seed; |
464a1ec9 | 163 | pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */ |
33c7c748 | 164 | unsigned int in_progress_resize, in_progress_destroy; |
14044b37 | 165 | void (*cds_lfht_call_rcu)(struct rcu_head *head, |
abc490a1 | 166 | void (*func)(struct rcu_head *head)); |
2ed95849 MD |
167 | }; |
168 | ||
abc490a1 MD |
169 | struct rcu_resize_work { |
170 | struct rcu_head head; | |
14044b37 | 171 | struct cds_lfht *ht; |
abc490a1 | 172 | }; |
2ed95849 | 173 | |
abc490a1 MD |
174 | /* |
175 | * Algorithm to reverse bits in a word by lookup table, extended to | |
176 | * 64-bit words. | |
f9830efd | 177 | * Source: |
abc490a1 | 178 | * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable |
f9830efd | 179 | * Originally from Public Domain. |
abc490a1 MD |
180 | */ |
181 | ||
182 | static const uint8_t BitReverseTable256[256] = | |
2ed95849 | 183 | { |
abc490a1 MD |
184 | #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64 |
185 | #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16) | |
186 | #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 ) | |
187 | R6(0), R6(2), R6(1), R6(3) | |
188 | }; | |
189 | #undef R2 | |
190 | #undef R4 | |
191 | #undef R6 | |
2ed95849 | 192 | |
abc490a1 MD |
193 | static |
194 | uint8_t bit_reverse_u8(uint8_t v) | |
195 | { | |
196 | return BitReverseTable256[v]; | |
197 | } | |
ab7d5fc6 | 198 | |
abc490a1 MD |
199 | static __attribute__((unused)) |
200 | uint32_t bit_reverse_u32(uint32_t v) | |
201 | { | |
202 | return ((uint32_t) bit_reverse_u8(v) << 24) | | |
203 | ((uint32_t) bit_reverse_u8(v >> 8) << 16) | | |
204 | ((uint32_t) bit_reverse_u8(v >> 16) << 8) | | |
205 | ((uint32_t) bit_reverse_u8(v >> 24)); | |
2ed95849 MD |
206 | } |
207 | ||
abc490a1 MD |
208 | static __attribute__((unused)) |
209 | uint64_t bit_reverse_u64(uint64_t v) | |
2ed95849 | 210 | { |
abc490a1 MD |
211 | return ((uint64_t) bit_reverse_u8(v) << 56) | |
212 | ((uint64_t) bit_reverse_u8(v >> 8) << 48) | | |
213 | ((uint64_t) bit_reverse_u8(v >> 16) << 40) | | |
214 | ((uint64_t) bit_reverse_u8(v >> 24) << 32) | | |
215 | ((uint64_t) bit_reverse_u8(v >> 32) << 24) | | |
216 | ((uint64_t) bit_reverse_u8(v >> 40) << 16) | | |
217 | ((uint64_t) bit_reverse_u8(v >> 48) << 8) | | |
218 | ((uint64_t) bit_reverse_u8(v >> 56)); | |
219 | } | |
220 | ||
221 | static | |
222 | unsigned long bit_reverse_ulong(unsigned long v) | |
223 | { | |
224 | #if (CAA_BITS_PER_LONG == 32) | |
225 | return bit_reverse_u32(v); | |
226 | #else | |
227 | return bit_reverse_u64(v); | |
228 | #endif | |
229 | } | |
230 | ||
f9830efd | 231 | /* |
24365af7 MD |
232 | * fls: returns the position of the most significant bit. |
233 | * Returns 0 if no bit is set, else returns the position of the most | |
234 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). | |
f9830efd | 235 | */ |
24365af7 MD |
236 | #if defined(__i386) || defined(__x86_64) |
237 | static inline | |
238 | unsigned int fls_u32(uint32_t x) | |
f9830efd | 239 | { |
24365af7 MD |
240 | int r; |
241 | ||
242 | asm("bsrl %1,%0\n\t" | |
243 | "jnz 1f\n\t" | |
244 | "movl $-1,%0\n\t" | |
245 | "1:\n\t" | |
246 | : "=r" (r) : "rm" (x)); | |
247 | return r + 1; | |
248 | } | |
249 | #define HAS_FLS_U32 | |
250 | #endif | |
251 | ||
252 | #if defined(__x86_64) | |
253 | static inline | |
254 | unsigned int fls_u64(uint64_t x) | |
255 | { | |
256 | long r; | |
257 | ||
258 | asm("bsrq %1,%0\n\t" | |
259 | "jnz 1f\n\t" | |
260 | "movq $-1,%0\n\t" | |
261 | "1:\n\t" | |
262 | : "=r" (r) : "rm" (x)); | |
263 | return r + 1; | |
264 | } | |
265 | #define HAS_FLS_U64 | |
266 | #endif | |
267 | ||
268 | #ifndef HAS_FLS_U64 | |
269 | static __attribute__((unused)) | |
270 | unsigned int fls_u64(uint64_t x) | |
271 | { | |
272 | unsigned int r = 64; | |
273 | ||
274 | if (!x) | |
275 | return 0; | |
276 | ||
277 | if (!(x & 0xFFFFFFFF00000000ULL)) { | |
278 | x <<= 32; | |
279 | r -= 32; | |
280 | } | |
281 | if (!(x & 0xFFFF000000000000ULL)) { | |
282 | x <<= 16; | |
283 | r -= 16; | |
284 | } | |
285 | if (!(x & 0xFF00000000000000ULL)) { | |
286 | x <<= 8; | |
287 | r -= 8; | |
288 | } | |
289 | if (!(x & 0xF000000000000000ULL)) { | |
290 | x <<= 4; | |
291 | r -= 4; | |
292 | } | |
293 | if (!(x & 0xC000000000000000ULL)) { | |
294 | x <<= 2; | |
295 | r -= 2; | |
296 | } | |
297 | if (!(x & 0x8000000000000000ULL)) { | |
298 | x <<= 1; | |
299 | r -= 1; | |
300 | } | |
301 | return r; | |
302 | } | |
303 | #endif | |
304 | ||
305 | #ifndef HAS_FLS_U32 | |
306 | static __attribute__((unused)) | |
307 | unsigned int fls_u32(uint32_t x) | |
308 | { | |
309 | unsigned int r = 32; | |
f9830efd | 310 | |
24365af7 MD |
311 | if (!x) |
312 | return 0; | |
313 | if (!(x & 0xFFFF0000U)) { | |
314 | x <<= 16; | |
315 | r -= 16; | |
316 | } | |
317 | if (!(x & 0xFF000000U)) { | |
318 | x <<= 8; | |
319 | r -= 8; | |
320 | } | |
321 | if (!(x & 0xF0000000U)) { | |
322 | x <<= 4; | |
323 | r -= 4; | |
324 | } | |
325 | if (!(x & 0xC0000000U)) { | |
326 | x <<= 2; | |
327 | r -= 2; | |
328 | } | |
329 | if (!(x & 0x80000000U)) { | |
330 | x <<= 1; | |
331 | r -= 1; | |
332 | } | |
333 | return r; | |
334 | } | |
335 | #endif | |
336 | ||
337 | unsigned int fls_ulong(unsigned long x) | |
f9830efd | 338 | { |
24365af7 MD |
339 | #if (CAA_BITS_PER_lONG == 32) |
340 | return fls_u32(x); | |
341 | #else | |
342 | return fls_u64(x); | |
343 | #endif | |
344 | } | |
f9830efd | 345 | |
24365af7 MD |
346 | int get_count_order_u32(uint32_t x) |
347 | { | |
348 | int order; | |
349 | ||
350 | order = fls_u32(x) - 1; | |
351 | if (x & (x - 1)) | |
352 | order++; | |
353 | return order; | |
354 | } | |
355 | ||
356 | int get_count_order_ulong(unsigned long x) | |
357 | { | |
358 | int order; | |
359 | ||
360 | order = fls_ulong(x) - 1; | |
361 | if (x & (x - 1)) | |
362 | order++; | |
363 | return order; | |
f9830efd MD |
364 | } |
365 | ||
366 | static | |
14044b37 | 367 | void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth); |
f9830efd MD |
368 | |
369 | static | |
14044b37 | 370 | void check_resize(struct cds_lfht *ht, struct rcu_table *t, |
f9830efd MD |
371 | uint32_t chain_len) |
372 | { | |
24365af7 | 373 | if (chain_len > 100) |
f0c29ed7 | 374 | dbg_printf("WARNING: large chain length: %u.\n", |
24365af7 | 375 | chain_len); |
3390d470 | 376 | if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD) |
14044b37 | 377 | cds_lfht_resize_lazy(ht, t, |
01370f0b | 378 | get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1))); |
f9830efd MD |
379 | } |
380 | ||
abc490a1 | 381 | static |
14044b37 | 382 | struct cds_lfht_node *clear_flag(struct cds_lfht_node *node) |
abc490a1 | 383 | { |
14044b37 | 384 | return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK); |
abc490a1 MD |
385 | } |
386 | ||
387 | static | |
14044b37 | 388 | int is_removed(struct cds_lfht_node *node) |
abc490a1 | 389 | { |
d37166c6 | 390 | return ((unsigned long) node) & REMOVED_FLAG; |
abc490a1 MD |
391 | } |
392 | ||
393 | static | |
14044b37 | 394 | struct cds_lfht_node *flag_removed(struct cds_lfht_node *node) |
abc490a1 | 395 | { |
14044b37 | 396 | return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG); |
abc490a1 MD |
397 | } |
398 | ||
f5596c94 | 399 | static |
14044b37 | 400 | int is_dummy(struct cds_lfht_node *node) |
f5596c94 MD |
401 | { |
402 | return ((unsigned long) node) & DUMMY_FLAG; | |
403 | } | |
404 | ||
405 | static | |
14044b37 | 406 | struct cds_lfht_node *flag_dummy(struct cds_lfht_node *node) |
f5596c94 | 407 | { |
14044b37 | 408 | return (struct cds_lfht_node *) (((unsigned long) node) | DUMMY_FLAG); |
f5596c94 MD |
409 | } |
410 | ||
abc490a1 | 411 | static |
f9830efd | 412 | unsigned long _uatomic_max(unsigned long *ptr, unsigned long v) |
abc490a1 MD |
413 | { |
414 | unsigned long old1, old2; | |
415 | ||
416 | old1 = uatomic_read(ptr); | |
417 | do { | |
418 | old2 = old1; | |
419 | if (old2 >= v) | |
f9830efd | 420 | return old2; |
124cf177 | 421 | } while ((old1 = test_cmpxchg(ptr, old2, v)) != old2); |
f9830efd | 422 | return v; |
abc490a1 MD |
423 | } |
424 | ||
273399de MD |
425 | /* |
426 | * Remove all logically deleted nodes from a bucket up to a certain node key. | |
427 | */ | |
428 | static | |
14044b37 | 429 | void _cds_lfht_gc_bucket(struct cds_lfht_node *dummy, struct cds_lfht_node *node) |
273399de | 430 | { |
14044b37 | 431 | struct cds_lfht_node *iter_prev, *iter, *next, *new_next; |
273399de MD |
432 | |
433 | for (;;) { | |
434 | iter_prev = dummy; | |
435 | /* We can always skip the dummy node initially */ | |
cc4fcb10 MD |
436 | iter = rcu_dereference(iter_prev->p.next); |
437 | assert(iter_prev->p.reverse_hash <= node->p.reverse_hash); | |
273399de | 438 | for (;;) { |
a2974903 | 439 | if (unlikely(!clear_flag(iter))) |
479c8a32 | 440 | return; |
76412f24 | 441 | if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash)) |
273399de | 442 | return; |
cc4fcb10 | 443 | next = rcu_dereference(clear_flag(iter)->p.next); |
76412f24 | 444 | if (likely(is_removed(next))) |
273399de | 445 | break; |
b453eae1 | 446 | iter_prev = clear_flag(iter); |
273399de MD |
447 | iter = next; |
448 | } | |
449 | assert(!is_removed(iter)); | |
f5596c94 MD |
450 | if (is_dummy(iter)) |
451 | new_next = flag_dummy(clear_flag(next)); | |
452 | else | |
453 | new_next = clear_flag(next); | |
124cf177 | 454 | (void) test_cmpxchg(&iter_prev->p.next, iter, new_next); |
273399de MD |
455 | } |
456 | } | |
457 | ||
abc490a1 | 458 | static |
14044b37 MD |
459 | struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, struct rcu_table *t, |
460 | struct cds_lfht_node *node, int unique, int dummy) | |
abc490a1 | 461 | { |
14044b37 | 462 | struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next, |
f5596c94 | 463 | *dummy_node; |
14044b37 | 464 | struct _cds_lfht_node *lookup; |
24365af7 | 465 | unsigned long hash, index, order; |
abc490a1 | 466 | |
18117871 | 467 | if (!t->size) { |
f5596c94 MD |
468 | assert(dummy); |
469 | node->p.next = flag_dummy(NULL); | |
18117871 MD |
470 | return node; /* Initial first add (head) */ |
471 | } | |
cc4fcb10 | 472 | hash = bit_reverse_ulong(node->p.reverse_hash); |
abc490a1 | 473 | for (;;) { |
f9830efd | 474 | uint32_t chain_len = 0; |
abc490a1 | 475 | |
11519af6 MD |
476 | /* |
477 | * iter_prev points to the non-removed node prior to the | |
478 | * insert location. | |
11519af6 | 479 | */ |
24365af7 MD |
480 | index = hash & (t->size - 1); |
481 | order = get_count_order_ulong(index + 1); | |
482 | lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)]; | |
14044b37 | 483 | iter_prev = (struct cds_lfht_node *) lookup; |
11519af6 | 484 | /* We can always skip the dummy node initially */ |
cc4fcb10 MD |
485 | iter = rcu_dereference(iter_prev->p.next); |
486 | assert(iter_prev->p.reverse_hash <= node->p.reverse_hash); | |
abc490a1 | 487 | for (;;) { |
a2974903 | 488 | if (unlikely(!clear_flag(iter))) |
273399de | 489 | goto insert; |
76412f24 | 490 | if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash)) |
273399de | 491 | goto insert; |
cc4fcb10 | 492 | next = rcu_dereference(clear_flag(iter)->p.next); |
76412f24 | 493 | if (unlikely(is_removed(next))) |
9dba85be | 494 | goto gc_node; |
e43f23f8 | 495 | if (unique |
1b81fe1a | 496 | && !is_dummy(next) |
e43f23f8 MD |
497 | && !ht->compare_fct(node->key, node->key_len, |
498 | clear_flag(iter)->key, | |
499 | clear_flag(iter)->key_len)) | |
18117871 | 500 | return clear_flag(iter); |
11519af6 | 501 | /* Only account for identical reverse hash once */ |
24365af7 MD |
502 | if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash |
503 | && !is_dummy(next)) | |
11519af6 MD |
504 | check_resize(ht, t, ++chain_len); |
505 | iter_prev = clear_flag(iter); | |
273399de | 506 | iter = next; |
abc490a1 | 507 | } |
273399de | 508 | insert: |
7ec59d3b | 509 | assert(node != clear_flag(iter)); |
11519af6 | 510 | assert(!is_removed(iter_prev)); |
f000907d | 511 | assert(iter_prev != node); |
f5596c94 | 512 | if (!dummy) |
1b81fe1a | 513 | node->p.next = clear_flag(iter); |
f5596c94 | 514 | else |
1b81fe1a | 515 | node->p.next = flag_dummy(clear_flag(iter)); |
f5596c94 MD |
516 | if (is_dummy(iter)) |
517 | new_node = flag_dummy(node); | |
518 | else | |
519 | new_node = node; | |
124cf177 | 520 | if (test_cmpxchg(&iter_prev->p.next, iter, |
f5596c94 | 521 | new_node) != iter) |
273399de | 522 | continue; /* retry */ |
11519af6 | 523 | else |
273399de | 524 | goto gc_end; |
9dba85be MD |
525 | gc_node: |
526 | assert(!is_removed(iter)); | |
f5596c94 MD |
527 | if (is_dummy(iter)) |
528 | new_next = flag_dummy(clear_flag(next)); | |
529 | else | |
530 | new_next = clear_flag(next); | |
124cf177 | 531 | (void) test_cmpxchg(&iter_prev->p.next, iter, new_next); |
273399de | 532 | /* retry */ |
464a1ec9 | 533 | } |
273399de MD |
534 | gc_end: |
535 | /* Garbage collect logically removed nodes in the bucket */ | |
24365af7 MD |
536 | index = hash & (t->size - 1); |
537 | order = get_count_order_ulong(index + 1); | |
538 | lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)]; | |
14044b37 MD |
539 | dummy_node = (struct cds_lfht_node *) lookup; |
540 | _cds_lfht_gc_bucket(dummy_node, node); | |
18117871 | 541 | return node; |
abc490a1 | 542 | } |
464a1ec9 | 543 | |
abc490a1 | 544 | static |
14044b37 MD |
545 | int _cds_lfht_remove(struct cds_lfht *ht, struct rcu_table *t, |
546 | struct cds_lfht_node *node) | |
abc490a1 | 547 | { |
14044b37 MD |
548 | struct cds_lfht_node *dummy, *next, *old; |
549 | struct _cds_lfht_node *lookup; | |
abc490a1 | 550 | int flagged = 0; |
24365af7 | 551 | unsigned long hash, index, order; |
5e28c532 | 552 | |
7ec59d3b | 553 | /* logically delete the node */ |
cc4fcb10 | 554 | old = rcu_dereference(node->p.next); |
7ec59d3b MD |
555 | do { |
556 | next = old; | |
76412f24 | 557 | if (unlikely(is_removed(next))) |
7ec59d3b | 558 | goto end; |
1b81fe1a | 559 | assert(!is_dummy(next)); |
124cf177 | 560 | old = test_cmpxchg(&node->p.next, next, |
7ec59d3b MD |
561 | flag_removed(next)); |
562 | } while (old != next); | |
563 | ||
564 | /* We performed the (logical) deletion. */ | |
565 | flagged = 1; | |
566 | ||
567 | /* | |
568 | * Ensure that the node is not visible to readers anymore: lookup for | |
273399de MD |
569 | * the node, and remove it (along with any other logically removed node) |
570 | * if found. | |
11519af6 | 571 | */ |
cc4fcb10 | 572 | hash = bit_reverse_ulong(node->p.reverse_hash); |
24365af7 MD |
573 | index = hash & (t->size - 1); |
574 | order = get_count_order_ulong(index + 1); | |
575 | lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)]; | |
14044b37 MD |
576 | dummy = (struct cds_lfht_node *) lookup; |
577 | _cds_lfht_gc_bucket(dummy, node); | |
2ed95849 | 578 | end: |
11519af6 MD |
579 | /* |
580 | * Only the flagging action indicated that we (and no other) | |
581 | * removed the node from the hash. | |
582 | */ | |
7ec59d3b | 583 | if (flagged) { |
cc4fcb10 | 584 | assert(is_removed(rcu_dereference(node->p.next))); |
11519af6 | 585 | return 0; |
7ec59d3b | 586 | } else |
11519af6 | 587 | return -ENOENT; |
abc490a1 | 588 | } |
2ed95849 | 589 | |
abc490a1 | 590 | static |
14044b37 | 591 | void init_table(struct cds_lfht *ht, struct rcu_table *t, |
24365af7 MD |
592 | unsigned long first_order, unsigned long len_order) |
593 | { | |
594 | unsigned long i, end_order; | |
595 | ||
f0c29ed7 | 596 | dbg_printf("init table: first_order %lu end_order %lu\n", |
24365af7 MD |
597 | first_order, first_order + len_order); |
598 | end_order = first_order + len_order; | |
599 | t->size = !first_order ? 0 : (1UL << (first_order - 1)); | |
600 | for (i = first_order; i < end_order; i++) { | |
601 | unsigned long j, len; | |
602 | ||
603 | len = !i ? 1 : 1UL << (i - 1); | |
f0c29ed7 | 604 | dbg_printf("init order %lu len: %lu\n", i, len); |
14044b37 | 605 | t->tbl[i] = calloc(len, sizeof(struct _cds_lfht_node)); |
24365af7 | 606 | for (j = 0; j < len; j++) { |
f0c29ed7 | 607 | dbg_printf("init entry: i %lu j %lu hash %lu\n", |
24365af7 | 608 | i, j, !i ? 0 : (1UL << (i - 1)) + j); |
14044b37 MD |
609 | struct cds_lfht_node *new_node = |
610 | (struct cds_lfht_node *) &t->tbl[i][j]; | |
24365af7 MD |
611 | new_node->p.reverse_hash = |
612 | bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j); | |
14044b37 | 613 | (void) _cds_lfht_add(ht, t, new_node, 0, 1); |
33c7c748 MD |
614 | if (CMM_LOAD_SHARED(ht->in_progress_destroy)) |
615 | break; | |
24365af7 MD |
616 | } |
617 | /* Update table size */ | |
618 | t->size = !i ? 1 : (1UL << i); | |
f0c29ed7 | 619 | dbg_printf("init new size: %lu\n", t->size); |
33c7c748 MD |
620 | if (CMM_LOAD_SHARED(ht->in_progress_destroy)) |
621 | break; | |
abc490a1 | 622 | } |
24365af7 | 623 | t->resize_target = t->size; |
11519af6 | 624 | t->resize_initiated = 0; |
2ed95849 MD |
625 | } |
626 | ||
14044b37 MD |
627 | struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct, |
628 | cds_lfht_compare_fct compare_fct, | |
629 | unsigned long hash_seed, | |
630 | unsigned long init_size, | |
631 | void (*cds_lfht_call_rcu)(struct rcu_head *head, | |
632 | void (*func)(struct rcu_head *head))) | |
abc490a1 | 633 | { |
14044b37 | 634 | struct cds_lfht *ht; |
24365af7 | 635 | unsigned long order; |
abc490a1 | 636 | |
8129be4e | 637 | /* init_size must be power of two */ |
49619ea0 | 638 | if (init_size && (init_size & (init_size - 1))) |
8129be4e | 639 | return NULL; |
14044b37 | 640 | ht = calloc(1, sizeof(struct cds_lfht)); |
abc490a1 | 641 | ht->hash_fct = hash_fct; |
732ad076 MD |
642 | ht->compare_fct = compare_fct; |
643 | ht->hash_seed = hash_seed; | |
14044b37 | 644 | ht->cds_lfht_call_rcu = cds_lfht_call_rcu; |
848d4088 | 645 | ht->in_progress_resize = 0; |
abc490a1 MD |
646 | /* this mutex should not nest in read-side C.S. */ |
647 | pthread_mutex_init(&ht->resize_mutex, NULL); | |
24365af7 | 648 | order = get_count_order_ulong(max(init_size, 1)) + 1; |
14044b37 MD |
649 | ht->t = calloc(1, sizeof(struct cds_lfht) |
650 | + (order * sizeof(struct _cds_lfht_node *))); | |
abc490a1 | 651 | ht->t->size = 0; |
f000907d | 652 | pthread_mutex_lock(&ht->resize_mutex); |
24365af7 | 653 | init_table(ht, ht->t, 0, order); |
f000907d | 654 | pthread_mutex_unlock(&ht->resize_mutex); |
abc490a1 MD |
655 | return ht; |
656 | } | |
657 | ||
14044b37 | 658 | struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len) |
2ed95849 | 659 | { |
395270b6 | 660 | struct rcu_table *t; |
14044b37 MD |
661 | struct cds_lfht_node *node, *next; |
662 | struct _cds_lfht_node *lookup; | |
24365af7 | 663 | unsigned long hash, reverse_hash, index, order; |
2ed95849 | 664 | |
732ad076 | 665 | hash = ht->hash_fct(key, key_len, ht->hash_seed); |
abc490a1 | 666 | reverse_hash = bit_reverse_ulong(hash); |
464a1ec9 | 667 | |
395270b6 | 668 | t = rcu_dereference(ht->t); |
24365af7 MD |
669 | index = hash & (t->size - 1); |
670 | order = get_count_order_ulong(index + 1); | |
671 | lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)]; | |
f0c29ed7 | 672 | dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n", |
24365af7 | 673 | hash, index, order, index & ((1UL << (order - 1)) - 1)); |
14044b37 | 674 | node = (struct cds_lfht_node *) lookup; |
2ed95849 | 675 | for (;;) { |
abc490a1 MD |
676 | if (unlikely(!node)) |
677 | break; | |
cc4fcb10 | 678 | if (unlikely(node->p.reverse_hash > reverse_hash)) { |
abc490a1 MD |
679 | node = NULL; |
680 | break; | |
2ed95849 | 681 | } |
1b81fe1a MD |
682 | next = rcu_dereference(node->p.next); |
683 | if (likely(!is_removed(next)) | |
684 | && !is_dummy(next) | |
49c2e2d6 | 685 | && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) { |
273399de | 686 | break; |
2ed95849 | 687 | } |
1b81fe1a | 688 | node = clear_flag(next); |
2ed95849 | 689 | } |
1b81fe1a | 690 | assert(!node || !is_dummy(rcu_dereference(node->p.next))); |
abc490a1 MD |
691 | return node; |
692 | } | |
e0ba718a | 693 | |
a481e5ff MD |
694 | struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht, |
695 | struct cds_lfht_node *node) | |
696 | { | |
697 | struct cds_lfht_node *next; | |
698 | unsigned long reverse_hash; | |
699 | void *key; | |
700 | size_t key_len; | |
701 | ||
702 | reverse_hash = node->p.reverse_hash; | |
703 | key = node->key; | |
704 | key_len = node->key_len; | |
705 | next = rcu_dereference(node->p.next); | |
706 | node = clear_flag(next); | |
707 | ||
708 | for (;;) { | |
709 | if (unlikely(!node)) | |
710 | break; | |
711 | if (unlikely(node->p.reverse_hash > reverse_hash)) { | |
712 | node = NULL; | |
713 | break; | |
714 | } | |
715 | next = rcu_dereference(node->p.next); | |
716 | if (likely(!is_removed(next)) | |
717 | && !is_dummy(next) | |
718 | && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) { | |
719 | break; | |
720 | } | |
721 | node = clear_flag(next); | |
722 | } | |
723 | assert(!node || !is_dummy(rcu_dereference(node->p.next))); | |
724 | return node; | |
725 | } | |
726 | ||
14044b37 | 727 | void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node) |
abc490a1 MD |
728 | { |
729 | struct rcu_table *t; | |
49c2e2d6 | 730 | unsigned long hash; |
ab7d5fc6 | 731 | |
49c2e2d6 | 732 | hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed); |
cc4fcb10 | 733 | node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash); |
2ed95849 | 734 | |
abc490a1 | 735 | t = rcu_dereference(ht->t); |
14044b37 | 736 | (void) _cds_lfht_add(ht, t, node, 0, 0); |
3eca1b8c MD |
737 | } |
738 | ||
14044b37 MD |
739 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
740 | struct cds_lfht_node *node) | |
3eca1b8c MD |
741 | { |
742 | struct rcu_table *t; | |
49c2e2d6 | 743 | unsigned long hash; |
3eca1b8c | 744 | |
49c2e2d6 | 745 | hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed); |
cc4fcb10 | 746 | node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash); |
3eca1b8c MD |
747 | |
748 | t = rcu_dereference(ht->t); | |
14044b37 | 749 | return _cds_lfht_add(ht, t, node, 1, 0); |
2ed95849 MD |
750 | } |
751 | ||
14044b37 | 752 | int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node) |
2ed95849 | 753 | { |
abc490a1 MD |
754 | struct rcu_table *t; |
755 | ||
756 | t = rcu_dereference(ht->t); | |
14044b37 | 757 | return _cds_lfht_remove(ht, t, node); |
2ed95849 | 758 | } |
ab7d5fc6 | 759 | |
abc490a1 | 760 | static |
14044b37 | 761 | int cds_lfht_delete_dummy(struct cds_lfht *ht) |
674f7a69 | 762 | { |
395270b6 | 763 | struct rcu_table *t; |
14044b37 MD |
764 | struct cds_lfht_node *node; |
765 | struct _cds_lfht_node *lookup; | |
24365af7 | 766 | unsigned long order, i; |
674f7a69 | 767 | |
abc490a1 MD |
768 | t = ht->t; |
769 | /* Check that the table is empty */ | |
24365af7 | 770 | lookup = &t->tbl[0][0]; |
14044b37 | 771 | node = (struct cds_lfht_node *) lookup; |
abc490a1 | 772 | do { |
1b81fe1a MD |
773 | node = clear_flag(node)->p.next; |
774 | if (!is_dummy(node)) | |
abc490a1 | 775 | return -EPERM; |
273399de | 776 | assert(!is_removed(node)); |
a2974903 | 777 | } while (clear_flag(node)); |
abc490a1 | 778 | /* Internal sanity check: all nodes left should be dummy */ |
24365af7 MD |
779 | for (order = 0; order < get_count_order_ulong(t->size) + 1; order++) { |
780 | unsigned long len; | |
781 | ||
782 | len = !order ? 1 : 1UL << (order - 1); | |
783 | for (i = 0; i < len; i++) { | |
f0c29ed7 | 784 | dbg_printf("delete order %lu i %lu hash %lu\n", |
24365af7 MD |
785 | order, i, |
786 | bit_reverse_ulong(t->tbl[order][i].reverse_hash)); | |
787 | assert(is_dummy(t->tbl[order][i].next)); | |
788 | } | |
789 | free(t->tbl[order]); | |
674f7a69 | 790 | } |
abc490a1 | 791 | return 0; |
674f7a69 MD |
792 | } |
793 | ||
794 | /* | |
795 | * Should only be called when no more concurrent readers nor writers can | |
796 | * possibly access the table. | |
797 | */ | |
14044b37 | 798 | int cds_lfht_destroy(struct cds_lfht *ht) |
674f7a69 | 799 | { |
5e28c532 MD |
800 | int ret; |
801 | ||
848d4088 | 802 | /* Wait for in-flight resize operations to complete */ |
33c7c748 | 803 | CMM_STORE_SHARED(ht->in_progress_destroy, 1); |
848d4088 MD |
804 | while (uatomic_read(&ht->in_progress_resize)) |
805 | poll(NULL, 0, 100); /* wait for 100ms */ | |
14044b37 | 806 | ret = cds_lfht_delete_dummy(ht); |
abc490a1 MD |
807 | if (ret) |
808 | return ret; | |
395270b6 | 809 | free(ht->t); |
674f7a69 | 810 | free(ht); |
5e28c532 | 811 | return ret; |
674f7a69 MD |
812 | } |
813 | ||
14044b37 | 814 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
273399de MD |
815 | unsigned long *count, |
816 | unsigned long *removed) | |
817 | { | |
818 | struct rcu_table *t; | |
14044b37 MD |
819 | struct cds_lfht_node *node, *next; |
820 | struct _cds_lfht_node *lookup; | |
24365af7 | 821 | unsigned long nr_dummy = 0; |
273399de MD |
822 | |
823 | *count = 0; | |
824 | *removed = 0; | |
825 | ||
826 | t = rcu_dereference(ht->t); | |
24365af7 MD |
827 | /* Count non-dummy nodes in the table */ |
828 | lookup = &t->tbl[0][0]; | |
14044b37 | 829 | node = (struct cds_lfht_node *) lookup; |
273399de | 830 | do { |
cc4fcb10 | 831 | next = rcu_dereference(node->p.next); |
273399de | 832 | if (is_removed(next)) { |
1b81fe1a | 833 | assert(!is_dummy(next)); |
273399de | 834 | (*removed)++; |
1b81fe1a | 835 | } else if (!is_dummy(next)) |
273399de | 836 | (*count)++; |
24365af7 MD |
837 | else |
838 | (nr_dummy)++; | |
273399de MD |
839 | node = clear_flag(next); |
840 | } while (node); | |
f0c29ed7 | 841 | dbg_printf("number of dummy nodes: %lu\n", nr_dummy); |
273399de MD |
842 | } |
843 | ||
abc490a1 | 844 | static |
14044b37 | 845 | void cds_lfht_free_table_cb(struct rcu_head *head) |
abc490a1 MD |
846 | { |
847 | struct rcu_table *t = | |
848 | caa_container_of(head, struct rcu_table, head); | |
849 | free(t); | |
850 | } | |
851 | ||
852 | /* called with resize mutex held */ | |
853 | static | |
14044b37 | 854 | void _do_cds_lfht_resize(struct cds_lfht *ht) |
464a1ec9 | 855 | { |
24365af7 | 856 | unsigned long new_size, old_size, old_order, new_order; |
395270b6 | 857 | struct rcu_table *new_t, *old_t; |
464a1ec9 | 858 | |
395270b6 MD |
859 | old_t = ht->t; |
860 | old_size = old_t->size; | |
24365af7 | 861 | old_order = get_count_order_ulong(old_size) + 1; |
464a1ec9 | 862 | |
f9830efd | 863 | new_size = CMM_LOAD_SHARED(old_t->resize_target); |
abc490a1 | 864 | if (old_size == new_size) |
464a1ec9 | 865 | return; |
24365af7 | 866 | new_order = get_count_order_ulong(new_size) + 1; |
f0c29ed7 | 867 | dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n", |
df03fab8 | 868 | old_size, old_order, new_size, new_order); |
14044b37 MD |
869 | new_t = malloc(sizeof(struct cds_lfht) |
870 | + (new_order * sizeof(struct _cds_lfht_node *))); | |
f000907d MD |
871 | assert(new_size > old_size); |
872 | memcpy(&new_t->tbl, &old_t->tbl, | |
14044b37 | 873 | old_order * sizeof(struct _cds_lfht_node *)); |
24365af7 | 874 | init_table(ht, new_t, old_order, new_order - old_order); |
f000907d MD |
875 | /* Changing table and size atomically wrt lookups */ |
876 | rcu_assign_pointer(ht->t, new_t); | |
14044b37 | 877 | ht->cds_lfht_call_rcu(&old_t->head, cds_lfht_free_table_cb); |
464a1ec9 MD |
878 | } |
879 | ||
abc490a1 | 880 | static |
f9830efd MD |
881 | unsigned long resize_target_update(struct rcu_table *t, |
882 | int growth_order) | |
464a1ec9 | 883 | { |
f9830efd MD |
884 | return _uatomic_max(&t->resize_target, |
885 | t->size << growth_order); | |
464a1ec9 MD |
886 | } |
887 | ||
14044b37 | 888 | void cds_lfht_resize(struct cds_lfht *ht, int growth) |
464a1ec9 | 889 | { |
f9830efd MD |
890 | struct rcu_table *t = rcu_dereference(ht->t); |
891 | unsigned long target_size; | |
892 | ||
f0c29ed7 MD |
893 | if (growth < 0) { |
894 | /* | |
895 | * Silently refuse to shrink hash table. (not supported) | |
896 | */ | |
897 | dbg_printf("shrinking hash table not supported.\n"); | |
898 | return; | |
899 | } | |
900 | ||
f9830efd MD |
901 | target_size = resize_target_update(t, growth); |
902 | if (t->size < target_size) { | |
11519af6 | 903 | CMM_STORE_SHARED(t->resize_initiated, 1); |
f9830efd | 904 | pthread_mutex_lock(&ht->resize_mutex); |
14044b37 | 905 | _do_cds_lfht_resize(ht); |
f9830efd MD |
906 | pthread_mutex_unlock(&ht->resize_mutex); |
907 | } | |
abc490a1 | 908 | } |
464a1ec9 | 909 | |
abc490a1 MD |
910 | static |
911 | void do_resize_cb(struct rcu_head *head) | |
912 | { | |
913 | struct rcu_resize_work *work = | |
914 | caa_container_of(head, struct rcu_resize_work, head); | |
14044b37 | 915 | struct cds_lfht *ht = work->ht; |
abc490a1 MD |
916 | |
917 | pthread_mutex_lock(&ht->resize_mutex); | |
14044b37 | 918 | _do_cds_lfht_resize(ht); |
abc490a1 MD |
919 | pthread_mutex_unlock(&ht->resize_mutex); |
920 | free(work); | |
848d4088 MD |
921 | cmm_smp_mb(); /* finish resize before decrement */ |
922 | uatomic_dec(&ht->in_progress_resize); | |
464a1ec9 MD |
923 | } |
924 | ||
abc490a1 | 925 | static |
14044b37 | 926 | void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth) |
ab7d5fc6 | 927 | { |
abc490a1 | 928 | struct rcu_resize_work *work; |
f9830efd | 929 | unsigned long target_size; |
abc490a1 | 930 | |
f9830efd | 931 | target_size = resize_target_update(t, growth); |
11519af6 | 932 | if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) { |
848d4088 MD |
933 | uatomic_inc(&ht->in_progress_resize); |
934 | cmm_smp_mb(); /* increment resize count before calling it */ | |
f9830efd MD |
935 | work = malloc(sizeof(*work)); |
936 | work->ht = ht; | |
14044b37 | 937 | ht->cds_lfht_call_rcu(&work->head, do_resize_cb); |
11519af6 | 938 | CMM_STORE_SHARED(t->resize_initiated, 1); |
f9830efd | 939 | } |
ab7d5fc6 | 940 | } |