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
;
165 /* Modify children and parents in the node copies */
172 * Order stores to node copies (children/parents) before stores that
173 * will make the copies visible to the rest of the tree.
177 /* Make parents point to the copies */
178 if (x
->p
== &rcu_rbtree_nil
)
179 _STORE_SHARED(*root
, yc
);
180 else if (x
== x
->p
->left
)
181 _STORE_SHARED(x
->p
->left
, yc
);
183 _STORE_SHARED(x
->p
->right
, yc
);
185 /* Assign children parents to copies */
186 _STORE_SHARED(xc
->right
->p
, xc
);
187 _STORE_SHARED(xc
->left
->p
, xc
);
188 _STORE_SHARED(yc
->right
->p
, yc
);
189 /* yc->left is xc, its parent is already set in node copy */
191 defer_rcu(rbfree
, x
);
192 defer_rcu(rbfree
, y
);
197 static void left_rotate(struct rcu_rbtree_node
**root
,
198 struct rcu_rbtree_node
*x
,
199 rcu_rbtree_alloc rballoc
)
201 struct rcu_rbtree_node
*y
;
205 if (y
->left
!= &rcu_rbtree_nil
)
208 if (x
->p
== &rcu_rbtree_nil
)
210 else if (x
== x
->p
->left
)
219 /* RCU: copy x and y, atomically point to new versions. GC old. */
220 /* Should be eventually followed by a smp_wmc() */
221 /* Returns the new x. Previous x->left references are changed to yc. */
222 static struct rcu_rbtree_node
*right_rotate(struct rcu_rbtree_node
**root
,
223 struct rcu_rbtree_node
*x
,
224 rcu_rbtree_alloc rballoc
,
225 rcu_rbtree_free rbfree
)
227 struct rcu_rbtree_node
*xc
, *y
, *yc
;
236 /* Modify children and parents in the node copies */
243 * Order stores to node copies (children/parents) before stores that
244 * will make the copies visible to the rest of the tree.
248 /* Make parents point to the copies */
249 if (x
->p
== &rcu_rbtree_nil
)
250 _STORE_SHARED(*root
, yc
);
251 else if (x
== x
->p
->right
)
252 _STORE_SHARED(x
->p
->right
, yc
);
254 _STORE_SHARED(x
->p
->left
, yc
);
256 /* Assign children parents to copies */
257 _STORE_SHARED(xc
->left
->p
, xc
);
258 _STORE_SHARED(xc
->right
->p
, xc
);
259 _STORE_SHARED(yc
->left
->p
, yc
);
260 /* yc->right is xc, its parent is already set in node copy */
262 defer_rcu(rbfree
, x
);
263 defer_rcu(rbfree
, y
);
268 static void right_rotate(struct rcu_rbtree_node
**root
,
269 struct rcu_rbtree_node
*x
,
270 rcu_rbtree_alloc rballoc
)
272 struct rcu_rbtree_node
*y
;
276 if (y
->right
!= &rcu_rbtree_nil
)
279 if (x
->p
== &rcu_rbtree_nil
)
281 else if (x
== x
->p
->right
)
290 static void rcu_rbtree_insert_fixup(struct rcu_rbtree_node
**root
,
291 struct rcu_rbtree_node
*z
,
292 rcu_rbtree_alloc rballoc
,
293 rcu_rbtree_free rbfree
)
295 struct rcu_rbtree_node
*y
;
297 while (z
->p
->color
== COLOR_RED
) {
298 if (z
->p
== z
->p
->p
->left
) {
300 if (y
->color
== COLOR_RED
) {
301 z
->p
->color
= COLOR_BLACK
;
302 y
->color
= COLOR_BLACK
;
303 z
->p
->p
->color
= COLOR_RED
;
306 if (z
== z
->p
->right
) {
308 z
= left_rotate(root
, z
,
311 z
->p
->color
= COLOR_BLACK
;
312 z
->p
->p
->color
= COLOR_RED
;
313 z
= right_rotate(root
, z
->p
->p
,
318 if (y
->color
== COLOR_RED
) {
319 z
->p
->color
= COLOR_BLACK
;
320 y
->color
= COLOR_BLACK
;
321 z
->p
->p
->color
= COLOR_RED
;
324 if (z
== z
->p
->left
) {
326 z
= right_rotate(root
, z
,
329 z
->p
->color
= COLOR_BLACK
;
330 z
->p
->p
->color
= COLOR_RED
;
331 z
= left_rotate(root
, z
->p
->p
,
336 (*root
)->color
= COLOR_BLACK
;
340 * rcu_rbtree_insert - Insert a node in the RCU rbtree
342 * Returns 0 on success, or < 0 on error.
344 int rcu_rbtree_insert(struct rcu_rbtree_node
**root
,
345 struct rcu_rbtree_node
*z
,
346 rcu_rbtree_comp comp
,
347 rcu_rbtree_alloc rballoc
,
348 rcu_rbtree_free rbfree
)
350 struct rcu_rbtree_node
*x
, *y
;
355 while (x
!= &rcu_rbtree_nil
) {
357 if (comp(z
->key
, x
->key
) < 0)
364 z
->left
= &rcu_rbtree_nil
;
365 z
->right
= &rcu_rbtree_nil
;
366 z
->color
= COLOR_RED
;
369 * Order stores to z (children/parents) before stores that will make it
370 * visible to the rest of the tree.
374 if (y
== &rcu_rbtree_nil
)
375 _STORE_SHARED(*root
, z
);
376 else if (comp(z
->key
, y
->key
) < 0)
377 _STORE_SHARED(y
->left
, z
);
379 _STORE_SHARED(y
->right
, z
);
380 rcu_rbtree_insert_fixup(root
, z
, rballoc
, rbfree
);
382 * Make sure to commit all _STORE_SHARED() for non-coherent caches.
390 * Transplant v into u position.
391 * Returns new copy of v.
393 static struct rcu_rbtree_node
*
394 rcu_rbtree_transplant(struct rcu_rbtree_node
**root
,
395 struct rcu_rbtree_node
*u
,
396 struct rcu_rbtree_node
*v
,
397 rcu_rbtree_alloc rballoc
,
398 rcu_rbtree_free rbfree
)
400 struct rcu_rbtree_node
*vc
;
405 /* Change vc parent pointer */
409 * Order stores to node copies (children/parents) before stores that
410 * will make the copies visible to the rest of the tree.
414 /* Assign upper-level pointer to vc, replacing u. */
415 if (u
->p
== &rcu_rbtree_nil
)
416 _STORE_SHARED(*root
, vc
);
417 else if (u
== u
->p
->left
)
418 _STORE_SHARED(u
->p
->left
, vc
);
420 _STORE_SHARED(u
->p
->right
, vc
);
423 * The children pointers in vc are the same as v. We can therefore
424 * reparent v's children to vc safely.
426 _STORE_SHARED(vc
->right
->p
, vc
);
427 _STORE_SHARED(vc
->left
->p
, vc
);
429 defer_rcu(rbfree
, v
);
433 static void rcu_rbtree_remove_fixup(struct rcu_rbtree_node
**root
,
434 struct rcu_rbtree_node
*x
,
435 rcu_rbtree_alloc rballoc
,
436 rcu_rbtree_free rbfree
)
438 while (x
!= *root
&& x
->color
== COLOR_BLACK
) {
439 if (x
== x
->p
->left
) {
440 struct rcu_rbtree_node
*w
;
443 if (w
->color
== COLOR_RED
) {
444 w
->color
== COLOR_BLACK
;
445 x
->p
->color
= COLOR_RED
;
446 left_rotate(root
, x
->p
, rballoc
, rbfree
);
447 /* x is a left node, not copied by rotation */
450 if (w
->left
->color
== COLOR_BLACK
451 && w
->right
->color
== COLOR_BLACK
) {
452 w
->color
= COLOR_RED
;
455 if (w
->right
->color
== COLOR_BLACK
) {
456 w
->left
->color
= COLOR_BLACK
;
457 w
->color
= COLOR_RED
;
458 right_rotate(root
, w
, rballoc
, rbfree
);
461 w
->color
= x
->p
->color
;
462 x
->p
->color
= COLOR_BLACK
;
463 w
->right
->color
= COLOR_BLACK
;
464 left_rotate(root
, x
->p
, rballoc
, rbfree
);
468 struct rcu_rbtree_node
*w
;
471 if (w
->color
== COLOR_RED
) {
472 w
->color
== COLOR_BLACK
;
473 x
->p
->color
= COLOR_RED
;
474 right_rotate(root
, x
->p
, rballoc
, rbfree
);
475 /* x is a right node, not copied by rotation */
478 if (w
->right
->color
== COLOR_BLACK
479 && w
->left
->color
== COLOR_BLACK
) {
480 w
->color
= COLOR_RED
;
483 if (w
->left
->color
== COLOR_BLACK
) {
484 w
->right
->color
= COLOR_BLACK
;
485 w
->color
= COLOR_RED
;
486 left_rotate(root
, w
, rballoc
, rbfree
);
489 w
->color
= x
->p
->color
;
490 x
->p
->color
= COLOR_BLACK
;
491 w
->left
->color
= COLOR_BLACK
;
492 right_rotate(root
, x
->p
, rballoc
, rbfree
);
497 x
->color
= COLOR_BLACK
;
500 /* Returns the new copy of y->right */
501 static struct rcu_rbtree_node
*
502 rcu_rbtree_remove_nonil(struct rcu_rbtree_node
**root
,
503 struct rcu_rbtree_node
*z
,
504 struct rcu_rbtree_node
*y
,
505 rcu_rbtree_comp comp
,
506 rcu_rbtree_alloc rballoc
,
507 rcu_rbtree_free rbfree
)
509 struct rcu_rbtree_node
*x
, *xc
, *yc
;
518 /* Update parent and children pointers within copies */
522 /* Transplant y->right (xc) into y, within copy */
524 /* But also change the right pointer of yc */
525 yc
->right
= z
->right
;
527 /* Transplant y into z, within copy */
531 yc
->color
= z
->color
;
534 * Order stores to node copies (children/parents) before stores that
535 * will make the copies visible to the rest of the tree.
539 /* Update external pointers */
542 /* Transplant y->right into y, external parent links */
544 /* Assign upper-level pointer to xc, replacing y. */
545 if (y
->p
== &rcu_rbtree_nil
)
546 _STORE_SHARED(*root
, xc
);
547 else if (y
== y
->p
->left
)
548 _STORE_SHARED(y
->p
->left
, xc
);
550 _STORE_SHARED(y
->p
->right
, xc
);
553 /* Transplant y into z, update external parent links */
554 if (z
->p
== &rcu_rbtree_nil
)
555 _STORE_SHARED(*root
, yc
);
556 else if (z
== z
->p
->left
)
557 _STORE_SHARED(z
->p
->left
, yc
);
559 _STORE_SHARED(z
->p
->right
, yc
);
561 /* Reparent xc's children to xc. */
562 _STORE_SHARED(xc
->right
->p
, xc
);
563 _STORE_SHARED(xc
->left
->p
, xc
);
564 /* Reparent yc's children to yc */
565 _STORE_SHARED(yc
->right
->p
, yc
);
566 _STORE_SHARED(yc
->left
->p
, yc
);
568 defer_rcu(rbfree
, x
);
569 defer_rcu(rbfree
, y
);
574 int rcu_rbtree_remove(struct rcu_rbtree_node
**root
,
575 struct rcu_rbtree_node
*z
,
576 rcu_rbtree_comp comp
,
577 rcu_rbtree_alloc rballoc
,
578 rcu_rbtree_free rbfree
)
580 struct rcu_rbtree_node
*x
, *y
;
581 unsigned int y_original_color
;
584 y_original_color
= y
->color
;
586 if (z
->left
== &rcu_rbtree_nil
) {
587 x
= rcu_rbtree_transplant(root
, z
, z
->right
, rballoc
, rbfree
);
588 } else if (z
->right
== &rcu_rbtree_nil
) {
589 x
= rcu_rbtree_transplant(root
, z
, z
->left
, rballoc
, rbfree
);
591 y
= rcu_rbtree_min(z
->right
, comp
);
592 y_original_color
= y
->color
;
593 x
= rcu_rbtree_remove_nonil(root
, z
, y
, comp
, rballoc
, rbfree
);
595 if (y_original_color
== COLOR_BLACK
)
596 rcu_rbtree_remove_fixup(root
, x
, rballoc
, rbfree
);
598 * Commit all _STORE_SHARED().