4 * Userspace RCU library - RCU Judy Array
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
27 #include <urcu/rcuja.h>
28 #include <urcu/compiler.h>
29 #include <urcu/arch.h>
31 #include <urcu-pointer.h>
32 #include <urcu/uatomic.h>
35 #include "rcuja-internal.h"
38 enum cds_ja_type_class
{
39 RCU_JA_LINEAR
= 0, /* Type A */
40 /* 32-bit: 1 to 25 children, 8 to 128 bytes */
41 /* 64-bit: 1 to 28 children, 16 to 256 bytes */
42 RCU_JA_POOL
= 1, /* Type B */
43 /* 32-bit: 26 to 100 children, 256 to 512 bytes */
44 /* 64-bit: 29 to 112 children, 512 to 1024 bytes */
45 RCU_JA_PIGEON
= 2, /* Type C */
46 /* 32-bit: 101 to 256 children, 1024 bytes */
47 /* 64-bit: 113 to 256 children, 2048 bytes */
48 /* Leaf nodes are implicit from their height in the tree */
51 RCU_JA_NULL
, /* not an encoded type, but keeps code regular */
55 enum cds_ja_type_class type_class
;
56 uint16_t min_child
; /* minimum number of children: 1 to 256 */
57 uint16_t max_child
; /* maximum number of children: 1 to 256 */
58 uint16_t max_linear_child
; /* per-pool max nr. children: 1 to 256 */
59 uint16_t order
; /* node size is (1 << order), in bytes */
60 uint16_t nr_pool_order
; /* number of pools */
61 uint16_t pool_size_order
; /* pool size */
65 * Iteration on the array to find the right node size for the number of
66 * children stops when it reaches .max_child == 256 (this is the largest
67 * possible node size, which contains 256 children).
68 * The min_child overlaps with the previous max_child to provide an
69 * hysteresis loop to reallocation for patterns of cyclic add/removal
70 * within the same node.
71 * The node the index within the following arrays is represented on 3
72 * bits. It identifies the node type, min/max number of children, and
74 * The max_child values for the RCU_JA_POOL below result from
75 * statistical approximation: over million populations, the max_child
76 * covers between 97% and 99% of the populations generated. Therefore, a
77 * fallback should exist to cover the rare extreme population unbalance
78 * cases, but it will not have a major impact on speed nor space
79 * consumption, since those are rare cases.
82 #if (CAA_BITS_PER_LONG < 64)
85 ja_type_0_max_child
= 1,
86 ja_type_1_max_child
= 3,
87 ja_type_2_max_child
= 6,
88 ja_type_3_max_child
= 12,
89 ja_type_4_max_child
= 25,
90 ja_type_5_max_child
= 48,
91 ja_type_6_max_child
= 92,
92 ja_type_7_max_child
= 256,
93 ja_type_8_max_child
= 0, /* NULL */
97 ja_type_0_max_linear_child
= 1,
98 ja_type_1_max_linear_child
= 3,
99 ja_type_2_max_linear_child
= 6,
100 ja_type_3_max_linear_child
= 12,
101 ja_type_4_max_linear_child
= 25,
102 ja_type_5_max_linear_child
= 24,
103 ja_type_6_max_linear_child
= 23,
107 ja_type_5_nr_pool_order
= 1,
108 ja_type_6_nr_pool_order
= 2,
111 const struct cds_ja_type ja_types
[] = {
112 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_0_max_child
, .max_linear_child
= ja_type_0_max_linear_child
, .order
= 3, },
113 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_1_max_child
, .max_linear_child
= ja_type_1_max_linear_child
, .order
= 4, },
114 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= ja_type_2_max_child
, .max_linear_child
= ja_type_2_max_linear_child
, .order
= 5, },
115 { .type_class
= RCU_JA_LINEAR
, .min_child
= 4, .max_child
= ja_type_3_max_child
, .max_linear_child
= ja_type_3_max_linear_child
, .order
= 6, },
116 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= ja_type_4_max_child
, .max_linear_child
= ja_type_4_max_linear_child
, .order
= 7, },
118 /* Pools may fill sooner than max_child */
119 { .type_class
= RCU_JA_POOL
, .min_child
= 20, .max_child
= ja_type_5_max_child
, .max_linear_child
= ja_type_5_max_linear_child
, .order
= 8, .nr_pool_order
= ja_type_5_nr_pool_order
, .pool_size_order
= 7, },
120 { .type_class
= RCU_JA_POOL
, .min_child
= 45, .max_child
= ja_type_6_max_child
, .max_linear_child
= ja_type_6_max_linear_child
, .order
= 9, .nr_pool_order
= ja_type_6_nr_pool_order
, .pool_size_order
= 7, },
123 * TODO: Upon node removal below min_child, if child pool is
124 * filled beyond capacity, we need to roll back to pigeon.
126 { .type_class
= RCU_JA_PIGEON
, .min_child
= 89, .max_child
= ja_type_7_max_child
, .order
= 10, },
128 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
130 #else /* !(CAA_BITS_PER_LONG < 64) */
131 /* 64-bit pointers */
133 ja_type_0_max_child
= 1,
134 ja_type_1_max_child
= 3,
135 ja_type_2_max_child
= 7,
136 ja_type_3_max_child
= 14,
137 ja_type_4_max_child
= 28,
138 ja_type_5_max_child
= 54,
139 ja_type_6_max_child
= 104,
140 ja_type_7_max_child
= 256,
141 ja_type_8_max_child
= 256,
145 ja_type_0_max_linear_child
= 1,
146 ja_type_1_max_linear_child
= 3,
147 ja_type_2_max_linear_child
= 7,
148 ja_type_3_max_linear_child
= 14,
149 ja_type_4_max_linear_child
= 28,
150 ja_type_5_max_linear_child
= 27,
151 ja_type_6_max_linear_child
= 26,
155 ja_type_5_nr_pool_order
= 1,
156 ja_type_6_nr_pool_order
= 2,
159 const struct cds_ja_type ja_types
[] = {
160 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_0_max_child
, .max_linear_child
= ja_type_0_max_linear_child
, .order
= 4, },
161 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_1_max_child
, .max_linear_child
= ja_type_1_max_linear_child
, .order
= 5, },
162 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= ja_type_2_max_child
, .max_linear_child
= ja_type_2_max_linear_child
, .order
= 6, },
163 { .type_class
= RCU_JA_LINEAR
, .min_child
= 5, .max_child
= ja_type_3_max_child
, .max_linear_child
= ja_type_3_max_linear_child
, .order
= 7, },
164 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= ja_type_4_max_child
, .max_linear_child
= ja_type_4_max_linear_child
, .order
= 8, },
166 /* Pools may fill sooner than max_child. */
167 { .type_class
= RCU_JA_POOL
, .min_child
= 22, .max_child
= ja_type_5_max_child
, .max_linear_child
= ja_type_5_max_linear_child
, .order
= 9, .nr_pool_order
= ja_type_5_nr_pool_order
, .pool_size_order
= 8, },
168 { .type_class
= RCU_JA_POOL
, .min_child
= 51, .max_child
= ja_type_6_max_child
, .max_linear_child
= ja_type_6_max_linear_child
, .order
= 10, .nr_pool_order
= ja_type_6_nr_pool_order
, .pool_size_order
= 8, },
171 * TODO: Upon node removal below min_child, if child pool is
172 * filled beyond capacity, we need to roll back to pigeon.
174 { .type_class
= RCU_JA_PIGEON
, .min_child
= 101, .max_child
= ja_type_7_max_child
, .order
= 11, },
176 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
178 #endif /* !(BITS_PER_LONG < 64) */
180 static inline __attribute__((unused
))
181 void static_array_size_check(void)
183 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) < JA_TYPE_MAX_NR
);
187 * The cds_ja_node contains the compressed node data needed for
188 * read-side. For linear and pool node configurations, it starts with a
189 * byte counting the number of children in the node. Then, the
190 * node-specific data is placed.
191 * The node mutex, if any is needed, protecting concurrent updated of
192 * each node is placed in a separate hash table indexed by node address.
193 * For the pigeon configuration, the number of children is also kept in
194 * a separate hash table, indexed by node address, because it is only
195 * required for updates.
198 #define DECLARE_LINEAR_NODE(index) \
201 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
202 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
205 #define DECLARE_POOL_NODE(index) \
209 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
210 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
211 } linear[1U << ja_type_## index ##_nr_pool_order]; \
214 struct cds_ja_inode
{
216 /* Linear configuration */
217 DECLARE_LINEAR_NODE(0) conf_0
;
218 DECLARE_LINEAR_NODE(1) conf_1
;
219 DECLARE_LINEAR_NODE(2) conf_2
;
220 DECLARE_LINEAR_NODE(3) conf_3
;
221 DECLARE_LINEAR_NODE(4) conf_4
;
223 /* Pool configuration */
224 DECLARE_POOL_NODE(5) conf_5
;
225 DECLARE_POOL_NODE(6) conf_6
;
227 /* Pigeon configuration */
229 struct cds_ja_inode_flag
*child
[ja_type_7_max_child
];
231 /* data aliasing nodes for computed accesses */
232 uint8_t data
[sizeof(struct cds_ja_inode_flag
*) * ja_type_7_max_child
];
242 struct cds_ja_inode
*alloc_cds_ja_node(const struct cds_ja_type
*ja_type
)
244 return calloc(1U << ja_type
->order
, sizeof(char));
247 void free_cds_ja_node(struct cds_ja_inode
*node
)
252 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
253 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
254 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
255 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
258 uint8_t *align_ptr_size(uint8_t *ptr
)
260 return (uint8_t *) JA_ALIGN((unsigned long) ptr
, sizeof(void *));
264 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type
*type
,
265 struct cds_ja_inode
*node
)
267 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
268 return rcu_dereference(node
->u
.data
[0]);
272 * The order in which values and pointers are does does not matter: if
273 * a value is missing, we return NULL. If a value is there, but its
274 * associated pointers is still NULL, we return NULL too.
277 struct cds_ja_inode_flag
*ja_linear_node_get_nth(const struct cds_ja_type
*type
,
278 struct cds_ja_inode
*node
,
279 struct cds_ja_inode_flag
***child_node_flag_ptr
,
284 struct cds_ja_inode_flag
**pointers
;
285 struct cds_ja_inode_flag
*ptr
;
288 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
290 nr_child
= ja_linear_node_get_nr_child(type
, node
);
291 cmm_smp_rmb(); /* read nr_child before values and pointers */
292 assert(nr_child
<= type
->max_linear_child
);
293 assert(type
->type_class
!= RCU_JA_LINEAR
|| nr_child
>= type
->min_child
);
295 values
= &node
->u
.data
[1];
296 for (i
= 0; i
< nr_child
; i
++) {
297 if (CMM_LOAD_SHARED(values
[i
]) == n
)
302 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
303 ptr
= rcu_dereference(pointers
[i
]);
304 if (caa_unlikely(child_node_flag_ptr
) && ptr
)
305 *child_node_flag_ptr
= &pointers
[i
];
310 void ja_linear_node_get_ith_pos(const struct cds_ja_type
*type
,
311 struct cds_ja_inode
*node
,
314 struct cds_ja_inode_flag
**iter
)
317 struct cds_ja_inode_flag
**pointers
;
319 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
320 assert(i
< ja_linear_node_get_nr_child(type
, node
));
322 values
= &node
->u
.data
[1];
324 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
329 struct cds_ja_inode_flag
*ja_pool_node_get_nth(const struct cds_ja_type
*type
,
330 struct cds_ja_inode
*node
,
331 struct cds_ja_inode_flag
***child_node_flag_ptr
,
334 struct cds_ja_inode
*linear
;
336 assert(type
->type_class
== RCU_JA_POOL
);
338 * TODO: currently, we select the pool by highest bits. We
339 * should support various encodings.
341 linear
= (struct cds_ja_inode
*)
342 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
343 return ja_linear_node_get_nth(type
, linear
, child_node_flag_ptr
, n
);
347 struct cds_ja_inode
*ja_pool_node_get_ith_pool(const struct cds_ja_type
*type
,
348 struct cds_ja_inode
*node
,
351 assert(type
->type_class
== RCU_JA_POOL
);
352 return (struct cds_ja_inode
*)
353 &node
->u
.data
[(unsigned int) i
<< type
->pool_size_order
];
357 struct cds_ja_inode_flag
*ja_pigeon_node_get_nth(const struct cds_ja_type
*type
,
358 struct cds_ja_inode
*node
,
359 struct cds_ja_inode_flag
***child_node_flag_ptr
,
362 struct cds_ja_inode_flag
**child_node_flag
;
364 assert(type
->type_class
== RCU_JA_PIGEON
);
365 child_node_flag
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
366 dbg_printf("ja_pigeon_node_get_nth child_node_flag_ptr %p\n",
368 if (caa_unlikely(child_node_flag_ptr
) && *child_node_flag
)
369 *child_node_flag_ptr
= child_node_flag
;
370 return rcu_dereference(*child_node_flag
);
374 struct cds_ja_inode_flag
*ja_pigeon_node_get_ith_pos(const struct cds_ja_type
*type
,
375 struct cds_ja_inode
*node
,
378 return ja_pigeon_node_get_nth(type
, node
, NULL
, i
);
382 * ja_node_get_nth: get nth item from a node.
383 * node_flag is already rcu_dereference'd.
386 struct cds_ja_inode_flag
* ja_node_get_nth(struct cds_ja_inode_flag
*node_flag
,
387 struct cds_ja_inode_flag
***child_node_flag_ptr
,
390 unsigned int type_index
;
391 struct cds_ja_inode
*node
;
392 const struct cds_ja_type
*type
;
394 node
= ja_node_ptr(node_flag
);
395 assert(node
!= NULL
);
396 type_index
= ja_node_type(node_flag
);
397 type
= &ja_types
[type_index
];
399 switch (type
->type_class
) {
401 return ja_linear_node_get_nth(type
, node
,
402 child_node_flag_ptr
, n
);
404 return ja_pool_node_get_nth(type
, node
,
405 child_node_flag_ptr
, n
);
407 return ja_pigeon_node_get_nth(type
, node
,
408 child_node_flag_ptr
, n
);
411 return (void *) -1UL;
416 int ja_linear_node_set_nth(const struct cds_ja_type
*type
,
417 struct cds_ja_inode
*node
,
418 struct cds_ja_shadow_node
*shadow_node
,
420 struct cds_ja_inode_flag
*child_node_flag
)
423 uint8_t *values
, *nr_child_ptr
;
424 struct cds_ja_inode_flag
**pointers
;
425 unsigned int i
, unused
= 0;
427 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
429 nr_child_ptr
= &node
->u
.data
[0];
430 dbg_printf("linear set nth: nr_child_ptr %p\n", nr_child_ptr
);
431 nr_child
= *nr_child_ptr
;
432 assert(nr_child
<= type
->max_linear_child
);
434 values
= &node
->u
.data
[1];
435 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
436 /* Check if node value is already populated */
437 for (i
= 0; i
< nr_child
; i
++) {
438 if (values
[i
] == n
) {
448 if (i
== nr_child
&& nr_child
>= type
->max_linear_child
) {
450 return -ERANGE
; /* recompact node */
452 return -ENOSPC
; /* No space left in this node type */
455 assert(pointers
[i
] == NULL
);
456 rcu_assign_pointer(pointers
[i
], child_node_flag
);
457 /* If we expanded the nr_child, increment it */
459 CMM_STORE_SHARED(values
[nr_child
], n
);
460 /* write pointer and value before nr_child */
462 CMM_STORE_SHARED(*nr_child_ptr
, nr_child
+ 1);
464 shadow_node
->nr_child
++;
465 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
466 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr
),
467 (unsigned int) shadow_node
->nr_child
,
474 int ja_pool_node_set_nth(const struct cds_ja_type
*type
,
475 struct cds_ja_inode
*node
,
476 struct cds_ja_shadow_node
*shadow_node
,
478 struct cds_ja_inode_flag
*child_node_flag
)
480 struct cds_ja_inode
*linear
;
482 assert(type
->type_class
== RCU_JA_POOL
);
483 linear
= (struct cds_ja_inode
*)
484 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
485 return ja_linear_node_set_nth(type
, linear
, shadow_node
,
490 int ja_pigeon_node_set_nth(const struct cds_ja_type
*type
,
491 struct cds_ja_inode
*node
,
492 struct cds_ja_shadow_node
*shadow_node
,
494 struct cds_ja_inode_flag
*child_node_flag
)
496 struct cds_ja_inode_flag
**ptr
;
498 assert(type
->type_class
== RCU_JA_PIGEON
);
499 ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
502 rcu_assign_pointer(*ptr
, child_node_flag
);
503 shadow_node
->nr_child
++;
508 * _ja_node_set_nth: set nth item within a node. Return an error
509 * (negative error value) if it is already there.
512 int _ja_node_set_nth(const struct cds_ja_type
*type
,
513 struct cds_ja_inode
*node
,
514 struct cds_ja_shadow_node
*shadow_node
,
516 struct cds_ja_inode_flag
*child_node_flag
)
518 switch (type
->type_class
) {
520 return ja_linear_node_set_nth(type
, node
, shadow_node
, n
,
523 return ja_pool_node_set_nth(type
, node
, shadow_node
, n
,
526 return ja_pigeon_node_set_nth(type
, node
, shadow_node
, n
,
539 int ja_linear_node_clear_ptr(const struct cds_ja_type
*type
,
540 struct cds_ja_inode
*node
,
541 struct cds_ja_shadow_node
*shadow_node
,
542 struct cds_ja_inode_flag
**node_flag_ptr
)
545 uint8_t *nr_child_ptr
;
547 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
549 nr_child_ptr
= &node
->u
.data
[0];
550 dbg_printf("linear clear ptr: nr_child_ptr %p\n", nr_child_ptr
);
551 nr_child
= *nr_child_ptr
;
552 assert(nr_child
<= type
->max_linear_child
);
554 if (shadow_node
->fallback_removal_count
) {
555 shadow_node
->fallback_removal_count
--;
557 if (shadow_node
->nr_child
<= type
->min_child
) {
558 /* We need to try recompacting the node */
562 assert(*node_flag_ptr
!= NULL
);
563 rcu_assign_pointer(*node_flag_ptr
, NULL
);
565 * Value and nr_child are never changed (would cause ABA issue).
566 * Instead, we leave the pointer to NULL and recompact the node
567 * once in a while. It is allowed to set a NULL pointer to a new
568 * value without recompaction though.
569 * Only update the shadow node accounting.
571 shadow_node
->nr_child
--;
572 dbg_printf("linear clear ptr: %u child, shadow: %u child, for node %p shadow %p\n",
573 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr
),
574 (unsigned int) shadow_node
->nr_child
,
581 int ja_pool_node_clear_ptr(const struct cds_ja_type
*type
,
582 struct cds_ja_inode
*node
,
583 struct cds_ja_shadow_node
*shadow_node
,
584 struct cds_ja_inode_flag
**node_flag_ptr
,
587 struct cds_ja_inode
*linear
;
589 assert(type
->type_class
== RCU_JA_POOL
);
590 linear
= (struct cds_ja_inode
*)
591 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
592 return ja_linear_node_clear_ptr(type
, linear
, shadow_node
, node_flag_ptr
);
596 int ja_pigeon_node_clear_ptr(const struct cds_ja_type
*type
,
597 struct cds_ja_inode
*node
,
598 struct cds_ja_shadow_node
*shadow_node
,
599 struct cds_ja_inode_flag
**node_flag_ptr
)
601 assert(type
->type_class
== RCU_JA_PIGEON
);
602 rcu_assign_pointer(*node_flag_ptr
, NULL
);
603 shadow_node
->nr_child
--;
608 * _ja_node_clear_ptr: clear ptr item within a node. Return an error
609 * (negative error value) if it is not found (-ENOENT).
612 int _ja_node_clear_ptr(const struct cds_ja_type
*type
,
613 struct cds_ja_inode
*node
,
614 struct cds_ja_shadow_node
*shadow_node
,
615 struct cds_ja_inode_flag
**node_flag_ptr
,
618 switch (type
->type_class
) {
620 return ja_linear_node_clear_ptr(type
, node
, shadow_node
, node_flag_ptr
);
622 return ja_pool_node_clear_ptr(type
, node
, shadow_node
, node_flag_ptr
, n
);
624 return ja_pigeon_node_clear_ptr(type
, node
, shadow_node
, node_flag_ptr
);
636 * ja_node_recompact_add: recompact a node, adding a new child.
637 * TODO: for pool type, take selection bit(s) into account.
638 * Return 0 on success, -EAGAIN if need to retry, or other negative
639 * error value otherwise.
642 int ja_node_recompact(enum ja_recompact mode
,
644 unsigned int old_type_index
,
645 const struct cds_ja_type
*old_type
,
646 struct cds_ja_inode
*old_node
,
647 struct cds_ja_shadow_node
*shadow_node
,
648 struct cds_ja_inode_flag
**old_node_flag_ptr
, uint8_t n
,
649 struct cds_ja_inode_flag
*child_node_flag
,
650 struct cds_ja_inode_flag
**nullify_node_flag_ptr
)
652 unsigned int new_type_index
;
653 struct cds_ja_inode
*new_node
;
654 struct cds_ja_shadow_node
*new_shadow_node
= NULL
;
655 const struct cds_ja_type
*new_type
;
656 struct cds_ja_inode_flag
*new_node_flag
, *old_node_flag
;
660 old_node_flag
= *old_node_flag_ptr
;
664 new_type_index
= old_type_index
;
666 case JA_RECOMPACT_ADD
:
667 if (!shadow_node
|| old_type_index
== NODE_INDEX_NULL
) {
670 new_type_index
= old_type_index
+ 1;
673 case JA_RECOMPACT_DEL
:
674 if (old_type_index
== 0) {
675 new_type_index
= NODE_INDEX_NULL
;
677 new_type_index
= old_type_index
- 1;
684 retry
: /* for fallback */
685 dbg_printf("Recompact from type %d to type %d\n",
686 old_type_index
, new_type_index
);
687 new_type
= &ja_types
[new_type_index
];
688 if (new_type_index
!= NODE_INDEX_NULL
) {
689 new_node
= alloc_cds_ja_node(new_type
);
692 new_node_flag
= ja_node_flag(new_node
, new_type_index
);
693 dbg_printf("Recompact inherit lock from %p\n", shadow_node
);
694 new_shadow_node
= rcuja_shadow_set(ja
->ht
, new_node_flag
, shadow_node
, ja
);
695 if (!new_shadow_node
) {
700 new_shadow_node
->fallback_removal_count
=
701 JA_FALLBACK_REMOVAL_COUNT
;
704 new_node_flag
= NULL
;
707 assert(mode
!= JA_RECOMPACT_ADD
|| old_type
->type_class
!= RCU_JA_PIGEON
);
709 if (new_type_index
== NODE_INDEX_NULL
)
712 switch (old_type
->type_class
) {
716 ja_linear_node_get_nr_child(old_type
, old_node
);
719 for (i
= 0; i
< nr_child
; i
++) {
720 struct cds_ja_inode_flag
*iter
;
723 ja_linear_node_get_ith_pos(old_type
, old_node
, i
, &v
, &iter
);
726 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
728 ret
= _ja_node_set_nth(new_type
, new_node
,
731 if (new_type
->type_class
== RCU_JA_POOL
&& ret
) {
732 goto fallback_toosmall
;
740 unsigned int pool_nr
;
742 for (pool_nr
= 0; pool_nr
< (1U << old_type
->nr_pool_order
); pool_nr
++) {
743 struct cds_ja_inode
*pool
=
744 ja_pool_node_get_ith_pool(old_type
,
747 ja_linear_node_get_nr_child(old_type
, pool
);
750 for (j
= 0; j
< nr_child
; j
++) {
751 struct cds_ja_inode_flag
*iter
;
754 ja_linear_node_get_ith_pos(old_type
, pool
,
758 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
760 ret
= _ja_node_set_nth(new_type
, new_node
,
763 if (new_type
->type_class
== RCU_JA_POOL
765 goto fallback_toosmall
;
773 assert(mode
== JA_RECOMPACT_ADD
);
780 assert(mode
== JA_RECOMPACT_DEL
);
781 nr_child
= shadow_node
->nr_child
;
782 for (i
= 0; i
< nr_child
; i
++) {
783 struct cds_ja_inode_flag
*iter
;
785 iter
= ja_pigeon_node_get_ith_pos(old_type
, old_node
, i
);
788 if (mode
== JA_RECOMPACT_DEL
&& *nullify_node_flag_ptr
== iter
)
790 ret
= _ja_node_set_nth(new_type
, new_node
,
793 if (new_type
->type_class
== RCU_JA_POOL
&& ret
) {
794 goto fallback_toosmall
;
807 if (JA_RECOMPACT_ADD
) {
809 ret
= _ja_node_set_nth(new_type
, new_node
,
814 /* Return pointer to new recompacted node through old_node_flag_ptr */
815 *old_node_flag_ptr
= new_node_flag
;
819 flags
= RCUJA_SHADOW_CLEAR_FREE_NODE
;
821 * It is OK to free the lock associated with a node
822 * going to NULL, since we are holding the parent lock.
823 * This synchronizes removal with re-add of that node.
825 if (new_type_index
== NODE_INDEX_NULL
)
826 flags
= RCUJA_SHADOW_CLEAR_FREE_LOCK
;
827 ret
= rcuja_shadow_clear(ja
->ht
, old_node_flag
, shadow_node
,
837 /* fallback if next pool is too small */
838 assert(new_shadow_node
);
839 ret
= rcuja_shadow_clear(ja
->ht
, new_node_flag
, new_shadow_node
,
840 RCUJA_SHADOW_CLEAR_FREE_NODE
);
843 /* Choose fallback type: pigeon */
844 new_type_index
= (1UL << JA_TYPE_BITS
) - 1;
845 dbg_printf("Fallback to type %d\n", new_type_index
);
846 uatomic_inc(&ja
->nr_fallback
);
852 * Return 0 on success, -EAGAIN if need to retry, or other negative
853 * error value otherwise.
856 int ja_node_set_nth(struct cds_ja
*ja
,
857 struct cds_ja_inode_flag
**node_flag
, uint8_t n
,
858 struct cds_ja_inode_flag
*child_node_flag
,
859 struct cds_ja_shadow_node
*shadow_node
)
862 unsigned int type_index
;
863 const struct cds_ja_type
*type
;
864 struct cds_ja_inode
*node
;
866 dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n",
867 (unsigned int) n
, ja_node_ptr(*node_flag
), shadow_node
);
869 node
= ja_node_ptr(*node_flag
);
870 type_index
= ja_node_type(*node_flag
);
871 type
= &ja_types
[type_index
];
872 ret
= _ja_node_set_nth(type
, node
, shadow_node
,
876 /* Not enough space in node, need to recompact. */
877 ret
= ja_node_recompact(JA_RECOMPACT_ADD
, ja
, type_index
, type
, node
,
878 shadow_node
, node_flag
, n
, child_node_flag
, NULL
);
881 /* Node needs to be recompacted. */
882 ret
= ja_node_recompact(JA_RECOMPACT
, ja
, type_index
, type
, node
,
883 shadow_node
, node_flag
, n
, child_node_flag
, NULL
);
890 * Return 0 on success, -EAGAIN if need to retry, or other negative
891 * error value otherwise.
894 int ja_node_clear_ptr(struct cds_ja
*ja
,
895 struct cds_ja_inode_flag
**node_flag_ptr
, /* Pointer to location to nullify */
896 struct cds_ja_inode_flag
**parent_node_flag_ptr
, /* Address of parent ptr in its parent */
897 struct cds_ja_shadow_node
*shadow_node
, /* of parent */
901 unsigned int type_index
;
902 const struct cds_ja_type
*type
;
903 struct cds_ja_inode
*node
;
905 dbg_printf("ja_node_clear_ptr for node %p, shadow %p, target ptr %p\n",
906 ja_node_ptr(*parent_node_flag_ptr
), shadow_node
, node_flag_ptr
);
908 node
= ja_node_ptr(*parent_node_flag_ptr
);
909 type_index
= ja_node_type(*parent_node_flag_ptr
);
910 type
= &ja_types
[type_index
];
911 ret
= _ja_node_clear_ptr(type
, node
, shadow_node
, node_flag_ptr
, n
);
913 /* Should to try recompaction. */
914 ret
= ja_node_recompact(JA_RECOMPACT_DEL
, ja
, type_index
, type
, node
,
915 shadow_node
, parent_node_flag_ptr
, n
, NULL
,
921 struct cds_hlist_head
cds_ja_lookup(struct cds_ja
*ja
, uint64_t key
)
923 unsigned int tree_depth
, i
;
924 struct cds_ja_inode_flag
*node_flag
;
925 struct cds_hlist_head head
= { NULL
};
927 if (caa_unlikely(key
> ja
->key_max
))
929 tree_depth
= ja
->tree_depth
;
930 node_flag
= rcu_dereference(ja
->root
);
932 /* level 0: root node */
933 if (!ja_node_ptr(node_flag
))
936 for (i
= 1; i
< tree_depth
; i
++) {
939 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
940 node_flag
= ja_node_get_nth(node_flag
, NULL
,
942 dbg_printf("cds_ja_lookup iter key lookup %u finds node_flag %p\n",
943 (unsigned int) iter_key
, node_flag
);
944 if (!ja_node_ptr(node_flag
))
948 /* Last level lookup succeded. We got an actual match. */
949 head
.next
= (struct cds_hlist_node
*) node_flag
;
954 * We reached an unpopulated node. Create it and the children we need,
955 * and then attach the entire branch to the current node. This may
956 * trigger recompaction of the current node. Locks needed: node lock
957 * (for add), and, possibly, parent node lock (to update pointer due to
958 * node recompaction).
960 * First take node lock, check if recompaction is needed, then take
961 * parent lock (if needed). Then we can proceed to create the new
962 * branch. Publish the new branch, and release locks.
963 * TODO: we currently always take the parent lock even when not needed.
966 int ja_attach_node(struct cds_ja
*ja
,
967 struct cds_ja_inode_flag
**node_flag_ptr
,
968 struct cds_ja_inode_flag
*node_flag
,
969 struct cds_ja_inode_flag
*parent_node_flag
,
972 struct cds_ja_node
*child_node
)
974 struct cds_ja_shadow_node
*shadow_node
= NULL
,
975 *parent_shadow_node
= NULL
;
976 struct cds_ja_inode
*node
= ja_node_ptr(node_flag
);
977 struct cds_ja_inode
*parent_node
= ja_node_ptr(parent_node_flag
);
978 struct cds_hlist_head head
;
979 struct cds_ja_inode_flag
*iter_node_flag
, *iter_dest_node_flag
;
981 struct cds_ja_inode_flag
*created_nodes
[JA_MAX_DEPTH
];
982 int nr_created_nodes
= 0;
984 dbg_printf("Attach node at level %u (node %p, node_flag %p)\n",
985 level
, node
, node_flag
);
988 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, node_flag
);
994 parent_shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
996 if (!parent_shadow_node
) {
1002 /* Create new branch, starting from bottom */
1003 CDS_INIT_HLIST_HEAD(&head
);
1004 cds_hlist_add_head_rcu(&child_node
->list
, &head
);
1005 iter_node_flag
= (struct cds_ja_inode_flag
*) head
.next
;
1007 for (i
= ja
->tree_depth
; i
> (int) level
; i
--) {
1010 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- i
)));
1011 dbg_printf("branch creation level %d, key %u\n",
1012 i
- 1, (unsigned int) iter_key
);
1013 iter_dest_node_flag
= NULL
;
1014 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
1020 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
1021 iter_node_flag
= iter_dest_node_flag
;
1027 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- level
)));
1028 /* We need to use set_nth on the previous level. */
1029 iter_dest_node_flag
= node_flag
;
1030 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
1036 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
1037 iter_node_flag
= iter_dest_node_flag
;
1040 /* Publish new branch */
1041 dbg_printf("Publish branch %p, replacing %p\n",
1042 iter_node_flag
, *node_flag_ptr
);
1043 rcu_assign_pointer(*node_flag_ptr
, iter_node_flag
);
1050 for (i
= 0; i
< nr_created_nodes
; i
++) {
1054 flags
= RCUJA_SHADOW_CLEAR_FREE_LOCK
;
1056 flags
|= RCUJA_SHADOW_CLEAR_FREE_NODE
;
1057 tmpret
= rcuja_shadow_clear(ja
->ht
,
1064 if (parent_shadow_node
)
1065 rcuja_shadow_unlock(parent_shadow_node
);
1068 rcuja_shadow_unlock(shadow_node
);
1074 * Lock the parent containing the hlist head pointer, and add node to list of
1075 * duplicates. Failure can happen if concurrent update changes the
1076 * parent before we get the lock. We return -EAGAIN in that case.
1077 * Return 0 on success, negative error value on failure.
1080 int ja_chain_node(struct cds_ja
*ja
,
1081 struct cds_ja_inode_flag
*parent_node_flag
,
1082 struct cds_hlist_head
*head
,
1083 struct cds_ja_node
*node
)
1085 struct cds_ja_shadow_node
*shadow_node
;
1087 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, parent_node_flag
);
1090 cds_hlist_add_head_rcu(&node
->list
, head
);
1091 rcuja_shadow_unlock(shadow_node
);
1095 int cds_ja_add(struct cds_ja
*ja
, uint64_t key
,
1096 struct cds_ja_node
*new_node
)
1098 unsigned int tree_depth
, i
;
1099 struct cds_ja_inode_flag
**node_flag_ptr
; /* in parent */
1100 struct cds_ja_inode_flag
*node_flag
,
1105 if (caa_unlikely(key
> ja
->key_max
))
1107 tree_depth
= ja
->tree_depth
;
1110 dbg_printf("cds_ja_add attempt: key %" PRIu64
", node %p\n",
1112 parent2_node_flag
= NULL
;
1114 (struct cds_ja_inode_flag
*) &ja
->root
; /* Use root ptr address as key for mutex */
1115 node_flag_ptr
= &ja
->root
;
1116 node_flag
= rcu_dereference(ja
->root
);
1118 /* Iterate on all internal levels */
1119 for (i
= 1; i
< tree_depth
; i
++) {
1122 dbg_printf("cds_ja_add iter node_flag_ptr %p node_flag %p\n",
1123 *node_flag_ptr
, node_flag
);
1124 if (!ja_node_ptr(node_flag
)) {
1125 ret
= ja_attach_node(ja
, node_flag_ptr
,
1126 parent_node_flag
, parent2_node_flag
,
1128 if (ret
== -EAGAIN
|| ret
== -EEXIST
)
1133 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
1134 parent2_node_flag
= parent_node_flag
;
1135 parent_node_flag
= node_flag
;
1136 node_flag
= ja_node_get_nth(node_flag
,
1139 dbg_printf("cds_ja_add iter key lookup %u finds node_flag %p node_flag_ptr %p\n",
1140 (unsigned int) iter_key
, node_flag
, *node_flag_ptr
);
1144 * We reached bottom of tree, simply add node to last internal
1145 * level, or chain it if key is already present.
1147 if (!ja_node_ptr(node_flag
)) {
1148 dbg_printf("cds_ja_add last node_flag_ptr %p node_flag %p\n",
1149 *node_flag_ptr
, node_flag
);
1150 ret
= ja_attach_node(ja
, node_flag_ptr
, parent_node_flag
,
1151 parent2_node_flag
, key
, i
, new_node
);
1153 ret
= ja_chain_node(ja
,
1155 (struct cds_hlist_head
*) node_flag_ptr
,
1165 * Note: there is no need to lookup the pointer address associated with
1166 * each node's nth item after taking the lock: it's already been done by
1167 * cds_ja_del while holding the rcu read-side lock, and our node rules
1168 * ensure that when a match value -> pointer is found in a node, it is
1169 * _NEVER_ changed for that node without recompaction, and recompaction
1170 * reallocates the node.
1173 int ja_detach_node(struct cds_ja
*ja
,
1174 struct cds_ja_inode_flag
**snapshot
,
1175 struct cds_ja_inode_flag
***snapshot_ptr
,
1176 uint8_t *snapshot_n
,
1179 struct cds_ja_node
*node
)
1181 struct cds_ja_shadow_node
*shadow_nodes
[JA_MAX_DEPTH
];
1182 struct cds_ja_inode_flag
**node_flag_ptr
= NULL
,
1183 *parent_node_flag
= NULL
,
1184 **parent_node_flag_ptr
= NULL
;
1185 struct cds_ja_inode_flag
*iter_node_flag
;
1186 int ret
, i
, nr_shadow
= 0, nr_clear
= 0;
1189 assert(nr_snapshot
== ja
->tree_depth
- 1);
1192 * From the last internal level node going up, get the node
1193 * lock, check if the node has only one child left. If it is the
1194 * case, we continue iterating upward. When we reach a node
1195 * which has more that one child left, we lock the parent, and
1196 * proceed to the node deletion (removing its children too).
1198 for (i
= nr_snapshot
- 1; i
>= 1; i
--) {
1199 struct cds_ja_shadow_node
*shadow_node
;
1201 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
1207 assert(shadow_node
->nr_child
> 0);
1208 shadow_nodes
[nr_shadow
++] = shadow_node
;
1210 if (i
== nr_snapshot
- 1) {
1212 * Re-check that last internal node level has
1213 * only one child, else trigger a retry.
1215 if (shadow_node
->nr_child
!= 1) {
1220 if (shadow_node
->nr_child
> 1 || i
== 1) {
1221 /* Lock parent and break */
1222 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
1228 shadow_nodes
[nr_shadow
++] = shadow_node
;
1229 node_flag_ptr
= snapshot_ptr
[i
];
1231 parent_node_flag_ptr
= snapshot_ptr
[i
- 1];
1232 parent_node_flag
= snapshot
[i
- 1];
1235 * Lock parent's parent, in case we need
1236 * to recompact parent.
1238 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
1244 shadow_nodes
[nr_shadow
++] = shadow_node
;
1251 * At this point, we want to delete all nodes in shadow_nodes
1252 * (except the last one, which is either the root or the parent
1253 * of the upmost node with 1 child). OK to as to free lock here,
1254 * because RCU read lock is held, and free only performed in
1258 for (i
= 0; i
< nr_clear
; i
++) {
1259 ret
= rcuja_shadow_clear(ja
->ht
,
1260 shadow_nodes
[i
]->node_flag
,
1262 RCUJA_SHADOW_CLEAR_FREE_NODE
1263 | RCUJA_SHADOW_CLEAR_FREE_LOCK
);
1267 iter_node_flag
= parent_node_flag
;
1268 /* Remove from parent */
1269 ret
= ja_node_clear_ptr(ja
,
1270 node_flag_ptr
, /* Pointer to location to nullify */
1271 &iter_node_flag
, /* Old new parent ptr in its parent */
1272 shadow_nodes
[nr_clear
], /* of parent */
1275 /* Update address of parent ptr in its parent */
1276 rcu_assign_pointer(*parent_node_flag_ptr
, iter_node_flag
);
1279 for (i
= 0; i
< nr_shadow
; i
++)
1280 rcuja_shadow_unlock(shadow_nodes
[i
]);
1285 int ja_unchain_node(struct cds_ja
*ja
,
1286 struct cds_ja_inode_flag
*parent_node_flag
,
1287 struct cds_ja_node
*node
)
1289 struct cds_ja_shadow_node
*shadow_node
;
1292 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, parent_node_flag
);
1296 * Retry if another thread removed all but one of duplicates
1299 if (shadow_node
->nr_child
== 1) {
1303 cds_hlist_del_rcu(&node
->list
);
1305 rcuja_shadow_unlock(shadow_node
);
1310 * Called with RCU read lock held.
1312 int cds_ja_del(struct cds_ja
*ja
, uint64_t key
,
1313 struct cds_ja_node
*node
)
1315 unsigned int tree_depth
, i
;
1316 struct cds_ja_inode_flag
*snapshot
[JA_MAX_DEPTH
];
1317 struct cds_ja_inode_flag
**snapshot_ptr
[JA_MAX_DEPTH
];
1318 uint8_t snapshot_n
[JA_MAX_DEPTH
];
1319 struct cds_ja_inode_flag
*node_flag
;
1320 struct cds_ja_inode_flag
**prev_node_flag_ptr
;
1321 int nr_snapshot
= 0;
1324 if (caa_unlikely(key
> ja
->key_max
))
1326 tree_depth
= ja
->tree_depth
;
1329 dbg_printf("cds_ja_del attempt: key %" PRIu64
", node %p\n",
1332 /* snapshot for level 0 is only for shadow node lookup */
1333 snapshot_n
[nr_snapshot
] = 0;
1334 snapshot_ptr
[nr_snapshot
] = NULL
;
1335 snapshot
[nr_snapshot
++] = (struct cds_ja_inode_flag
*) &ja
->root
;
1336 node_flag
= rcu_dereference(ja
->root
);
1337 prev_node_flag_ptr
= &ja
->root
;
1339 /* Iterate on all internal levels */
1340 for (i
= 1; i
< tree_depth
; i
++) {
1343 dbg_printf("cds_ja_del iter node_flag %p\n",
1345 if (!ja_node_ptr(node_flag
)) {
1348 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
1349 if (nr_snapshot
<= 1)
1350 snapshot_n
[nr_snapshot
] = 0;
1352 snapshot_n
[nr_snapshot
- 1] = iter_key
;
1354 snapshot_ptr
[nr_snapshot
] = prev_node_flag_ptr
;
1355 snapshot
[nr_snapshot
++] = node_flag
;
1356 node_flag
= ja_node_get_nth(node_flag
,
1357 &prev_node_flag_ptr
,
1359 dbg_printf("cds_ja_del iter key lookup %u finds node_flag %p, prev_node_flag_ptr %p\n",
1360 (unsigned int) iter_key
, node_flag
,
1361 prev_node_flag_ptr
);
1365 * We reached bottom of tree, try to find the node we are trying
1366 * to remove. Fail if we cannot find it.
1368 if (!ja_node_ptr(node_flag
)) {
1371 struct cds_hlist_head
*hlist_head
;
1372 struct cds_hlist_node
*hlist_node
;
1373 struct cds_ja_node
*entry
, *match
= NULL
;
1376 hlist_head
= (struct cds_hlist_head
*) ja_node_ptr(node_flag
);
1377 cds_hlist_for_each_entry_rcu(entry
,
1390 * Removing last of duplicates.
1392 snapshot_ptr
[nr_snapshot
] = prev_node_flag_ptr
;
1393 snapshot
[nr_snapshot
++] = node_flag
;
1394 ret
= ja_detach_node(ja
, snapshot
, snapshot_ptr
,
1395 snapshot_n
, nr_snapshot
, key
, node
);
1397 ret
= ja_unchain_node(ja
, node_flag
, entry
);
1405 struct cds_ja
*_cds_ja_new(unsigned int key_bits
,
1406 const struct rcu_flavor_struct
*flavor
)
1410 struct cds_ja_shadow_node
*root_shadow_node
;
1412 ja
= calloc(sizeof(*ja
), 1);
1418 ja
->key_max
= UINT8_MAX
;
1421 ja
->key_max
= UINT16_MAX
;
1424 ja
->key_max
= UINT32_MAX
;
1427 ja
->key_max
= UINT64_MAX
;
1433 /* ja->root is NULL */
1434 /* tree_depth 0 is for pointer to root node */
1435 ja
->tree_depth
= (key_bits
>> JA_LOG2_BITS_PER_BYTE
) + 1;
1436 assert(ja
->tree_depth
<= JA_MAX_DEPTH
);
1437 ja
->ht
= rcuja_create_ht(flavor
);
1442 * Note: we should not free this node until judy array destroy.
1444 root_shadow_node
= rcuja_shadow_set(ja
->ht
,
1445 (struct cds_ja_inode_flag
*) &ja
->root
,
1447 if (!root_shadow_node
) {
1451 root_shadow_node
->level
= 0;
1456 ret
= rcuja_delete_ht(ja
->ht
);
1466 * Called from RCU read-side CS.
1468 __attribute__((visibility("protected")))
1469 void rcuja_free_all_children(struct cds_ja_shadow_node
*shadow_node
,
1470 struct cds_ja_inode_flag
*node_flag
,
1471 void (*free_node_cb
)(struct rcu_head
*head
))
1473 const struct rcu_flavor_struct
*flavor
;
1474 unsigned int type_index
;
1475 struct cds_ja_inode
*node
;
1476 const struct cds_ja_type
*type
;
1478 flavor
= cds_lfht_rcu_flavor(shadow_node
->ja
->ht
);
1479 node
= ja_node_ptr(node_flag
);
1480 assert(node
!= NULL
);
1481 type_index
= ja_node_type(node_flag
);
1482 type
= &ja_types
[type_index
];
1484 switch (type
->type_class
) {
1488 ja_linear_node_get_nr_child(type
, node
);
1491 for (i
= 0; i
< nr_child
; i
++) {
1492 struct cds_ja_inode_flag
*iter
;
1493 struct cds_hlist_head head
;
1494 struct cds_ja_node
*entry
;
1495 struct cds_hlist_node
*pos
;
1498 ja_linear_node_get_ith_pos(type
, node
, i
, &v
, &iter
);
1501 head
.next
= (struct cds_hlist_node
*) iter
;
1502 cds_hlist_for_each_entry_rcu(entry
, pos
, &head
, list
) {
1503 flavor
->update_call_rcu(&entry
->head
, free_node_cb
);
1510 unsigned int pool_nr
;
1512 for (pool_nr
= 0; pool_nr
< (1U << type
->nr_pool_order
); pool_nr
++) {
1513 struct cds_ja_inode
*pool
=
1514 ja_pool_node_get_ith_pool(type
, node
, pool_nr
);
1516 ja_linear_node_get_nr_child(type
, pool
);
1519 for (j
= 0; j
< nr_child
; j
++) {
1520 struct cds_ja_inode_flag
*iter
;
1521 struct cds_hlist_head head
;
1522 struct cds_ja_node
*entry
;
1523 struct cds_hlist_node
*pos
;
1526 ja_linear_node_get_ith_pos(type
, node
, j
, &v
, &iter
);
1529 head
.next
= (struct cds_hlist_node
*) iter
;
1530 cds_hlist_for_each_entry_rcu(entry
, pos
, &head
, list
) {
1531 flavor
->update_call_rcu(&entry
->head
, free_node_cb
);
1544 nr_child
= shadow_node
->nr_child
;
1545 for (i
= 0; i
< nr_child
; i
++) {
1546 struct cds_ja_inode_flag
*iter
;
1547 struct cds_hlist_head head
;
1548 struct cds_ja_node
*entry
;
1549 struct cds_hlist_node
*pos
;
1551 iter
= ja_pigeon_node_get_ith_pos(type
, node
, i
);
1554 head
.next
= (struct cds_hlist_node
*) iter
;
1555 cds_hlist_for_each_entry_rcu(entry
, pos
, &head
, list
) {
1556 flavor
->update_call_rcu(&entry
->head
, free_node_cb
);
1567 * There should be no more concurrent add to the judy array while it is
1568 * being destroyed (ensured by the caller).
1570 int cds_ja_destroy(struct cds_ja
*ja
,
1571 void (*free_node_cb
)(struct rcu_head
*head
))
1575 rcuja_shadow_prune(ja
->ht
,
1576 RCUJA_SHADOW_CLEAR_FREE_NODE
| RCUJA_SHADOW_CLEAR_FREE_LOCK
,
1578 ret
= rcuja_delete_ht(ja
->ht
);
1581 if (uatomic_read(&ja
->nr_fallback
))
1583 "[warning] RCU Judy Array used %lu fallback node(s)\n",
1584 uatomic_read(&ja
->nr_fallback
));