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