| 1 | #ifndef _URCU_RCULFHASH_H |
| 2 | #define _URCU_RCULFHASH_H |
| 3 | |
| 4 | /* |
| 5 | * urcu/rculfhash.h |
| 6 | * |
| 7 | * Userspace RCU library - Lock-Free RCU Hash Table |
| 8 | * |
| 9 | * Copyright 2011 - 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 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | /* |
| 37 | * cds_lfht_node: Contains the next pointers and reverse-hash |
| 38 | * value required for lookup and traversal of the hash table. |
| 39 | * |
| 40 | * struct cds_lfht_node should be aligned on 4-bytes boundaries because |
| 41 | * the two lower bits are used as flags. |
| 42 | * |
| 43 | * struct cds_lfht_node can be embedded into a structure (as a field). |
| 44 | * caa_container_of() can be used to get the structure from the struct |
| 45 | * cds_lfht_node after a lookup. |
| 46 | * |
| 47 | * The structure which embeds it typically holds the key (or key-value |
| 48 | * pair) of the object. The caller code is responsible for calculation |
| 49 | * of the hash value for cds_lfht APIs. |
| 50 | */ |
| 51 | struct cds_lfht_node { |
| 52 | struct cds_lfht_node *next; /* ptr | BUCKET_FLAG | REMOVED_FLAG */ |
| 53 | unsigned long reverse_hash; |
| 54 | } __attribute__((aligned(4))); |
| 55 | |
| 56 | /* cds_lfht_iter: Used to track state while traversing a hash chain. */ |
| 57 | struct cds_lfht_iter { |
| 58 | struct cds_lfht_node *node, *next; |
| 59 | }; |
| 60 | |
| 61 | static inline |
| 62 | struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) |
| 63 | { |
| 64 | return iter->node; |
| 65 | } |
| 66 | |
| 67 | struct cds_lfht; |
| 68 | |
| 69 | /* |
| 70 | * Caution ! |
| 71 | * Ensure reader and writer threads are registered as urcu readers. |
| 72 | */ |
| 73 | |
| 74 | typedef int (*cds_lfht_match_fct)(struct cds_lfht_node *node, void *key); |
| 75 | |
| 76 | /* |
| 77 | * cds_lfht_node_init - initialize a hash table node |
| 78 | * @node: the node to initialize. |
| 79 | * |
| 80 | * This function is kept to be eventually used for debugging purposes |
| 81 | * (detection of memory corruption). |
| 82 | */ |
| 83 | static inline |
| 84 | void cds_lfht_node_init(struct cds_lfht_node *node) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Hash table creation flags. |
| 90 | */ |
| 91 | enum { |
| 92 | CDS_LFHT_AUTO_RESIZE = (1U << 0), |
| 93 | CDS_LFHT_ACCOUNTING = (1U << 1), |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly. |
| 98 | */ |
| 99 | struct cds_lfht *_cds_lfht_new(unsigned long init_size, |
| 100 | unsigned long min_alloc_size, |
| 101 | int flags, |
| 102 | void (*cds_lfht_call_rcu)(struct rcu_head *head, |
| 103 | void (*func)(struct rcu_head *head)), |
| 104 | void (*cds_lfht_synchronize_rcu)(void), |
| 105 | void (*cds_lfht_rcu_read_lock)(void), |
| 106 | void (*cds_lfht_rcu_read_unlock)(void), |
| 107 | void (*cds_lfht_rcu_thread_offline)(void), |
| 108 | void (*cds_lfht_rcu_thread_online)(void), |
| 109 | void (*cds_lfht_rcu_register_thread)(void), |
| 110 | void (*cds_lfht_rcu_unregister_thread)(void), |
| 111 | pthread_attr_t *attr); |
| 112 | |
| 113 | /* |
| 114 | * cds_lfht_new - allocate a hash table. |
| 115 | * @init_size: number of nodes to allocate initially. Must be power of two. |
| 116 | * @min_alloc_size: the smallest allocation size to use. Must be power of two. |
| 117 | * @flags: hash table creation flags (can be combined with bitwise or: '|'). |
| 118 | * 0: no flags. |
| 119 | * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. |
| 120 | * @attr: optional resize worker thread attributes. NULL for default. |
| 121 | * |
| 122 | * Return NULL on error. |
| 123 | * Note: the RCU flavor must be already included before the hash table header. |
| 124 | * |
| 125 | * The programmer is responsible for ensuring that resize operation has a |
| 126 | * priority equal to hash table updater threads. It should be performed by |
| 127 | * specifying the appropriate priority in the pthread "attr" argument, and, |
| 128 | * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have |
| 129 | * this priority level. Having lower priority for call_rcu and resize threads |
| 130 | * does not pose any correctness issue, but the resize operations could be |
| 131 | * starved by updates, thus leading to long hash table bucket chains. |
| 132 | * Threads calling this API need to be registered RCU read-side threads. |
| 133 | */ |
| 134 | static inline |
| 135 | struct cds_lfht *cds_lfht_new(unsigned long init_size, |
| 136 | unsigned long min_alloc_size, |
| 137 | int flags, |
| 138 | pthread_attr_t *attr) |
| 139 | { |
| 140 | return _cds_lfht_new(init_size, min_alloc_size, flags, |
| 141 | call_rcu, synchronize_rcu, rcu_read_lock, |
| 142 | rcu_read_unlock, rcu_thread_offline, |
| 143 | rcu_thread_online, rcu_register_thread, |
| 144 | rcu_unregister_thread, attr); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * cds_lfht_destroy - destroy a hash table. |
| 149 | * @ht: the hash table to destroy. |
| 150 | * @attr: (output) resize worker thread attributes, as received by cds_lfht_new. |
| 151 | * The caller will typically want to free this pointer if dynamically |
| 152 | * allocated. The attr point can be NULL if the caller does not |
| 153 | * need to be informed of the value passed to cds_lfht_new(). |
| 154 | * |
| 155 | * Return 0 on success, negative error value on error. |
| 156 | * Threads calling this API need to be registered RCU read-side threads. |
| 157 | */ |
| 158 | int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); |
| 159 | |
| 160 | /* |
| 161 | * cds_lfht_count_nodes - count the number of nodes in the hash table. |
| 162 | * @ht: the hash table. |
| 163 | * @split_count_before: Sample the node count split-counter before traversal. |
| 164 | * @count: Traverse the hash table, count the number of nodes observed. |
| 165 | * @removed: Number of logically removed nodes observed during traversal. |
| 166 | * @split_count_after: Sample the node count split-counter after traversal. |
| 167 | * |
| 168 | * Call with rcu_read_lock held. |
| 169 | * Threads calling this API need to be registered RCU read-side threads. |
| 170 | */ |
| 171 | void cds_lfht_count_nodes(struct cds_lfht *ht, |
| 172 | long *split_count_before, |
| 173 | unsigned long *count, |
| 174 | unsigned long *removed, |
| 175 | long *split_count_after); |
| 176 | |
| 177 | /* |
| 178 | * cds_lfht_lookup - lookup a node by key. |
| 179 | * @ht: the hash table. |
| 180 | * @hash: the key hash. |
| 181 | * @match: the key match function. |
| 182 | * @key: the current node key. |
| 183 | * @iter: Node, if found (output). *iter->node set to NULL if not found. |
| 184 | * |
| 185 | * Call with rcu_read_lock held. |
| 186 | * Threads calling this API need to be registered RCU read-side threads. |
| 187 | */ |
| 188 | void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash, |
| 189 | cds_lfht_match_fct match, void *key, |
| 190 | struct cds_lfht_iter *iter); |
| 191 | |
| 192 | /* |
| 193 | * cds_lfht_next_duplicate - get the next item with same key (after a lookup). |
| 194 | * @ht: the hash table. |
| 195 | * @match: the key match function. |
| 196 | * @key: the current node key. |
| 197 | * @iter: Node, if found (output). *iter->node set to NULL if not found. |
| 198 | * |
| 199 | * Uses an iterator initialized by a lookup. |
| 200 | * Sets *iter-node to the following node with same key. |
| 201 | * Sets *iter->node to NULL if no following node exists with same key. |
| 202 | * RCU read-side lock must be held across cds_lfht_lookup and |
| 203 | * cds_lfht_next calls, and also between cds_lfht_next calls using the |
| 204 | * node returned by a previous cds_lfht_next. |
| 205 | * Call with rcu_read_lock held. |
| 206 | * Threads calling this API need to be registered RCU read-side threads. |
| 207 | */ |
| 208 | void cds_lfht_next_duplicate(struct cds_lfht *ht, |
| 209 | cds_lfht_match_fct match, void *key, |
| 210 | struct cds_lfht_iter *iter); |
| 211 | |
| 212 | /* |
| 213 | * cds_lfht_first - get the first node in the table. |
| 214 | * @ht: the hash table. |
| 215 | * @iter: First node, if exists (output). *iter->node set to NULL if not found. |
| 216 | * |
| 217 | * Output in "*iter". *iter->node set to NULL if table is empty. |
| 218 | * Call with rcu_read_lock held. |
| 219 | * Threads calling this API need to be registered RCU read-side threads. |
| 220 | */ |
| 221 | void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
| 222 | |
| 223 | /* |
| 224 | * cds_lfht_next - get the next node in the table. |
| 225 | * @ht: the hash table. |
| 226 | * @iter: Next node, if exists (output). *iter->node set to NULL if not found. |
| 227 | * |
| 228 | * Input/Output in "*iter". *iter->node set to NULL if *iter was |
| 229 | * pointing to the last table node. |
| 230 | * Call with rcu_read_lock held. |
| 231 | * Threads calling this API need to be registered RCU read-side threads. |
| 232 | */ |
| 233 | void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
| 234 | |
| 235 | /* |
| 236 | * cds_lfht_add - add a node to the hash table. |
| 237 | * @ht: the hash table. |
| 238 | * @hash: the key hash. |
| 239 | * @node: the node to add. |
| 240 | * |
| 241 | * This function supports adding redundant keys into the table. |
| 242 | * Call with rcu_read_lock held. |
| 243 | * Threads calling this API need to be registered RCU read-side threads. |
| 244 | */ |
| 245 | void cds_lfht_add(struct cds_lfht *ht, unsigned long hash, |
| 246 | struct cds_lfht_node *node); |
| 247 | |
| 248 | /* |
| 249 | * cds_lfht_add_unique - add a node to hash table, if key is not present. |
| 250 | * @ht: the hash table. |
| 251 | * @hash: the node's hash. |
| 252 | * @match: the key match function. |
| 253 | * @key: the node's key. |
| 254 | * @node: the node to try adding. |
| 255 | * |
| 256 | * Return the node added upon success. |
| 257 | * Return the unique node already present upon failure. If |
| 258 | * cds_lfht_add_unique fails, the node passed as parameter should be |
| 259 | * freed by the caller. |
| 260 | * Call with rcu_read_lock held. |
| 261 | * Threads calling this API need to be registered RCU read-side threads. |
| 262 | * |
| 263 | * The semantic of this function is that if only this function is used |
| 264 | * to add keys into the table, no duplicated keys should ever be |
| 265 | * observable in the table. The same guarantee apply for combination of |
| 266 | * add_unique and add_replace (see below). |
| 267 | */ |
| 268 | struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht, |
| 269 | unsigned long hash, |
| 270 | cds_lfht_match_fct match, |
| 271 | void *key, |
| 272 | struct cds_lfht_node *node); |
| 273 | |
| 274 | /* |
| 275 | * cds_lfht_add_replace - replace or add a node within hash table. |
| 276 | * @ht: the hash table. |
| 277 | * @hash: the node's hash. |
| 278 | * @match: the key match function. |
| 279 | * @key: the node's key. |
| 280 | * @node: the node to add. |
| 281 | * |
| 282 | * Return the node replaced upon success. If no node matching the key |
| 283 | * was present, return NULL, which also means the operation succeeded. |
| 284 | * This replacement operation should never fail. |
| 285 | * Call with rcu_read_lock held. |
| 286 | * Threads calling this API need to be registered RCU read-side threads. |
| 287 | * After successful replacement, a grace period must be waited for before |
| 288 | * freeing the memory reserved for the returned node. |
| 289 | * |
| 290 | * The semantic of replacement vs lookups is the following: if lookups |
| 291 | * are performed between a key unique insertion and its removal, we |
| 292 | * guarantee that the lookups and get next will always find exactly one |
| 293 | * instance of the key if it is replaced concurrently with the lookups. |
| 294 | * |
| 295 | * Providing this semantic allows us to ensure that replacement-only |
| 296 | * schemes will never generate duplicated keys. It also allows us to |
| 297 | * guarantee that a combination of add_replace and add_unique updates |
| 298 | * will never generate duplicated keys. |
| 299 | */ |
| 300 | struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht, |
| 301 | unsigned long hash, |
| 302 | cds_lfht_match_fct match, |
| 303 | void *key, |
| 304 | struct cds_lfht_node *node); |
| 305 | |
| 306 | /* |
| 307 | * cds_lfht_replace - replace a node pointer to by iter within hash table. |
| 308 | * @ht: the hash table. |
| 309 | * @old_iter: the iterator position of the node to replace. |
| 310 | * @now_node: the new node to try using for replacement. |
| 311 | * |
| 312 | * Return 0 if replacement is successful, negative value otherwise. |
| 313 | * Replacing a NULL old node or an already removed node will fail with a |
| 314 | * negative value. |
| 315 | * Old node can be looked up with cds_lfht_lookup and cds_lfht_next. |
| 316 | * RCU read-side lock must be held between lookup and replacement. |
| 317 | * Call with rcu_read_lock held. |
| 318 | * Threads calling this API need to be registered RCU read-side threads. |
| 319 | * After successful replacement, a grace period must be waited for before |
| 320 | * freeing the memory reserved for the old node (which can be accessed |
| 321 | * with cds_lfht_iter_get_node). |
| 322 | * |
| 323 | * The semantic of replacement vs lookups is the following: if lookups |
| 324 | * are performed between a key unique insertion and its removal, we |
| 325 | * guarantee that the lookups and get next will always find exactly one |
| 326 | * instance of the key if it is replaced concurrently with the lookups. |
| 327 | * |
| 328 | * Providing this semantic allows us to ensure that replacement-only |
| 329 | * schemes will never generate duplicated keys. It also allows us to |
| 330 | * guarantee that a combination of add_replace and add_unique updates |
| 331 | * will never generate duplicated keys. |
| 332 | */ |
| 333 | int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter, |
| 334 | struct cds_lfht_node *new_node); |
| 335 | |
| 336 | /* |
| 337 | * cds_lfht_del - remove node pointed to by iterator from hash table. |
| 338 | * @ht: the hash table. |
| 339 | * @iter: the iterator position of the node to delete. |
| 340 | * |
| 341 | * Return 0 if the node is successfully removed, negative value |
| 342 | * otherwise. |
| 343 | * Replacing a NULL node or an already removed node will fail with a |
| 344 | * negative value. |
| 345 | * Node can be looked up with cds_lfht_lookup and cds_lfht_next. |
| 346 | * cds_lfht_iter_get_node. |
| 347 | * RCU read-side lock must be held between lookup and removal. |
| 348 | * Call with rcu_read_lock held. |
| 349 | * Threads calling this API need to be registered RCU read-side threads. |
| 350 | * After successful removal, a grace period must be waited for before |
| 351 | * freeing the memory reserved for old node (which can be accessed with |
| 352 | * cds_lfht_iter_get_node). |
| 353 | */ |
| 354 | int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter); |
| 355 | |
| 356 | /* |
| 357 | * cds_lfht_resize - Force a hash table resize |
| 358 | * @ht: the hash table. |
| 359 | * @new_size: update to this hash table size. |
| 360 | * |
| 361 | * Threads calling this API need to be registered RCU read-side threads. |
| 362 | */ |
| 363 | void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); |
| 364 | |
| 365 | /* |
| 366 | * Note: cds_lfht_for_each are safe for element removal during |
| 367 | * iteration. |
| 368 | */ |
| 369 | #define cds_lfht_for_each(ht, iter, node) \ |
| 370 | for (cds_lfht_first(ht, iter), \ |
| 371 | node = cds_lfht_iter_get_node(iter); \ |
| 372 | node != NULL; \ |
| 373 | cds_lfht_next(ht, iter), \ |
| 374 | node = cds_lfht_iter_get_node(iter)) |
| 375 | |
| 376 | #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \ |
| 377 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ |
| 378 | node = cds_lfht_iter_get_node(iter); \ |
| 379 | node != NULL; \ |
| 380 | cds_lfht_next_duplicate(ht, match, key, iter), \ |
| 381 | node = cds_lfht_iter_get_node(iter)) |
| 382 | |
| 383 | #define cds_lfht_for_each_entry(ht, iter, pos, member) \ |
| 384 | for (cds_lfht_first(ht, iter), \ |
| 385 | pos = caa_container_of(cds_lfht_iter_get_node(iter), \ |
| 386 | typeof(*(pos)), member); \ |
| 387 | &(pos)->member != NULL; \ |
| 388 | cds_lfht_next(ht, iter), \ |
| 389 | pos = caa_container_of(cds_lfht_iter_get_node(iter), \ |
| 390 | typeof(*(pos)), member)) |
| 391 | |
| 392 | #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \ |
| 393 | iter, pos, member) \ |
| 394 | for (cds_lfht_lookup(ht, hash, match, key, iter), \ |
| 395 | pos = caa_container_of(cds_lfht_iter_get_node(iter), \ |
| 396 | typeof(*(pos)), member); \ |
| 397 | &(pos)->member != NULL; \ |
| 398 | cds_lfht_next_duplicate(ht, match, key, iter), \ |
| 399 | pos = caa_container_of(cds_lfht_iter_get_node(iter), \ |
| 400 | typeof(*(pos)), member)) |
| 401 | |
| 402 | #ifdef __cplusplus |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | #endif /* _URCU_RCULFHASH_H */ |