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.
36 #include <urcu-rbtree.h>
37 #include <urcu-pointer.h>
41 * Deal with memory allocation errors.
42 * Can be ensured by reserving a pool of memory entries before doing the
43 * insertion, which will have to be function of number of
44 * transplantations/rotations required for the operation.
47 /* Sentinel (bottom nodes). Don't care about p, left, right and key values */
48 struct rcu_rbtree_node rcu_rbtree_nil
= {
53 * Iterative rbtree search.
55 struct rcu_rbtree_node
* rcu_rbtree_search(struct rcu_rbtree_node
*x
,
56 void *k
, rcu_rbtree_comp comp
)
58 x
= rcu_dereference(x
);
60 while (x
!= NULL
&& k
!= x
->key
) {
62 x
= rcu_dereference(x
->left
);
64 x
= rcu_dereference(x
->right
);
69 struct rcu_rbtree_node
*rcu_rbtree_min(struct rcu_rbtree_node
*x
,
72 struct rcu_rbtree_node
*xl
;
74 x
= rcu_dereference(x
);
76 while ((xl
= rcu_dereference(x
->left
)) != NULL
)
81 struct rcu_rbtree_node
*rcu_rbtree_max(struct rcu_rbtree_node
*x
,
84 struct rcu_rbtree_node
*xr
;
86 x
= rcu_dereference(x
);
88 while ((xr
= rcu_dereference(x
->right
)) != NULL
)
94 * next and prev need to have mutex held to ensure that parent pointer is
97 struct rcu_rbtree_node
*rcu_rbtree_next(struct rcu_rbtree_node
*x
,
100 struct rcu_rbtree_node
*xr
, *y
;
102 x
= rcu_dereference(x
);
104 if ((xr
= rcu_dereference(x
->right
)) != NULL
)
105 return rcu_rbtree_min(xr
, comp
);
106 y
= rcu_dereference(x
->p
);
107 while (y
!= NULL
&& x
== rcu_dereference(y
->right
)) {
109 y
= rcu_dereference(y
->p
);
114 struct rcu_rbtree_node
*rcu_rbtree_prev(struct rcu_rbtree_node
*x
,
115 rcu_rbtree_comp comp
)
117 struct rcu_rbtree_node
*xl
, *y
;
119 x
= rcu_dereference(x
);
121 if ((xl
= rcu_dereference(x
->left
)) != NULL
)
122 return rcu_rbtree_max(xl
, comp
);
123 y
= rcu_dereference(x
->p
);
124 while (y
!= NULL
&& x
== rcu_dereference(y
->left
)) {
126 y
= rcu_dereference(y
->p
);
132 * We have to ensure these assumptions are correct for prev/next
135 * with x being a right child, the assumption that:
137 * or if x is a left child, the assumption that:
140 * This explains why we have to allocate a vc copy of the node for left_rotate,
141 * right_rotate and transplant operations.
143 * We always ensure that the right/left child and correct parent is set in the
144 * node copies *before* we reparent the children and make the upper-level point
148 /* RCU: copy x and y, atomically point to new versions. GC old. */
149 /* Should be eventually followed by a smp_wmc() */
150 /* Returns the new x. Previous x->right references are changed to yc. */
151 static struct rcu_rbtree_node
*left_rotate(struct rcu_rbtree_node
**root
,
152 struct rcu_rbtree_node
*x
,
153 rcu_rbtree_alloc rballoc
,
154 rcu_rbtree_free rbfree
)
156 struct rcu_rbtree_node
*xc
, *y
, *yc
;
160 if (x
!= &rcu_rbtree_nil
) {
163 /* Modify children and parents in the node copies */
167 xc
= &rcu_rbtree_nil
;
169 if (y
!= &rcu_rbtree_nil
) {
172 /* Modify children and parents in the node copies */
176 yc
= &rcu_rbtree_nil
;
179 * Order stores to node copies (children/parents) before stores that
180 * will make the copies visible to the rest of the tree.
184 /* Make parents point to the copies */
185 if (x
->p
== &rcu_rbtree_nil
)
186 _STORE_SHARED(*root
, yc
);
187 else if (x
== x
->p
->left
)
188 _STORE_SHARED(x
->p
->left
, yc
);
190 _STORE_SHARED(x
->p
->right
, yc
);
192 /* Assign children parents to copies */
193 if (x
!= &rcu_rbtree_nil
) {
194 _STORE_SHARED(xc
->right
->p
, xc
);
195 _STORE_SHARED(xc
->left
->p
, xc
);
196 defer_rcu(rbfree
, x
);
198 if (y
!= &rcu_rbtree_nil
) {
199 _STORE_SHARED(yc
->right
->p
, yc
);
200 defer_rcu(rbfree
, y
);
201 /* yc->left is xc, its parent is already set in node copy */
207 static void left_rotate(struct rcu_rbtree_node
**root
,
208 struct rcu_rbtree_node
*x
,
209 rcu_rbtree_alloc rballoc
)
211 struct rcu_rbtree_node
*y
;
215 if (y
->left
!= &rcu_rbtree_nil
)
218 if (x
->p
== &rcu_rbtree_nil
)
220 else if (x
== x
->p
->left
)
229 /* RCU: copy x and y, atomically point to new versions. GC old. */
230 /* Should be eventually followed by a smp_wmc() */
231 /* Returns the new x. Previous x->left references are changed to yc. */
232 static struct rcu_rbtree_node
*right_rotate(struct rcu_rbtree_node
**root
,
233 struct rcu_rbtree_node
*x
,
234 rcu_rbtree_alloc rballoc
,
235 rcu_rbtree_free rbfree
)
237 struct rcu_rbtree_node
*xc
, *y
, *yc
;
241 if (x
!= &rcu_rbtree_nil
) {
244 /* Modify children and parents in the node copies */
248 xc
= &rcu_rbtree_nil
;
250 if (y
!= &rcu_rbtree_nil
) {
253 /* Modify children and parents in the node copies */
257 yc
= &rcu_rbtree_nil
;
260 * Order stores to node copies (children/parents) before stores that
261 * will make the copies visible to the rest of the tree.
265 /* Make parents point to the copies */
266 if (x
->p
== &rcu_rbtree_nil
)
267 _STORE_SHARED(*root
, yc
);
268 else if (x
== x
->p
->right
)
269 _STORE_SHARED(x
->p
->right
, yc
);
271 _STORE_SHARED(x
->p
->left
, yc
);
273 /* Assign children parents to copies */
274 if (x
!= &rcu_rbtree_nil
) {
275 _STORE_SHARED(xc
->left
->p
, xc
);
276 _STORE_SHARED(xc
->right
->p
, xc
);
277 defer_rcu(rbfree
, x
);
279 if (y
!= &rcu_rbtree_nil
) {
280 _STORE_SHARED(yc
->left
->p
, yc
);
281 defer_rcu(rbfree
, y
);
282 /* yc->right is xc, its parent is already set in node copy */
288 static void right_rotate(struct rcu_rbtree_node
**root
,
289 struct rcu_rbtree_node
*x
,
290 rcu_rbtree_alloc rballoc
)
292 struct rcu_rbtree_node
*y
;
296 if (y
->right
!= &rcu_rbtree_nil
)
299 if (x
->p
== &rcu_rbtree_nil
)
301 else if (x
== x
->p
->right
)
310 static void rcu_rbtree_insert_fixup(struct rcu_rbtree_node
**root
,
311 struct rcu_rbtree_node
*z
,
312 rcu_rbtree_alloc rballoc
,
313 rcu_rbtree_free rbfree
)
315 struct rcu_rbtree_node
*y
;
317 while (z
->p
->color
== COLOR_RED
) {
318 if (z
->p
== z
->p
->p
->left
) {
320 if (y
->color
== COLOR_RED
) {
321 z
->p
->color
= COLOR_BLACK
;
322 y
->color
= COLOR_BLACK
;
323 z
->p
->p
->color
= COLOR_RED
;
326 if (z
== z
->p
->right
) {
328 z
= left_rotate(root
, z
,
331 z
->p
->color
= COLOR_BLACK
;
332 z
->p
->p
->color
= COLOR_RED
;
333 z
= right_rotate(root
, z
->p
->p
,
338 if (y
->color
== COLOR_RED
) {
339 z
->p
->color
= COLOR_BLACK
;
340 y
->color
= COLOR_BLACK
;
341 z
->p
->p
->color
= COLOR_RED
;
344 if (z
== z
->p
->left
) {
346 z
= right_rotate(root
, z
,
349 z
->p
->color
= COLOR_BLACK
;
350 z
->p
->p
->color
= COLOR_RED
;
351 z
= left_rotate(root
, z
->p
->p
,
356 (*root
)->color
= COLOR_BLACK
;
360 * rcu_rbtree_insert - Insert a node in the RCU rbtree
362 * Returns 0 on success, or < 0 on error.
364 int rcu_rbtree_insert(struct rcu_rbtree_node
**root
,
365 struct rcu_rbtree_node
*z
,
366 rcu_rbtree_comp comp
,
367 rcu_rbtree_alloc rballoc
,
368 rcu_rbtree_free rbfree
)
370 struct rcu_rbtree_node
*x
, *y
;
375 while (x
!= &rcu_rbtree_nil
) {
377 if (comp(z
->key
, x
->key
) < 0)
384 z
->left
= &rcu_rbtree_nil
;
385 z
->right
= &rcu_rbtree_nil
;
386 z
->color
= COLOR_RED
;
389 * Order stores to z (children/parents) before stores that will make it
390 * visible to the rest of the tree.
394 if (y
== &rcu_rbtree_nil
)
395 _STORE_SHARED(*root
, z
);
396 else if (comp(z
->key
, y
->key
) < 0)
397 _STORE_SHARED(y
->left
, z
);
399 _STORE_SHARED(y
->right
, z
);
400 rcu_rbtree_insert_fixup(root
, z
, rballoc
, rbfree
);
402 * Make sure to commit all _STORE_SHARED() for non-coherent caches.
410 * Transplant v into u position.
411 * Returns new copy of v.
413 static struct rcu_rbtree_node
*
414 rcu_rbtree_transplant(struct rcu_rbtree_node
**root
,
415 struct rcu_rbtree_node
*u
,
416 struct rcu_rbtree_node
*v
,
417 rcu_rbtree_alloc rballoc
,
418 rcu_rbtree_free rbfree
)
420 struct rcu_rbtree_node
*vc
;
422 if (v
!= &rcu_rbtree_nil
) {
426 /* Change vc parent pointer */
430 * Order stores to node copies (children/parents) before stores
431 * that will make the copies visible to the rest of the tree.
435 vc
= &rcu_rbtree_nil
;
438 /* Assign upper-level pointer to vc, replacing u. */
439 if (u
->p
== &rcu_rbtree_nil
)
440 _STORE_SHARED(*root
, vc
);
441 else if (u
== u
->p
->left
)
442 _STORE_SHARED(u
->p
->left
, vc
);
444 _STORE_SHARED(u
->p
->right
, vc
);
446 if (v
!= &rcu_rbtree_nil
) {
448 * The children pointers in vc are the same as v. We can
449 * therefore reparent v's children to vc safely.
451 _STORE_SHARED(vc
->right
->p
, vc
);
452 _STORE_SHARED(vc
->left
->p
, vc
);
454 defer_rcu(rbfree
, v
);
459 static void rcu_rbtree_remove_fixup(struct rcu_rbtree_node
**root
,
460 struct rcu_rbtree_node
*x
,
461 rcu_rbtree_alloc rballoc
,
462 rcu_rbtree_free rbfree
)
464 while (x
!= *root
&& x
->color
== COLOR_BLACK
) {
465 if (x
== x
->p
->left
) {
466 struct rcu_rbtree_node
*w
;
469 if (w
->color
== COLOR_RED
) {
470 w
->color
== COLOR_BLACK
;
471 x
->p
->color
= COLOR_RED
;
472 left_rotate(root
, x
->p
, rballoc
, rbfree
);
473 /* x is a left node, not copied by rotation */
476 if (w
->left
->color
== COLOR_BLACK
477 && w
->right
->color
== COLOR_BLACK
) {
478 w
->color
= COLOR_RED
;
481 if (w
->right
->color
== COLOR_BLACK
) {
482 w
->left
->color
= COLOR_BLACK
;
483 w
->color
= COLOR_RED
;
484 right_rotate(root
, w
, rballoc
, rbfree
);
487 w
->color
= x
->p
->color
;
488 x
->p
->color
= COLOR_BLACK
;
489 w
->right
->color
= COLOR_BLACK
;
490 left_rotate(root
, x
->p
, rballoc
, rbfree
);
494 struct rcu_rbtree_node
*w
;
497 if (w
->color
== COLOR_RED
) {
498 w
->color
== COLOR_BLACK
;
499 x
->p
->color
= COLOR_RED
;
500 right_rotate(root
, x
->p
, rballoc
, rbfree
);
501 /* x is a right node, not copied by rotation */
504 if (w
->right
->color
== COLOR_BLACK
505 && w
->left
->color
== COLOR_BLACK
) {
506 w
->color
= COLOR_RED
;
509 if (w
->left
->color
== COLOR_BLACK
) {
510 w
->right
->color
= COLOR_BLACK
;
511 w
->color
= COLOR_RED
;
512 left_rotate(root
, w
, rballoc
, rbfree
);
515 w
->color
= x
->p
->color
;
516 x
->p
->color
= COLOR_BLACK
;
517 w
->left
->color
= COLOR_BLACK
;
518 right_rotate(root
, x
->p
, rballoc
, rbfree
);
523 x
->color
= COLOR_BLACK
;
526 /* Returns the new copy of y->right */
527 static struct rcu_rbtree_node
*
528 rcu_rbtree_remove_nonil(struct rcu_rbtree_node
**root
,
529 struct rcu_rbtree_node
*z
,
530 struct rcu_rbtree_node
*y
,
531 rcu_rbtree_comp comp
,
532 rcu_rbtree_alloc rballoc
,
533 rcu_rbtree_free rbfree
)
535 struct rcu_rbtree_node
*x
, *xc
, *yc
;
544 /* Update parent and children pointers within copies */
548 /* Transplant y->right (xc) into y, within copy */
550 /* But also change the right pointer of yc */
551 yc
->right
= z
->right
;
553 /* Transplant y into z, within copy */
557 yc
->color
= z
->color
;
560 * Order stores to node copies (children/parents) before stores that
561 * will make the copies visible to the rest of the tree.
565 /* Update external pointers */
568 /* Transplant y->right into y, external parent links */
570 /* Assign upper-level pointer to xc, replacing y. */
571 if (y
->p
== &rcu_rbtree_nil
)
572 _STORE_SHARED(*root
, xc
);
573 else if (y
== y
->p
->left
)
574 _STORE_SHARED(y
->p
->left
, xc
);
576 _STORE_SHARED(y
->p
->right
, xc
);
579 /* Transplant y into z, update external parent links */
580 if (z
->p
== &rcu_rbtree_nil
)
581 _STORE_SHARED(*root
, yc
);
582 else if (z
== z
->p
->left
)
583 _STORE_SHARED(z
->p
->left
, yc
);
585 _STORE_SHARED(z
->p
->right
, yc
);
587 /* Reparent xc's children to xc. */
588 _STORE_SHARED(xc
->right
->p
, xc
);
589 _STORE_SHARED(xc
->left
->p
, xc
);
590 /* Reparent yc's children to yc */
591 _STORE_SHARED(yc
->right
->p
, yc
);
592 _STORE_SHARED(yc
->left
->p
, yc
);
594 defer_rcu(rbfree
, x
);
595 defer_rcu(rbfree
, y
);
600 int rcu_rbtree_remove(struct rcu_rbtree_node
**root
,
601 struct rcu_rbtree_node
*z
,
602 rcu_rbtree_comp comp
,
603 rcu_rbtree_alloc rballoc
,
604 rcu_rbtree_free rbfree
)
606 struct rcu_rbtree_node
*x
, *y
;
607 unsigned int y_original_color
;
610 y_original_color
= y
->color
;
612 if (z
->left
== &rcu_rbtree_nil
) {
613 x
= rcu_rbtree_transplant(root
, z
, z
->right
, rballoc
, rbfree
);
614 } else if (z
->right
== &rcu_rbtree_nil
) {
615 x
= rcu_rbtree_transplant(root
, z
, z
->left
, rballoc
, rbfree
);
617 y
= rcu_rbtree_min(z
->right
, comp
);
618 y_original_color
= y
->color
;
619 x
= rcu_rbtree_remove_nonil(root
, z
, y
, comp
, rballoc
, rbfree
);
621 if (y_original_color
== COLOR_BLACK
)
622 rcu_rbtree_remove_fixup(root
, x
, rballoc
, rbfree
);
624 * Commit all _STORE_SHARED().