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
165 * Add internal mutex locking within the RBTree, for debugging. Enable this
166 * define and add mutexes to RCU readers to debug races with with rotation or
169 /* #define RBTREE_INTERNAL_LOCKING */
171 #ifdef RBTREE_INTERNAL_LOCKING
172 static pthread_mutex_t test_mutex
= PTHREAD_MUTEX_INITIALIZER
;
173 static pthread_mutex_t outer_mutex
= PTHREAD_MUTEX_INITIALIZER
;
176 void lock_outer_mutex(void)
178 pthread_mutex_lock(&outer_mutex
);
182 void unlock_outer_mutex(void)
184 pthread_mutex_unlock(&outer_mutex
);
188 void lock_test_mutex(void)
190 pthread_mutex_lock(&test_mutex
);
194 void unlock_test_mutex(void)
196 pthread_mutex_unlock(&test_mutex
);
200 void lock_outer_mutex(void)
205 void unlock_outer_mutex(void)
210 void lock_test_mutex(void)
215 void unlock_test_mutex(void)
221 void set_parent(struct rcu_rbtree_node
*node
,
222 struct rcu_rbtree_node
*parent
,
225 _CMM_STORE_SHARED(node
->parent
, ((unsigned long) parent
) | pos
);
229 struct rcu_rbtree_node
*get_parent(struct rcu_rbtree_node
*node
)
231 return (struct rcu_rbtree_node
*) (node
->parent
& ~1UL);
235 unsigned int get_pos(struct rcu_rbtree_node
*node
)
237 return (unsigned int) (node
->parent
& 1UL);
241 struct rcu_rbtree_node
*get_parent_and_pos(struct rcu_rbtree_node
*node
,
244 unsigned long parent_pos
= rcu_dereference(node
->parent
);
246 *pos
= (unsigned int) (parent_pos
& 1UL);
247 return (struct rcu_rbtree_node
*) (parent_pos
& ~1UL);
251 void set_decay(struct rcu_rbtree_node
*x
, struct rcu_rbtree_node
*xc
)
257 struct rcu_rbtree_node
*get_decay(struct rcu_rbtree_node
*x
)
261 while (x
->decay_next
)
267 struct rcu_rbtree_node
*is_decay(struct rcu_rbtree_node
*x
)
269 return x
->decay_next
;
273 struct rcu_rbtree_node
*_rcu_rbtree_alloc_node(struct rcu_rbtree
*rbtree
)
275 return rbtree
->rballoc(sizeof(struct rcu_rbtree_node
));
279 void _rcu_rbtree_free_node(struct rcu_head
*head
)
281 struct rcu_rbtree_node
*node
=
282 caa_container_of(head
, struct rcu_rbtree_node
, head
);
283 node
->rbtree
->rbfree(node
);
286 #ifdef RBTREE_RCU_SUPPORT
289 struct rcu_rbtree_node
*dup_decay_node(struct rcu_rbtree
*rbtree
,
290 struct rcu_rbtree_node
*x
)
292 struct rcu_rbtree_node
*xc
;
294 if (rcu_rbtree_is_nil(rbtree
, x
))
297 xc
= _rcu_rbtree_alloc_node(rbtree
);
298 memcpy(xc
, x
, sizeof(*xc
));
299 xc
->decay_next
= NULL
;
301 rbtree
->call_rcu(&x
->head
, _rcu_rbtree_free_node
);
305 #else /* RBTREE_RCU_SUPPORT */
308 struct rcu_rbtree_node
*dup_decay_node(struct rcu_rbtree
*rbtree
,
309 struct rcu_rbtree_node
*x
)
317 * Info for range lookups:
318 * Range lookup information is only valid when used when searching for
319 * ranges. It should never be used in next/prev traversal because the
320 * pointers to parents are not in sync with the parent vision of the
324 void set_left(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
,
325 struct rcu_rbtree_node
*left
)
331 void set_right(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
,
332 struct rcu_rbtree_node
*right
)
334 node
->_right
= right
;
338 void *calculate_node_max_end(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
)
343 if (!rcu_rbtree_is_nil(rbtree
, node
->_right
)) {
344 if (rbtree
->comp(max_end
, node
->_right
->max_end
) < 0)
345 max_end
= node
->_right
->max_end
;
347 if (!rcu_rbtree_is_nil(rbtree
, node
->_left
)) {
348 if (rbtree
->comp(max_end
, node
->_left
->max_end
) < 0)
349 max_end
= node
->_left
->max_end
;
356 * Deal with memory allocation errors.
357 * Can be ensured by reserving a pool of memory entries before doing the
358 * insertion, which will have to be function of number of
359 * transplantations/rotations required for the operation (which is a
360 * multiple of the tree height).
365 void show_tree(struct rcu_rbtree
*rbtree
)
367 struct rcu_rbtree_node
*node
;
369 node
= rcu_rbtree_min(rbtree
, rbtree
->root
);
370 while (!rcu_rbtree_is_nil(rbtree
, node
)) {
371 assert(!is_decay(node
));
372 printf("{ b:%lX e:%lX pb: %lX r:%lX l:%lX %s %s %s} ",
373 (unsigned long) node
->begin
,
374 (unsigned long) node
->end
,
375 (unsigned long) get_parent(node
)->begin
,
376 (unsigned long) node
->_right
->begin
,
377 (unsigned long) node
->_left
->begin
,
378 node
->color
? "red" : "black",
379 get_pos(node
) ? "right" : "left",
380 rcu_rbtree_is_nil(rbtree
, node
) ? "nil" : "");
381 node
= rcu_rbtree_next(rbtree
, node
);
386 #define check_max_end(rbtree, x) \
388 if (rcu_rbtree_is_nil(rbtree, x)) \
390 assert(rbtree->comp(x->max_end, \
391 calculate_node_max_end(rbtree, x)) == 0); \
396 void show_tree(struct rcu_rbtree
*rbtree
)
401 void check_max_end(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*x
)
407 struct rcu_rbtree_node
*make_nil(struct rcu_rbtree
*rbtree
)
409 return &rbtree
->nil_node
;
413 * Iterative rbtree search.
415 struct rcu_rbtree_node
*rcu_rbtree_search(struct rcu_rbtree
*rbtree
,
416 struct rcu_rbtree_node
*x
,
419 struct rcu_rbtree_node
*xl
;
421 dbg_printf("searching point 0x%lx\n", (unsigned long) point
);
422 x
= rcu_dereference(x
);
424 while (!rcu_rbtree_is_nil(rbtree
, x
)) {
426 xl
= rcu_dereference(x
->_left
);
427 dbg_printf("search x %lx x_end %lx x_max_end %lx\n", (unsigned long) x
->begin
,
428 (unsigned long) x
->end
, (unsigned long) x
->max_end
);
429 dbg_printf("search xl %lx xl_end %lx xl_max_end %lx\n", (unsigned long) xl
->begin
,
430 (unsigned long) xl
->end
, (unsigned long) xl
->max_end
);
431 if (!rcu_rbtree_is_nil(rbtree
, xl
)
432 && (rbtree
->comp(xl
->max_end
, point
) > 0)) {
433 dbg_printf("go left\n");
435 } else if (rbtree
->comp(x
->begin
, point
) <= 0
436 && rbtree
->comp(point
, x
->end
) < 0) {
437 dbg_printf("got it!\n");
439 } else if (rbtree
->comp(point
, x
->begin
) > 0) {
440 dbg_printf("go right\n");
441 x
= rcu_dereference(x
->_right
);
443 dbg_printf("not found!\n");
444 x
= make_nil(rbtree
);
447 if (rcu_rbtree_is_nil(rbtree
, x
))
448 dbg_printf("Reached bottom of tree.\n");
453 struct rcu_rbtree_node
*rcu_rbtree_search_range(struct rcu_rbtree
*rbtree
,
454 struct rcu_rbtree_node
*x
,
455 void *begin
, void *end
)
457 struct rcu_rbtree_node
*node
;
459 node
= rcu_rbtree_search(rbtree
, x
, begin
);
460 if (rcu_rbtree_is_nil(rbtree
, node
))
462 if (rbtree
->comp(node
->end
, end
) < 0)
463 return NULL
; /* High is outside lookup range */
468 * Search by exact range start value.
470 struct rcu_rbtree_node
*rcu_rbtree_search_begin_key(struct rcu_rbtree
*rbtree
,
471 struct rcu_rbtree_node
*x
,
474 x
= rcu_dereference(x
);
477 while (!rcu_rbtree_is_nil(rbtree
, x
) && (comp
= rbtree
->comp(k
, x
->begin
)) != 0) {
480 x
= rcu_dereference(x
->_left
);
482 x
= rcu_dereference(x
->_right
);
488 struct rcu_rbtree_node
*rcu_rbtree_min_dup_decay(struct rcu_rbtree
*rbtree
,
489 struct rcu_rbtree_node
*x
,
490 struct rcu_rbtree_node
**zr
)
492 struct rcu_rbtree_node
*xl
;
494 x
= rcu_dereference(x
);
496 if (rcu_rbtree_is_nil(rbtree
, x
)) {
500 *zr
= x
= dup_decay_node(rbtree
, x
);
502 while (!rcu_rbtree_is_nil(rbtree
, xl
= rcu_dereference(x
->_left
))) {
503 x
= dup_decay_node(rbtree
, xl
);
504 set_parent(x
, get_decay(get_parent(x
)), get_pos(x
));
505 get_parent(x
)->_left
= get_decay(get_parent(x
)->_left
);
511 struct rcu_rbtree_node
*rcu_rbtree_min_update_decay(struct rcu_rbtree
*rbtree
,
512 struct rcu_rbtree_node
*x
)
514 struct rcu_rbtree_node
*xl
;
516 x
= rcu_dereference(x
);
518 if (rcu_rbtree_is_nil(rbtree
, x
))
521 set_parent(x
->_right
, get_decay(get_parent(x
->_right
)),
523 set_parent(x
->_left
, get_decay(get_parent(x
->_left
)),
527 while (!rcu_rbtree_is_nil(rbtree
, xl
= rcu_dereference(x
->_left
))) {
529 set_parent(x
->_right
, get_decay(get_parent(x
->_right
)),
531 set_parent(x
->_left
, get_decay(get_parent(x
->_left
)),
537 struct rcu_rbtree_node
*rcu_rbtree_min(struct rcu_rbtree
*rbtree
,
538 struct rcu_rbtree_node
*x
)
540 struct rcu_rbtree_node
*xl
;
542 x
= rcu_dereference(x
);
544 if (rcu_rbtree_is_nil(rbtree
, x
))
547 while (!rcu_rbtree_is_nil(rbtree
, xl
= rcu_dereference(x
->_left
)))
552 struct rcu_rbtree_node
*rcu_rbtree_max(struct rcu_rbtree
*rbtree
,
553 struct rcu_rbtree_node
*x
)
555 struct rcu_rbtree_node
*xr
;
557 x
= rcu_dereference(x
);
559 if (rcu_rbtree_is_nil(rbtree
, x
))
562 while (!rcu_rbtree_is_nil(rbtree
, xr
= rcu_dereference(x
->_right
)))
568 * RCU read lock must be held across the next/prev calls to ensure validity of
571 struct rcu_rbtree_node
*rcu_rbtree_next(struct rcu_rbtree
*rbtree
,
572 struct rcu_rbtree_node
*x
)
574 struct rcu_rbtree_node
*xr
, *y
;
577 x
= rcu_dereference(x
);
579 if (!rcu_rbtree_is_nil(rbtree
, xr
= rcu_dereference(x
->_right
)))
580 return rcu_rbtree_min(rbtree
, xr
);
581 y
= get_parent_and_pos(x
, &x_pos
);
582 while (!rcu_rbtree_is_nil(rbtree
, y
) && x_pos
== IS_RIGHT
) {
584 y
= get_parent_and_pos(y
, &x_pos
);
589 struct rcu_rbtree_node
*rcu_rbtree_prev(struct rcu_rbtree
*rbtree
,
590 struct rcu_rbtree_node
*x
)
592 struct rcu_rbtree_node
*xl
, *y
;
595 x
= rcu_dereference(x
);
597 if (!rcu_rbtree_is_nil(rbtree
, xl
= rcu_dereference(x
->_left
)))
598 return rcu_rbtree_max(rbtree
, xl
);
599 y
= get_parent_and_pos(x
, &x_pos
);
600 while (!rcu_rbtree_is_nil(rbtree
, y
) && x_pos
== IS_LEFT
) {
602 y
= get_parent_and_pos(y
, &x_pos
);
608 * "node" needs to be non-visible by readers.
611 void populate_node_end(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
,
612 unsigned int copy_parents
, struct rcu_rbtree_node
*stop
)
614 struct rcu_rbtree_node
*prev
= NULL
, *orig_node
= node
, *top
;
620 assert(!rcu_rbtree_is_nil(rbtree
, node
));
622 if (prev
&& copy_parents
) {
623 node
= dup_decay_node(rbtree
, node
);
624 if (get_pos(prev
) == IS_RIGHT
)
628 set_parent(prev
, node
, get_pos(prev
));
631 max_end
= calculate_node_max_end(rbtree
, node
);
633 * Compare the node max_end keys to make sure we replace
634 * references to a key belonging to a node we remove
635 * from the tree. Otherwise we would still be using this
636 * pointer as an invalid reference after garbage
637 * collection of the node and of its associated
638 * begin/end pointers.
640 if (max_end
!= node
->max_end
) {
641 node
->max_end
= max_end
;
643 top
= get_parent(node
);
644 cmm_smp_wmb(); /* write into node before publish */
645 /* make new branch visible to readers */
646 if (rcu_rbtree_is_nil(rbtree
, top
))
647 _CMM_STORE_SHARED(rbtree
->root
, node
);
648 if (get_pos(node
) == IS_LEFT
)
649 _CMM_STORE_SHARED(top
->_left
, node
);
651 _CMM_STORE_SHARED(top
->_right
, node
);
655 /* Check for propagation stop */
660 node
= get_parent(node
);
661 } while (!rcu_rbtree_is_nil(rbtree
, node
));
663 top
= node
; /* nil */
664 cmm_smp_wmb(); /* write into node before publish */
665 /* make new branch visible to readers */
666 _CMM_STORE_SHARED(rbtree
->root
, prev
);
671 /* update children */
674 assert(!rcu_rbtree_is_nil(rbtree
, node
));
675 set_parent(node
->_left
, get_decay(get_parent(node
->_left
)), IS_LEFT
);
676 set_parent(node
->_right
, get_decay(get_parent(node
->_right
)), IS_RIGHT
);
677 } while ((node
= get_parent(node
)) != top
);
681 * We have to ensure these assumptions are correct for prev/next
684 * with x being a right child, the assumption that:
685 * get_parent(x)->_right == x
686 * or if x is a left child, the assumption that:
687 * get_parent(x)->_left == x
689 * This explains why we have to allocate a vc copy of the node for left_rotate,
690 * right_rotate and transplant operations.
692 * We always ensure that the right/left child and correct parent is set in the
693 * node copies *before* we reparent the children and make the upper-level point
697 /* RCU: copy x and y, atomically point to new versions. GC old. */
698 /* Should be eventually followed by a cmm_smp_wmc() */
700 #ifdef RBTREE_RCU_SUPPORT_ROTATE_LEFT
703 void left_rotate(struct rcu_rbtree
*rbtree
,
704 struct rcu_rbtree_node
*x
)
706 struct rcu_rbtree_node
*y
, *y_left
;
708 dbg_printf("left rotate %lx\n", (unsigned long) x
->begin
);
713 /* Now operate on new copy, decay old versions */
714 x
= dup_decay_node(rbtree
, x
);
715 y
= dup_decay_node(rbtree
, y
);
716 y_left
= dup_decay_node(rbtree
, y_left
);
718 check_max_end(rbtree
, get_parent(x
));
719 check_max_end(rbtree
, x
);
720 check_max_end(rbtree
, y
);
722 /* Internal node modifications */
723 set_parent(y
, get_parent(x
), get_pos(x
));
724 set_parent(x
, y
, IS_LEFT
);
725 set_left(rbtree
, y
, x
);
726 set_right(rbtree
, x
, y_left
);
728 if (!rcu_rbtree_is_nil(rbtree
, y_left
))
729 set_parent(y_left
, x
, IS_RIGHT
);
732 * We only changed the relative position of x and y wrt their
733 * children, and reparented y (but are keeping the same nodes in
734 * place, so its parent does not need to have end value
737 x
->max_end
= calculate_node_max_end(rbtree
, x
);
738 y
->max_end
= calculate_node_max_end(rbtree
, y
);
740 cmm_smp_wmb(); /* write into node before publish */
742 /* External references update (visible by readers) */
743 if (rcu_rbtree_is_nil(rbtree
, get_parent(y
)))
744 _CMM_STORE_SHARED(rbtree
->root
, y
);
745 else if (get_pos(y
) == IS_LEFT
)
746 _CMM_STORE_SHARED(get_parent(y
)->_left
, y
);
748 _CMM_STORE_SHARED(get_parent(y
)->_right
, y
);
750 /* Point children to new copy (parent only used by updates/next/prev) */
751 set_parent(x
->_left
, get_decay(get_parent(x
->_left
)),
753 set_parent(y
->_right
, get_decay(get_parent(y
->_right
)),
755 if (!rcu_rbtree_is_nil(rbtree
, y_left
)) {
756 set_parent(y_left
->_right
,
757 get_decay(get_parent(y_left
->_right
)),
758 get_pos(y_left
->_right
));
759 set_parent(y_left
->_left
,
760 get_decay(get_parent(y_left
->_left
)),
761 get_pos(y_left
->_left
));
765 assert(y
== rbtree
->root
|| get_parent(y
)->_left
== y
766 || get_parent(y
)->_right
== y
);
767 assert(x
== rbtree
->root
|| get_parent(x
)->_left
== x
768 || get_parent(x
)->_right
== x
);
769 assert(rcu_rbtree_is_nil(rbtree
, x
->_right
) || get_parent(x
->_right
) == x
);
770 assert(rcu_rbtree_is_nil(rbtree
, x
->_left
) || get_parent(x
->_left
) == x
);
771 assert(rcu_rbtree_is_nil(rbtree
, y
->_right
) || get_parent(y
->_right
) == y
);
772 assert(rcu_rbtree_is_nil(rbtree
, y
->_left
) || get_parent(y
->_left
) == y
);
773 assert(!is_decay(rbtree
->root
));
774 assert(!is_decay(x
));
775 assert(!is_decay(y
));
776 assert(!is_decay(x
->_right
));
777 assert(!is_decay(x
->_left
));
778 assert(!is_decay(y
->_right
));
779 assert(!is_decay(y
->_left
));
780 check_max_end(rbtree
, get_parent(y
));
781 check_max_end(rbtree
, x
);
782 check_max_end(rbtree
, y
);
787 /* non-rcu version */
789 void left_rotate(struct rcu_rbtree
*rbtree
,
790 struct rcu_rbtree_node
*x
)
792 struct rcu_rbtree_node
*y
;
796 x
->_right
= y
->_left
;
797 if (!rcu_rbtree_is_nil(rbtree
, y
->_left
))
798 set_parent(y
->_left
, x
, IS_RIGHT
);
799 set_parent(y
, get_parent(x
), get_pos(x
));
800 if (rcu_rbtree_is_nil(rbtree
, get_parent(x
)))
802 else if (x
== get_parent(x
)->_left
) {
803 get_parent(x
)->_left
= y
;
805 get_parent(x
)->_right
= y
;
808 set_parent(x
, y
, IS_LEFT
);
811 * We only changed the relative position of x and y wrt their
812 * children, and reparented y (but are keeping the same nodes in
813 * place, so its parent does not need to have end value
816 x
->max_end
= calculate_node_max_end(rbtree
, x
);
817 y
->max_end
= calculate_node_max_end(rbtree
, y
);
824 #ifdef RBTREE_RCU_SUPPORT_ROTATE_RIGHT
826 void right_rotate(struct rcu_rbtree
*rbtree
,
827 struct rcu_rbtree_node
*x
)
829 struct rcu_rbtree_node
*y
, *y_right
;
831 dbg_printf("right rotate %lx\n", (unsigned long) x
->begin
);
836 /* Now operate on new copy, decay old versions */
837 x
= dup_decay_node(rbtree
, x
);
838 y
= dup_decay_node(rbtree
, y
);
839 y_right
= dup_decay_node(rbtree
, y_right
);
841 check_max_end(rbtree
, get_parent(x
));
842 check_max_end(rbtree
, x
);
843 check_max_end(rbtree
, y
);
845 /* Internal node modifications */
846 set_parent(y
, get_parent(x
), get_pos(x
));
847 set_parent(x
, y
, IS_RIGHT
);
848 set_right(rbtree
, y
, x
);
849 set_left(rbtree
, x
, y_right
);
851 if (!rcu_rbtree_is_nil(rbtree
, y_right
))
852 set_parent(y_right
, x
, IS_LEFT
);
855 * We only changed the relative position of x and y wrt their
856 * children, and reparented y (but are keeping the same nodes in
857 * place, so its parent does not need to have end value
860 x
->max_end
= calculate_node_max_end(rbtree
, x
);
861 y
->max_end
= calculate_node_max_end(rbtree
, y
);
863 cmm_smp_wmb(); /* write into node before publish */
865 /* External references update (visible by readers) */
866 if (rcu_rbtree_is_nil(rbtree
, get_parent(y
)))
867 _CMM_STORE_SHARED(rbtree
->root
, y
);
868 else if (get_pos(y
) == IS_RIGHT
)
869 _CMM_STORE_SHARED(get_parent(y
)->_right
, y
);
871 _CMM_STORE_SHARED(get_parent(y
)->_left
, y
);
873 /* Point children to new copy (parent only used by updates/next/prev) */
874 set_parent(x
->_right
, get_decay(get_parent(x
->_right
)),
876 set_parent(y
->_left
, get_decay(get_parent(y
->_left
)),
878 if (!rcu_rbtree_is_nil(rbtree
, y_right
)) {
879 set_parent(y_right
->_left
,
880 get_decay(get_parent(y_right
->_left
)),
881 get_pos(y_right
->_left
));
882 set_parent(y_right
->_right
,
883 get_decay(get_parent(y_right
->_right
)),
884 get_pos(y_right
->_right
));
888 assert(y
== rbtree
->root
|| get_parent(y
)->_right
== y
889 || get_parent(y
)->_left
== y
);
890 assert(x
== rbtree
->root
|| get_parent(x
)->_right
== x
891 || get_parent(x
)->_left
== x
);
892 assert(rcu_rbtree_is_nil(rbtree
, x
->_left
) || get_parent(x
->_left
) == x
);
893 assert(rcu_rbtree_is_nil(rbtree
, x
->_right
) || get_parent(x
->_right
) == x
);
894 assert(rcu_rbtree_is_nil(rbtree
, y
->_left
) || get_parent(y
->_left
) == y
);
895 assert(rcu_rbtree_is_nil(rbtree
, y
->_right
) || get_parent(y
->_right
) == y
);
896 assert(!is_decay(rbtree
->root
));
897 assert(!is_decay(x
));
898 assert(!is_decay(y
));
899 assert(!is_decay(x
->_left
));
900 assert(!is_decay(x
->_right
));
901 assert(!is_decay(y
->_left
));
902 assert(!is_decay(y
->_right
));
903 check_max_end(rbtree
, x
);
904 check_max_end(rbtree
, y
);
905 check_max_end(rbtree
, get_parent(y
));
910 /* non-rcu version */
912 void right_rotate(struct rcu_rbtree
*rbtree
,
913 struct rcu_rbtree_node
*x
)
915 struct rcu_rbtree_node
*y
;
919 x
->_left
= y
->_right
;
920 if (!rcu_rbtree_is_nil(rbtree
, y
->_right
))
921 set_parent(y
->_right
, x
, IS_LEFT
);
922 set_parent(y
, get_parent(x
), get_pos(x
));
923 if (rcu_rbtree_is_nil(rbtree
, get_parent(x
)))
925 else if (x
== get_parent(x
)->_right
) {
926 get_parent(x
)->_right
= y
;
928 get_parent(x
)->_left
= y
;
931 set_parent(x
, y
, IS_RIGHT
);
934 * We only changed the relative position of x and y wrt their
935 * children, and reparented y (but are keeping the same nodes in
936 * place, so its parent does not need to have end value
939 x
->max_end
= calculate_node_max_end(rbtree
, x
);
940 y
->max_end
= calculate_node_max_end(rbtree
, y
);
947 static void rcu_rbtree_insert_fixup(struct rcu_rbtree
*rbtree
,
948 struct rcu_rbtree_node
*z
)
950 struct rcu_rbtree_node
*y
;
952 dbg_printf("insert fixup %p\n", z
->begin
);
953 assert(!is_decay(rbtree
->root
));
955 while (get_parent(z
)->color
== COLOR_RED
) {
956 if (get_parent(z
) == get_parent(get_parent(z
))->_left
) {
957 y
= get_parent(get_parent(z
))->_right
;
958 if (y
->color
== COLOR_RED
) {
959 get_parent(z
)->color
= COLOR_BLACK
;
960 y
->color
= COLOR_BLACK
;
961 get_parent(get_parent(z
))->color
= COLOR_RED
;
962 z
= get_parent(get_parent(z
));
964 if (z
== get_parent(z
)->_right
) {
966 left_rotate(rbtree
, z
);
968 assert(!is_decay(rbtree
->root
));
970 get_parent(z
)->color
= COLOR_BLACK
;
971 get_parent(get_parent(z
))->color
= COLOR_RED
;
972 assert(!is_decay(z
));
973 assert(!is_decay(get_parent(z
)));
974 assert(!is_decay(get_parent(get_parent(z
))));
975 right_rotate(rbtree
, get_parent(get_parent(z
)));
976 assert(!is_decay(z
));
977 assert(!is_decay(rbtree
->root
));
980 y
= get_parent(get_parent(z
))->_left
;
981 if (y
->color
== COLOR_RED
) {
982 get_parent(z
)->color
= COLOR_BLACK
;
983 y
->color
= COLOR_BLACK
;
984 get_parent(get_parent(z
))->color
= COLOR_RED
;
985 z
= get_parent(get_parent(z
));
987 if (z
== get_parent(z
)->_left
) {
989 right_rotate(rbtree
, z
);
991 assert(!is_decay(rbtree
->root
));
993 get_parent(z
)->color
= COLOR_BLACK
;
994 get_parent(get_parent(z
))->color
= COLOR_RED
;
995 left_rotate(rbtree
, get_parent(get_parent(z
)));
996 assert(!is_decay(z
));
997 assert(!is_decay(rbtree
->root
));
1001 rbtree
->root
->color
= COLOR_BLACK
;
1005 * rcu_rbtree_insert - Insert a node in the RCU rbtree
1007 * Returns 0 on success, or < 0 on error.
1009 int rcu_rbtree_insert(struct rcu_rbtree
*rbtree
,
1010 void *begin
, void *end
)
1012 struct rcu_rbtree_node
*x
, *y
, *z
;
1014 z
= _rcu_rbtree_alloc_node(rbtree
);
1020 dbg_printf("insert %p\n", z
->begin
);
1021 assert(!is_decay(rbtree
->root
));
1023 y
= make_nil(rbtree
);
1025 while (!rcu_rbtree_is_nil(rbtree
, x
)) {
1027 if (rbtree
->comp(z
->begin
, x
->begin
) < 0)
1033 z
->_left
= make_nil(rbtree
);
1034 z
->_right
= make_nil(rbtree
);
1035 z
->color
= COLOR_RED
;
1036 z
->decay_next
= NULL
;
1037 z
->max_end
= z
->end
;
1040 if (rcu_rbtree_is_nil(rbtree
, y
)) {
1041 set_parent(z
, y
, IS_RIGHT
); /* pos arbitrary for root node */
1043 * Order stores to z (children/parents) before stores
1044 * that will make it visible to the rest of the tree.
1047 _CMM_STORE_SHARED(rbtree
->root
, z
);
1048 } else if (rbtree
->comp(z
->begin
, y
->begin
) < 0) {
1049 y
= dup_decay_node(rbtree
, y
);
1050 set_parent(z
, y
, IS_LEFT
);
1051 if (get_pos(z
) == IS_LEFT
)
1052 _CMM_STORE_SHARED(y
->_left
, z
);
1054 _CMM_STORE_SHARED(y
->_right
, z
);
1055 populate_node_end(rbtree
, y
, 1, NULL
);
1057 y
= dup_decay_node(rbtree
, y
);
1058 set_parent(z
, y
, IS_RIGHT
);
1059 if (get_pos(z
) == IS_LEFT
)
1060 _CMM_STORE_SHARED(y
->_left
, z
);
1062 _CMM_STORE_SHARED(y
->_right
, z
);
1063 populate_node_end(rbtree
, y
, 1, NULL
);
1065 rcu_rbtree_insert_fixup(rbtree
, z
);
1067 * Make sure to commit all _CMM_STORE_SHARED() for non-coherent caches.
1071 check_max_end(rbtree
, z
);
1072 check_max_end(rbtree
, y
);
1078 * Transplant v into u position.
1081 #ifdef RBTREE_RCU_SUPPORT_TRANSPLANT
1084 void rcu_rbtree_transplant(struct rcu_rbtree
*rbtree
,
1085 struct rcu_rbtree_node
*u
,
1086 struct rcu_rbtree_node
*v
,
1087 unsigned int copy_parents
,
1088 struct rcu_rbtree_node
*stop
)
1090 dbg_printf("transplant %p\n", v
->begin
);
1092 if (!rcu_rbtree_is_nil(rbtree
, v
))
1093 v
= dup_decay_node(rbtree
, v
);
1095 if (rcu_rbtree_is_nil(rbtree
, get_parent(u
))) {
1096 /* pos is arbitrary for root node */
1097 set_parent(v
, get_parent(u
), IS_RIGHT
);
1098 cmm_smp_wmb(); /* write into node before publish */
1099 _CMM_STORE_SHARED(rbtree
->root
, v
);
1101 struct rcu_rbtree_node
*vp
;
1105 vp
= dup_decay_node(rbtree
, vp
);
1106 set_parent(v
, vp
, get_pos(u
));
1107 if (get_pos(v
) == IS_LEFT
)
1108 _CMM_STORE_SHARED(vp
->_left
, v
);
1110 _CMM_STORE_SHARED(vp
->_right
, v
);
1111 populate_node_end(rbtree
, vp
, copy_parents
, stop
);
1112 check_max_end(rbtree
, vp
);
1115 /* Point children to new copy (parent only used by updates/next/prev) */
1116 if (!rcu_rbtree_is_nil(rbtree
, v
)) {
1117 set_parent(v
->_right
, get_decay(get_parent(v
->_right
)),
1118 get_pos(v
->_right
));
1119 set_parent(v
->_left
, get_decay(get_parent(v
->_left
)),
1122 assert(!is_decay(rbtree
->root
));
1123 check_max_end(rbtree
, v
);
1128 /* Non-RCU version */
1130 void rcu_rbtree_transplant(struct rcu_rbtree
*rbtree
,
1131 struct rcu_rbtree_node
*u
,
1132 struct rcu_rbtree_node
*v
,
1133 unsigned int copy_parents
,
1134 struct rcu_rbtree_node
*stop
)
1136 dbg_printf("transplant %p\n", v
->begin
);
1139 if (rcu_rbtree_is_nil(rbtree
, get_parent(u
))) {
1142 if (u
== get_parent(u
)->_left
)
1143 get_parent(u
)->_left
= v
;
1145 get_parent(u
)->_right
= v
;
1146 populate_node_end(rbtree
, get_parent(u
), copy_parents
, stop
);
1148 set_parent(v
, get_parent(u
), get_pos(u
));
1149 unlock_test_mutex();
1154 static void rcu_rbtree_remove_fixup(struct rcu_rbtree
*rbtree
,
1155 struct rcu_rbtree_node
*x
)
1157 dbg_printf("remove fixup %p\n", x
->begin
);
1159 while (x
!= rbtree
->root
&& x
->color
== COLOR_BLACK
) {
1160 assert(!is_decay(get_parent(x
)));
1161 assert(!is_decay(get_parent(x
)->_left
));
1162 if (x
== get_parent(x
)->_left
) {
1163 struct rcu_rbtree_node
*w
;
1165 w
= get_parent(x
)->_right
;
1167 if (w
->color
== COLOR_RED
) {
1168 w
->color
= COLOR_BLACK
;
1169 get_parent(x
)->color
= COLOR_RED
;
1170 left_rotate(rbtree
, get_parent(x
));
1172 assert(!is_decay(rbtree
->root
));
1173 w
= get_parent(x
)->_right
;
1175 if (w
->_left
->color
== COLOR_BLACK
1176 && w
->_right
->color
== COLOR_BLACK
) {
1177 w
->color
= COLOR_RED
;
1179 assert(!is_decay(rbtree
->root
));
1180 assert(!is_decay(x
));
1182 if (w
->_right
->color
== COLOR_BLACK
) {
1183 w
->_left
->color
= COLOR_BLACK
;
1184 w
->color
= COLOR_RED
;
1185 right_rotate(rbtree
, w
);
1186 assert(!is_decay(rbtree
->root
));
1188 w
= get_parent(x
)->_right
;
1190 w
->color
= get_parent(x
)->color
;
1191 get_parent(x
)->color
= COLOR_BLACK
;
1192 w
->_right
->color
= COLOR_BLACK
;
1193 left_rotate(rbtree
, get_parent(x
));
1194 assert(!is_decay(rbtree
->root
));
1198 struct rcu_rbtree_node
*w
;
1200 w
= get_parent(x
)->_left
;
1202 if (w
->color
== COLOR_RED
) {
1203 w
->color
= COLOR_BLACK
;
1204 get_parent(x
)->color
= COLOR_RED
;
1205 right_rotate(rbtree
, get_parent(x
));
1206 assert(!is_decay(rbtree
->root
));
1208 w
= get_parent(x
)->_left
;
1210 if (w
->_right
->color
== COLOR_BLACK
1211 && w
->_left
->color
== COLOR_BLACK
) {
1212 w
->color
= COLOR_RED
;
1214 assert(!is_decay(rbtree
->root
));
1215 assert(!is_decay(x
));
1217 if (w
->_left
->color
== COLOR_BLACK
) {
1218 w
->_right
->color
= COLOR_BLACK
;
1219 w
->color
= COLOR_RED
;
1220 left_rotate(rbtree
, w
);
1221 assert(!is_decay(rbtree
->root
));
1223 w
= get_parent(x
)->_left
;
1225 w
->color
= get_parent(x
)->color
;
1226 get_parent(x
)->color
= COLOR_BLACK
;
1227 w
->_left
->color
= COLOR_BLACK
;
1228 right_rotate(rbtree
, get_parent(x
));
1229 assert(!is_decay(rbtree
->root
));
1234 x
->color
= COLOR_BLACK
;
1238 * Delete z. All non-copied children left/right positions are unchanged.
1241 void rcu_rbtree_remove_nonil(struct rcu_rbtree
*rbtree
,
1242 struct rcu_rbtree_node
*z
,
1243 struct rcu_rbtree_node
*y
)
1245 struct rcu_rbtree_node
*x
;
1247 dbg_printf("remove nonil %p\n", z
->begin
);
1250 assert(!is_decay(z
));
1251 assert(!is_decay(y
));
1252 assert(!is_decay(y
->_right
));
1253 assert(!is_decay(get_parent(y
)));
1255 assert(!is_decay(x
));
1256 if (get_parent(y
) == z
) {
1257 y
= dup_decay_node(rbtree
, y
);
1258 set_parent(x
, y
, get_pos(x
)); /* parent for nil */
1259 /* y is z's right node */
1260 set_left(rbtree
, y
, z
->_left
);
1261 y
->max_end
= calculate_node_max_end(rbtree
, y
);
1262 rcu_rbtree_transplant(rbtree
, z
, y
, 1, NULL
);
1264 struct rcu_rbtree_node
*oy_right
, *z_right
;
1267 * Need to make sure y is always visible by readers.
1269 y
= rcu_rbtree_min_dup_decay(rbtree
, z
->_right
, &z_right
);
1270 assert(!is_decay(y
));
1271 assert(!is_decay(z
));
1272 oy_right
= y
->_right
;
1275 * The max child begin of z_right does not change, because
1276 * we're only changing its left children.
1278 y
->_right
= z_right
;
1279 set_parent(y
->_right
, y
, IS_RIGHT
);
1280 assert(!is_decay(z
->_left
));
1281 y
->_left
= z
->_left
;
1282 assert(!is_decay(oy_right
));
1284 * Transplant of oy_right to old y's location will only
1285 * trigger a "end" value update of the already copied branch
1286 * (which is not visible yet). We are transplanting
1287 * oy_right as a left child of old y's parent, so the
1288 * min values update propagated upward necessarily stops
1291 rcu_rbtree_transplant(rbtree
, y
, oy_right
, 0, y
);
1292 y
->max_end
= calculate_node_max_end(rbtree
, y
);
1293 rcu_rbtree_transplant(rbtree
, z
, y
, 1, NULL
);
1294 /* Update children */
1295 (void) rcu_rbtree_min_update_decay(rbtree
, y
->_right
);
1298 assert(!is_decay(z
));
1299 assert(!is_decay(z
->_left
));
1300 y
->color
= z
->color
;
1301 set_parent(y
->_left
, y
, IS_LEFT
);
1302 set_parent(y
->_right
, get_decay(get_parent(y
->_right
)), IS_RIGHT
);
1303 assert(!is_decay(y
->_left
));
1304 assert(!is_decay(y
->_right
));
1307 int rcu_rbtree_remove(struct rcu_rbtree
*rbtree
,
1308 struct rcu_rbtree_node
*z
)
1310 struct rcu_rbtree_node
*x
, *y
;
1311 unsigned int y_original_color
;
1313 assert(!is_decay(rbtree
->root
));
1314 dbg_printf("remove %p\n", z
->begin
);
1317 assert(!is_decay(z
));
1319 y_original_color
= y
->color
;
1321 if (rcu_rbtree_is_nil(rbtree
, z
->_left
)) {
1322 rcu_rbtree_transplant(rbtree
, z
, z
->_right
, 1, NULL
);
1323 assert(!is_decay(z
));
1324 x
= get_decay(z
->_right
);
1326 } else if (rcu_rbtree_is_nil(rbtree
, z
->_right
)) {
1327 rcu_rbtree_transplant(rbtree
, z
, z
->_left
, 1, NULL
);
1328 assert(!is_decay(z
));
1329 x
= get_decay(z
->_left
);
1332 y
= rcu_rbtree_min(rbtree
, z
->_right
);
1333 assert(!is_decay(y
));
1334 y_original_color
= y
->color
;
1336 rcu_rbtree_remove_nonil(rbtree
, z
, y
);
1340 if (y_original_color
== COLOR_BLACK
)
1341 rcu_rbtree_remove_fixup(rbtree
, x
);
1343 check_max_end(rbtree
, x
);
1344 check_max_end(rbtree
, get_decay(y
));
1346 * Commit all _CMM_STORE_SHARED().
1349 #ifdef RBTREE_RCU_SUPPORT
1350 rbtree
->call_rcu(&z
->head
, _rcu_rbtree_free_node
);
1352 _rcu_rbtree_free_node(&z
->head
);