Commit | Line | Data |
---|---|---|
00131368 MD |
1 | /* |
2 | * urcu-rbtree.c | |
3 | * | |
4 | * Userspace RCU library - Red-Black Tree | |
5 | * | |
6 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
21 | * | |
22 | * Implementation of RCU-adapted data structures and operations based on the RB | |
23 | * tree algorithms found in chapter 12 of: | |
24 | * | |
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. | |
28 | */ | |
29 | ||
30 | #define _BSD_SOURCE | |
31 | #define _LGPL_SOURCE | |
32 | ||
33 | #include <stdio.h> | |
34 | #include <pthread.h> | |
f6e888c5 | 35 | #include <assert.h> |
00131368 MD |
36 | |
37 | #include <urcu-rbtree.h> | |
38 | #include <urcu-pointer.h> | |
39 | ||
40 | /* | |
41 | * TODO | |
42 | * Deal with memory allocation errors. | |
43 | * Can be ensured by reserving a pool of memory entries before doing the | |
44 | * insertion, which will have to be function of number of | |
45 | * transplantations/rotations required for the operation. | |
46 | */ | |
47 | ||
48 | /* Sentinel (bottom nodes). Don't care about p, left, right and key values */ | |
db5b1669 | 49 | struct rcu_rbtree_node rcu_rbtree_nil = { |
00131368 MD |
50 | .color = COLOR_BLACK, |
51 | }; | |
52 | ||
53 | /* | |
54 | * Iterative rbtree search. | |
55 | */ | |
56 | struct rcu_rbtree_node* rcu_rbtree_search(struct rcu_rbtree_node *x, | |
57 | void *k, rcu_rbtree_comp comp) | |
58 | { | |
59 | x = rcu_dereference(x); | |
60 | ||
a23deda7 | 61 | while (x != &rcu_rbtree_nil && k != x->key) { |
00131368 MD |
62 | if (k < x->key) |
63 | x = rcu_dereference(x->left); | |
64 | else | |
65 | x = rcu_dereference(x->right); | |
66 | } | |
67 | return x; | |
68 | } | |
69 | ||
70 | struct rcu_rbtree_node *rcu_rbtree_min(struct rcu_rbtree_node *x, | |
71 | rcu_rbtree_comp comp) | |
72 | { | |
73 | struct rcu_rbtree_node *xl; | |
74 | ||
75 | x = rcu_dereference(x); | |
76 | ||
a23deda7 | 77 | while ((xl = rcu_dereference(x->left)) != &rcu_rbtree_nil) |
00131368 MD |
78 | x = xl; |
79 | return x; | |
80 | } | |
81 | ||
82 | struct rcu_rbtree_node *rcu_rbtree_max(struct rcu_rbtree_node *x, | |
83 | rcu_rbtree_comp comp) | |
84 | { | |
85 | struct rcu_rbtree_node *xr; | |
86 | ||
87 | x = rcu_dereference(x); | |
88 | ||
a23deda7 | 89 | while ((xr = rcu_dereference(x->right)) != &rcu_rbtree_nil) |
00131368 MD |
90 | x = xr; |
91 | return x; | |
92 | } | |
93 | ||
94 | /* | |
95 | * next and prev need to have mutex held to ensure that parent pointer is | |
96 | * coherent. | |
97 | */ | |
98 | struct rcu_rbtree_node *rcu_rbtree_next(struct rcu_rbtree_node *x, | |
99 | rcu_rbtree_comp comp) | |
100 | { | |
6ad1f7f7 | 101 | struct rcu_rbtree_node *xr, *y, *yredir, *yr, *yrredir; |
00131368 MD |
102 | |
103 | x = rcu_dereference(x); | |
104 | ||
a23deda7 | 105 | if ((xr = rcu_dereference(x->right)) != &rcu_rbtree_nil) |
00131368 MD |
106 | return rcu_rbtree_min(xr, comp); |
107 | y = rcu_dereference(x->p); | |
f6e888c5 MD |
108 | while ((yredir = rcu_dereference(y->redir)) != NULL) |
109 | y = yredir; | |
6ad1f7f7 MD |
110 | for (;;) { |
111 | if (y == &rcu_rbtree_nil) | |
112 | break; | |
113 | yr = rcu_dereference(y->right); | |
114 | while ((yrredir = rcu_dereference(yr->redir)) != NULL) | |
115 | yr = yrredir; | |
116 | if (x != yr) | |
117 | break; | |
00131368 MD |
118 | x = y; |
119 | y = rcu_dereference(y->p); | |
f6e888c5 MD |
120 | while ((yredir = rcu_dereference(y->redir)) != NULL) |
121 | y = yredir; | |
00131368 MD |
122 | } |
123 | return y; | |
124 | } | |
125 | ||
126 | struct rcu_rbtree_node *rcu_rbtree_prev(struct rcu_rbtree_node *x, | |
127 | rcu_rbtree_comp comp) | |
128 | { | |
6ad1f7f7 | 129 | struct rcu_rbtree_node *xl, *y, *yredir, *yl, *ylredir;; |
00131368 MD |
130 | |
131 | x = rcu_dereference(x); | |
132 | ||
a23deda7 | 133 | if ((xl = rcu_dereference(x->left)) != &rcu_rbtree_nil) |
00131368 MD |
134 | return rcu_rbtree_max(xl, comp); |
135 | y = rcu_dereference(x->p); | |
f6e888c5 MD |
136 | while ((yredir = rcu_dereference(y->redir)) != NULL) |
137 | y = yredir; | |
6ad1f7f7 MD |
138 | for (;;) { |
139 | if (y == &rcu_rbtree_nil) | |
140 | break; | |
141 | yl = rcu_dereference(y->left); | |
142 | while ((ylredir = rcu_dereference(yl->redir)) != NULL) | |
143 | yl = ylredir; | |
144 | if (x != yl) | |
145 | break; | |
00131368 MD |
146 | x = y; |
147 | y = rcu_dereference(y->p); | |
f6e888c5 MD |
148 | while ((yredir = rcu_dereference(y->redir)) != NULL) |
149 | y = yredir; | |
00131368 MD |
150 | } |
151 | return y; | |
152 | } | |
153 | ||
154 | /* | |
155 | * We have to ensure these assumptions are correct for prev/next | |
156 | * traversal: | |
157 | * | |
158 | * with x being a right child, the assumption that: | |
159 | * x->p->right == x | |
160 | * or if x is a left child, the assumption that: | |
161 | * x->p->left == x | |
162 | * | |
163 | * This explains why we have to allocate a vc copy of the node for left_rotate, | |
164 | * right_rotate and transplant operations. | |
165 | * | |
166 | * We always ensure that the right/left child and correct parent is set in the | |
167 | * node copies *before* we reparent the children and make the upper-level point | |
168 | * to the copy. | |
169 | */ | |
170 | ||
171 | /* RCU: copy x and y, atomically point to new versions. GC old. */ | |
172 | /* Should be eventually followed by a smp_wmc() */ | |
173 | /* Returns the new x. Previous x->right references are changed to yc. */ | |
174 | static struct rcu_rbtree_node *left_rotate(struct rcu_rbtree_node **root, | |
175 | struct rcu_rbtree_node *x, | |
176 | rcu_rbtree_alloc rballoc, | |
177 | rcu_rbtree_free rbfree) | |
178 | { | |
179 | struct rcu_rbtree_node *xc, *y, *yc; | |
180 | ||
181 | y = x->right; | |
182 | ||
21cddea3 MD |
183 | if (x != &rcu_rbtree_nil) { |
184 | xc = rballoc(); | |
185 | *xc = *x; | |
a23deda7 MD |
186 | } |
187 | ||
188 | if (y != &rcu_rbtree_nil) { | |
189 | yc = rballoc(); | |
190 | *yc = *y; | |
191 | } | |
192 | ||
193 | /* Modify children and parents in the node copies */ | |
194 | if (x != &rcu_rbtree_nil) { | |
21cddea3 MD |
195 | xc->right = y->left; |
196 | xc->p = yc; | |
197 | } else | |
198 | xc = &rcu_rbtree_nil; | |
199 | ||
200 | if (y != &rcu_rbtree_nil) { | |
21cddea3 MD |
201 | yc->left = xc; |
202 | yc->p = x->p; | |
203 | } else | |
204 | yc = &rcu_rbtree_nil; | |
00131368 MD |
205 | |
206 | /* | |
207 | * Order stores to node copies (children/parents) before stores that | |
208 | * will make the copies visible to the rest of the tree. | |
209 | */ | |
210 | smp_wmb(); | |
211 | ||
f6e888c5 MD |
212 | /* |
213 | * redirect old nodes to new. | |
214 | */ | |
9a9703e6 MD |
215 | _STORE_SHARED(x->redir, xc); |
216 | _STORE_SHARED(y->redir, yc); | |
f6e888c5 MD |
217 | |
218 | /* | |
219 | * Ensure that redirections are visible before updating external | |
220 | * pointers. | |
221 | */ | |
222 | smp_wmb(); | |
223 | ||
00131368 | 224 | /* Make parents point to the copies */ |
db5b1669 | 225 | if (x->p == &rcu_rbtree_nil) |
00131368 MD |
226 | _STORE_SHARED(*root, yc); |
227 | else if (x == x->p->left) | |
228 | _STORE_SHARED(x->p->left, yc); | |
229 | else | |
230 | _STORE_SHARED(x->p->right, yc); | |
231 | ||
232 | /* Assign children parents to copies */ | |
21cddea3 MD |
233 | if (x != &rcu_rbtree_nil) { |
234 | _STORE_SHARED(xc->right->p, xc); | |
235 | _STORE_SHARED(xc->left->p, xc); | |
236 | defer_rcu(rbfree, x); | |
237 | } | |
238 | if (y != &rcu_rbtree_nil) { | |
239 | _STORE_SHARED(yc->right->p, yc); | |
240 | defer_rcu(rbfree, y); | |
241 | /* yc->left is xc, its parent is already set in node copy */ | |
242 | } | |
00131368 MD |
243 | return xc; |
244 | } | |
245 | ||
246 | #if 0 /* orig */ | |
247 | static void left_rotate(struct rcu_rbtree_node **root, | |
248 | struct rcu_rbtree_node *x, | |
249 | rcu_rbtree_alloc rballoc) | |
250 | { | |
251 | struct rcu_rbtree_node *y; | |
252 | ||
253 | y = x->right; | |
254 | x->right = y->left; | |
db5b1669 | 255 | if (y->left != &rcu_rbtree_nil) |
00131368 MD |
256 | y->left->p = x; |
257 | y->p = x->p; | |
db5b1669 | 258 | if (x->p == &rcu_rbtree_nil) |
00131368 MD |
259 | *root = y; |
260 | else if (x == x->p->left) | |
261 | x->p->left = y; | |
262 | else | |
263 | x->p->right = y; | |
264 | y->left = x; | |
265 | x->p = y; | |
266 | } | |
267 | #endif //0 | |
268 | ||
269 | /* RCU: copy x and y, atomically point to new versions. GC old. */ | |
270 | /* Should be eventually followed by a smp_wmc() */ | |
271 | /* Returns the new x. Previous x->left references are changed to yc. */ | |
272 | static struct rcu_rbtree_node *right_rotate(struct rcu_rbtree_node **root, | |
273 | struct rcu_rbtree_node *x, | |
274 | rcu_rbtree_alloc rballoc, | |
275 | rcu_rbtree_free rbfree) | |
276 | { | |
277 | struct rcu_rbtree_node *xc, *y, *yc; | |
278 | ||
279 | y = x->left; | |
280 | ||
21cddea3 MD |
281 | if (x != &rcu_rbtree_nil) { |
282 | xc = rballoc(); | |
283 | *xc = *x; | |
a23deda7 MD |
284 | } |
285 | ||
286 | if (y != &rcu_rbtree_nil) { | |
287 | yc = rballoc(); | |
288 | *yc = *y; | |
289 | } | |
290 | ||
291 | /* Modify children and parents in the node copies */ | |
292 | if (x != &rcu_rbtree_nil) { | |
21cddea3 MD |
293 | xc->left = y->right; |
294 | xc->p = yc; | |
295 | } else | |
296 | xc = &rcu_rbtree_nil; | |
297 | ||
298 | if (y != &rcu_rbtree_nil) { | |
21cddea3 MD |
299 | yc->right = xc; |
300 | yc->p = x->p; | |
301 | } else | |
302 | yc = &rcu_rbtree_nil; | |
00131368 MD |
303 | |
304 | /* | |
305 | * Order stores to node copies (children/parents) before stores that | |
306 | * will make the copies visible to the rest of the tree. | |
307 | */ | |
308 | smp_wmb(); | |
309 | ||
f6e888c5 MD |
310 | /* |
311 | * redirect old nodes to new. | |
312 | */ | |
9a9703e6 MD |
313 | _STORE_SHARED(x->redir, xc); |
314 | _STORE_SHARED(y->redir, yc); | |
f6e888c5 MD |
315 | |
316 | /* | |
317 | * Ensure that redirections are visible before updating external | |
318 | * pointers. | |
319 | */ | |
320 | smp_wmb(); | |
321 | ||
00131368 | 322 | /* Make parents point to the copies */ |
db5b1669 | 323 | if (x->p == &rcu_rbtree_nil) |
00131368 MD |
324 | _STORE_SHARED(*root, yc); |
325 | else if (x == x->p->right) | |
326 | _STORE_SHARED(x->p->right, yc); | |
327 | else | |
328 | _STORE_SHARED(x->p->left, yc); | |
329 | ||
330 | /* Assign children parents to copies */ | |
21cddea3 MD |
331 | if (x != &rcu_rbtree_nil) { |
332 | _STORE_SHARED(xc->left->p, xc); | |
333 | _STORE_SHARED(xc->right->p, xc); | |
334 | defer_rcu(rbfree, x); | |
335 | } | |
336 | if (y != &rcu_rbtree_nil) { | |
337 | _STORE_SHARED(yc->left->p, yc); | |
338 | defer_rcu(rbfree, y); | |
339 | /* yc->right is xc, its parent is already set in node copy */ | |
340 | } | |
00131368 MD |
341 | return xc; |
342 | } | |
343 | ||
344 | #if 0 //orig | |
345 | static void right_rotate(struct rcu_rbtree_node **root, | |
346 | struct rcu_rbtree_node *x, | |
347 | rcu_rbtree_alloc rballoc) | |
348 | { | |
349 | struct rcu_rbtree_node *y; | |
350 | ||
351 | y = x->left; | |
352 | x->left = y->right; | |
db5b1669 | 353 | if (y->right != &rcu_rbtree_nil) |
00131368 MD |
354 | y->right->p = x; |
355 | y->p = x->p; | |
db5b1669 | 356 | if (x->p == &rcu_rbtree_nil) |
00131368 MD |
357 | *root = y; |
358 | else if (x == x->p->right) | |
359 | x->p->right = y; | |
360 | else | |
361 | x->p->left = y; | |
362 | y->right = x; | |
363 | x->p = y; | |
364 | } | |
365 | #endif //0 | |
366 | ||
367 | static void rcu_rbtree_insert_fixup(struct rcu_rbtree_node **root, | |
368 | struct rcu_rbtree_node *z, | |
369 | rcu_rbtree_alloc rballoc, | |
370 | rcu_rbtree_free rbfree) | |
371 | { | |
372 | struct rcu_rbtree_node *y; | |
373 | ||
374 | while (z->p->color == COLOR_RED) { | |
375 | if (z->p == z->p->p->left) { | |
376 | y = z->p->p->right; | |
377 | if (y->color == COLOR_RED) { | |
378 | z->p->color = COLOR_BLACK; | |
379 | y->color = COLOR_BLACK; | |
380 | z->p->p->color = COLOR_RED; | |
381 | z = z->p->p; | |
382 | } else { | |
383 | if (z == z->p->right) { | |
384 | z = z->p; | |
385 | z = left_rotate(root, z, | |
386 | rballoc, rbfree); | |
387 | } | |
388 | z->p->color = COLOR_BLACK; | |
389 | z->p->p->color = COLOR_RED; | |
a23deda7 | 390 | right_rotate(root, z->p->p, rballoc, rbfree); |
00131368 MD |
391 | } |
392 | } else { | |
393 | y = z->p->p->left; | |
394 | if (y->color == COLOR_RED) { | |
395 | z->p->color = COLOR_BLACK; | |
396 | y->color = COLOR_BLACK; | |
397 | z->p->p->color = COLOR_RED; | |
398 | z = z->p->p; | |
399 | } else { | |
400 | if (z == z->p->left) { | |
401 | z = z->p; | |
402 | z = right_rotate(root, z, | |
403 | rballoc, rbfree); | |
404 | } | |
405 | z->p->color = COLOR_BLACK; | |
406 | z->p->p->color = COLOR_RED; | |
a23deda7 | 407 | left_rotate(root, z->p->p, rballoc, rbfree); |
00131368 MD |
408 | } |
409 | } | |
410 | } | |
411 | (*root)->color = COLOR_BLACK; | |
412 | } | |
413 | ||
414 | /* | |
415 | * rcu_rbtree_insert - Insert a node in the RCU rbtree | |
416 | * | |
417 | * Returns 0 on success, or < 0 on error. | |
418 | */ | |
419 | int rcu_rbtree_insert(struct rcu_rbtree_node **root, | |
420 | struct rcu_rbtree_node *z, | |
421 | rcu_rbtree_comp comp, | |
422 | rcu_rbtree_alloc rballoc, | |
423 | rcu_rbtree_free rbfree) | |
424 | { | |
425 | struct rcu_rbtree_node *x, *y; | |
426 | ||
db5b1669 | 427 | y = &rcu_rbtree_nil; |
00131368 MD |
428 | x = *root; |
429 | ||
db5b1669 | 430 | while (x != &rcu_rbtree_nil) { |
00131368 MD |
431 | y = x; |
432 | if (comp(z->key, x->key) < 0) | |
433 | x = x->left; | |
434 | else | |
435 | x = x->right; | |
436 | } | |
437 | ||
438 | z->p = y; | |
db5b1669 MD |
439 | z->left = &rcu_rbtree_nil; |
440 | z->right = &rcu_rbtree_nil; | |
00131368 | 441 | z->color = COLOR_RED; |
f6e888c5 | 442 | z->redir = NULL; |
00131368 MD |
443 | |
444 | /* | |
445 | * Order stores to z (children/parents) before stores that will make it | |
446 | * visible to the rest of the tree. | |
447 | */ | |
448 | smp_wmb(); | |
449 | ||
db5b1669 | 450 | if (y == &rcu_rbtree_nil) |
00131368 MD |
451 | _STORE_SHARED(*root, z); |
452 | else if (comp(z->key, y->key) < 0) | |
453 | _STORE_SHARED(y->left, z); | |
454 | else | |
455 | _STORE_SHARED(y->right, z); | |
456 | rcu_rbtree_insert_fixup(root, z, rballoc, rbfree); | |
457 | /* | |
458 | * Make sure to commit all _STORE_SHARED() for non-coherent caches. | |
459 | */ | |
460 | smp_wmc(); | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | /* | |
466 | * Transplant v into u position. | |
467 | * Returns new copy of v. | |
468 | */ | |
469 | static struct rcu_rbtree_node * | |
470 | rcu_rbtree_transplant(struct rcu_rbtree_node **root, | |
471 | struct rcu_rbtree_node *u, | |
472 | struct rcu_rbtree_node *v, | |
473 | rcu_rbtree_alloc rballoc, | |
474 | rcu_rbtree_free rbfree) | |
475 | { | |
476 | struct rcu_rbtree_node *vc; | |
477 | ||
21cddea3 MD |
478 | if (v != &rcu_rbtree_nil) { |
479 | vc = rballoc(); | |
480 | *vc = *v; | |
00131368 | 481 | |
21cddea3 MD |
482 | /* Change vc parent pointer */ |
483 | vc->p = u->p; | |
00131368 | 484 | |
21cddea3 MD |
485 | /* |
486 | * Order stores to node copies (children/parents) before stores | |
487 | * that will make the copies visible to the rest of the tree. | |
488 | */ | |
489 | smp_wmb(); | |
f6e888c5 MD |
490 | |
491 | /* | |
492 | * redirect old node to new. | |
493 | */ | |
9a9703e6 | 494 | _STORE_SHARED(v->redir, vc); |
f6e888c5 MD |
495 | |
496 | /* | |
497 | * Ensure that redirections are visible before updating external | |
498 | * pointers. | |
499 | */ | |
500 | smp_wmb(); | |
21cddea3 MD |
501 | } else { |
502 | vc = &rcu_rbtree_nil; | |
503 | } | |
00131368 MD |
504 | |
505 | /* Assign upper-level pointer to vc, replacing u. */ | |
db5b1669 | 506 | if (u->p == &rcu_rbtree_nil) |
00131368 MD |
507 | _STORE_SHARED(*root, vc); |
508 | else if (u == u->p->left) | |
509 | _STORE_SHARED(u->p->left, vc); | |
510 | else | |
511 | _STORE_SHARED(u->p->right, vc); | |
512 | ||
21cddea3 MD |
513 | if (v != &rcu_rbtree_nil) { |
514 | /* | |
515 | * The children pointers in vc are the same as v. We can | |
516 | * therefore reparent v's children to vc safely. | |
517 | */ | |
518 | _STORE_SHARED(vc->right->p, vc); | |
519 | _STORE_SHARED(vc->left->p, vc); | |
00131368 | 520 | |
21cddea3 MD |
521 | defer_rcu(rbfree, v); |
522 | } | |
00131368 MD |
523 | return vc; |
524 | } | |
525 | ||
526 | static void rcu_rbtree_remove_fixup(struct rcu_rbtree_node **root, | |
527 | struct rcu_rbtree_node *x, | |
528 | rcu_rbtree_alloc rballoc, | |
529 | rcu_rbtree_free rbfree) | |
530 | { | |
531 | while (x != *root && x->color == COLOR_BLACK) { | |
532 | if (x == x->p->left) { | |
f6e888c5 | 533 | struct rcu_rbtree_node *w, *t; |
00131368 MD |
534 | |
535 | w = x->p->right; | |
536 | if (w->color == COLOR_RED) { | |
537 | w->color == COLOR_BLACK; | |
538 | x->p->color = COLOR_RED; | |
f6e888c5 MD |
539 | t = left_rotate(root, x->p, rballoc, rbfree); |
540 | assert(x->p->left == t->left); | |
00131368 MD |
541 | /* x is a left node, not copied by rotation */ |
542 | w = x->p->right; | |
543 | } | |
544 | if (w->left->color == COLOR_BLACK | |
545 | && w->right->color == COLOR_BLACK) { | |
546 | w->color = COLOR_RED; | |
547 | x = x->p; | |
548 | } else { | |
549 | if (w->right->color == COLOR_BLACK) { | |
550 | w->left->color = COLOR_BLACK; | |
551 | w->color = COLOR_RED; | |
552 | right_rotate(root, w, rballoc, rbfree); | |
553 | w = x->p->right; | |
554 | } | |
555 | w->color = x->p->color; | |
556 | x->p->color = COLOR_BLACK; | |
557 | w->right->color = COLOR_BLACK; | |
558 | left_rotate(root, x->p, rballoc, rbfree); | |
559 | x = *root; | |
560 | } | |
561 | } else { | |
562 | struct rcu_rbtree_node *w; | |
563 | ||
564 | w = x->p->left; | |
565 | if (w->color == COLOR_RED) { | |
566 | w->color == COLOR_BLACK; | |
567 | x->p->color = COLOR_RED; | |
568 | right_rotate(root, x->p, rballoc, rbfree); | |
569 | /* x is a right node, not copied by rotation */ | |
570 | w = x->p->left; | |
571 | } | |
572 | if (w->right->color == COLOR_BLACK | |
573 | && w->left->color == COLOR_BLACK) { | |
574 | w->color = COLOR_RED; | |
575 | x = x->p; | |
576 | } else { | |
577 | if (w->left->color == COLOR_BLACK) { | |
578 | w->right->color = COLOR_BLACK; | |
579 | w->color = COLOR_RED; | |
580 | left_rotate(root, w, rballoc, rbfree); | |
581 | w = x->p->left; | |
582 | } | |
583 | w->color = x->p->color; | |
584 | x->p->color = COLOR_BLACK; | |
585 | w->left->color = COLOR_BLACK; | |
586 | right_rotate(root, x->p, rballoc, rbfree); | |
587 | x = *root; | |
588 | } | |
589 | } | |
590 | } | |
591 | x->color = COLOR_BLACK; | |
592 | } | |
593 | ||
594 | /* Returns the new copy of y->right */ | |
595 | static struct rcu_rbtree_node * | |
596 | rcu_rbtree_remove_nonil(struct rcu_rbtree_node **root, | |
597 | struct rcu_rbtree_node *z, | |
598 | struct rcu_rbtree_node *y, | |
599 | rcu_rbtree_comp comp, | |
600 | rcu_rbtree_alloc rballoc, | |
601 | rcu_rbtree_free rbfree) | |
602 | { | |
603 | struct rcu_rbtree_node *x, *xc, *yc; | |
604 | ||
605 | x = y->right; | |
606 | ||
a23deda7 MD |
607 | if (x != &rcu_rbtree_nil) { |
608 | xc = rballoc(); | |
609 | *xc = *x; | |
610 | } else | |
611 | xc = &rcu_rbtree_nil; | |
612 | ||
00131368 | 613 | yc = rballoc(); |
00131368 MD |
614 | *yc = *y; |
615 | ||
616 | /* Update parent and children pointers within copies */ | |
617 | if (y->p == z) | |
618 | xc->p = yc; | |
619 | else { | |
620 | /* Transplant y->right (xc) into y, within copy */ | |
621 | xc->p = y->p; | |
622 | /* But also change the right pointer of yc */ | |
623 | yc->right = z->right; | |
624 | } | |
625 | /* Transplant y into z, within copy */ | |
626 | yc->p = z->p; | |
627 | ||
628 | yc->left = z->left; | |
629 | yc->color = z->color; | |
630 | ||
631 | /* | |
632 | * Order stores to node copies (children/parents) before stores that | |
633 | * will make the copies visible to the rest of the tree. | |
634 | */ | |
635 | smp_wmb(); | |
636 | ||
f6e888c5 MD |
637 | /* |
638 | * redirect old nodes to new. | |
639 | */ | |
640 | if (x != &rcu_rbtree_nil) | |
9a9703e6 MD |
641 | _STORE_SHARED(x->redir, xc); |
642 | _STORE_SHARED(y->redir, yc); | |
f6e888c5 MD |
643 | |
644 | /* | |
645 | * Ensure that redirections are visible before updating external | |
646 | * pointers. | |
647 | */ | |
648 | smp_wmb(); | |
649 | ||
00131368 MD |
650 | /* Update external pointers */ |
651 | ||
652 | if (y->p != z) { | |
653 | /* Transplant y->right into y, external parent links */ | |
654 | ||
655 | /* Assign upper-level pointer to xc, replacing y. */ | |
db5b1669 | 656 | if (y->p == &rcu_rbtree_nil) |
00131368 MD |
657 | _STORE_SHARED(*root, xc); |
658 | else if (y == y->p->left) | |
659 | _STORE_SHARED(y->p->left, xc); | |
660 | else | |
661 | _STORE_SHARED(y->p->right, xc); | |
662 | } | |
663 | ||
664 | /* Transplant y into z, update external parent links */ | |
db5b1669 | 665 | if (z->p == &rcu_rbtree_nil) |
00131368 MD |
666 | _STORE_SHARED(*root, yc); |
667 | else if (z == z->p->left) | |
668 | _STORE_SHARED(z->p->left, yc); | |
669 | else | |
670 | _STORE_SHARED(z->p->right, yc); | |
671 | ||
a23deda7 MD |
672 | if (x != &rcu_rbtree_nil) { |
673 | /* Reparent xc's children to xc. */ | |
674 | _STORE_SHARED(xc->right->p, xc); | |
675 | _STORE_SHARED(xc->left->p, xc); | |
676 | defer_rcu(rbfree, x); | |
677 | } | |
678 | ||
00131368 MD |
679 | /* Reparent yc's children to yc */ |
680 | _STORE_SHARED(yc->right->p, yc); | |
681 | _STORE_SHARED(yc->left->p, yc); | |
00131368 MD |
682 | defer_rcu(rbfree, y); |
683 | ||
684 | return xc; | |
685 | } | |
686 | ||
687 | int rcu_rbtree_remove(struct rcu_rbtree_node **root, | |
688 | struct rcu_rbtree_node *z, | |
689 | rcu_rbtree_comp comp, | |
690 | rcu_rbtree_alloc rballoc, | |
691 | rcu_rbtree_free rbfree) | |
692 | { | |
693 | struct rcu_rbtree_node *x, *y; | |
694 | unsigned int y_original_color; | |
695 | ||
696 | y = z; | |
697 | y_original_color = y->color; | |
698 | ||
db5b1669 | 699 | if (z->left == &rcu_rbtree_nil) { |
00131368 | 700 | x = rcu_rbtree_transplant(root, z, z->right, rballoc, rbfree); |
db5b1669 | 701 | } else if (z->right == &rcu_rbtree_nil) { |
00131368 MD |
702 | x = rcu_rbtree_transplant(root, z, z->left, rballoc, rbfree); |
703 | } else { | |
704 | y = rcu_rbtree_min(z->right, comp); | |
705 | y_original_color = y->color; | |
706 | x = rcu_rbtree_remove_nonil(root, z, y, comp, rballoc, rbfree); | |
707 | } | |
708 | if (y_original_color == COLOR_BLACK) | |
709 | rcu_rbtree_remove_fixup(root, x, rballoc, rbfree); | |
710 | /* | |
711 | * Commit all _STORE_SHARED(). | |
712 | */ | |
713 | smp_wmc(); | |
714 | ||
715 | return 0; | |
716 | } |