7 * Userspace RCU library - Red-Black Tree
9 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Implementation of RCU-adapted data structures and operations based on the RB
26 * tree algorithms found in chapter 12 of:
28 * Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and
29 * Clifford Stein. Introduction to Algorithms, Third Edition. The MIT
30 * Press, September 2009.
34 #include <urcu-call-rcu.h>
43 * Node key comparison function.
44 * < 0 : a lower than b.
45 * > 0 : a greater than b.
48 typedef int (*rcu_rbtree_comp
)(void *a
, void *b
);
51 * Allocation and deletion functions.
53 typedef void *(*rcu_rbtree_alloc
)(size_t size
);
54 typedef void (*rcu_rbtree_free
)(void *ptr
);
57 * struct rcu_rbtree_node must be aligned at least on 2 bytes.
58 * Lowest bit reserved for position (left/right) in pointer to parent.
60 * Set "high" to key + 1 to insert single-value nodes.
62 struct rcu_rbtree_node
{
63 /* useful node information returned by search */
64 void *begin
; /* Start of range (inclusive) */
65 void *end
; /* range end (exclusive) */
66 /* internally reserved */
67 void *max_end
; /* max range end of node and children */
68 /* parent uses low bit for "0 -> is left, 1 -> is right" */
70 /* _left and _right must be updated with set_left(), set_right() */
71 struct rcu_rbtree_node
*_left
, *_right
;
72 struct rcu_rbtree_node
*decay_next
;
73 struct rcu_rbtree
*rbtree
;
74 struct rcu_head head
; /* For delayed free */
79 struct rcu_rbtree_node
*root
;
80 struct rcu_rbtree_node nil_node
;
82 rcu_rbtree_alloc rballoc
;
83 rcu_rbtree_free rbfree
;
84 void (*call_rcu
)(struct rcu_head
*head
,
85 void (*func
)(struct rcu_head
*head
));
88 #define DEFINE_RCU_RBTREE(x, _comp, _rballoc, _rbfree, _call_rcu) \
89 struct rcu_rbtree x = \
92 .rballoc = _rballoc, \
94 .call_rcu = _call_rcu, \
96 .color = COLOR_BLACK, \
98 .root = &(x).nil_node, \
102 * Each of the search primitive and "prev"/"next" iteration must be called with
103 * the RCU read-side lock held.
105 * Insertion and removal must be protected by a mutex. At the moment, insertion
106 * and removal use defer_rcu, so calling them with rcu read-lock held is
111 * Node insertion. Returns 0 on success. May fail with -ENOMEM.
112 * Caller must have exclusive write access and hold RCU read-side lock.
114 int rcu_rbtree_insert(struct rcu_rbtree
*rbtree
,
115 void *begin
, void *end
);
118 * Remove node from tree.
119 * Must wait for a grace period after removal before performing deletion of the
120 * node. Note: it is illegal to re-use the same node pointer passed to "insert"
121 * also to "remove", because it may have been copied and garbage-collected since
122 * the insertion. A "search" for the key in the tree should be done to get
124 * Returns 0 on success. May fail with -ENOMEM.
126 * Caller must have exclusive write access and hold RCU read-side lock
127 * across "search" and "remove".
129 int rcu_rbtree_remove(struct rcu_rbtree
*rbtree
,
130 struct rcu_rbtree_node
*node
);
135 * For all these read-side privimitives, RCU read-side lock must be held
136 * across the duration for which the search is done and the returned
137 * rbtree node is expected to be valid.
141 * Search point in range starting from node x (node x is typically the
142 * rbtree root node). Returns nil node if not found.
144 struct rcu_rbtree_node
*rcu_rbtree_search(struct rcu_rbtree
*rbtree
,
145 struct rcu_rbtree_node
*x
,
149 * Search range starting from node x (typically the rbtree root node).
150 * Returns the first range containing the range specified as parameters.
151 * Returns nil node if not found.
153 * Note: ranges in the rbtree should not partially overlap when this search
154 * range function is used. Otherwise, a range matching the low value (but not
155 * containing the high value) could hide a range that would match this query.
156 * It is OK for the ranges to overlap entirely though.
158 struct rcu_rbtree_node
*rcu_rbtree_search_range(struct rcu_rbtree
*rbtree
,
159 struct rcu_rbtree_node
*x
,
160 void *begin
, void *end
);
163 * Search exact range begin value starting from node x (typically rbtree
164 * root node). Returns nil node if not found.
165 * This function is only useful if you do not use the range feature at
166 * all and only care about range begin values.
168 struct rcu_rbtree_node
*rcu_rbtree_search_begin_key(struct rcu_rbtree
*rbtree
,
169 struct rcu_rbtree_node
*x
,
173 * Search for minimum node of the tree under node x.
175 struct rcu_rbtree_node
*rcu_rbtree_min(struct rcu_rbtree
*rbtree
,
176 struct rcu_rbtree_node
*x
);
179 * Search for maximum node of the tree under node x.
181 struct rcu_rbtree_node
*rcu_rbtree_max(struct rcu_rbtree
*rbtree
,
182 struct rcu_rbtree_node
*x
);
185 * Get next node after node x.
187 struct rcu_rbtree_node
*rcu_rbtree_next(struct rcu_rbtree
*rbtree
,
188 struct rcu_rbtree_node
*x
);
191 * Get previous node before node x.
193 struct rcu_rbtree_node
*rcu_rbtree_prev(struct rcu_rbtree
*rbtree
,
194 struct rcu_rbtree_node
*x
);
197 * Sentinel (bottom nodes).
198 * Don't care about p, left, right, pos and key values.
201 int rcu_rbtree_is_nil(struct rcu_rbtree
*rbtree
, struct rcu_rbtree_node
*node
)
203 return node
== &rbtree
->nil_node
;
206 #endif /* URCU_RBTREE_H */
This page took 0.03489 seconds and 4 git commands to generate.