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.
38 #include <urcu/rcurbtree.h>
39 #include <urcu-pointer.h>
40 #include <urcu-call-rcu.h>
45 #define dbg_printf(args...) printf(args)
47 #define dbg_printf(args...)
51 void set_decay(struct rcu_rbtree_node
*x
, struct rcu_rbtree_node
*xc
)
57 struct rcu_rbtree_node
*get_decay(struct rcu_rbtree_node
*x
)
65 struct rcu_rbtree_node
*is_decay(struct rcu_rbtree_node
*x
)
71 struct rcu_rbtree_node
*dup_decay_node(struct rcu_rbtree
*rbtree
,
72 struct rcu_rbtree_node
*x
)
74 struct rcu_rbtree_node
*xc
;
76 if (rcu_rbtree_is_nil(x
))
79 xc
= rbtree
->rballoc();
80 memcpy(xc
, x
, sizeof(struct rcu_rbtree_node
));
81 xc
->decay_next
= NULL
;
83 call_rcu(&x
->head
, rbtree
->rbfree
);
89 * Deal with memory allocation errors.
90 * Can be ensured by reserving a pool of memory entries before doing the
91 * insertion, which will have to be function of number of
92 * transplantations/rotations required for the operation.
96 void show_tree(struct rcu_rbtree
*rbtree
)
98 struct rcu_rbtree_node
*node
;
100 node
= rcu_rbtree_min(rbtree
, rbtree
->root
);
101 while (!rcu_rbtree_is_nil(node
)) {
102 assert(!is_decay(node
));
103 printf("{ 0x%lX p:%lX r:%lX l:%lX %s %s %s} ",
104 (unsigned long)node
->key
,
105 (unsigned long) node
->p
->key
,
106 (unsigned long) node
->right
->key
,
107 (unsigned long) node
->left
->key
,
108 node
->color
? "red" : "black",
109 node
->pos
? "right" : "left",
110 node
->nil
? "nil" : "");
111 node
= rcu_rbtree_next(rbtree
, node
);
117 struct rcu_rbtree_node
*make_nil(struct rcu_rbtree
*rbtree
)
119 return &rbtree
->nil_node
;
123 * Iterative rbtree search.
125 struct rcu_rbtree_node
* rcu_rbtree_search(struct rcu_rbtree
*rbtree
,
126 struct rcu_rbtree_node
*x
,
129 x
= rcu_dereference(x
);
131 while (!rcu_rbtree_is_nil(x
) && k
!= x
->key
) {
132 if (rbtree
->comp(k
, x
->key
) < 0)
133 x
= rcu_dereference(x
->left
);
135 x
= rcu_dereference(x
->right
);
140 struct rcu_rbtree_node
*rcu_rbtree_min(struct rcu_rbtree
*rbtree
,
141 struct rcu_rbtree_node
*x
)
143 struct rcu_rbtree_node
*xl
;
145 x
= rcu_dereference(x
);
147 if (rcu_rbtree_is_nil(x
))
150 while (!rcu_rbtree_is_nil(xl
= rcu_dereference(x
->left
)))
155 struct rcu_rbtree_node
*rcu_rbtree_max(struct rcu_rbtree
*rbtree
,
156 struct rcu_rbtree_node
*x
)
158 struct rcu_rbtree_node
*xr
;
160 x
= rcu_dereference(x
);
162 if (rcu_rbtree_is_nil(x
))
165 while (!rcu_rbtree_is_nil(xr
= rcu_dereference(x
->right
)))
172 * Updates should wait for a grace period between update of the
173 * redirect pointer and update of the parent child pointer. This is to make sure
174 * that no reference to the old entry exist.
178 * next and prev need to have mutex held to ensure that parent pointer is
181 struct rcu_rbtree_node
*rcu_rbtree_next(struct rcu_rbtree
*rbtree
,
182 struct rcu_rbtree_node
*x
)
184 struct rcu_rbtree_node
*xr
, *y
;
186 x
= rcu_dereference(x
);
188 if (!rcu_rbtree_is_nil(xr
= rcu_dereference(x
->right
)))
189 return rcu_rbtree_min(rbtree
, xr
);
190 y
= rcu_dereference(x
->p
);
191 while (!rcu_rbtree_is_nil(y
) && x
->pos
== IS_RIGHT
) {
193 y
= rcu_dereference(y
->p
);
198 struct rcu_rbtree_node
*rcu_rbtree_prev(struct rcu_rbtree
*rbtree
,
199 struct rcu_rbtree_node
*x
)
201 struct rcu_rbtree_node
*xl
, *y
;
203 x
= rcu_dereference(x
);
205 if (!rcu_rbtree_is_nil(xl
= rcu_dereference(x
->left
)))
206 return rcu_rbtree_min(rbtree
, xl
);
207 y
= rcu_dereference(x
->p
);
208 while (!rcu_rbtree_is_nil(y
) && x
->pos
== IS_LEFT
) {
210 y
= rcu_dereference(y
->p
);
216 * We have to ensure these assumptions are correct for prev/next
219 * with x being a right child, the assumption that:
221 * or if x is a left child, the assumption that:
224 * This explains why we have to allocate a vc copy of the node for left_rotate,
225 * right_rotate and transplant operations.
227 * We always ensure that the right/left child and correct parent is set in the
228 * node copies *before* we reparent the children and make the upper-level point
232 /* RCU: copy x and y, atomically point to new versions. GC old. */
233 /* Should be eventually followed by a cmm_smp_wmc() */
236 void left_rotate(struct rcu_rbtree
*rbtree
,
237 struct rcu_rbtree_node
*x
)
239 struct rcu_rbtree_node
*y
;
243 /* Now operate on new copy, decay old versions */
244 x
= dup_decay_node(rbtree
, x
);
245 y
= dup_decay_node(rbtree
, y
);
248 if (!rcu_rbtree_is_nil(y
->left
)) {
250 y
->left
->pos
= IS_RIGHT
;
253 if (rcu_rbtree_is_nil(x
->p
))
255 else if (x
->pos
== IS_LEFT
) {
265 /* Point children to new copy */
266 if (!rcu_rbtree_is_nil(x
->left
))
268 if (!rcu_rbtree_is_nil(y
->right
))
272 assert(y
== rbtree
->root
|| y
->p
->left
== y
|| y
->p
->right
== y
);
273 assert(x
== rbtree
->root
|| x
->p
->left
== x
|| x
->p
->right
== x
);
274 assert(rcu_rbtree_is_nil(x
->right
) || x
->right
->p
== x
);
275 assert(rcu_rbtree_is_nil(x
->left
) || x
->left
->p
== x
);
276 assert(rcu_rbtree_is_nil(y
->right
) || y
->right
->p
== y
);
277 assert(rcu_rbtree_is_nil(y
->left
) || y
->left
->p
== y
);
278 assert(!is_decay(rbtree
->root
));
279 assert(!is_decay(x
));
280 assert(!is_decay(y
));
281 assert(!is_decay(x
->right
));
282 assert(!is_decay(x
->left
));
283 assert(!is_decay(y
->right
));
284 assert(!is_decay(y
->left
));
288 void right_rotate(struct rcu_rbtree
*rbtree
,
289 struct rcu_rbtree_node
*x
)
291 struct rcu_rbtree_node
*y
;
295 /* Now operate on new copy, decay old versions */
296 x
= dup_decay_node(rbtree
, x
);
297 y
= dup_decay_node(rbtree
, y
);
300 if (!rcu_rbtree_is_nil(y
->right
)) {
302 y
->right
->pos
= IS_LEFT
;
305 if (rcu_rbtree_is_nil(x
->p
))
307 else if (x
->pos
== IS_RIGHT
) {
317 /* Point children to new copy */
318 if (!rcu_rbtree_is_nil(x
->right
))
320 if (!rcu_rbtree_is_nil(y
->left
))
324 assert(y
== rbtree
->root
|| y
->p
->right
== y
|| y
->p
->left
== y
);
325 assert(x
== rbtree
->root
|| x
->p
->right
== x
|| x
->p
->left
== x
);
326 assert(rcu_rbtree_is_nil(x
->left
) || x
->left
->p
== x
);
327 assert(rcu_rbtree_is_nil(x
->right
) || x
->right
->p
== x
);
328 assert(rcu_rbtree_is_nil(y
->left
) || y
->left
->p
== y
);
329 assert(rcu_rbtree_is_nil(y
->right
) || y
->right
->p
== y
);
330 assert(!is_decay(rbtree
->root
));
331 assert(!is_decay(x
));
332 assert(!is_decay(y
));
333 assert(!is_decay(x
->left
));
334 assert(!is_decay(x
->right
));
335 assert(!is_decay(y
->left
));
336 assert(!is_decay(y
->right
));
339 static void rcu_rbtree_insert_fixup(struct rcu_rbtree
*rbtree
,
340 struct rcu_rbtree_node
*z
)
342 struct rcu_rbtree_node
*y
;
344 dbg_printf("insert fixup %p\n", z
->key
);
345 assert(!is_decay(rbtree
->root
));
347 while (z
->p
->color
== COLOR_RED
) {
348 if (z
->p
== z
->p
->p
->left
) {
350 if (y
->color
== COLOR_RED
) {
351 z
->p
->color
= COLOR_BLACK
;
352 y
->color
= COLOR_BLACK
;
353 z
->p
->p
->color
= COLOR_RED
;
356 if (z
== z
->p
->right
) {
358 left_rotate(rbtree
, z
);
360 assert(!is_decay(rbtree
->root
));
362 z
->p
->color
= COLOR_BLACK
;
363 z
->p
->p
->color
= COLOR_RED
;
364 assert(!is_decay(z
));
365 assert(!is_decay(z
->p
));
366 assert(!is_decay(z
->p
->p
));
367 right_rotate(rbtree
, z
->p
->p
);
368 assert(!is_decay(z
));
369 assert(!is_decay(rbtree
->root
));
373 if (y
->color
== COLOR_RED
) {
374 z
->p
->color
= COLOR_BLACK
;
375 y
->color
= COLOR_BLACK
;
376 z
->p
->p
->color
= COLOR_RED
;
379 if (z
== z
->p
->left
) {
381 right_rotate(rbtree
, z
);
383 assert(!is_decay(rbtree
->root
));
385 z
->p
->color
= COLOR_BLACK
;
386 z
->p
->p
->color
= COLOR_RED
;
387 left_rotate(rbtree
, z
->p
->p
);
388 assert(!is_decay(z
));
389 assert(!is_decay(rbtree
->root
));
393 rbtree
->root
->color
= COLOR_BLACK
;
397 * rcu_rbtree_insert - Insert a node in the RCU rbtree
399 * Returns 0 on success, or < 0 on error.
401 int rcu_rbtree_insert(struct rcu_rbtree
*rbtree
,
402 struct rcu_rbtree_node
*z
)
404 struct rcu_rbtree_node
*x
, *y
;
406 dbg_printf("insert %p\n", z
->key
);
407 assert(!is_decay(rbtree
->root
));
409 y
= make_nil(rbtree
);
411 rbtree
->root
= make_nil(rbtree
);
413 while (!rcu_rbtree_is_nil(x
)) {
415 if (rbtree
->comp(z
->key
, x
->key
) < 0)
423 z
->left
= make_nil(rbtree
);
424 z
->right
= make_nil(rbtree
);
425 z
->color
= COLOR_RED
;
427 z
->decay_next
= NULL
;
429 if (rcu_rbtree_is_nil(y
))
430 z
->pos
= IS_RIGHT
; /* arbitrary for root node */
431 else if (rbtree
->comp(z
->key
, y
->key
) < 0)
437 * Order stores to z (children/parents) before stores that will make it
438 * visible to the rest of the tree.
442 if (rcu_rbtree_is_nil(y
))
443 _CMM_STORE_SHARED(rbtree
->root
, z
);
444 else if (rbtree
->comp(z
->key
, y
->key
) < 0)
445 _CMM_STORE_SHARED(y
->left
, z
);
447 _CMM_STORE_SHARED(y
->right
, z
);
448 rcu_rbtree_insert_fixup(rbtree
, z
);
450 * Make sure to commit all _CMM_STORE_SHARED() for non-coherent caches.
459 * Transplant v into u position.
462 void rcu_rbtree_transplant(struct rcu_rbtree
*rbtree
,
463 struct rcu_rbtree_node
*u
,
464 struct rcu_rbtree_node
*v
)
466 dbg_printf("transplant %p\n", v
->key
);
468 if (!rcu_rbtree_is_nil(v
))
469 v
= dup_decay_node(rbtree
, v
);
471 if (rcu_rbtree_is_nil(u
->p
)) {
473 cmm_smp_wmb(); /* write into node before publish */
474 _CMM_STORE_SHARED(rbtree
->root
, v
);
475 } else if (u
== u
->p
->left
) {
478 cmm_smp_wmb(); /* write into node before publish */
479 _CMM_STORE_SHARED(u
->p
->left
, v
);
483 cmm_smp_wmb(); /* write into node before publish */
484 _CMM_STORE_SHARED(u
->p
->right
, v
);
486 /* Set children parent to new node */
487 if (!rcu_rbtree_is_nil(v
)) {
491 assert(!is_decay(rbtree
->root
));
494 static void rcu_rbtree_remove_fixup(struct rcu_rbtree
*rbtree
,
495 struct rcu_rbtree_node
*x
)
497 dbg_printf("remove fixup %p\n", x
->key
);
499 while (x
!= rbtree
->root
&& x
->color
== COLOR_BLACK
) {
500 assert(!is_decay(x
->p
));
501 assert(!is_decay(x
->p
->left
));
502 if (x
== x
->p
->left
) {
503 struct rcu_rbtree_node
*w
;
507 if (w
->color
== COLOR_RED
) {
508 w
->color
= COLOR_BLACK
;
509 x
->p
->color
= COLOR_RED
;
510 left_rotate(rbtree
, x
->p
);
512 assert(!is_decay(rbtree
->root
));
515 if (w
->left
->color
== COLOR_BLACK
516 && w
->right
->color
== COLOR_BLACK
) {
517 w
->color
= COLOR_RED
;
519 assert(!is_decay(rbtree
->root
));
520 assert(!is_decay(x
));
522 if (w
->right
->color
== COLOR_BLACK
) {
523 w
->left
->color
= COLOR_BLACK
;
524 w
->color
= COLOR_RED
;
525 right_rotate(rbtree
, w
);
526 assert(!is_decay(rbtree
->root
));
530 w
->color
= x
->p
->color
;
531 x
->p
->color
= COLOR_BLACK
;
532 w
->right
->color
= COLOR_BLACK
;
533 left_rotate(rbtree
, x
->p
);
534 assert(!is_decay(rbtree
->root
));
538 struct rcu_rbtree_node
*w
;
542 if (w
->color
== COLOR_RED
) {
543 w
->color
= COLOR_BLACK
;
544 x
->p
->color
= COLOR_RED
;
545 right_rotate(rbtree
, x
->p
);
546 assert(!is_decay(rbtree
->root
));
550 if (w
->right
->color
== COLOR_BLACK
551 && w
->left
->color
== COLOR_BLACK
) {
552 w
->color
= COLOR_RED
;
554 assert(!is_decay(rbtree
->root
));
555 assert(!is_decay(x
));
557 if (w
->left
->color
== COLOR_BLACK
) {
558 w
->right
->color
= COLOR_BLACK
;
559 w
->color
= COLOR_RED
;
560 left_rotate(rbtree
, w
);
561 assert(!is_decay(rbtree
->root
));
565 w
->color
= x
->p
->color
;
566 x
->p
->color
= COLOR_BLACK
;
567 w
->left
->color
= COLOR_BLACK
;
568 right_rotate(rbtree
, x
->p
);
569 assert(!is_decay(rbtree
->root
));
574 x
->color
= COLOR_BLACK
;
578 * Delete z. All non-copied children left/right positions are unchanged.
581 void rcu_rbtree_remove_nonil(struct rcu_rbtree
*rbtree
,
582 struct rcu_rbtree_node
*z
,
583 struct rcu_rbtree_node
*y
)
585 struct rcu_rbtree_node
*x
;
587 dbg_printf("remove nonil %p\n", z
->key
);
590 assert(!is_decay(z
));
591 assert(!is_decay(y
));
592 assert(!is_decay(y
->right
));
593 assert(!is_decay(y
->p
));
595 assert(!is_decay(x
));
599 rcu_rbtree_transplant(rbtree
, y
, y
->right
);
600 assert(!is_decay(y
));
601 assert(!is_decay(z
));
602 assert(!is_decay(z
->right
));
606 rcu_rbtree_transplant(rbtree
, z
, y
);
608 assert(!is_decay(z
));
609 assert(!is_decay(z
->left
));
615 int rcu_rbtree_remove(struct rcu_rbtree
*rbtree
,
616 struct rcu_rbtree_node
*z
)
618 struct rcu_rbtree_node
*x
, *y
;
619 unsigned int y_original_color
;
621 assert(!is_decay(rbtree
->root
));
622 dbg_printf("remove %p\n", z
->key
);
625 assert(!is_decay(z
));
627 y_original_color
= y
->color
;
629 if (rcu_rbtree_is_nil(z
->left
)) {
630 rcu_rbtree_transplant(rbtree
, z
, z
->right
);
631 assert(!is_decay(z
));
632 x
= get_decay(z
->right
);
634 } else if (rcu_rbtree_is_nil(z
->right
)) {
635 rcu_rbtree_transplant(rbtree
, z
, z
->left
);
636 assert(!is_decay(z
));
637 x
= get_decay(z
->left
);
640 y
= rcu_rbtree_min(rbtree
, z
->right
);
641 assert(!is_decay(y
));
642 y_original_color
= y
->color
;
643 rcu_rbtree_remove_nonil(rbtree
, z
, y
);
644 x
= get_decay(y
->right
);
647 if (y_original_color
== COLOR_BLACK
)
648 rcu_rbtree_remove_fixup(rbtree
, x
);
651 * Commit all _CMM_STORE_SHARED().
This page took 0.042526 seconds and 5 git commands to generate.