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 | ||
2ed95849 MD |
23 | #define _LGPL_SOURCE |
24 | #include <stdlib.h> | |
e0ba718a MD |
25 | #include <errno.h> |
26 | #include <assert.h> | |
27 | #include <stdio.h> | |
abc490a1 | 28 | #include <stdint.h> |
f000907d | 29 | #include <string.h> |
e0ba718a | 30 | |
2ed95849 | 31 | #include <urcu.h> |
abc490a1 | 32 | #include <urcu-call-rcu.h> |
a42cc659 MD |
33 | #include <urcu/arch.h> |
34 | #include <urcu/uatomic.h> | |
674f7a69 | 35 | #include <urcu/jhash.h> |
a42cc659 | 36 | #include <urcu/compiler.h> |
abc490a1 | 37 | #include <urcu/rculfhash.h> |
5e28c532 | 38 | #include <stdio.h> |
464a1ec9 | 39 | #include <pthread.h> |
44395fb7 | 40 | |
f9830efd MD |
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 | ||
65e8e729 MD |
49 | #define CHAIN_LEN_TARGET 1 |
50 | #define CHAIN_LEN_RESIZE_THRESHOLD 2 | |
2ed95849 | 51 | |
abc490a1 MD |
52 | #ifndef max |
53 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
54 | #endif | |
2ed95849 | 55 | |
d37166c6 | 56 | #define REMOVED_FLAG (1UL << 0) |
f5596c94 MD |
57 | #define DUMMY_FLAG (1UL << 1) |
58 | #define FLAGS_MASK ((1UL << 2) - 1) | |
d37166c6 | 59 | |
395270b6 | 60 | struct rcu_table { |
abc490a1 | 61 | unsigned long size; /* always a power of 2 */ |
f9830efd | 62 | unsigned long resize_target; |
11519af6 | 63 | int resize_initiated; |
abc490a1 | 64 | struct rcu_head head; |
395270b6 MD |
65 | struct rcu_ht_node *tbl[0]; |
66 | }; | |
67 | ||
2ed95849 | 68 | struct rcu_ht { |
395270b6 | 69 | struct rcu_table *t; /* shared */ |
2ed95849 | 70 | ht_hash_fct hash_fct; |
732ad076 MD |
71 | ht_compare_fct compare_fct; |
72 | unsigned long hash_seed; | |
464a1ec9 | 73 | pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */ |
848d4088 | 74 | unsigned int in_progress_resize; |
abc490a1 MD |
75 | void (*ht_call_rcu)(struct rcu_head *head, |
76 | void (*func)(struct rcu_head *head)); | |
2ed95849 MD |
77 | }; |
78 | ||
abc490a1 MD |
79 | struct rcu_resize_work { |
80 | struct rcu_head head; | |
2ed95849 | 81 | struct rcu_ht *ht; |
abc490a1 | 82 | }; |
2ed95849 | 83 | |
abc490a1 MD |
84 | /* |
85 | * Algorithm to reverse bits in a word by lookup table, extended to | |
86 | * 64-bit words. | |
f9830efd | 87 | * Source: |
abc490a1 | 88 | * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable |
f9830efd | 89 | * Originally from Public Domain. |
abc490a1 MD |
90 | */ |
91 | ||
92 | static const uint8_t BitReverseTable256[256] = | |
2ed95849 | 93 | { |
abc490a1 MD |
94 | #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64 |
95 | #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16) | |
96 | #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 ) | |
97 | R6(0), R6(2), R6(1), R6(3) | |
98 | }; | |
99 | #undef R2 | |
100 | #undef R4 | |
101 | #undef R6 | |
2ed95849 | 102 | |
abc490a1 MD |
103 | static |
104 | uint8_t bit_reverse_u8(uint8_t v) | |
105 | { | |
106 | return BitReverseTable256[v]; | |
107 | } | |
ab7d5fc6 | 108 | |
abc490a1 MD |
109 | static __attribute__((unused)) |
110 | uint32_t bit_reverse_u32(uint32_t v) | |
111 | { | |
112 | return ((uint32_t) bit_reverse_u8(v) << 24) | | |
113 | ((uint32_t) bit_reverse_u8(v >> 8) << 16) | | |
114 | ((uint32_t) bit_reverse_u8(v >> 16) << 8) | | |
115 | ((uint32_t) bit_reverse_u8(v >> 24)); | |
2ed95849 MD |
116 | } |
117 | ||
abc490a1 MD |
118 | static __attribute__((unused)) |
119 | uint64_t bit_reverse_u64(uint64_t v) | |
2ed95849 | 120 | { |
abc490a1 MD |
121 | return ((uint64_t) bit_reverse_u8(v) << 56) | |
122 | ((uint64_t) bit_reverse_u8(v >> 8) << 48) | | |
123 | ((uint64_t) bit_reverse_u8(v >> 16) << 40) | | |
124 | ((uint64_t) bit_reverse_u8(v >> 24) << 32) | | |
125 | ((uint64_t) bit_reverse_u8(v >> 32) << 24) | | |
126 | ((uint64_t) bit_reverse_u8(v >> 40) << 16) | | |
127 | ((uint64_t) bit_reverse_u8(v >> 48) << 8) | | |
128 | ((uint64_t) bit_reverse_u8(v >> 56)); | |
129 | } | |
130 | ||
131 | static | |
132 | unsigned long bit_reverse_ulong(unsigned long v) | |
133 | { | |
134 | #if (CAA_BITS_PER_LONG == 32) | |
135 | return bit_reverse_u32(v); | |
136 | #else | |
137 | return bit_reverse_u64(v); | |
138 | #endif | |
139 | } | |
140 | ||
f9830efd MD |
141 | /* |
142 | * Algorithm to find the log2 of a 32-bit unsigned integer. | |
143 | * source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup | |
144 | * Originally from Public Domain. | |
145 | */ | |
146 | static const char LogTable256[256] = | |
147 | { | |
148 | #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n | |
149 | -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, | |
150 | LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6), | |
151 | LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7) | |
152 | }; | |
153 | ||
154 | uint32_t log2_u32(uint32_t v) | |
155 | { | |
156 | uint32_t t, tt; | |
157 | ||
158 | if ((tt = (v >> 16))) | |
159 | return (t = (tt >> 8)) | |
160 | ? 24 + LogTable256[t] | |
161 | : 16 + LogTable256[tt]; | |
162 | else | |
163 | return (t = (v >> 8)) | |
164 | ? 8 + LogTable256[t] | |
165 | : LogTable256[v]; | |
166 | } | |
167 | ||
168 | static | |
169 | void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth); | |
170 | ||
171 | static | |
172 | void check_resize(struct rcu_ht *ht, struct rcu_table *t, | |
173 | uint32_t chain_len) | |
174 | { | |
3390d470 MD |
175 | if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD) |
176 | ht_resize_lazy(ht, t, | |
65e8e729 | 177 | log2_u32(chain_len - CHAIN_LEN_TARGET - 1)); |
f9830efd MD |
178 | } |
179 | ||
abc490a1 MD |
180 | static |
181 | struct rcu_ht_node *clear_flag(struct rcu_ht_node *node) | |
182 | { | |
d37166c6 | 183 | return (struct rcu_ht_node *) (((unsigned long) node) & ~FLAGS_MASK); |
abc490a1 MD |
184 | } |
185 | ||
186 | static | |
187 | int is_removed(struct rcu_ht_node *node) | |
188 | { | |
d37166c6 | 189 | return ((unsigned long) node) & REMOVED_FLAG; |
abc490a1 MD |
190 | } |
191 | ||
192 | static | |
193 | struct rcu_ht_node *flag_removed(struct rcu_ht_node *node) | |
194 | { | |
d37166c6 | 195 | return (struct rcu_ht_node *) (((unsigned long) node) | REMOVED_FLAG); |
abc490a1 MD |
196 | } |
197 | ||
f5596c94 MD |
198 | static |
199 | int is_dummy(struct rcu_ht_node *node) | |
200 | { | |
201 | return ((unsigned long) node) & DUMMY_FLAG; | |
202 | } | |
203 | ||
204 | static | |
205 | struct rcu_ht_node *flag_dummy(struct rcu_ht_node *node) | |
206 | { | |
207 | return (struct rcu_ht_node *) (((unsigned long) node) | DUMMY_FLAG); | |
208 | } | |
209 | ||
abc490a1 | 210 | static |
f9830efd | 211 | unsigned long _uatomic_max(unsigned long *ptr, unsigned long v) |
abc490a1 MD |
212 | { |
213 | unsigned long old1, old2; | |
214 | ||
215 | old1 = uatomic_read(ptr); | |
216 | do { | |
217 | old2 = old1; | |
218 | if (old2 >= v) | |
f9830efd | 219 | return old2; |
abc490a1 | 220 | } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2); |
f9830efd | 221 | return v; |
abc490a1 MD |
222 | } |
223 | ||
273399de MD |
224 | /* |
225 | * Remove all logically deleted nodes from a bucket up to a certain node key. | |
226 | */ | |
227 | static | |
228 | void _ht_gc_bucket(struct rcu_ht_node *dummy, struct rcu_ht_node *node) | |
229 | { | |
f5596c94 | 230 | struct rcu_ht_node *iter_prev, *iter, *next, *new_next; |
273399de MD |
231 | |
232 | for (;;) { | |
233 | iter_prev = dummy; | |
234 | /* We can always skip the dummy node initially */ | |
cc4fcb10 MD |
235 | iter = rcu_dereference(iter_prev->p.next); |
236 | assert(iter_prev->p.reverse_hash <= node->p.reverse_hash); | |
273399de | 237 | for (;;) { |
a2974903 | 238 | if (unlikely(!clear_flag(iter))) |
479c8a32 | 239 | return; |
cc4fcb10 | 240 | if (clear_flag(iter)->p.reverse_hash > node->p.reverse_hash) |
273399de | 241 | return; |
cc4fcb10 | 242 | next = rcu_dereference(clear_flag(iter)->p.next); |
273399de MD |
243 | if (is_removed(next)) |
244 | break; | |
273399de MD |
245 | iter_prev = iter; |
246 | iter = next; | |
247 | } | |
248 | assert(!is_removed(iter)); | |
f5596c94 MD |
249 | if (is_dummy(iter)) |
250 | new_next = flag_dummy(clear_flag(next)); | |
251 | else | |
252 | new_next = clear_flag(next); | |
253 | (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next); | |
273399de MD |
254 | } |
255 | } | |
256 | ||
abc490a1 | 257 | static |
18117871 | 258 | struct rcu_ht_node *_ht_add(struct rcu_ht *ht, struct rcu_table *t, |
f5596c94 | 259 | struct rcu_ht_node *node, int unique, int dummy) |
abc490a1 | 260 | { |
f5596c94 MD |
261 | struct rcu_ht_node *iter_prev, *iter, *next, *new_node, *new_next, |
262 | *dummy_node; | |
49c2e2d6 | 263 | unsigned long hash; |
abc490a1 | 264 | |
18117871 | 265 | if (!t->size) { |
cc4fcb10 | 266 | assert(node->p.dummy); |
f5596c94 MD |
267 | assert(dummy); |
268 | node->p.next = flag_dummy(NULL); | |
18117871 MD |
269 | return node; /* Initial first add (head) */ |
270 | } | |
cc4fcb10 | 271 | hash = bit_reverse_ulong(node->p.reverse_hash); |
abc490a1 | 272 | for (;;) { |
f9830efd | 273 | uint32_t chain_len = 0; |
abc490a1 | 274 | |
11519af6 MD |
275 | /* |
276 | * iter_prev points to the non-removed node prior to the | |
277 | * insert location. | |
11519af6 | 278 | */ |
49c2e2d6 | 279 | iter_prev = rcu_dereference(t->tbl[hash & (t->size - 1)]); |
11519af6 | 280 | /* We can always skip the dummy node initially */ |
cc4fcb10 MD |
281 | iter = rcu_dereference(iter_prev->p.next); |
282 | assert(iter_prev->p.reverse_hash <= node->p.reverse_hash); | |
abc490a1 | 283 | for (;;) { |
a2974903 | 284 | if (unlikely(!clear_flag(iter))) |
273399de | 285 | goto insert; |
cc4fcb10 | 286 | if (clear_flag(iter)->p.reverse_hash > node->p.reverse_hash) |
273399de | 287 | goto insert; |
cc4fcb10 | 288 | next = rcu_dereference(clear_flag(iter)->p.next); |
273399de | 289 | if (is_removed(next)) |
9dba85be | 290 | goto gc_node; |
e43f23f8 | 291 | if (unique |
cc4fcb10 | 292 | && !clear_flag(iter)->p.dummy |
e43f23f8 MD |
293 | && !ht->compare_fct(node->key, node->key_len, |
294 | clear_flag(iter)->key, | |
295 | clear_flag(iter)->key_len)) | |
18117871 | 296 | return clear_flag(iter); |
11519af6 | 297 | /* Only account for identical reverse hash once */ |
cc4fcb10 | 298 | if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash) |
11519af6 MD |
299 | check_resize(ht, t, ++chain_len); |
300 | iter_prev = clear_flag(iter); | |
273399de | 301 | iter = next; |
abc490a1 | 302 | } |
273399de | 303 | insert: |
7ec59d3b | 304 | assert(node != clear_flag(iter)); |
11519af6 | 305 | assert(!is_removed(iter_prev)); |
f000907d | 306 | assert(iter_prev != node); |
f5596c94 MD |
307 | if (!dummy) |
308 | node->p.next = iter; | |
309 | else | |
310 | node->p.next = flag_dummy(iter); | |
311 | if (is_dummy(iter)) | |
312 | new_node = flag_dummy(node); | |
313 | else | |
314 | new_node = node; | |
cc4fcb10 | 315 | if (uatomic_cmpxchg(&iter_prev->p.next, iter, |
f5596c94 | 316 | new_node) != iter) |
273399de | 317 | continue; /* retry */ |
11519af6 | 318 | else |
273399de | 319 | goto gc_end; |
9dba85be MD |
320 | gc_node: |
321 | assert(!is_removed(iter)); | |
f5596c94 MD |
322 | if (is_dummy(iter)) |
323 | new_next = flag_dummy(clear_flag(next)); | |
324 | else | |
325 | new_next = clear_flag(next); | |
326 | (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next); | |
273399de | 327 | /* retry */ |
464a1ec9 | 328 | } |
273399de MD |
329 | gc_end: |
330 | /* Garbage collect logically removed nodes in the bucket */ | |
f5596c94 MD |
331 | dummy_node = rcu_dereference(t->tbl[hash & (t->size - 1)]); |
332 | _ht_gc_bucket(dummy_node, node); | |
18117871 | 333 | return node; |
abc490a1 | 334 | } |
464a1ec9 | 335 | |
abc490a1 MD |
336 | static |
337 | int _ht_remove(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node) | |
338 | { | |
273399de | 339 | struct rcu_ht_node *dummy, *next, *old; |
abc490a1 | 340 | int flagged = 0; |
49c2e2d6 | 341 | unsigned long hash; |
5e28c532 | 342 | |
7ec59d3b | 343 | /* logically delete the node */ |
cc4fcb10 | 344 | old = rcu_dereference(node->p.next); |
7ec59d3b MD |
345 | do { |
346 | next = old; | |
347 | if (is_removed(next)) | |
348 | goto end; | |
cc4fcb10 MD |
349 | assert(!node->p.dummy); |
350 | old = uatomic_cmpxchg(&node->p.next, next, | |
7ec59d3b MD |
351 | flag_removed(next)); |
352 | } while (old != next); | |
353 | ||
354 | /* We performed the (logical) deletion. */ | |
355 | flagged = 1; | |
356 | ||
357 | /* | |
358 | * Ensure that the node is not visible to readers anymore: lookup for | |
273399de MD |
359 | * the node, and remove it (along with any other logically removed node) |
360 | * if found. | |
11519af6 | 361 | */ |
cc4fcb10 | 362 | hash = bit_reverse_ulong(node->p.reverse_hash); |
49c2e2d6 | 363 | dummy = rcu_dereference(t->tbl[hash & (t->size - 1)]); |
273399de | 364 | _ht_gc_bucket(dummy, node); |
2ed95849 | 365 | end: |
11519af6 MD |
366 | /* |
367 | * Only the flagging action indicated that we (and no other) | |
368 | * removed the node from the hash. | |
369 | */ | |
7ec59d3b | 370 | if (flagged) { |
cc4fcb10 | 371 | assert(is_removed(rcu_dereference(node->p.next))); |
11519af6 | 372 | return 0; |
7ec59d3b | 373 | } else |
11519af6 | 374 | return -ENOENT; |
abc490a1 | 375 | } |
2ed95849 | 376 | |
abc490a1 MD |
377 | static |
378 | void init_table(struct rcu_ht *ht, struct rcu_table *t, | |
379 | unsigned long first, unsigned long len) | |
380 | { | |
381 | unsigned long i, end; | |
382 | ||
383 | end = first + len; | |
384 | for (i = first; i < end; i++) { | |
385 | /* Update table size when power of two */ | |
386 | if (i != 0 && !(i & (i - 1))) | |
387 | t->size = i; | |
cc4fcb10 MD |
388 | t->tbl[i] = calloc(1, sizeof(struct _rcu_ht_node)); |
389 | t->tbl[i]->p.dummy = 1; | |
390 | t->tbl[i]->p.reverse_hash = bit_reverse_ulong(i); | |
f5596c94 | 391 | (void) _ht_add(ht, t, t->tbl[i], 0, 1); |
abc490a1 | 392 | } |
f9830efd | 393 | t->resize_target = t->size = end; |
11519af6 | 394 | t->resize_initiated = 0; |
2ed95849 MD |
395 | } |
396 | ||
abc490a1 | 397 | struct rcu_ht *ht_new(ht_hash_fct hash_fct, |
732ad076 MD |
398 | ht_compare_fct compare_fct, |
399 | unsigned long hash_seed, | |
abc490a1 MD |
400 | unsigned long init_size, |
401 | void (*ht_call_rcu)(struct rcu_head *head, | |
402 | void (*func)(struct rcu_head *head))) | |
403 | { | |
404 | struct rcu_ht *ht; | |
405 | ||
406 | ht = calloc(1, sizeof(struct rcu_ht)); | |
407 | ht->hash_fct = hash_fct; | |
732ad076 MD |
408 | ht->compare_fct = compare_fct; |
409 | ht->hash_seed = hash_seed; | |
f000907d | 410 | ht->ht_call_rcu = ht_call_rcu; |
848d4088 | 411 | ht->in_progress_resize = 0; |
abc490a1 MD |
412 | /* this mutex should not nest in read-side C.S. */ |
413 | pthread_mutex_init(&ht->resize_mutex, NULL); | |
414 | ht->t = calloc(1, sizeof(struct rcu_table) | |
415 | + (max(init_size, 1) * sizeof(struct rcu_ht_node *))); | |
416 | ht->t->size = 0; | |
f000907d | 417 | pthread_mutex_lock(&ht->resize_mutex); |
abc490a1 | 418 | init_table(ht, ht->t, 0, max(init_size, 1)); |
f000907d | 419 | pthread_mutex_unlock(&ht->resize_mutex); |
abc490a1 MD |
420 | return ht; |
421 | } | |
422 | ||
732ad076 | 423 | struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key, size_t key_len) |
2ed95849 | 424 | { |
395270b6 | 425 | struct rcu_table *t; |
abc490a1 MD |
426 | struct rcu_ht_node *node; |
427 | unsigned long hash, reverse_hash; | |
2ed95849 | 428 | |
732ad076 | 429 | hash = ht->hash_fct(key, key_len, ht->hash_seed); |
abc490a1 | 430 | reverse_hash = bit_reverse_ulong(hash); |
464a1ec9 | 431 | |
395270b6 | 432 | t = rcu_dereference(ht->t); |
abc490a1 | 433 | node = rcu_dereference(t->tbl[hash & (t->size - 1)]); |
2ed95849 | 434 | for (;;) { |
abc490a1 MD |
435 | if (unlikely(!node)) |
436 | break; | |
cc4fcb10 | 437 | if (unlikely(node->p.reverse_hash > reverse_hash)) { |
abc490a1 MD |
438 | node = NULL; |
439 | break; | |
2ed95849 | 440 | } |
cc4fcb10 MD |
441 | if (likely(!is_removed(rcu_dereference(node->p.next))) |
442 | && !node->p.dummy | |
49c2e2d6 | 443 | && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) { |
273399de | 444 | break; |
2ed95849 | 445 | } |
cc4fcb10 | 446 | node = clear_flag(rcu_dereference(node->p.next)); |
2ed95849 | 447 | } |
cc4fcb10 | 448 | assert(!node || !node->p.dummy); |
abc490a1 MD |
449 | return node; |
450 | } | |
e0ba718a | 451 | |
f000907d | 452 | void ht_add(struct rcu_ht *ht, struct rcu_ht_node *node) |
abc490a1 MD |
453 | { |
454 | struct rcu_table *t; | |
49c2e2d6 | 455 | unsigned long hash; |
ab7d5fc6 | 456 | |
49c2e2d6 | 457 | hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed); |
cc4fcb10 | 458 | node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash); |
2ed95849 | 459 | |
abc490a1 | 460 | t = rcu_dereference(ht->t); |
f5596c94 | 461 | (void) _ht_add(ht, t, node, 0, 0); |
3eca1b8c MD |
462 | } |
463 | ||
18117871 | 464 | struct rcu_ht_node *ht_add_unique(struct rcu_ht *ht, struct rcu_ht_node *node) |
3eca1b8c MD |
465 | { |
466 | struct rcu_table *t; | |
49c2e2d6 | 467 | unsigned long hash; |
3eca1b8c | 468 | |
49c2e2d6 | 469 | hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed); |
cc4fcb10 | 470 | node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash); |
3eca1b8c MD |
471 | |
472 | t = rcu_dereference(ht->t); | |
f5596c94 | 473 | return _ht_add(ht, t, node, 1, 0); |
2ed95849 MD |
474 | } |
475 | ||
abc490a1 | 476 | int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node) |
2ed95849 | 477 | { |
abc490a1 MD |
478 | struct rcu_table *t; |
479 | ||
480 | t = rcu_dereference(ht->t); | |
abc490a1 | 481 | return _ht_remove(ht, t, node); |
2ed95849 | 482 | } |
ab7d5fc6 | 483 | |
abc490a1 MD |
484 | static |
485 | int ht_delete_dummy(struct rcu_ht *ht) | |
674f7a69 | 486 | { |
395270b6 | 487 | struct rcu_table *t; |
abc490a1 MD |
488 | struct rcu_ht_node *node; |
489 | unsigned long i; | |
674f7a69 | 490 | |
abc490a1 MD |
491 | t = ht->t; |
492 | /* Check that the table is empty */ | |
493 | node = t->tbl[0]; | |
494 | do { | |
cc4fcb10 | 495 | if (!node->p.dummy) |
abc490a1 | 496 | return -EPERM; |
cc4fcb10 | 497 | node = node->p.next; |
273399de | 498 | assert(!is_removed(node)); |
a2974903 | 499 | } while (clear_flag(node)); |
abc490a1 | 500 | /* Internal sanity check: all nodes left should be dummy */ |
395270b6 | 501 | for (i = 0; i < t->size; i++) { |
cc4fcb10 | 502 | assert(t->tbl[i]->p.dummy); |
abc490a1 | 503 | free(t->tbl[i]); |
674f7a69 | 504 | } |
abc490a1 | 505 | return 0; |
674f7a69 MD |
506 | } |
507 | ||
508 | /* | |
509 | * Should only be called when no more concurrent readers nor writers can | |
510 | * possibly access the table. | |
511 | */ | |
5e28c532 | 512 | int ht_destroy(struct rcu_ht *ht) |
674f7a69 | 513 | { |
5e28c532 MD |
514 | int ret; |
515 | ||
848d4088 MD |
516 | /* Wait for in-flight resize operations to complete */ |
517 | while (uatomic_read(&ht->in_progress_resize)) | |
518 | poll(NULL, 0, 100); /* wait for 100ms */ | |
abc490a1 MD |
519 | ret = ht_delete_dummy(ht); |
520 | if (ret) | |
521 | return ret; | |
395270b6 | 522 | free(ht->t); |
674f7a69 | 523 | free(ht); |
5e28c532 | 524 | return ret; |
674f7a69 MD |
525 | } |
526 | ||
273399de MD |
527 | void ht_count_nodes(struct rcu_ht *ht, |
528 | unsigned long *count, | |
529 | unsigned long *removed) | |
530 | { | |
531 | struct rcu_table *t; | |
532 | struct rcu_ht_node *node, *next; | |
533 | ||
534 | *count = 0; | |
535 | *removed = 0; | |
536 | ||
537 | t = rcu_dereference(ht->t); | |
538 | /* Check that the table is empty */ | |
539 | node = rcu_dereference(t->tbl[0]); | |
540 | do { | |
cc4fcb10 | 541 | next = rcu_dereference(node->p.next); |
273399de | 542 | if (is_removed(next)) { |
cc4fcb10 | 543 | assert(!node->p.dummy); |
273399de | 544 | (*removed)++; |
cc4fcb10 | 545 | } else if (!node->p.dummy) |
273399de MD |
546 | (*count)++; |
547 | node = clear_flag(next); | |
548 | } while (node); | |
549 | } | |
550 | ||
abc490a1 MD |
551 | static |
552 | void ht_free_table_cb(struct rcu_head *head) | |
553 | { | |
554 | struct rcu_table *t = | |
555 | caa_container_of(head, struct rcu_table, head); | |
556 | free(t); | |
557 | } | |
558 | ||
559 | /* called with resize mutex held */ | |
560 | static | |
561 | void _do_ht_resize(struct rcu_ht *ht) | |
464a1ec9 | 562 | { |
abc490a1 | 563 | unsigned long new_size, old_size; |
395270b6 | 564 | struct rcu_table *new_t, *old_t; |
464a1ec9 | 565 | |
395270b6 MD |
566 | old_t = ht->t; |
567 | old_size = old_t->size; | |
464a1ec9 | 568 | |
f9830efd MD |
569 | new_size = CMM_LOAD_SHARED(old_t->resize_target); |
570 | dbg_printf("rculfhash: resize from %lu to %lu buckets\n", | |
571 | old_size, new_size); | |
abc490a1 | 572 | if (old_size == new_size) |
464a1ec9 | 573 | return; |
f000907d | 574 | new_t = malloc(sizeof(struct rcu_table) |
abc490a1 | 575 | + (new_size * sizeof(struct rcu_ht_node *))); |
f000907d MD |
576 | assert(new_size > old_size); |
577 | memcpy(&new_t->tbl, &old_t->tbl, | |
578 | old_size * sizeof(struct rcu_ht_node *)); | |
579 | init_table(ht, new_t, old_size, new_size - old_size); | |
f000907d MD |
580 | /* Changing table and size atomically wrt lookups */ |
581 | rcu_assign_pointer(ht->t, new_t); | |
582 | ht->ht_call_rcu(&old_t->head, ht_free_table_cb); | |
464a1ec9 MD |
583 | } |
584 | ||
abc490a1 | 585 | static |
f9830efd MD |
586 | unsigned long resize_target_update(struct rcu_table *t, |
587 | int growth_order) | |
464a1ec9 | 588 | { |
f9830efd MD |
589 | return _uatomic_max(&t->resize_target, |
590 | t->size << growth_order); | |
464a1ec9 MD |
591 | } |
592 | ||
464a1ec9 MD |
593 | void ht_resize(struct rcu_ht *ht, int growth) |
594 | { | |
f9830efd MD |
595 | struct rcu_table *t = rcu_dereference(ht->t); |
596 | unsigned long target_size; | |
597 | ||
598 | target_size = resize_target_update(t, growth); | |
599 | if (t->size < target_size) { | |
11519af6 | 600 | CMM_STORE_SHARED(t->resize_initiated, 1); |
f9830efd MD |
601 | pthread_mutex_lock(&ht->resize_mutex); |
602 | _do_ht_resize(ht); | |
603 | pthread_mutex_unlock(&ht->resize_mutex); | |
604 | } | |
abc490a1 | 605 | } |
464a1ec9 | 606 | |
abc490a1 MD |
607 | static |
608 | void do_resize_cb(struct rcu_head *head) | |
609 | { | |
610 | struct rcu_resize_work *work = | |
611 | caa_container_of(head, struct rcu_resize_work, head); | |
612 | struct rcu_ht *ht = work->ht; | |
613 | ||
614 | pthread_mutex_lock(&ht->resize_mutex); | |
615 | _do_ht_resize(ht); | |
616 | pthread_mutex_unlock(&ht->resize_mutex); | |
617 | free(work); | |
848d4088 MD |
618 | cmm_smp_mb(); /* finish resize before decrement */ |
619 | uatomic_dec(&ht->in_progress_resize); | |
464a1ec9 MD |
620 | } |
621 | ||
abc490a1 | 622 | static |
f000907d | 623 | void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth) |
ab7d5fc6 | 624 | { |
abc490a1 | 625 | struct rcu_resize_work *work; |
f9830efd | 626 | unsigned long target_size; |
abc490a1 | 627 | |
f9830efd | 628 | target_size = resize_target_update(t, growth); |
11519af6 | 629 | if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) { |
848d4088 MD |
630 | uatomic_inc(&ht->in_progress_resize); |
631 | cmm_smp_mb(); /* increment resize count before calling it */ | |
f9830efd MD |
632 | work = malloc(sizeof(*work)); |
633 | work->ht = ht; | |
634 | ht->ht_call_rcu(&work->head, do_resize_cb); | |
11519af6 | 635 | CMM_STORE_SHARED(t->resize_initiated, 1); |
f9830efd | 636 | } |
ab7d5fc6 | 637 | } |