Commit | Line | Data |
---|---|---|
195e72d3 MD |
1 | /* |
2 | * rcuja/rcuja-hashtable.c | |
3 | * | |
4 | * Userspace RCU library - RCU Judy Array Shadow Node Hash Table | |
5 | * | |
6 | * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
23 | #define _LGPL_SOURCE | |
24 | #include <stdint.h> | |
25 | #include <errno.h> | |
26 | #include <limits.h> | |
195e72d3 MD |
27 | #include <urcu/rcuja.h> |
28 | #include <urcu/compiler.h> | |
29 | #include <urcu/arch.h> | |
30 | #include <assert.h> | |
31 | #include <urcu-pointer.h> | |
d22c185b MD |
32 | #include <stdlib.h> |
33 | #include <time.h> | |
195e72d3 MD |
34 | |
35 | #include "rcuja-internal.h" | |
36 | #include "bitfield.h" | |
37 | ||
d22c185b MD |
38 | static unsigned long hash_seed; |
39 | ||
40 | /* | |
41 | * Hash function | |
42 | * Source: http://burtleburtle.net/bob/c/lookup3.c | |
43 | * Originally Public Domain | |
44 | */ | |
45 | ||
46 | #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | |
47 | ||
48 | #define mix(a, b, c) \ | |
49 | do { \ | |
50 | a -= c; a ^= rot(c, 4); c += b; \ | |
51 | b -= a; b ^= rot(a, 6); a += c; \ | |
52 | c -= b; c ^= rot(b, 8); b += a; \ | |
53 | a -= c; a ^= rot(c, 16); c += b; \ | |
54 | b -= a; b ^= rot(a, 19); a += c; \ | |
55 | c -= b; c ^= rot(b, 4); b += a; \ | |
56 | } while (0) | |
57 | ||
58 | #define final(a, b, c) \ | |
59 | { \ | |
60 | c ^= b; c -= rot(b, 14); \ | |
61 | a ^= c; a -= rot(c, 11); \ | |
62 | b ^= a; b -= rot(a, 25); \ | |
63 | c ^= b; c -= rot(b, 16); \ | |
64 | a ^= c; a -= rot(c, 4);\ | |
65 | b ^= a; b -= rot(a, 14); \ | |
66 | c ^= b; c -= rot(b, 24); \ | |
67 | } | |
68 | ||
69 | static inline __attribute__((unused)) | |
70 | uint32_t hash_u32( | |
71 | const uint32_t *k, /* the key, an array of uint32_t values */ | |
72 | size_t length, /* the length of the key, in uint32_ts */ | |
73 | uint32_t initval) /* the previous hash, or an arbitrary value */ | |
74 | { | |
75 | uint32_t a, b, c; | |
76 | ||
77 | /* Set up the internal state */ | |
78 | a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval; | |
79 | ||
80 | /*----------------------------------------- handle most of the key */ | |
81 | while (length > 3) { | |
82 | a += k[0]; | |
83 | b += k[1]; | |
84 | c += k[2]; | |
85 | mix(a, b, c); | |
86 | length -= 3; | |
87 | k += 3; | |
88 | } | |
89 | ||
90 | /*----------------------------------- handle the last 3 uint32_t's */ | |
91 | switch (length) { /* all the case statements fall through */ | |
92 | case 3: c += k[2]; | |
93 | case 2: b += k[1]; | |
94 | case 1: a += k[0]; | |
95 | final(a, b, c); | |
96 | case 0: /* case 0: nothing left to add */ | |
97 | break; | |
98 | } | |
99 | /*---------------------------------------------- report the result */ | |
100 | return c; | |
101 | } | |
102 | ||
103 | static inline | |
104 | void hashword2( | |
105 | const uint32_t *k, /* the key, an array of uint32_t values */ | |
106 | size_t length, /* the length of the key, in uint32_ts */ | |
107 | uint32_t *pc, /* IN: seed OUT: primary hash value */ | |
108 | uint32_t *pb) /* IN: more seed OUT: secondary hash value */ | |
109 | { | |
110 | uint32_t a, b, c; | |
111 | ||
112 | /* Set up the internal state */ | |
113 | a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc; | |
114 | c += *pb; | |
115 | ||
116 | /*----------------------------------------- handle most of the key */ | |
117 | while (length > 3) { | |
118 | a += k[0]; | |
119 | b += k[1]; | |
120 | c += k[2]; | |
121 | mix(a, b, c); | |
122 | length -= 3; | |
123 | k += 3; | |
124 | } | |
125 | ||
126 | /*----------------------------------- handle the last 3 uint32_t's */ | |
127 | switch (length) { /* all the case statements fall through */ | |
128 | case 3: c += k[2]; | |
129 | case 2: b += k[1]; | |
130 | case 1: a += k[0]; | |
131 | final(a, b, c); | |
132 | case 0: /* case 0: nothing left to add */ | |
133 | break; | |
134 | } | |
135 | /*---------------------------------------------- report the result */ | |
136 | *pc = c; | |
137 | *pb = b; | |
138 | } | |
139 | ||
140 | #if (CAA_BITS_PER_LONG == 32) | |
141 | static | |
142 | unsigned long hash_pointer(const void *_key, unsigned long seed) | |
143 | { | |
144 | unsigned int key = (unsigned int) _key; | |
145 | ||
146 | return hash_u32(&key, 1, seed); | |
147 | } | |
148 | #else | |
149 | static | |
150 | unsigned long hash_pointer(const void *_key, unsigned long seed) | |
151 | { | |
152 | union { | |
153 | uint64_t v64; | |
154 | uint32_t v32[2]; | |
155 | } v; | |
156 | union { | |
157 | uint64_t v64; | |
158 | uint32_t v32[2]; | |
159 | } key; | |
160 | ||
161 | v.v64 = (uint64_t) seed; | |
162 | key.v64 = (uint64_t) _key; | |
163 | hashword2(key.v32, 2, &v.v32[0], &v.v32[1]); | |
164 | return v.v64; | |
165 | } | |
166 | #endif | |
167 | ||
168 | static | |
169 | int match_pointer(struct cds_lfht_node *node, const void *key) | |
170 | { | |
d96bfb0d MD |
171 | struct cds_ja_shadow_node *shadow = |
172 | caa_container_of(node, struct cds_ja_shadow_node, ht_node); | |
d22c185b | 173 | |
3d8fe307 | 174 | return (key == shadow->node_flag); |
d22c185b MD |
175 | } |
176 | ||
177 | __attribute__((visibility("protected"))) | |
d96bfb0d | 178 | struct cds_ja_shadow_node *rcuja_shadow_lookup_lock(struct cds_lfht *ht, |
3d8fe307 | 179 | struct cds_ja_inode_flag *node_flag) |
d22c185b MD |
180 | { |
181 | struct cds_lfht_iter iter; | |
182 | struct cds_lfht_node *lookup_node; | |
d96bfb0d | 183 | struct cds_ja_shadow_node *shadow_node; |
5eb692c0 | 184 | const struct rcu_flavor_struct *flavor; |
d22c185b MD |
185 | int ret; |
186 | ||
5eb692c0 MD |
187 | flavor = cds_lfht_rcu_flavor(ht); |
188 | flavor->read_lock(); | |
3d8fe307 MD |
189 | cds_lfht_lookup(ht, hash_pointer(node_flag, hash_seed), |
190 | match_pointer, node_flag, &iter); | |
d22c185b MD |
191 | |
192 | lookup_node = cds_lfht_iter_get_node(&iter); | |
193 | if (!lookup_node) { | |
194 | shadow_node = NULL; | |
195 | goto rcu_unlock; | |
196 | } | |
197 | shadow_node = caa_container_of(lookup_node, | |
d96bfb0d | 198 | struct cds_ja_shadow_node, ht_node); |
a2a7ff59 | 199 | dbg_printf("Lock %p\n", shadow_node->lock); |
e1db2db5 | 200 | ret = pthread_mutex_lock(shadow_node->lock); |
d22c185b MD |
201 | assert(!ret); |
202 | if (cds_lfht_is_node_deleted(lookup_node)) { | |
e1db2db5 | 203 | ret = pthread_mutex_unlock(shadow_node->lock); |
d22c185b MD |
204 | assert(!ret); |
205 | shadow_node = NULL; | |
206 | } | |
207 | rcu_unlock: | |
5eb692c0 | 208 | flavor->read_unlock(); |
d22c185b MD |
209 | return shadow_node; |
210 | } | |
211 | ||
25fde237 | 212 | __attribute__((visibility("protected"))) |
d96bfb0d | 213 | void rcuja_shadow_unlock(struct cds_ja_shadow_node *shadow_node) |
d22c185b MD |
214 | { |
215 | int ret; | |
216 | ||
a2a7ff59 | 217 | dbg_printf("Unlock %p\n", shadow_node->lock); |
e1db2db5 | 218 | ret = pthread_mutex_unlock(shadow_node->lock); |
d22c185b MD |
219 | assert(!ret); |
220 | } | |
221 | ||
222 | __attribute__((visibility("protected"))) | |
f07b240f | 223 | struct cds_ja_shadow_node *rcuja_shadow_set(struct cds_lfht *ht, |
3d8fe307 MD |
224 | struct cds_ja_inode_flag *new_node_flag, |
225 | struct cds_ja_shadow_node *inherit_from, | |
226 | struct cds_ja *ja) | |
d22c185b | 227 | { |
d96bfb0d | 228 | struct cds_ja_shadow_node *shadow_node; |
d22c185b | 229 | struct cds_lfht_node *ret_node; |
5eb692c0 | 230 | const struct rcu_flavor_struct *flavor; |
d22c185b MD |
231 | |
232 | shadow_node = calloc(sizeof(*shadow_node), 1); | |
233 | if (!shadow_node) | |
f07b240f | 234 | return NULL; |
d22c185b | 235 | |
3d8fe307 MD |
236 | shadow_node->node_flag = new_node_flag; |
237 | shadow_node->ja = ja; | |
e1db2db5 MD |
238 | /* |
239 | * Lock can be inherited from previous node at this position. | |
240 | */ | |
241 | if (inherit_from) { | |
242 | shadow_node->lock = inherit_from->lock; | |
243 | } else { | |
244 | shadow_node->lock = calloc(sizeof(*shadow_node->lock), 1); | |
245 | if (!shadow_node->lock) { | |
246 | free(shadow_node); | |
f07b240f | 247 | return NULL; |
e1db2db5 MD |
248 | } |
249 | pthread_mutex_init(shadow_node->lock, NULL); | |
250 | } | |
d22c185b | 251 | |
5eb692c0 MD |
252 | flavor = cds_lfht_rcu_flavor(ht); |
253 | flavor->read_lock(); | |
d22c185b | 254 | ret_node = cds_lfht_add_unique(ht, |
3d8fe307 | 255 | hash_pointer(new_node_flag, hash_seed), |
d22c185b | 256 | match_pointer, |
3d8fe307 | 257 | new_node_flag, |
d22c185b | 258 | &shadow_node->ht_node); |
5eb692c0 | 259 | flavor->read_unlock(); |
d22c185b MD |
260 | |
261 | if (ret_node != &shadow_node->ht_node) { | |
262 | free(shadow_node); | |
f07b240f | 263 | return NULL; |
d22c185b | 264 | } |
f07b240f | 265 | return shadow_node; |
d22c185b MD |
266 | } |
267 | ||
a2a7ff59 MD |
268 | static |
269 | void free_shadow_node(struct rcu_head *head) | |
270 | { | |
271 | struct cds_ja_shadow_node *shadow_node = | |
272 | caa_container_of(head, struct cds_ja_shadow_node, head); | |
273 | free(shadow_node); | |
274 | } | |
275 | ||
d22c185b | 276 | static |
25fde237 | 277 | void free_shadow_node_and_node(struct rcu_head *head) |
d22c185b | 278 | { |
d96bfb0d MD |
279 | struct cds_ja_shadow_node *shadow_node = |
280 | caa_container_of(head, struct cds_ja_shadow_node, head); | |
3d8fe307 | 281 | free(ja_node_ptr(shadow_node->node_flag)); |
d22c185b MD |
282 | free(shadow_node); |
283 | } | |
284 | ||
a2a7ff59 MD |
285 | static |
286 | void free_shadow_node_and_lock(struct rcu_head *head) | |
287 | { | |
288 | struct cds_ja_shadow_node *shadow_node = | |
289 | caa_container_of(head, struct cds_ja_shadow_node, head); | |
290 | free(shadow_node->lock); | |
291 | free(shadow_node); | |
292 | } | |
293 | ||
e1db2db5 MD |
294 | static |
295 | void free_shadow_node_and_node_and_lock(struct rcu_head *head) | |
296 | { | |
d96bfb0d MD |
297 | struct cds_ja_shadow_node *shadow_node = |
298 | caa_container_of(head, struct cds_ja_shadow_node, head); | |
3d8fe307 MD |
299 | assert(shadow_node->level); |
300 | free(ja_node_ptr(shadow_node->node_flag)); | |
e1db2db5 MD |
301 | free(shadow_node->lock); |
302 | free(shadow_node); | |
303 | } | |
304 | ||
d22c185b | 305 | __attribute__((visibility("protected"))) |
e1db2db5 | 306 | int rcuja_shadow_clear(struct cds_lfht *ht, |
3d8fe307 | 307 | struct cds_ja_inode_flag *node_flag, |
a2a7ff59 | 308 | struct cds_ja_shadow_node *shadow_node, |
e1db2db5 | 309 | unsigned int flags) |
d22c185b MD |
310 | { |
311 | struct cds_lfht_iter iter; | |
312 | struct cds_lfht_node *lookup_node; | |
5eb692c0 | 313 | const struct rcu_flavor_struct *flavor; |
d22c185b | 314 | int ret, lockret; |
a2a7ff59 | 315 | int lookup_shadow = 0; |
d22c185b | 316 | |
5eb692c0 MD |
317 | flavor = cds_lfht_rcu_flavor(ht); |
318 | flavor->read_lock(); | |
a2a7ff59 | 319 | |
3d8fe307 MD |
320 | cds_lfht_lookup(ht, hash_pointer(node_flag, hash_seed), |
321 | match_pointer, node_flag, &iter); | |
d22c185b MD |
322 | lookup_node = cds_lfht_iter_get_node(&iter); |
323 | if (!lookup_node) { | |
324 | ret = -ENOENT; | |
325 | goto rcu_unlock; | |
326 | } | |
a2a7ff59 MD |
327 | |
328 | if (!shadow_node) { | |
329 | shadow_node = caa_container_of(lookup_node, | |
330 | struct cds_ja_shadow_node, ht_node); | |
331 | lockret = pthread_mutex_lock(shadow_node->lock); | |
332 | assert(!lockret); | |
333 | lookup_shadow = 1; | |
334 | } | |
d22c185b MD |
335 | |
336 | /* | |
337 | * Holding the mutex across deletion, and by also re-checking if | |
338 | * the node is deleted with mutex held at lookup ensure that we | |
339 | * don't let RCU JA use a node being removed. | |
340 | */ | |
341 | ret = cds_lfht_del(ht, lookup_node); | |
342 | if (!ret) { | |
582a6ade | 343 | if ((flags & RCUJA_SHADOW_CLEAR_FREE_NODE) |
3d8fe307 | 344 | && shadow_node->level) { |
a2a7ff59 MD |
345 | if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) { |
346 | flavor->update_call_rcu(&shadow_node->head, | |
347 | free_shadow_node_and_node_and_lock); | |
348 | } else { | |
349 | flavor->update_call_rcu(&shadow_node->head, | |
350 | free_shadow_node_and_node); | |
351 | } | |
e1db2db5 | 352 | } else { |
a2a7ff59 MD |
353 | if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) { |
354 | flavor->update_call_rcu(&shadow_node->head, | |
355 | free_shadow_node_and_lock); | |
356 | } else { | |
357 | flavor->update_call_rcu(&shadow_node->head, | |
358 | free_shadow_node); | |
359 | } | |
e1db2db5 | 360 | } |
d22c185b | 361 | } |
a2a7ff59 MD |
362 | if (lookup_shadow) { |
363 | lockret = pthread_mutex_unlock(shadow_node->lock); | |
364 | assert(!lockret); | |
365 | } | |
d22c185b | 366 | rcu_unlock: |
5eb692c0 | 367 | flavor->read_unlock(); |
d22c185b MD |
368 | |
369 | return ret; | |
370 | } | |
371 | ||
be9a7474 MD |
372 | /* |
373 | * Delete all shadow nodes and nodes from hash table, along with their | |
374 | * associated lock. | |
375 | */ | |
376 | __attribute__((visibility("protected"))) | |
377 | void rcuja_shadow_prune(struct cds_lfht *ht, | |
3d8fe307 MD |
378 | unsigned int flags, |
379 | void (*free_node_cb)(struct rcu_head *head)) | |
be9a7474 MD |
380 | { |
381 | const struct rcu_flavor_struct *flavor; | |
382 | struct cds_ja_shadow_node *shadow_node; | |
383 | struct cds_lfht_iter iter; | |
384 | int ret, lockret; | |
385 | ||
386 | flavor = cds_lfht_rcu_flavor(ht); | |
387 | flavor->read_lock(); | |
388 | cds_lfht_for_each_entry(ht, &iter, shadow_node, ht_node) { | |
389 | lockret = pthread_mutex_lock(shadow_node->lock); | |
390 | assert(!lockret); | |
391 | ||
392 | ret = cds_lfht_del(ht, &shadow_node->ht_node); | |
393 | if (!ret) { | |
582a6ade | 394 | if ((flags & RCUJA_SHADOW_CLEAR_FREE_NODE) |
3d8fe307 MD |
395 | && shadow_node->level) { |
396 | if (shadow_node->level == shadow_node->ja->tree_depth - 1) { | |
397 | rcuja_free_all_children(shadow_node, | |
398 | shadow_node->node_flag, | |
399 | free_node_cb); | |
400 | } | |
582a6ade MD |
401 | if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) { |
402 | flavor->update_call_rcu(&shadow_node->head, | |
403 | free_shadow_node_and_node_and_lock); | |
404 | } else { | |
405 | flavor->update_call_rcu(&shadow_node->head, | |
406 | free_shadow_node_and_node); | |
407 | } | |
408 | } else { | |
409 | if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) { | |
410 | flavor->update_call_rcu(&shadow_node->head, | |
411 | free_shadow_node_and_lock); | |
412 | } else { | |
413 | flavor->update_call_rcu(&shadow_node->head, | |
414 | free_shadow_node); | |
415 | } | |
416 | } | |
be9a7474 MD |
417 | } |
418 | lockret = pthread_mutex_unlock(shadow_node->lock); | |
419 | assert(!lockret); | |
420 | } | |
421 | flavor->read_unlock(); | |
422 | } | |
423 | ||
195e72d3 | 424 | __attribute__((visibility("protected"))) |
5eb692c0 | 425 | struct cds_lfht *rcuja_create_ht(const struct rcu_flavor_struct *flavor) |
195e72d3 | 426 | { |
5eb692c0 | 427 | return _cds_lfht_new(1, 1, 0, |
195e72d3 | 428 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, |
5eb692c0 | 429 | NULL, flavor, NULL); |
195e72d3 MD |
430 | } |
431 | ||
432 | __attribute__((visibility("protected"))) | |
be9a7474 | 433 | int rcuja_delete_ht(struct cds_lfht *ht) |
195e72d3 | 434 | { |
be9a7474 | 435 | return cds_lfht_destroy(ht, NULL); |
195e72d3 | 436 | } |
d22c185b MD |
437 | |
438 | __attribute__((constructor)) | |
439 | void rcuja_ht_init(void) | |
440 | { | |
441 | hash_seed = (unsigned long) time(NULL); | |
442 | } |