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