4 * Userspace RCU library - Red-Black Tree
6 * Copyright (c) 2010 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
22 * Implementation of RCU-adapted data structures and operations based on the RB
23 * tree algorithms found in chapter 12 of:
25 * Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and
26 * Clifford Stein. Introduction to Algorithms, Third Edition. The MIT
27 * Press, September 2009.
40 #include <urcu/rcurbtree.h>
41 #include <urcu-pointer.h>
42 #include <urcu-call-rcu.h>
43 #include <urcu/compiler.h>
46 * Explanation of next/prev walk coherency and search coherency when
47 * performed concurrently with updates.
49 * next/prev walk coherency with respect to concurrent updates:
51 * There are 3 scenarios for which we need to model and validate this:
52 * rotation, transplant and "teleportation" (the latter being a remote
53 * transplant in a remove non-nil case).
55 * - rotation left (right is symmetric)
56 * xl and yr point to the same parent nodes before/after left
57 * rotation. yll and ylr also point to the same parent node
58 * before/after left rotation.
59 * As we are copying x, y and yl (the 3 nodes which parent/child
60 * relationship are changed) to "new" version of this node cluster,
61 * all external references to the cluster either point to the old
62 * cluster or the new one. If we take this cluster as a "black box"
63 * from the point of view of next/prev traversal, all we have to
64 * ensure is that the old and the new cluster behave in the exact same
65 * way with respect to traversal order.
68 * In this operation, we transplant a copy of "v" into its parent
69 * location (u), thus replacing it. The children of "v", vl and vr,
70 * still point to v (new version) after the transplant, so it does not
71 * change the behavior when considering the next/prev traversal. "v"
72 * being copied to a new version ensures that the parent pointers of v
73 * are pointing to its new parent (parent of u) before it is published
74 * to readers (by setting the child pointer of u's parent to the new
78 * This one is probably the most tricky and will require some ascii
81 * We want to remove z from this tree:
99 * What we are going to do is to "teleport" y into z's location,
100 * reparenting yr to b. We are taking care to create a new cluster
101 * copy that is isolated from any reader. We will represent the new
102 * members of the cluster with capital letters.
118 * In this transient state, we notice that the pointers within the
119 * cluster all point to the new cluster nodes, and they point to the
120 * correct external nodes. However, no external pointer point to the
121 * cluster (yet). The first pointer to point to this cluster will be
122 * "zp->right". It will therefore make the cluster visible for search.
124 * In this intermediate state, we can walk through the new cluster
125 * when coming from the top (in a next/prev traversal), but can come
126 * back to the old cluster when going back up from the children nodes.
127 * All we have to ensure is that the two clusters, taken as a black
128 * box from a next/prev traversal perspective, yield to the exact same
131 * Search coherency with concurrent updates:
133 * Simple "search" (only going down the tree) is also handled by this
134 * cluster scheme. The explanation is a subset of the prev/next
135 * explanation, where we don't have to care about the intermediate
136 * stages where the children point to the old cluster, because we only
137 * ever use the top level pointers to go down into the children nodes,
138 * we never go back up. So by simply making sure that all the cluster
139 * internal nodes pointers are setup correctly before making the
140 * cluster visible to the readers (by setting the parent pointer to
141 * the topmost new node in the cluster), we are sure that readers will
142 * see a coherent view of the cluster at all times.
146 #define dbg_printf(args...) printf(args)
147 #define dbg_usleep(usecs) usleep(usecs)
149 #define dbg_printf(args...)
150 #define dbg_usleep(usecs)
154 * Undefine this to enable the non-RCU rotate and transplant functions
155 * (for debugging). Note that these versions don't support the tree
156 * max_end updates, so lookups must be performed with
157 * rcu_rbtree_search_begin_key when using this debug facility.
159 #define RBTREE_RCU_SUPPORT_ROTATE_LEFT
160 #define RBTREE_RCU_SUPPORT_ROTATE_RIGHT
161 #define RBTREE_RCU_SUPPORT_TRANSPLANT
162 #define RBTREE_RCU_SUPPORT
164 #ifdef RBTREE_RCU_SUPPORT
165 #define c_rcu_dereference(x) rcu_dereference(x)
167 #define c_rcu_dereference(x) (x)
171 * Add internal mutex locking within the RBTree, for debugging. Enable this
172 * define and add mutexes to RCU readers to debug races with with rotation or
175 /* #define RBTREE_INTERNAL_LOCKING */
177 #ifdef RBTREE_INTERNAL_LOCKING
178 static pthread_mutex_t test_mutex
= PTHREAD_MUTEX_INITIALIZER
;
179 static pthread_mutex_t outer_mutex
= PTHREAD_MUTEX_INITIALIZER
;
182 void lock_outer_mutex(void)
184 pthread_mutex_lock(&outer_mutex
);
188 void unlock_outer_mutex(void)
190 pthread_mutex_unlock(&outer_mutex
);
194 void lock_test_mutex(void)
196 pthread_mutex_lock(&test_mutex
);
200 void unlock_test_mutex(void)
202 pthread_mutex_unlock(&test_mutex
);
206 void lock_outer_mutex(void)
211 void unlock_outer_mutex(void)
216 void lock_test_mutex(void)
221 void unlock_test_mutex(void)
227 void set_parent(struct rcu_rbtree_node
*node
,
228 struct rcu_rbtree_node
*parent
,
231 _CMM_STORE_SHARED(node
->parent
, ((unsigned long) parent
) | pos
);
235 struct rcu_rbtree_node
*get_parent(struct rcu_rbtree_node
*node
)
237 return (struct rcu_rbtree_node
*) (node
->parent
& ~1UL);
241 unsigned int get_pos(struct rcu_rbtree_node
*node
)
243 return (unsigned int) (node
->parent
& 1UL);
247 struct rcu_rbtree_node
*get_parent_and_pos(struct rcu_rbtree_node
*node
,
250 unsigned long parent_pos
= c_rcu_dereference(node
->parent
);
252 *pos
= (unsigned int) (parent_pos
& 1UL);
253 return (struct rcu_rbtree_node
*) (parent_pos
& ~1UL);
257 void set_decay(struct rcu_rbtree_node
*x
, struct rcu_rbtree_node
*xc
)
263 struct rcu_rbtree_node
*get_decay(struct rcu_rbtree_node
*x
)
267 while (x
->decay_next
)
273 struct rcu_rbtree_node
*is_decay(struct rcu_rbtree_node
*x
)
275 return x
->decay_next
;
279 struct rcu_rbtree_node
*_rcu_rbtree_alloc_node(struct rcu_rbtree
*rbtree
)
281 return rbtree
->rballoc(sizeof(struct rcu_rbtree_node
));
285 void _rcu_rbtree_free_node(struct rcu_head
*head
)
287 struct rcu_rbtree_node
*node
=
288 caa_container_of(head
, struct rcu_rbtree_node
, head
);
289 node
->rbtree
->rbfree(node
);
292 #ifdef RBTREE_RCU_SUPPORT
295 struct rcu_rbtree_node
*dup_decay_node(struct rcu_rbtree
*rbtree
,
296 struct rcu_rbtree_node
*x
)
298 struct rcu_rbtree_node
*xc
;
300 if (rcu_rbtree_is_nil(rbtree
, x
))
303 xc
= _rcu_rbtree_alloc_node(rbtree
);
304 memcpy(xc
, x
, sizeof(*xc
));
305 xc
->decay_next
= NULL
;
307 rbtree
->call_rcu(&x
->head
, _rcu_rbtree_free_node
);
311 #else /* RBTREE_RCU_SUPPORT */
314 struct rcu_rbtree_node
*dup_decay_node(struct rcu_rbtree
*rbtree
,
315 struct rcu_rbtree_node
*x
)
323 * Info for range lookups:
324 * Range lookup information is only valid when used when searching for
325 * ranges. It should never be used in next/prev traversal because the
326 * pointers to parents are not in sync with the parent vision of the
330 void set_left(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
,
331 struct rcu_rbtree_node
*left
)
337 void set_right(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
,
338 struct rcu_rbtree_node
*right
)
340 node
->_right
= right
;
344 void *calculate_node_max_end(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
)
349 if (!rcu_rbtree_is_nil(rbtree
, node
->_right
)) {
350 if (rbtree
->comp(max_end
, node
->_right
->max_end
) < 0)
351 max_end
= node
->_right
->max_end
;
353 if (!rcu_rbtree_is_nil(rbtree
, node
->_left
)) {
354 if (rbtree
->comp(max_end
, node
->_left
->max_end
) < 0)
355 max_end
= node
->_left
->max_end
;
362 * Deal with memory allocation errors.
363 * Can be ensured by reserving a pool of memory entries before doing the
364 * insertion, which will have to be function of number of
365 * transplantations/rotations required for the operation (which is a
366 * multiple of the tree height).
371 void show_tree(struct rcu_rbtree
*rbtree
)
373 struct rcu_rbtree_node
*node
;
375 node
= rcu_rbtree_min(rbtree
, rbtree
->root
);
376 while (!rcu_rbtree_is_nil(rbtree
, node
)) {
377 assert(!is_decay(node
));
378 printf("{ b:%lX e:%lX pb: %lX r:%lX l:%lX %s %s %s} ",
379 (unsigned long) node
->begin
,
380 (unsigned long) node
->end
,
381 (unsigned long) get_parent(node
)->begin
,
382 (unsigned long) node
->_right
->begin
,
383 (unsigned long) node
->_left
->begin
,
384 node
->color
? "red" : "black",
385 get_pos(node
) ? "right" : "left",
386 rcu_rbtree_is_nil(rbtree
, node
) ? "nil" : "");
387 node
= rcu_rbtree_next(rbtree
, node
);
392 #define check_max_end(rbtree, x) \
394 if (rcu_rbtree_is_nil(rbtree, x)) \
396 assert(rbtree->comp(x->max_end, \
397 calculate_node_max_end(rbtree, x)) == 0); \
402 void show_tree(struct rcu_rbtree
*rbtree
)
407 void check_max_end(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*x
)
413 struct rcu_rbtree_node
*make_nil(struct rcu_rbtree
*rbtree
)
415 return &rbtree
->nil_node
;
419 * Iterative rbtree search.
421 struct rcu_rbtree_node
*rcu_rbtree_search(struct rcu_rbtree
*rbtree
,
422 struct rcu_rbtree_node
*x
,
425 struct rcu_rbtree_node
*xl
;
427 dbg_printf("searching point 0x%lx\n", (unsigned long) point
);
428 x
= c_rcu_dereference(x
);
430 while (!rcu_rbtree_is_nil(rbtree
, x
)) {
432 xl
= c_rcu_dereference(x
->_left
);
433 dbg_printf("search x %lx x_end %lx x_max_end %lx\n", (unsigned long) x
->begin
,
434 (unsigned long) x
->end
, (unsigned long) x
->max_end
);
435 dbg_printf("search xl %lx xl_end %lx xl_max_end %lx\n", (unsigned long) xl
->begin
,
436 (unsigned long) xl
->end
, (unsigned long) xl
->max_end
);
437 if (!rcu_rbtree_is_nil(rbtree
, xl
)
438 && (rbtree
->comp(xl
->max_end
, point
) > 0)) {
439 dbg_printf("go left\n");
441 } else if (rbtree
->comp(x
->begin
, point
) <= 0
442 && rbtree
->comp(point
, x
->end
) < 0) {
443 dbg_printf("got it!\n");
445 } else if (rbtree
->comp(point
, x
->begin
) > 0) {
446 dbg_printf("go right\n");
447 x
= c_rcu_dereference(x
->_right
);
449 dbg_printf("not found!\n");
450 x
= make_nil(rbtree
);
453 if (rcu_rbtree_is_nil(rbtree
, x
))
454 dbg_printf("Reached bottom of tree.\n");
459 struct rcu_rbtree_node
*rcu_rbtree_search_range(struct rcu_rbtree
*rbtree
,
460 struct rcu_rbtree_node
*x
,
461 void *begin
, void *end
)
463 struct rcu_rbtree_node
*node
;
465 node
= rcu_rbtree_search(rbtree
, x
, begin
);
466 if (rcu_rbtree_is_nil(rbtree
, node
))
468 if (rbtree
->comp(node
->end
, end
) < 0)
469 return NULL
; /* High is outside lookup range */
474 * Search by exact range start value.
476 struct rcu_rbtree_node
*rcu_rbtree_search_begin_key(struct rcu_rbtree
*rbtree
,
477 struct rcu_rbtree_node
*x
,
480 x
= c_rcu_dereference(x
);
483 while (!rcu_rbtree_is_nil(rbtree
, x
) && (comp
= rbtree
->comp(k
, x
->begin
)) != 0) {
486 x
= c_rcu_dereference(x
->_left
);
488 x
= c_rcu_dereference(x
->_right
);
494 struct rcu_rbtree_node
*rcu_rbtree_min_dup_decay(struct rcu_rbtree
*rbtree
,
495 struct rcu_rbtree_node
*x
,
496 struct rcu_rbtree_node
**zr
)
498 struct rcu_rbtree_node
*xl
;
500 x
= c_rcu_dereference(x
);
502 if (rcu_rbtree_is_nil(rbtree
, x
)) {
506 *zr
= x
= dup_decay_node(rbtree
, x
);
508 while (!rcu_rbtree_is_nil(rbtree
, xl
= c_rcu_dereference(x
->_left
))) {
509 x
= dup_decay_node(rbtree
, xl
);
510 set_parent(x
, get_decay(get_parent(x
)), get_pos(x
));
511 get_parent(x
)->_left
= get_decay(get_parent(x
)->_left
);
517 struct rcu_rbtree_node
*rcu_rbtree_min_update_decay(struct rcu_rbtree
*rbtree
,
518 struct rcu_rbtree_node
*x
)
520 struct rcu_rbtree_node
*xl
;
522 x
= c_rcu_dereference(x
);
524 if (rcu_rbtree_is_nil(rbtree
, x
))
527 set_parent(x
->_right
, get_decay(get_parent(x
->_right
)),
529 set_parent(x
->_left
, get_decay(get_parent(x
->_left
)),
533 while (!rcu_rbtree_is_nil(rbtree
, xl
= c_rcu_dereference(x
->_left
))) {
535 set_parent(x
->_right
, get_decay(get_parent(x
->_right
)),
537 set_parent(x
->_left
, get_decay(get_parent(x
->_left
)),
543 struct rcu_rbtree_node
*rcu_rbtree_min(struct rcu_rbtree
*rbtree
,
544 struct rcu_rbtree_node
*x
)
546 struct rcu_rbtree_node
*xl
;
548 x
= c_rcu_dereference(x
);
550 if (rcu_rbtree_is_nil(rbtree
, x
))
553 while (!rcu_rbtree_is_nil(rbtree
, xl
= c_rcu_dereference(x
->_left
)))
558 struct rcu_rbtree_node
*rcu_rbtree_max(struct rcu_rbtree
*rbtree
,
559 struct rcu_rbtree_node
*x
)
561 struct rcu_rbtree_node
*xr
;
563 x
= c_rcu_dereference(x
);
565 if (rcu_rbtree_is_nil(rbtree
, x
))
568 while (!rcu_rbtree_is_nil(rbtree
, xr
= c_rcu_dereference(x
->_right
)))
574 * RCU read lock must be held across the next/prev calls to ensure validity of
577 struct rcu_rbtree_node
*rcu_rbtree_next(struct rcu_rbtree
*rbtree
,
578 struct rcu_rbtree_node
*x
)
580 struct rcu_rbtree_node
*xr
, *y
;
583 x
= c_rcu_dereference(x
);
585 if (!rcu_rbtree_is_nil(rbtree
, xr
= c_rcu_dereference(x
->_right
)))
586 return rcu_rbtree_min(rbtree
, xr
);
587 y
= get_parent_and_pos(x
, &x_pos
);
588 while (!rcu_rbtree_is_nil(rbtree
, y
) && x_pos
== IS_RIGHT
) {
590 y
= get_parent_and_pos(y
, &x_pos
);
595 struct rcu_rbtree_node
*rcu_rbtree_prev(struct rcu_rbtree
*rbtree
,
596 struct rcu_rbtree_node
*x
)
598 struct rcu_rbtree_node
*xl
, *y
;
601 x
= c_rcu_dereference(x
);
603 if (!rcu_rbtree_is_nil(rbtree
, xl
= c_rcu_dereference(x
->_left
)))
604 return rcu_rbtree_max(rbtree
, xl
);
605 y
= get_parent_and_pos(x
, &x_pos
);
606 while (!rcu_rbtree_is_nil(rbtree
, y
) && x_pos
== IS_LEFT
) {
608 y
= get_parent_and_pos(y
, &x_pos
);
614 * "node" needs to be non-visible by readers.
617 void populate_node_end(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
,
618 unsigned int copy_parents
, struct rcu_rbtree_node
*stop
)
620 struct rcu_rbtree_node
*prev
= NULL
, *orig_node
= node
, *top
;
626 assert(!rcu_rbtree_is_nil(rbtree
, node
));
628 if (prev
&& copy_parents
) {
629 node
= dup_decay_node(rbtree
, node
);
630 if (get_pos(prev
) == IS_RIGHT
)
634 set_parent(prev
, node
, get_pos(prev
));
637 max_end
= calculate_node_max_end(rbtree
, node
);
639 * Compare the node max_end keys to make sure we replace
640 * references to a key belonging to a node we remove
641 * from the tree. Otherwise we would still be using this
642 * pointer as an invalid reference after garbage
643 * collection of the node and of its associated
644 * begin/end pointers.
646 if (max_end
!= node
->max_end
) {
647 node
->max_end
= max_end
;
649 top
= get_parent(node
);
650 cmm_smp_wmb(); /* write into node before publish */
651 /* make new branch visible to readers */
652 if (rcu_rbtree_is_nil(rbtree
, top
))
653 _CMM_STORE_SHARED(rbtree
->root
, node
);
654 if (get_pos(node
) == IS_LEFT
)
655 _CMM_STORE_SHARED(top
->_left
, node
);
657 _CMM_STORE_SHARED(top
->_right
, node
);
661 /* Check for propagation stop */
666 node
= get_parent(node
);
667 } while (!rcu_rbtree_is_nil(rbtree
, node
));
669 top
= node
; /* nil */
670 cmm_smp_wmb(); /* write into node before publish */
671 /* make new branch visible to readers */
672 _CMM_STORE_SHARED(rbtree
->root
, prev
);
677 /* update children */
680 assert(!rcu_rbtree_is_nil(rbtree
, node
));
681 set_parent(node
->_left
, get_decay(get_parent(node
->_left
)), IS_LEFT
);
682 set_parent(node
->_right
, get_decay(get_parent(node
->_right
)), IS_RIGHT
);
683 } while ((node
= get_parent(node
)) != top
);
687 * We have to ensure these assumptions are correct for prev/next
690 * with x being a right child, the assumption that:
691 * get_parent(x)->_right == x
692 * or if x is a left child, the assumption that:
693 * get_parent(x)->_left == x
695 * This explains why we have to allocate a vc copy of the node for left_rotate,
696 * right_rotate and transplant operations.
698 * We always ensure that the right/left child and correct parent is set in the
699 * node copies *before* we reparent the children and make the upper-level point
703 /* RCU: copy x and y, atomically point to new versions. GC old. */
704 /* Should be eventually followed by a cmm_smp_wmc() */
706 #ifdef RBTREE_RCU_SUPPORT_ROTATE_LEFT
709 void left_rotate(struct rcu_rbtree
*rbtree
,
710 struct rcu_rbtree_node
*x
)
712 struct rcu_rbtree_node
*y
, *y_left
;
714 dbg_printf("left rotate %lx\n", (unsigned long) x
->begin
);
719 /* Now operate on new copy, decay old versions */
720 x
= dup_decay_node(rbtree
, x
);
721 y
= dup_decay_node(rbtree
, y
);
722 y_left
= dup_decay_node(rbtree
, y_left
);
724 check_max_end(rbtree
, get_parent(x
));
725 check_max_end(rbtree
, x
);
726 check_max_end(rbtree
, y
);
728 /* Internal node modifications */
729 set_parent(y
, get_parent(x
), get_pos(x
));
730 set_parent(x
, y
, IS_LEFT
);
731 set_left(rbtree
, y
, x
);
732 set_right(rbtree
, x
, y_left
);
734 if (!rcu_rbtree_is_nil(rbtree
, y_left
))
735 set_parent(y_left
, x
, IS_RIGHT
);
738 * We only changed the relative position of x and y wrt their
739 * children, and reparented y (but are keeping the same nodes in
740 * place, so its parent does not need to have end value
743 x
->max_end
= calculate_node_max_end(rbtree
, x
);
744 y
->max_end
= calculate_node_max_end(rbtree
, y
);
746 cmm_smp_wmb(); /* write into node before publish */
748 /* External references update (visible by readers) */
749 if (rcu_rbtree_is_nil(rbtree
, get_parent(y
)))
750 _CMM_STORE_SHARED(rbtree
->root
, y
);
751 else if (get_pos(y
) == IS_LEFT
)
752 _CMM_STORE_SHARED(get_parent(y
)->_left
, y
);
754 _CMM_STORE_SHARED(get_parent(y
)->_right
, y
);
756 /* Point children to new copy (parent only used by updates/next/prev) */
757 set_parent(x
->_left
, get_decay(get_parent(x
->_left
)),
759 set_parent(y
->_right
, get_decay(get_parent(y
->_right
)),
761 if (!rcu_rbtree_is_nil(rbtree
, y_left
)) {
762 set_parent(y_left
->_right
,
763 get_decay(get_parent(y_left
->_right
)),
764 get_pos(y_left
->_right
));
765 set_parent(y_left
->_left
,
766 get_decay(get_parent(y_left
->_left
)),
767 get_pos(y_left
->_left
));
771 assert(y
== rbtree
->root
|| get_parent(y
)->_left
== y
772 || get_parent(y
)->_right
== y
);
773 assert(x
== rbtree
->root
|| get_parent(x
)->_left
== x
774 || get_parent(x
)->_right
== x
);
775 assert(rcu_rbtree_is_nil(rbtree
, x
->_right
) || get_parent(x
->_right
) == x
);
776 assert(rcu_rbtree_is_nil(rbtree
, x
->_left
) || get_parent(x
->_left
) == x
);
777 assert(rcu_rbtree_is_nil(rbtree
, y
->_right
) || get_parent(y
->_right
) == y
);
778 assert(rcu_rbtree_is_nil(rbtree
, y
->_left
) || get_parent(y
->_left
) == y
);
779 assert(!is_decay(rbtree
->root
));
780 assert(!is_decay(x
));
781 assert(!is_decay(y
));
782 assert(!is_decay(x
->_right
));
783 assert(!is_decay(x
->_left
));
784 assert(!is_decay(y
->_right
));
785 assert(!is_decay(y
->_left
));
786 check_max_end(rbtree
, get_parent(y
));
787 check_max_end(rbtree
, x
);
788 check_max_end(rbtree
, y
);
793 /* non-rcu version */
795 void left_rotate(struct rcu_rbtree
*rbtree
,
796 struct rcu_rbtree_node
*x
)
798 struct rcu_rbtree_node
*y
;
802 x
->_right
= y
->_left
;
803 if (!rcu_rbtree_is_nil(rbtree
, y
->_left
))
804 set_parent(y
->_left
, x
, IS_RIGHT
);
805 set_parent(y
, get_parent(x
), get_pos(x
));
806 if (rcu_rbtree_is_nil(rbtree
, get_parent(x
)))
808 else if (x
== get_parent(x
)->_left
) {
809 get_parent(x
)->_left
= y
;
811 get_parent(x
)->_right
= y
;
814 set_parent(x
, y
, IS_LEFT
);
817 * We only changed the relative position of x and y wrt their
818 * children, and reparented y (but are keeping the same nodes in
819 * place, so its parent does not need to have end value
822 x
->max_end
= calculate_node_max_end(rbtree
, x
);
823 y
->max_end
= calculate_node_max_end(rbtree
, y
);
830 #ifdef RBTREE_RCU_SUPPORT_ROTATE_RIGHT
832 void right_rotate(struct rcu_rbtree
*rbtree
,
833 struct rcu_rbtree_node
*x
)
835 struct rcu_rbtree_node
*y
, *y_right
;
837 dbg_printf("right rotate %lx\n", (unsigned long) x
->begin
);
842 /* Now operate on new copy, decay old versions */
843 x
= dup_decay_node(rbtree
, x
);
844 y
= dup_decay_node(rbtree
, y
);
845 y_right
= dup_decay_node(rbtree
, y_right
);
847 check_max_end(rbtree
, get_parent(x
));
848 check_max_end(rbtree
, x
);
849 check_max_end(rbtree
, y
);
851 /* Internal node modifications */
852 set_parent(y
, get_parent(x
), get_pos(x
));
853 set_parent(x
, y
, IS_RIGHT
);
854 set_right(rbtree
, y
, x
);
855 set_left(rbtree
, x
, y_right
);
857 if (!rcu_rbtree_is_nil(rbtree
, y_right
))
858 set_parent(y_right
, x
, IS_LEFT
);
861 * We only changed the relative position of x and y wrt their
862 * children, and reparented y (but are keeping the same nodes in
863 * place, so its parent does not need to have end value
866 x
->max_end
= calculate_node_max_end(rbtree
, x
);
867 y
->max_end
= calculate_node_max_end(rbtree
, y
);
869 cmm_smp_wmb(); /* write into node before publish */
871 /* External references update (visible by readers) */
872 if (rcu_rbtree_is_nil(rbtree
, get_parent(y
)))
873 _CMM_STORE_SHARED(rbtree
->root
, y
);
874 else if (get_pos(y
) == IS_RIGHT
)
875 _CMM_STORE_SHARED(get_parent(y
)->_right
, y
);
877 _CMM_STORE_SHARED(get_parent(y
)->_left
, y
);
879 /* Point children to new copy (parent only used by updates/next/prev) */
880 set_parent(x
->_right
, get_decay(get_parent(x
->_right
)),
882 set_parent(y
->_left
, get_decay(get_parent(y
->_left
)),
884 if (!rcu_rbtree_is_nil(rbtree
, y_right
)) {
885 set_parent(y_right
->_left
,
886 get_decay(get_parent(y_right
->_left
)),
887 get_pos(y_right
->_left
));
888 set_parent(y_right
->_right
,
889 get_decay(get_parent(y_right
->_right
)),
890 get_pos(y_right
->_right
));
894 assert(y
== rbtree
->root
|| get_parent(y
)->_right
== y
895 || get_parent(y
)->_left
== y
);
896 assert(x
== rbtree
->root
|| get_parent(x
)->_right
== x
897 || get_parent(x
)->_left
== x
);
898 assert(rcu_rbtree_is_nil(rbtree
, x
->_left
) || get_parent(x
->_left
) == x
);
899 assert(rcu_rbtree_is_nil(rbtree
, x
->_right
) || get_parent(x
->_right
) == x
);
900 assert(rcu_rbtree_is_nil(rbtree
, y
->_left
) || get_parent(y
->_left
) == y
);
901 assert(rcu_rbtree_is_nil(rbtree
, y
->_right
) || get_parent(y
->_right
) == y
);
902 assert(!is_decay(rbtree
->root
));
903 assert(!is_decay(x
));
904 assert(!is_decay(y
));
905 assert(!is_decay(x
->_left
));
906 assert(!is_decay(x
->_right
));
907 assert(!is_decay(y
->_left
));
908 assert(!is_decay(y
->_right
));
909 check_max_end(rbtree
, x
);
910 check_max_end(rbtree
, y
);
911 check_max_end(rbtree
, get_parent(y
));
916 /* non-rcu version */
918 void right_rotate(struct rcu_rbtree
*rbtree
,
919 struct rcu_rbtree_node
*x
)
921 struct rcu_rbtree_node
*y
;
925 x
->_left
= y
->_right
;
926 if (!rcu_rbtree_is_nil(rbtree
, y
->_right
))
927 set_parent(y
->_right
, x
, IS_LEFT
);
928 set_parent(y
, get_parent(x
), get_pos(x
));
929 if (rcu_rbtree_is_nil(rbtree
, get_parent(x
)))
931 else if (x
== get_parent(x
)->_right
) {
932 get_parent(x
)->_right
= y
;
934 get_parent(x
)->_left
= y
;
937 set_parent(x
, y
, IS_RIGHT
);
940 * We only changed the relative position of x and y wrt their
941 * children, and reparented y (but are keeping the same nodes in
942 * place, so its parent does not need to have end value
945 x
->max_end
= calculate_node_max_end(rbtree
, x
);
946 y
->max_end
= calculate_node_max_end(rbtree
, y
);
953 static void rcu_rbtree_insert_fixup(struct rcu_rbtree
*rbtree
,
954 struct rcu_rbtree_node
*z
)
956 struct rcu_rbtree_node
*y
;
958 dbg_printf("insert fixup %p\n", z
->begin
);
959 assert(!is_decay(rbtree
->root
));
961 while (get_parent(z
)->color
== COLOR_RED
) {
962 if (get_parent(z
) == get_parent(get_parent(z
))->_left
) {
963 y
= get_parent(get_parent(z
))->_right
;
964 if (y
->color
== COLOR_RED
) {
965 get_parent(z
)->color
= COLOR_BLACK
;
966 y
->color
= COLOR_BLACK
;
967 get_parent(get_parent(z
))->color
= COLOR_RED
;
968 z
= get_parent(get_parent(z
));
970 if (z
== get_parent(z
)->_right
) {
972 left_rotate(rbtree
, z
);
974 assert(!is_decay(rbtree
->root
));
976 get_parent(z
)->color
= COLOR_BLACK
;
977 get_parent(get_parent(z
))->color
= COLOR_RED
;
978 assert(!is_decay(z
));
979 assert(!is_decay(get_parent(z
)));
980 assert(!is_decay(get_parent(get_parent(z
))));
981 right_rotate(rbtree
, get_parent(get_parent(z
)));
982 assert(!is_decay(z
));
983 assert(!is_decay(rbtree
->root
));
986 y
= get_parent(get_parent(z
))->_left
;
987 if (y
->color
== COLOR_RED
) {
988 get_parent(z
)->color
= COLOR_BLACK
;
989 y
->color
= COLOR_BLACK
;
990 get_parent(get_parent(z
))->color
= COLOR_RED
;
991 z
= get_parent(get_parent(z
));
993 if (z
== get_parent(z
)->_left
) {
995 right_rotate(rbtree
, z
);
997 assert(!is_decay(rbtree
->root
));
999 get_parent(z
)->color
= COLOR_BLACK
;
1000 get_parent(get_parent(z
))->color
= COLOR_RED
;
1001 left_rotate(rbtree
, get_parent(get_parent(z
)));
1002 assert(!is_decay(z
));
1003 assert(!is_decay(rbtree
->root
));
1007 rbtree
->root
->color
= COLOR_BLACK
;
1011 * rcu_rbtree_insert - Insert a node in the RCU rbtree
1013 * Returns 0 on success, or < 0 on error.
1015 int rcu_rbtree_insert(struct rcu_rbtree
*rbtree
,
1016 void *begin
, void *end
)
1018 struct rcu_rbtree_node
*x
, *y
, *z
;
1020 z
= _rcu_rbtree_alloc_node(rbtree
);
1026 dbg_printf("insert %p\n", z
->begin
);
1027 assert(!is_decay(rbtree
->root
));
1029 y
= make_nil(rbtree
);
1031 while (!rcu_rbtree_is_nil(rbtree
, x
)) {
1033 if (rbtree
->comp(z
->begin
, x
->begin
) < 0)
1039 z
->_left
= make_nil(rbtree
);
1040 z
->_right
= make_nil(rbtree
);
1041 z
->color
= COLOR_RED
;
1042 z
->decay_next
= NULL
;
1043 z
->max_end
= z
->end
;
1046 if (rcu_rbtree_is_nil(rbtree
, y
)) {
1047 set_parent(z
, y
, IS_RIGHT
); /* pos arbitrary for root node */
1049 * Order stores to z (children/parents) before stores
1050 * that will make it visible to the rest of the tree.
1053 _CMM_STORE_SHARED(rbtree
->root
, z
);
1054 } else if (rbtree
->comp(z
->begin
, y
->begin
) < 0) {
1055 y
= dup_decay_node(rbtree
, y
);
1056 set_parent(z
, y
, IS_LEFT
);
1057 if (get_pos(z
) == IS_LEFT
)
1058 _CMM_STORE_SHARED(y
->_left
, z
);
1060 _CMM_STORE_SHARED(y
->_right
, z
);
1061 populate_node_end(rbtree
, y
, 1, NULL
);
1063 y
= dup_decay_node(rbtree
, y
);
1064 set_parent(z
, y
, IS_RIGHT
);
1065 if (get_pos(z
) == IS_LEFT
)
1066 _CMM_STORE_SHARED(y
->_left
, z
);
1068 _CMM_STORE_SHARED(y
->_right
, z
);
1069 populate_node_end(rbtree
, y
, 1, NULL
);
1071 rcu_rbtree_insert_fixup(rbtree
, z
);
1073 * Make sure to commit all _CMM_STORE_SHARED() for non-coherent caches.
1077 check_max_end(rbtree
, z
);
1078 check_max_end(rbtree
, y
);
1084 * Transplant v into u position.
1087 #ifdef RBTREE_RCU_SUPPORT_TRANSPLANT
1090 void rcu_rbtree_transplant(struct rcu_rbtree
*rbtree
,
1091 struct rcu_rbtree_node
*u
,
1092 struct rcu_rbtree_node
*v
,
1093 unsigned int copy_parents
,
1094 struct rcu_rbtree_node
*stop
)
1096 dbg_printf("transplant %p\n", v
->begin
);
1098 if (!rcu_rbtree_is_nil(rbtree
, v
))
1099 v
= dup_decay_node(rbtree
, v
);
1101 if (rcu_rbtree_is_nil(rbtree
, get_parent(u
))) {
1102 /* pos is arbitrary for root node */
1103 set_parent(v
, get_parent(u
), IS_RIGHT
);
1104 cmm_smp_wmb(); /* write into node before publish */
1105 _CMM_STORE_SHARED(rbtree
->root
, v
);
1107 struct rcu_rbtree_node
*vp
;
1111 vp
= dup_decay_node(rbtree
, vp
);
1112 set_parent(v
, vp
, get_pos(u
));
1113 if (get_pos(v
) == IS_LEFT
)
1114 _CMM_STORE_SHARED(vp
->_left
, v
);
1116 _CMM_STORE_SHARED(vp
->_right
, v
);
1117 populate_node_end(rbtree
, vp
, copy_parents
, stop
);
1118 check_max_end(rbtree
, vp
);
1121 /* Point children to new copy (parent only used by updates/next/prev) */
1122 if (!rcu_rbtree_is_nil(rbtree
, v
)) {
1123 set_parent(v
->_right
, get_decay(get_parent(v
->_right
)),
1124 get_pos(v
->_right
));
1125 set_parent(v
->_left
, get_decay(get_parent(v
->_left
)),
1128 assert(!is_decay(rbtree
->root
));
1129 check_max_end(rbtree
, v
);
1134 /* Non-RCU version */
1136 void rcu_rbtree_transplant(struct rcu_rbtree
*rbtree
,
1137 struct rcu_rbtree_node
*u
,
1138 struct rcu_rbtree_node
*v
,
1139 unsigned int copy_parents
,
1140 struct rcu_rbtree_node
*stop
)
1142 dbg_printf("transplant %p\n", v
->begin
);
1145 if (rcu_rbtree_is_nil(rbtree
, get_parent(u
))) {
1148 if (u
== get_parent(u
)->_left
)
1149 get_parent(u
)->_left
= v
;
1151 get_parent(u
)->_right
= v
;
1152 populate_node_end(rbtree
, get_parent(u
), copy_parents
, stop
);
1154 set_parent(v
, get_parent(u
), get_pos(u
));
1155 unlock_test_mutex();
1160 static void rcu_rbtree_remove_fixup(struct rcu_rbtree
*rbtree
,
1161 struct rcu_rbtree_node
*x
)
1163 dbg_printf("remove fixup %p\n", x
->begin
);
1165 while (x
!= rbtree
->root
&& x
->color
== COLOR_BLACK
) {
1166 assert(!is_decay(get_parent(x
)));
1167 assert(!is_decay(get_parent(x
)->_left
));
1168 if (x
== get_parent(x
)->_left
) {
1169 struct rcu_rbtree_node
*w
;
1171 w
= get_parent(x
)->_right
;
1173 if (w
->color
== COLOR_RED
) {
1174 w
->color
= COLOR_BLACK
;
1175 get_parent(x
)->color
= COLOR_RED
;
1176 left_rotate(rbtree
, get_parent(x
));
1178 assert(!is_decay(rbtree
->root
));
1179 w
= get_parent(x
)->_right
;
1181 if (w
->_left
->color
== COLOR_BLACK
1182 && w
->_right
->color
== COLOR_BLACK
) {
1183 w
->color
= COLOR_RED
;
1185 assert(!is_decay(rbtree
->root
));
1186 assert(!is_decay(x
));
1188 if (w
->_right
->color
== COLOR_BLACK
) {
1189 w
->_left
->color
= COLOR_BLACK
;
1190 w
->color
= COLOR_RED
;
1191 right_rotate(rbtree
, w
);
1192 assert(!is_decay(rbtree
->root
));
1194 w
= get_parent(x
)->_right
;
1196 w
->color
= get_parent(x
)->color
;
1197 get_parent(x
)->color
= COLOR_BLACK
;
1198 w
->_right
->color
= COLOR_BLACK
;
1199 left_rotate(rbtree
, get_parent(x
));
1200 assert(!is_decay(rbtree
->root
));
1204 struct rcu_rbtree_node
*w
;
1206 w
= get_parent(x
)->_left
;
1208 if (w
->color
== COLOR_RED
) {
1209 w
->color
= COLOR_BLACK
;
1210 get_parent(x
)->color
= COLOR_RED
;
1211 right_rotate(rbtree
, get_parent(x
));
1212 assert(!is_decay(rbtree
->root
));
1214 w
= get_parent(x
)->_left
;
1216 if (w
->_right
->color
== COLOR_BLACK
1217 && w
->_left
->color
== COLOR_BLACK
) {
1218 w
->color
= COLOR_RED
;
1220 assert(!is_decay(rbtree
->root
));
1221 assert(!is_decay(x
));
1223 if (w
->_left
->color
== COLOR_BLACK
) {
1224 w
->_right
->color
= COLOR_BLACK
;
1225 w
->color
= COLOR_RED
;
1226 left_rotate(rbtree
, w
);
1227 assert(!is_decay(rbtree
->root
));
1229 w
= get_parent(x
)->_left
;
1231 w
->color
= get_parent(x
)->color
;
1232 get_parent(x
)->color
= COLOR_BLACK
;
1233 w
->_left
->color
= COLOR_BLACK
;
1234 right_rotate(rbtree
, get_parent(x
));
1235 assert(!is_decay(rbtree
->root
));
1240 x
->color
= COLOR_BLACK
;
1244 * Delete z. All non-copied children left/right positions are unchanged.
1247 void rcu_rbtree_remove_nonil(struct rcu_rbtree
*rbtree
,
1248 struct rcu_rbtree_node
*z
,
1249 struct rcu_rbtree_node
*y
)
1251 struct rcu_rbtree_node
*x
;
1253 dbg_printf("remove nonil %p\n", z
->begin
);
1256 assert(!is_decay(z
));
1257 assert(!is_decay(y
));
1258 assert(!is_decay(y
->_right
));
1259 assert(!is_decay(get_parent(y
)));
1261 assert(!is_decay(x
));
1262 if (get_parent(y
) == z
) {
1263 y
= dup_decay_node(rbtree
, y
);
1264 set_parent(x
, y
, get_pos(x
)); /* parent for nil */
1265 /* y is z's right node */
1266 set_left(rbtree
, y
, z
->_left
);
1267 y
->max_end
= calculate_node_max_end(rbtree
, y
);
1268 rcu_rbtree_transplant(rbtree
, z
, y
, 1, NULL
);
1270 struct rcu_rbtree_node
*oy_right
, *z_right
;
1273 * Need to make sure y is always visible by readers.
1275 y
= rcu_rbtree_min_dup_decay(rbtree
, z
->_right
, &z_right
);
1276 assert(!is_decay(y
));
1277 assert(!is_decay(z
));
1278 oy_right
= y
->_right
;
1281 * The max child begin of z_right does not change, because
1282 * we're only changing its left children.
1284 y
->_right
= z_right
;
1285 set_parent(y
->_right
, y
, IS_RIGHT
);
1286 assert(!is_decay(z
->_left
));
1287 y
->_left
= z
->_left
;
1288 assert(!is_decay(oy_right
));
1290 * Transplant of oy_right to old y's location will only
1291 * trigger a "end" value update of the already copied branch
1292 * (which is not visible yet). We are transplanting
1293 * oy_right as a left child of old y's parent, so the
1294 * min values update propagated upward necessarily stops
1297 rcu_rbtree_transplant(rbtree
, y
, oy_right
, 0, y
);
1298 y
->max_end
= calculate_node_max_end(rbtree
, y
);
1299 rcu_rbtree_transplant(rbtree
, z
, y
, 1, NULL
);
1300 /* Update children */
1301 (void) rcu_rbtree_min_update_decay(rbtree
, y
->_right
);
1304 assert(!is_decay(z
));
1305 assert(!is_decay(z
->_left
));
1306 y
->color
= z
->color
;
1307 set_parent(y
->_left
, y
, IS_LEFT
);
1308 set_parent(y
->_right
, get_decay(get_parent(y
->_right
)), IS_RIGHT
);
1309 assert(!is_decay(y
->_left
));
1310 assert(!is_decay(y
->_right
));
1313 int rcu_rbtree_remove(struct rcu_rbtree
*rbtree
,
1314 struct rcu_rbtree_node
*z
)
1316 struct rcu_rbtree_node
*x
, *y
;
1317 unsigned int y_original_color
;
1319 assert(!is_decay(rbtree
->root
));
1320 dbg_printf("remove %p\n", z
->begin
);
1323 assert(!is_decay(z
));
1325 y_original_color
= y
->color
;
1327 if (rcu_rbtree_is_nil(rbtree
, z
->_left
)) {
1328 rcu_rbtree_transplant(rbtree
, z
, z
->_right
, 1, NULL
);
1329 assert(!is_decay(z
));
1330 x
= get_decay(z
->_right
);
1332 } else if (rcu_rbtree_is_nil(rbtree
, z
->_right
)) {
1333 rcu_rbtree_transplant(rbtree
, z
, z
->_left
, 1, NULL
);
1334 assert(!is_decay(z
));
1335 x
= get_decay(z
->_left
);
1338 y
= rcu_rbtree_min(rbtree
, z
->_right
);
1339 assert(!is_decay(y
));
1340 y_original_color
= y
->color
;
1342 rcu_rbtree_remove_nonil(rbtree
, z
, y
);
1346 if (y_original_color
== COLOR_BLACK
)
1347 rcu_rbtree_remove_fixup(rbtree
, x
);
1349 check_max_end(rbtree
, x
);
1350 check_max_end(rbtree
, get_decay(y
));
1352 * Commit all _CMM_STORE_SHARED().
1355 #ifdef RBTREE_RCU_SUPPORT
1356 rbtree
->call_rcu(&z
->head
, _rcu_rbtree_free_node
);
1358 _rcu_rbtree_free_node(&z
->head
);