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