rcuja: derived work from HP Judy array
[userspace-rcu.git] / urcu / rcuja.h
CommitLineData
d553beef
MD
1#ifndef _URCU_RCUJA_H
2#define _URCU_RCUJA_H
3
4/*
5 * urcu/rcuja.h
6 *
7 * Userspace RCU library - RCU Judy Array
8 *
9 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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.
15 *
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.
20 *
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
24 *
25 * Include this file _after_ including your URCU flavor.
26 */
27
28#include <stdint.h>
29#include <urcu/compiler.h>
30#include <urcu-call-rcu.h>
31#include <urcu-flavor.h>
32#include <stdint.h>
33#include <urcu/rcuhlist.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
03ec1aeb
MD
39/*
40 * Duplicate nodes with the same key are chained into a singly-linked
41 * list. The last item of this list has a NULL next pointer.
42 */
d553beef 43struct cds_ja_node {
03ec1aeb 44 struct cds_ja_node *next;
d553beef
MD
45};
46
47struct cds_ja;
48
49/*
50 * cds_ja_node_init - initialize a judy array node
51 * @node: the node to initialize.
52 *
53 * This function is kept to be eventually used for debugging purposes
54 * (detection of memory corruption).
55 */
56static inline
57void cds_ja_node_init(struct cds_ja_node *node)
58{
59}
60
b41da4d1
MD
61/*
62 * cds_ja_lookup - look up by key.
63 * @ja: the Judy array.
64 * @key: key to look up.
65 *
66 * Returns the first node of a duplicate chain if a match is found, else
67 * returns NULL.
68 * A RCU read-side lock should be held across call to this function and
69 * use of its return value.
70 */
03ec1aeb 71struct cds_ja_node *cds_ja_lookup(struct cds_ja *ja, uint64_t key);
b41da4d1
MD
72
73/*
b023ba9f 74 * cds_ja_lookup_below_equal - look up first node with key <= @key.
b41da4d1
MD
75 * @ja: the Judy array.
76 * @key: key to look up.
36305a3d 77 * @result_key: key found.
b41da4d1
MD
78 *
79 * Returns the first node of a duplicate chain if a node is present in
b023ba9f 80 * the tree which has a key below or equal to @key, else returns NULL.
b41da4d1
MD
81 * A RCU read-side lock should be held across call to this function and
82 * use of its return value.
83 */
b023ba9f 84struct cds_ja_node *cds_ja_lookup_below_equal(struct cds_ja *ja,
36305a3d 85 uint64_t key, uint64_t *result_key);
b023ba9f
MD
86
87/*
88 * cds_ja_lookup_above_equal - look up first node with key >= @key.
89 * @ja: the Judy array.
90 * @key: key to look up.
36305a3d 91 * @result_key: key found.
b023ba9f
MD
92 *
93 * Returns the first node of a duplicate chain if a node is present in
94 * the tree which has a key above or equal to @key, else returns NULL.
95 * A RCU read-side lock should be held across call to this function and
96 * use of its return value.
97 */
98struct cds_ja_node *cds_ja_lookup_above_equal(struct cds_ja *ja,
36305a3d 99 uint64_t key, uint64_t *result_key);
d553beef 100
d8536f92
MD
101/*
102 * cds_ja_add - Add @node at @key, allowing duplicates.
103 * @ja: the Judy array.
104 * @key: key at which @node should be added.
105 * @node: node to add.
106 *
107 * Returns 0 on success, negative error value on error.
108 * A RCU read-side lock should be held across call to this function.
109 */
d553beef 110int cds_ja_add(struct cds_ja *ja, uint64_t key,
d8536f92 111 struct cds_ja_node *node);
d553beef 112
d8536f92
MD
113/*
114 * cds_ja_add_unique - Add @node at @key, without duplicates.
115 * @ja: the Judy array.
116 * @key: key at which @node should be added.
117 * @node: node to add.
118 *
119 * Returns @node if successfully added, else returns the already
120 * existing node (acts as a RCU lookup).
121 * A RCU read-side lock should be held across call to this function and
122 * use of its return value.
123 */
75d573aa 124struct cds_ja_node *cds_ja_add_unique(struct cds_ja *ja, uint64_t key,
d8536f92 125 struct cds_ja_node *node);
75d573aa 126
d8536f92
MD
127/*
128 * cds_ja_del - Remove @node at @key.
129 * @ja: the Judy array.
130 * @key: key at which @node is expected.
131 * @node: node to remove.
132 *
133 * Returns 0 on success, negative error value on error.
134 * A RCU read-side lock should be held across call to this function.
135 */
d553beef
MD
136int cds_ja_del(struct cds_ja *ja, uint64_t key,
137 struct cds_ja_node *node);
138
139struct cds_ja *_cds_ja_new(unsigned int key_bits,
140 const struct rcu_flavor_struct *flavor);
141
d8536f92
MD
142/*
143 * cds_ja_new - Create a Judy array.
144 * @key_bits: Number of bits for key.
145 *
146 * Returns non-NULL pointer on success, else NULL on error. @key_bits
147 * needs to be multiple of 8, either: 8, 16, 24, 32, 40, 48, 56, or 64.
148 */
d553beef
MD
149static inline
150struct cds_ja *cds_ja_new(unsigned int key_bits)
151{
152 return _cds_ja_new(key_bits, &rcu_flavor);
153}
154
d8536f92
MD
155/*
156 * cds_ja_destroy - Destroy a Judy array.
157 * @ja: the Judy array.
d8536f92
MD
158 *
159 * Returns 0 on success, negative error value on error.
dc0e9798
MD
160 * There should be no more concurrent add, delete, nor look-up performed
161 * on the Judy array while it is being destroyed (ensured by the caller).
125a6dae
MD
162 * RCU read-side lock should _not_ be held when calling this function,
163 * however, QSBR threads need to be online.
d8536f92 164 */
99e6e3dc 165int cds_ja_destroy(struct cds_ja *ja);
d553beef 166
03ec1aeb 167/*
c01cfe73
MD
168 * cds_ja_for_each_duplicate_rcu: Iterate through duplicates.
169 * @pos: struct cds_ja_node *, start of duplicate list and loop cursor.
170 *
03ec1aeb
MD
171 * Iterate through duplicates returned by cds_ja_lookup*()
172 * This must be done while rcu_read_lock() is held.
173 * Receives a struct cds_ja_node * as parameter, which is used as start
174 * of duplicate list and loop cursor.
c01cfe73 175 * _NOT_ safe against node removal within iteration.
03ec1aeb 176 */
99e6e3dc 177#define cds_ja_for_each_duplicate_rcu(pos) \
03ec1aeb
MD
178 for (; (pos) != NULL; (pos) = rcu_dereference((pos)->next))
179
c01cfe73 180/*
99e6e3dc 181 * cds_ja_for_each_duplicate_safe: Iterate through duplicates.
c01cfe73
MD
182 * @pos: struct cds_ja_node *, start of duplicate list and loop cursor.
183 * @p: struct cds_ja_node *, temporary pointer to next.
184 *
99e6e3dc 185 * Iterate through duplicates returned by cds_ja_lookup*().
c01cfe73 186 * Safe against node removal within iteration.
99e6e3dc 187 * This must be done while rcu_read_lock() is held.
c01cfe73
MD
188 */
189#define cds_ja_for_each_duplicate_safe_rcu(pos, p) \
190 for (; (pos) != NULL ? \
191 ((p) = rcu_dereference((pos)->next), 1) : 0; \
192 (pos) = (p))
193
194/*
195 * cds_ja_for_each_key_rcu: Iterate over all keys in ascending order.
196 * @ja: Judy array on which iteration should be done.
197 * @key: Key cursor, needs to be a uint64_t.
198 * @pos: struct cds_ja_node *, used as loop cursor.
199 *
200 * Iterate over all keys of a RCU Judy array (_not_ duplicates) in
201 * ascending order.
202 * This must be done while rcu_read_lock() is held.
203 * Safe against node removal during iteration.
204 */
205#define cds_ja_for_each_key_rcu(ja, key, pos) \
206 for ((key) = 0; \
207 ((pos) = cds_ja_lookup_above_equal(ja, key, &(key))); )
208
209/*
210 * cds_ja_for_each_key_prev_rcu: Iterate over all keys in descending order.
211 * @ja: Judy array on which iteration should be done.
212 * @key: Key cursor, needs to be a uint64_t.
213 * @pos: struct cds_ja_node *, used as loop cursor.
214 *
215 * Iterate over all keys of a RCU Judy array (_not_ duplicates) in
216 * descending order.
217 * This must be done while rcu_read_lock() is held.
218 * Safe against node removal during iteration.
219 */
220#define cds_ja_for_each_key_prev_rcu(ja, key, pos) \
221 for ((key) = UINT64_MAX; \
222 ((pos) = cds_ja_lookup_below_equal(ja, key, &(key))); )
223
d553beef
MD
224#ifdef __cplusplus
225}
226#endif
227
228#endif /* _URCU_RCUJA_H */
This page took 0.032334 seconds and 4 git commands to generate.