[7ef2249] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Adam Hraska
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[14c9aa6] | 29 |
|
---|
[7ef2249] | 30 | /** @addtogroup genericadt
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @file
|
---|
[14c9aa6] | 36 | * @brief Scalable resizable concurrent lock-free hash table.
|
---|
[991d2d8] | 37 | *
|
---|
| 38 | * CHT is a concurrent hash table that is scalable resizable and lock-free.
|
---|
| 39 | * resizable = the number of buckets of the table increases or decreases
|
---|
| 40 | * depending on the average number of elements per bucket (ie load)
|
---|
| 41 | * scalable = accessing the table from more cpus increases performance
|
---|
| 42 | * almost linearly
|
---|
| 43 | * lock-free = common operations never block; even if any of the operations
|
---|
| 44 | * is preempted or interrupted at any time, other operations will still
|
---|
| 45 | * make forward progress
|
---|
| 46 | *
|
---|
| 47 | * CHT is designed for read mostly scenarios. Performance degrades as the
|
---|
| 48 | * fraction of updates (insert/remove) increases. Other data structures
|
---|
| 49 | * significantly outperform CHT if the fraction of updates exceeds ~40%.
|
---|
| 50 | *
|
---|
| 51 | * CHT tolerates hardware exceptions and may be accessed from exception
|
---|
| 52 | * handlers as long as the underlying RCU implementation is exception safe.
|
---|
| 53 | *
|
---|
| 54 | * @par Caveats
|
---|
| 55 | *
|
---|
| 56 | * 0) Never assume an item is still in the table.
|
---|
| 57 | * The table may be accessed concurrently; therefore, other threads may
|
---|
| 58 | * insert or remove an item at any time. Do not assume an item is still
|
---|
| 59 | * in the table if cht_find() just returned it to you. Similarly, an
|
---|
| 60 | * item may have already been inserted by the time cht_find() returns NULL.
|
---|
| 61 | *
|
---|
| 62 | * 1) Always use RCU read locks when searching the table.
|
---|
| 63 | * Holding an RCU lock guarantees that an item found in the table remains
|
---|
| 64 | * valid (eg is not freed) even if the item was removed from the table
|
---|
| 65 | * in the meantime by another thread.
|
---|
| 66 | *
|
---|
| 67 | * 2) Never update values in place.
|
---|
| 68 | * Do not update items in the table in place, ie directly. The changes
|
---|
| 69 | * will not propagate to other readers (on other cpus) immediately or even
|
---|
| 70 | * correctly. Some readers may then encounter items that have only some
|
---|
| 71 | * of their fields changed or are completely inconsistent.
|
---|
| 72 | *
|
---|
| 73 | * Instead consider inserting an updated/changed copy of the item and
|
---|
| 74 | * removing the original item. Or contact the maintainer to provide
|
---|
| 75 | * you with a function that atomically replaces an item with a copy.
|
---|
| 76 | *
|
---|
| 77 | * 3) Use cht_insert_unique() instead of checking for duplicates with cht_find()
|
---|
| 78 | * The following code is prone to race conditions:
|
---|
| 79 | * @code
|
---|
| 80 | * if (NULL == cht_find(&h, key)) {
|
---|
| 81 | * // If another thread inserts and item here, we'll insert a duplicate.
|
---|
| 82 | * cht_insert(&h, item);
|
---|
| 83 | * }
|
---|
| 84 | * @endcode
|
---|
| 85 | * See cht_insert_unique() on how to correctly fix this.
|
---|
| 86 | *
|
---|
| 87 | *
|
---|
| 88 | * @par Semantics
|
---|
| 89 | *
|
---|
| 90 | * Lazy readers = cht_find_lazy(), cht_find_next_lazy()
|
---|
| 91 | * Readers = lazy readers, cht_find(), cht_find_next()
|
---|
| 92 | * Updates = cht_insert(), cht_insert_unique(), cht_remove_key(),
|
---|
| 93 | * cht_remove_item()
|
---|
| 94 | *
|
---|
| 95 | * Readers (but not lazy readers) are guaranteed to see the effects
|
---|
| 96 | * of @e completed updates. In other words, if cht_find() is invoked
|
---|
| 97 | * after a cht_insert() @e returned eg on another cpu, cht_find() is
|
---|
| 98 | * guaranteed to see the inserted item.
|
---|
| 99 | *
|
---|
| 100 | * Similarly, updates see the effects of @e completed updates. For example,
|
---|
| 101 | * issuing cht_remove() after a cht_insert() for that key returned (even
|
---|
| 102 | * on another cpu) is guaranteed to remove the inserted item.
|
---|
| 103 | *
|
---|
| 104 | * Reading or updating the table concurrently with other updates
|
---|
| 105 | * always returns consistent data and never corrupts the table.
|
---|
| 106 | * However the effects of concurrent updates may or may not be
|
---|
| 107 | * visible to all other concurrent readers or updaters. Eg, not
|
---|
| 108 | * all readers may see that an item has already been inserted
|
---|
| 109 | * if cht_insert() has not yet returned.
|
---|
| 110 | *
|
---|
| 111 | * Lazy readers are guaranteed to eventually see updates but it
|
---|
| 112 | * may take some time (possibly milliseconds) after the update
|
---|
| 113 | * completes for the change to propagate to lazy readers on all
|
---|
| 114 | * cpus.
|
---|
| 115 | *
|
---|
| 116 | * @par Implementation
|
---|
| 117 | *
|
---|
| 118 | * Collisions in CHT are resolved with chaining. The number of buckets
|
---|
| 119 | * is always a power of 2. Each bucket is represented with a single linked
|
---|
| 120 | * lock-free list [1]. Items in buckets are sorted by their mixed hashes
|
---|
| 121 | * in ascending order. All buckets are terminated with a single global
|
---|
| 122 | * sentinel node whose mixed hash value is the greatest possible.
|
---|
[7ef2249] | 123 | *
|
---|
[991d2d8] | 124 | * CHT with 2^k buckets uses the k most significant bits of a hash value
|
---|
| 125 | * to determine the bucket number where an item is to be stored. To
|
---|
| 126 | * avoid storing all items in a single bucket if the user supplied
|
---|
| 127 | * hash function does not produce uniform hashes, hash values are
|
---|
| 128 | * mixed first so that the top bits of a mixed hash change even if hash
|
---|
| 129 | * values differ only in the least significant bits. The mixed hash
|
---|
| 130 | * values are cached in cht_link.hash (which is overwritten once the
|
---|
| 131 | * item is scheduled for removal via rcu_call).
|
---|
| 132 | *
|
---|
| 133 | * A new item is inserted before all other existing items in the bucket
|
---|
| 134 | * with the same hash value as the newly inserted item (a la the original
|
---|
| 135 | * lock-free list [2]). Placing new items at the start of a same-hash
|
---|
| 136 | * sequence of items (eg duplicates) allows us to easily check for duplicates
|
---|
| 137 | * in cht_insert_unique(). The function can first check that there are
|
---|
| 138 | * no duplicates of the newly inserted item amongst the items with the
|
---|
| 139 | * same hash as the new item. If there were no duplicates the new item
|
---|
| 140 | * is linked before the same-hash items. Inserting a duplicate while
|
---|
| 141 | * the function is checking for duplicates is detected as a change of
|
---|
| 142 | * the link to the first checked same-hash item (and the search for
|
---|
| 143 | * duplicates can be restarted).
|
---|
| 144 | *
|
---|
| 145 | * @par Table resize algorithm
|
---|
| 146 | *
|
---|
| 147 | * Table resize is based on [3] and [5]. First, a new bucket head array
|
---|
| 148 | * is allocated and initialized. Second, old bucket heads are moved
|
---|
| 149 | * to the new bucket head array with the protocol mentioned in [5].
|
---|
| 150 | * At this point updaters start using the new bucket heads. Third,
|
---|
| 151 | * buckets are split (or joined) so that the table can make use of
|
---|
| 152 | * the extra bucket head slots in the new array (or stop wasting space
|
---|
| 153 | * with the unnecessary extra slots in the old array). Splitting
|
---|
| 154 | * or joining buckets employs a custom protocol. Last, the new array
|
---|
| 155 | * replaces the original bucket array.
|
---|
| 156 | *
|
---|
| 157 | * A single background work item (of the system work queue) guides
|
---|
| 158 | * resizing of the table. If an updater detects that the bucket it
|
---|
| 159 | * is about to access is undergoing a resize (ie its head is moving
|
---|
| 160 | * or it needs to be split/joined), it helps out and completes the
|
---|
| 161 | * head move or the bucket split/join.
|
---|
| 162 | *
|
---|
| 163 | * The table always grows or shrinks by a factor of 2. Because items
|
---|
| 164 | * are assigned a bucket based on the top k bits of their mixed hash
|
---|
| 165 | * values, when growing the table each bucket is split into two buckets
|
---|
| 166 | * and all items of the two new buckets come from the single bucket in the
|
---|
| 167 | * original table. Ie items from separate buckets in the original table
|
---|
| 168 | * never intermix in the new buckets. Moreover
|
---|
| 169 | * since the buckets are sorted by their mixed hash values the items
|
---|
| 170 | * at the beginning of the old bucket will end up in the first new
|
---|
| 171 | * bucket while all the remaining items of the old bucket will end up
|
---|
| 172 | * in the second new bucket. Therefore, there is a single point where
|
---|
| 173 | * to split the linked list of the old bucket into two correctly sorted
|
---|
| 174 | * linked lists of the new buckets:
|
---|
| 175 | * .- bucket split
|
---|
| 176 | * |
|
---|
| 177 | * <-- first --> v <-- second -->
|
---|
| 178 | * [old] --> [00b] -> [01b] -> [10b] -> [11b] -> sentinel
|
---|
| 179 | * ^ ^
|
---|
| 180 | * [new0] -- -+ |
|
---|
| 181 | * [new1] -- -- -- -- -- -- -- -+
|
---|
| 182 | *
|
---|
| 183 | * Resize in greater detail:
|
---|
| 184 | *
|
---|
| 185 | * a) First, a resizer (a single background system work queue item
|
---|
| 186 | * in charge of resizing the table) allocates and initializes a new
|
---|
| 187 | * bucket head array. New bucket heads are pointed to the sentinel
|
---|
| 188 | * and marked Invalid (in the lower order bits of the pointer to the
|
---|
| 189 | * next item, ie the sentinel in this case):
|
---|
| 190 | *
|
---|
| 191 | * [old, N] --> [00b] -> [01b] -> [10b] -> [11b] -> sentinel
|
---|
| 192 | * ^ ^
|
---|
| 193 | * [new0, Inv] -------------------------------------+ |
|
---|
| 194 | * [new1, Inv] ---------------------------------------+
|
---|
| 195 | *
|
---|
| 196 | *
|
---|
| 197 | * b) Second, the resizer starts moving old bucket heads with the following
|
---|
| 198 | * lock-free protocol (from [5]) where cas(variable, expected_val, new_val)
|
---|
| 199 | * is short for compare-and-swap:
|
---|
| 200 | *
|
---|
| 201 | * old head new0 head transition to next state
|
---|
| 202 | * -------- --------- ------------------------
|
---|
| 203 | * addr, N sentinel, Inv cas(old, (addr, N), (addr, Const))
|
---|
| 204 | * .. mark the old head as immutable, so that
|
---|
| 205 | * updaters do not relink it to other nodes
|
---|
| 206 | * until the head move is done.
|
---|
| 207 | * addr, Const sentinel, Inv cas(new0, (sentinel, Inv), (addr, N))
|
---|
| 208 | * .. move the address to the new head and mark
|
---|
| 209 | * the new head normal so updaters can start
|
---|
| 210 | * using it.
|
---|
| 211 | * addr, Const addr, N cas(old, (addr, Const), (addr, Inv))
|
---|
| 212 | * .. mark the old head Invalid to signify
|
---|
| 213 | * the head move is done.
|
---|
| 214 | * addr, Inv addr, N
|
---|
| 215 | *
|
---|
| 216 | * Notice that concurrent updaters may step in at any point and correctly
|
---|
| 217 | * complete the head move without disrupting the resizer. At worst, the
|
---|
| 218 | * resizer or other concurrent updaters will attempt a number of CAS() that
|
---|
| 219 | * will correctly fail.
|
---|
| 220 | *
|
---|
| 221 | * [old, Inv] -> [00b] -> [01b] -> [10b] -> [11b] -> sentinel
|
---|
| 222 | * ^ ^
|
---|
| 223 | * [new0, N] ----+ |
|
---|
| 224 | * [new1, Inv] --------------------------------------+
|
---|
| 225 | *
|
---|
| 226 | *
|
---|
| 227 | * c) Third, buckets are split if the table is growing; or joined if
|
---|
| 228 | * shrinking (by the resizer or updaters depending on whoever accesses
|
---|
| 229 | * the bucket first). See split_bucket() and join_buckets() for details.
|
---|
| 230 | *
|
---|
| 231 | * 1) Mark the last item of new0 with JOIN_FOLLOWS:
|
---|
| 232 | * [old, Inv] -> [00b] -> [01b, JF] -> [10b] -> [11b] -> sentinel
|
---|
| 233 | * ^ ^
|
---|
| 234 | * [new0, N] ----+ |
|
---|
| 235 | * [new1, Inv] ------------------------------------------+
|
---|
| 236 | *
|
---|
| 237 | * 2) Mark the first item of new1 with JOIN_NODE:
|
---|
| 238 | * [old, Inv] -> [00b] -> [01b, JF] -> [10b, JN] -> [11b] -> sentinel
|
---|
| 239 | * ^ ^
|
---|
| 240 | * [new0, N] ----+ |
|
---|
| 241 | * [new1, Inv] ----------------------------------------------+
|
---|
| 242 | *
|
---|
| 243 | * 3) Point new1 to the join-node and mark new1 NORMAL.
|
---|
| 244 | * [old, Inv] -> [00b] -> [01b, JF] -> [10b, JN] -> [11b] -> sentinel
|
---|
| 245 | * ^ ^
|
---|
| 246 | * [new0, N] ----+ |
|
---|
| 247 | * [new1, N] --------------------------+
|
---|
| 248 | *
|
---|
| 249 | *
|
---|
| 250 | * d) Fourth, the resizer cleans up extra marks added during bucket
|
---|
| 251 | * splits/joins but only when it is sure all updaters are accessing
|
---|
| 252 | * the table via the new bucket heads only (ie it is certain there
|
---|
| 253 | * are no delayed updaters unaware of the resize and accessing the
|
---|
| 254 | * table via the old bucket head).
|
---|
| 255 | *
|
---|
| 256 | * [old, Inv] ---+
|
---|
| 257 | * v
|
---|
| 258 | * [new0, N] --> [00b] -> [01b, N] ---+
|
---|
| 259 | * v
|
---|
| 260 | * [new1, N] --> [10b, N] -> [11b] -> sentinel
|
---|
| 261 | *
|
---|
| 262 | *
|
---|
| 263 | * e) Last, the resizer publishes the new bucket head array for everyone
|
---|
| 264 | * to see and use. This signals the end of the resize and the old bucket
|
---|
| 265 | * array is freed.
|
---|
| 266 | *
|
---|
| 267 | *
|
---|
| 268 | * To understand details of how the table is resized, read [1, 3, 5]
|
---|
| 269 | * and comments in join_buckets(), split_bucket().
|
---|
| 270 | *
|
---|
| 271 | *
|
---|
| 272 | * [1] High performance dynamic lock-free hash tables and list-based sets,
|
---|
| 273 | * Michael, 2002
|
---|
| 274 | * http://www.research.ibm.com/people/m/michael/spaa-2002.pdf
|
---|
| 275 | * [2] Lock-free linked lists using compare-and-swap,
|
---|
| 276 | * Valois, 1995
|
---|
| 277 | * http://people.csail.mit.edu/bushl2/rpi/portfolio/lockfree-grape/documents/lock-free-linked-lists.pdf
|
---|
| 278 | * [3] Resizable, scalable, concurrent hash tables via relativistic programming,
|
---|
| 279 | * Triplett, 2011
|
---|
| 280 | * http://www.usenix.org/event/atc11/tech/final_files/Triplett.pdf
|
---|
| 281 | * [4] Split-ordered Lists: Lock-free Extensible Hash Tables,
|
---|
| 282 | * Shavit, 2006
|
---|
| 283 | * http://www.cs.ucf.edu/~dcm/Teaching/COT4810-Spring2011/Literature/SplitOrderedLists.pdf
|
---|
| 284 | * [5] Towards a Scalable Non-blocking Coding Style,
|
---|
| 285 | * Click, 2008
|
---|
| 286 | * http://www.azulsystems.com/events/javaone_2008/2008_CodingNonBlock.pdf
|
---|
[7ef2249] | 287 | */
|
---|
| 288 |
|
---|
[991d2d8] | 289 |
|
---|
[7ef2249] | 290 | #include <adt/cht.h>
|
---|
[14c9aa6] | 291 | #include <adt/hash.h>
|
---|
[7ef2249] | 292 | #include <debug.h>
|
---|
| 293 | #include <memstr.h>
|
---|
| 294 | #include <mm/slab.h>
|
---|
[14c9aa6] | 295 | #include <arch/barrier.h>
|
---|
[7ef2249] | 296 | #include <compiler/barrier.h>
|
---|
| 297 | #include <atomic.h>
|
---|
| 298 | #include <synch/rcu.h>
|
---|
| 299 |
|
---|
[14c9aa6] | 300 |
|
---|
[0b7bcb8] | 301 | /* Logarithm of the min bucket count. Must be at least 3. 2^6 == 64 buckets. */
|
---|
[3bb732b] | 302 | #define CHT_MIN_ORDER 6
|
---|
| 303 | /* Logarithm of the max bucket count. */
|
---|
| 304 | #define CHT_MAX_ORDER (8 * sizeof(size_t))
|
---|
| 305 | /* Minimum number of hash table buckets. */
|
---|
| 306 | #define CHT_MIN_BUCKET_CNT (1 << CHT_MIN_ORDER)
|
---|
[0b7bcb8] | 307 | /* Does not have to be a power of 2. */
|
---|
[7ef2249] | 308 | #define CHT_MAX_LOAD 2
|
---|
| 309 |
|
---|
| 310 | typedef cht_ptr_t marked_ptr_t;
|
---|
| 311 | typedef bool (*equal_pred_t)(void *arg, const cht_link_t *item);
|
---|
| 312 |
|
---|
[b9cb911] | 313 | /** The following mark items and bucket heads.
|
---|
| 314 | *
|
---|
| 315 | * They are stored in the two low order bits of the next item pointers.
|
---|
| 316 | * Some marks may be combined. Some marks share the same binary value and
|
---|
| 317 | * are distinguished only by context (eg bucket head vs an ordinary item),
|
---|
| 318 | * in particular by walk_mode_t.
|
---|
| 319 | */
|
---|
[7ef2249] | 320 | typedef enum mark {
|
---|
[b9cb911] | 321 | /** Normal non-deleted item or a valid bucket head. */
|
---|
[7ef2249] | 322 | N_NORMAL = 0,
|
---|
[b9cb911] | 323 | /** Logically deleted item that might have already been unlinked.
|
---|
| 324 | *
|
---|
| 325 | * May be combined with N_JOIN and N_JOIN_FOLLOWS. Applicable only
|
---|
| 326 | * to items; never to bucket heads.
|
---|
| 327 | *
|
---|
| 328 | * Once marked deleted an item remains marked deleted.
|
---|
| 329 | */
|
---|
[7ef2249] | 330 | N_DELETED = 1,
|
---|
[b9cb911] | 331 | /** Immutable bucket head.
|
---|
| 332 | *
|
---|
| 333 | * The bucket is being moved or joined with another and its (old) head
|
---|
| 334 | * must not be modified.
|
---|
| 335 | *
|
---|
| 336 | * May be combined with N_INVALID. Applicable only to old bucket heads,
|
---|
| 337 | * ie cht_t.b and not cht_t.new_b.
|
---|
| 338 | */
|
---|
[14c9aa6] | 339 | N_CONST = 1,
|
---|
[b9cb911] | 340 | /** Invalid bucket head. The bucket head must not be modified.
|
---|
| 341 | *
|
---|
| 342 | * Old bucket heads (ie cht_t.b) are marked invalid if they have
|
---|
| 343 | * already been moved to cht_t.new_b or if the bucket had already
|
---|
| 344 | * been merged with another when shrinking the table. New bucket
|
---|
| 345 | * heads (ie cht_t.new_b) are marked invalid if the old bucket had
|
---|
| 346 | * not yet been moved or if an old bucket had not yet been split
|
---|
| 347 | * when growing the table.
|
---|
| 348 | */
|
---|
[14c9aa6] | 349 | N_INVALID = 3,
|
---|
[b9cb911] | 350 | /** The item is a join node, ie joining two buckets
|
---|
| 351 | *
|
---|
| 352 | * A join node is either the first node of the second part of
|
---|
| 353 | * a bucket to be split; or it is the first node of the bucket
|
---|
| 354 | * to be merged into/appended to/joined with another bucket.
|
---|
| 355 | *
|
---|
| 356 | * May be combined with N_DELETED. Applicable only to items, never
|
---|
| 357 | * to bucket heads.
|
---|
| 358 | *
|
---|
| 359 | * Join nodes are referred to from two different buckets and may,
|
---|
| 360 | * therefore, not be safely/atomically unlinked from both buckets.
|
---|
| 361 | * As a result join nodes are not unlinked but rather just marked
|
---|
| 362 | * deleted. Once resize completes join nodes marked deleted are
|
---|
| 363 | * garbage collected.
|
---|
| 364 | */
|
---|
[7ef2249] | 365 | N_JOIN = 2,
|
---|
[b9cb911] | 366 | /** The next node is a join node and will soon be marked so.
|
---|
| 367 | *
|
---|
| 368 | * A join-follows node is the last node of the first part of bucket
|
---|
| 369 | * that is to be split, ie it is the last node that will remain
|
---|
| 370 | * in the same bucket after splitting it.
|
---|
| 371 | *
|
---|
| 372 | * May be combined with N_DELETED. Applicable to items as well
|
---|
| 373 | * as to bucket heads of the bucket to be split (but only in cht_t.new_b).
|
---|
| 374 | */
|
---|
[7ef2249] | 375 | N_JOIN_FOLLOWS = 2,
|
---|
[b9cb911] | 376 | /** Bit mask to filter out the address to the next item from the next ptr. */
|
---|
[7ef2249] | 377 | N_MARK_MASK = 3
|
---|
| 378 | } mark_t;
|
---|
| 379 |
|
---|
[b9cb911] | 380 | /** Determines */
|
---|
[7ef2249] | 381 | typedef enum walk_mode {
|
---|
[b9cb911] | 382 | /** The table is not resizing. */
|
---|
[7ef2249] | 383 | WM_NORMAL = 4,
|
---|
[b9cb911] | 384 | /** The table is undergoing a resize. Join nodes may be encountered. */
|
---|
[7ef2249] | 385 | WM_LEAVE_JOIN,
|
---|
[b9cb911] | 386 | /** The table is growing. A join-follows node may be encountered. */
|
---|
[7ef2249] | 387 | WM_MOVE_JOIN_FOLLOWS
|
---|
| 388 | } walk_mode_t;
|
---|
| 389 |
|
---|
[b9cb911] | 390 | /** Bucket position window. */
|
---|
[7ef2249] | 391 | typedef struct wnd {
|
---|
[b9cb911] | 392 | /** Pointer to cur's predecessor. */
|
---|
[7ef2249] | 393 | marked_ptr_t *ppred;
|
---|
[b9cb911] | 394 | /** Current item. */
|
---|
[7ef2249] | 395 | cht_link_t *cur;
|
---|
[b9cb911] | 396 | /** Last encountered item. Deleted or not. */
|
---|
[7ef2249] | 397 | cht_link_t *last;
|
---|
| 398 | } wnd_t;
|
---|
| 399 |
|
---|
[0b7bcb8] | 400 |
|
---|
[6eaed07] | 401 | /* Sentinel node used by all buckets. Stores the greatest possible hash value.*/
|
---|
| 402 | static const cht_link_t sentinel = {
|
---|
[205832b] | 403 | /* NULL and N_NORMAL */
|
---|
| 404 | .link = 0 | N_NORMAL,
|
---|
[6eaed07] | 405 | .hash = -1
|
---|
| 406 | };
|
---|
| 407 |
|
---|
| 408 |
|
---|
[14c9aa6] | 409 | static size_t size_to_order(size_t bucket_cnt, size_t min_order);
|
---|
[c28413a9] | 410 | static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid,
|
---|
| 411 | bool can_block);
|
---|
[5e5cef3] | 412 | static inline cht_link_t *find_lazy(cht_t *h, void *key);
|
---|
[14c9aa6] | 413 | static cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key,
|
---|
| 414 | size_t search_hash);
|
---|
| 415 | static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash,
|
---|
| 416 | marked_ptr_t old_head, size_t old_idx);
|
---|
[04d66804] | 417 | static bool insert_impl(cht_t *h, cht_link_t *item, cht_link_t **dup_item);
|
---|
[14c9aa6] | 418 | static bool insert_at(cht_link_t *item, const wnd_t *wnd, walk_mode_t walk_mode,
|
---|
| 419 | bool *resizing);
|
---|
[04d66804] | 420 | static bool has_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
|
---|
| 421 | cht_link_t *cur, cht_link_t **dup_item);
|
---|
[14c9aa6] | 422 | static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
|
---|
| 423 | cht_link_t *start);
|
---|
| 424 | static bool remove_pred(cht_t *h, size_t hash, equal_pred_t pred, void *pred_arg);
|
---|
| 425 | static bool delete_at(cht_t *h, wnd_t *wnd, walk_mode_t walk_mode,
|
---|
| 426 | bool *deleted_but_gc, bool *resizing);
|
---|
| 427 | static bool mark_deleted(cht_link_t *cur, walk_mode_t walk_mode, bool *resizing);
|
---|
| 428 | static bool unlink_from_pred(wnd_t *wnd, walk_mode_t walk_mode, bool *resizing);
|
---|
| 429 | static bool find_wnd_and_gc_pred(cht_t *h, size_t hash, walk_mode_t walk_mode,
|
---|
| 430 | equal_pred_t pred, void *pred_arg, wnd_t *wnd, bool *resizing);
|
---|
| 431 | static bool find_wnd_and_gc(cht_t *h, size_t hash, walk_mode_t walk_mode,
|
---|
| 432 | wnd_t *wnd, bool *resizing);
|
---|
| 433 | static bool gc_deleted_node(cht_t *h, walk_mode_t walk_mode, wnd_t *wnd,
|
---|
| 434 | bool *resizing);
|
---|
| 435 | static bool join_completed(cht_t *h, const wnd_t *wnd);
|
---|
| 436 | static void upd_resizing_head(cht_t *h, size_t hash, marked_ptr_t **phead,
|
---|
| 437 | bool *join_finishing, walk_mode_t *walk_mode);
|
---|
| 438 | static void item_removed(cht_t *h);
|
---|
| 439 | static void item_inserted(cht_t *h);
|
---|
| 440 | static void free_later(cht_t *h, cht_link_t *item);
|
---|
| 441 | static void help_head_move(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head);
|
---|
| 442 | static void start_head_move(marked_ptr_t *psrc_head);
|
---|
| 443 | static void mark_const(marked_ptr_t *psrc_head);
|
---|
| 444 | static void complete_head_move(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head);
|
---|
| 445 | static void split_bucket(cht_t *h, marked_ptr_t *psrc_head,
|
---|
| 446 | marked_ptr_t *pdest_head, size_t split_hash);
|
---|
| 447 | static void mark_join_follows(cht_t *h, marked_ptr_t *psrc_head,
|
---|
| 448 | size_t split_hash, wnd_t *wnd);
|
---|
| 449 | static void mark_join_node(cht_link_t *join_node);
|
---|
| 450 | static void join_buckets(cht_t *h, marked_ptr_t *psrc_head,
|
---|
| 451 | marked_ptr_t *pdest_head, size_t split_hash);
|
---|
| 452 | static void link_to_join_node(cht_t *h, marked_ptr_t *pdest_head,
|
---|
| 453 | cht_link_t *join_node, size_t split_hash);
|
---|
| 454 | static void resize_table(work_t *arg);
|
---|
| 455 | static void grow_table(cht_t *h);
|
---|
| 456 | static void shrink_table(cht_t *h);
|
---|
| 457 | static void cleanup_join_node(cht_t *h, marked_ptr_t *new_head);
|
---|
| 458 | static void clear_join_and_gc(cht_t *h, cht_link_t *join_node,
|
---|
| 459 | marked_ptr_t *new_head);
|
---|
| 460 | static void cleanup_join_follows(cht_t *h, marked_ptr_t *new_head);
|
---|
| 461 | static marked_ptr_t make_link(const cht_link_t *next, mark_t mark);
|
---|
[7ef2249] | 462 | static cht_link_t * get_next(marked_ptr_t link);
|
---|
| 463 | static mark_t get_mark(marked_ptr_t link);
|
---|
[14c9aa6] | 464 | static void next_wnd(wnd_t *wnd);
|
---|
| 465 | static bool same_node_pred(void *node, const cht_link_t *item2);
|
---|
[6eaed07] | 466 | static size_t calc_key_hash(cht_t *h, void *key);
|
---|
[7ef2249] | 467 | static size_t node_hash(cht_t *h, const cht_link_t *item);
|
---|
[6eaed07] | 468 | static size_t calc_node_hash(cht_t *h, const cht_link_t *item);
|
---|
| 469 | static void memoize_node_hash(cht_t *h, cht_link_t *item);
|
---|
[3bb732b] | 470 | static size_t calc_split_hash(size_t split_idx, size_t order);
|
---|
[7ef2249] | 471 | static size_t calc_bucket_idx(size_t hash, size_t order);
|
---|
[14c9aa6] | 472 | static size_t grow_to_split_idx(size_t old_idx);
|
---|
[7ef2249] | 473 | static size_t grow_idx(size_t idx);
|
---|
| 474 | static size_t shrink_idx(size_t idx);
|
---|
[14c9aa6] | 475 | static marked_ptr_t cas_link(marked_ptr_t *link, const cht_link_t *cur_next,
|
---|
| 476 | mark_t cur_mark, const cht_link_t *new_next, mark_t new_mark);
|
---|
| 477 | static marked_ptr_t _cas_link(marked_ptr_t *link, marked_ptr_t cur,
|
---|
| 478 | marked_ptr_t new);
|
---|
| 479 | static void cas_order_barrier(void);
|
---|
[7ef2249] | 480 |
|
---|
[c28413a9] | 481 | static void dummy_remove_callback(cht_link_t *item)
|
---|
| 482 | {
|
---|
| 483 | /* empty */
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | /** Creates a concurrent hash table.
|
---|
| 487 | *
|
---|
| 488 | * @param h Valid pointer to a cht_t instance.
|
---|
| 489 | * @param op Item specific operations. All operations are compulsory.
|
---|
| 490 | * @return True if successfully created the table. False otherwise.
|
---|
| 491 | */
|
---|
| 492 | bool cht_create_simple(cht_t *h, cht_ops_t *op)
|
---|
| 493 | {
|
---|
| 494 | return cht_create(h, 0, 0, 0, false, op);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[b9cb911] | 497 | /** Creates a concurrent hash table.
|
---|
| 498 | *
|
---|
| 499 | * @param h Valid pointer to a cht_t instance.
|
---|
| 500 | * @param init_size The initial number of buckets the table should contain.
|
---|
| 501 | * The table may be shrunk below this value if deemed necessary.
|
---|
| 502 | * Uses the default value if 0.
|
---|
| 503 | * @param min_size Minimum number of buckets that the table should contain.
|
---|
| 504 | * The number of buckets never drops below this value,
|
---|
| 505 | * although it may be rounded up internally as appropriate.
|
---|
| 506 | * Uses the default value if 0.
|
---|
| 507 | * @param max_load Maximum average number of items per bucket that allowed
|
---|
| 508 | * before the table grows.
|
---|
[c28413a9] | 509 | * @param can_block If true creating the table blocks until enough memory
|
---|
| 510 | * is available (possibly indefinitely). Otherwise,
|
---|
| 511 | * table creation does not block and returns immediately
|
---|
| 512 | * even if not enough memory is available.
|
---|
[b9cb911] | 513 | * @param op Item specific operations. All operations are compulsory.
|
---|
| 514 | * @return True if successfully created the table. False otherwise.
|
---|
| 515 | */
|
---|
[0b7bcb8] | 516 | bool cht_create(cht_t *h, size_t init_size, size_t min_size, size_t max_load,
|
---|
[c28413a9] | 517 | bool can_block, cht_ops_t *op)
|
---|
[7ef2249] | 518 | {
|
---|
| 519 | ASSERT(h);
|
---|
| 520 | ASSERT(op && op->hash && op->key_hash && op->equal && op->key_equal);
|
---|
[6eaed07] | 521 | /* Memoized hashes are stored in the rcu_link.func function pointer. */
|
---|
| 522 | ASSERT(sizeof(size_t) == sizeof(rcu_func_t));
|
---|
| 523 | ASSERT(sentinel.hash == (uintptr_t)sentinel.rcu_link.func);
|
---|
[7ef2249] | 524 |
|
---|
[3bb732b] | 525 | /* All operations are compulsory. */
|
---|
[7ef2249] | 526 | if (!op || !op->hash || !op->key_hash || !op->equal || !op->key_equal)
|
---|
| 527 | return false;
|
---|
| 528 |
|
---|
[14c9aa6] | 529 | size_t min_order = size_to_order(min_size, CHT_MIN_ORDER);
|
---|
| 530 | size_t order = size_to_order(init_size, min_order);
|
---|
[7ef2249] | 531 |
|
---|
[c28413a9] | 532 | h->b = alloc_buckets(order, false, can_block);
|
---|
[7ef2249] | 533 |
|
---|
| 534 | if (!h->b)
|
---|
| 535 | return false;
|
---|
| 536 |
|
---|
[0b7bcb8] | 537 | h->max_load = (max_load == 0) ? CHT_MAX_LOAD : max_load;
|
---|
[14c9aa6] | 538 | h->min_order = min_order;
|
---|
[205832b] | 539 | h->new_b = NULL;
|
---|
[7ef2249] | 540 | h->op = op;
|
---|
| 541 | atomic_set(&h->item_cnt, 0);
|
---|
| 542 | atomic_set(&h->resize_reqs, 0);
|
---|
[c28413a9] | 543 |
|
---|
| 544 | if (NULL == op->remove_callback) {
|
---|
| 545 | h->op->remove_callback = dummy_remove_callback;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
[6eaed07] | 548 | /*
|
---|
| 549 | * Cached item hashes are stored in item->rcu_link.func. Once the item
|
---|
| 550 | * is deleted rcu_link.func will contain the value of invalid_hash.
|
---|
| 551 | */
|
---|
| 552 | h->invalid_hash = (uintptr_t)h->op->remove_callback;
|
---|
| 553 |
|
---|
[7ef2249] | 554 | /* Ensure the initialization takes place before we start using the table. */
|
---|
| 555 | write_barrier();
|
---|
| 556 |
|
---|
| 557 | return true;
|
---|
| 558 | }
|
---|
| 559 |
|
---|
[b9cb911] | 560 | /** Allocates and initializes 2^order buckets.
|
---|
| 561 | *
|
---|
| 562 | * All bucket heads are initialized to point to the sentinel node.
|
---|
| 563 | *
|
---|
| 564 | * @param order The number of buckets to allocate is 2^order.
|
---|
| 565 | * @param set_invalid Bucket heads are marked invalid if true; otherwise
|
---|
| 566 | * they are marked N_NORMAL.
|
---|
[c28413a9] | 567 | * @param can_block If true memory allocation blocks until enough memory
|
---|
| 568 | * is available (possibly indefinitely). Otherwise,
|
---|
| 569 | * memory allocation does not block.
|
---|
[b9cb911] | 570 | * @return Newly allocated and initialized buckets or NULL if not enough memory.
|
---|
| 571 | */
|
---|
[c28413a9] | 572 | static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid, bool can_block)
|
---|
[3bb732b] | 573 | {
|
---|
| 574 | size_t bucket_cnt = (1 << order);
|
---|
[14c9aa6] | 575 | size_t bytes =
|
---|
| 576 | sizeof(cht_buckets_t) + (bucket_cnt - 1) * sizeof(marked_ptr_t);
|
---|
[c28413a9] | 577 | cht_buckets_t *b = malloc(bytes, can_block ? 0 : FRAME_ATOMIC);
|
---|
[3bb732b] | 578 |
|
---|
| 579 | if (!b)
|
---|
[205832b] | 580 | return NULL;
|
---|
[3bb732b] | 581 |
|
---|
| 582 | b->order = order;
|
---|
| 583 |
|
---|
[6eaed07] | 584 | marked_ptr_t head_link = set_invalid
|
---|
| 585 | ? make_link(&sentinel, N_INVALID)
|
---|
| 586 | : make_link(&sentinel, N_NORMAL);
|
---|
[3bb732b] | 587 |
|
---|
| 588 | for (size_t i = 0; i < bucket_cnt; ++i) {
|
---|
| 589 | b->head[i] = head_link;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | return b;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
[b9cb911] | 595 | /** Returns the smallest k such that bucket_cnt <= 2^k and min_order <= k.*/
|
---|
[14c9aa6] | 596 | static size_t size_to_order(size_t bucket_cnt, size_t min_order)
|
---|
[3bb732b] | 597 | {
|
---|
[14c9aa6] | 598 | size_t order = min_order;
|
---|
[3bb732b] | 599 |
|
---|
| 600 | /* Find a power of two such that bucket_cnt <= 2^order */
|
---|
| 601 | do {
|
---|
[14c9aa6] | 602 | if (bucket_cnt <= ((size_t)1 << order))
|
---|
[3bb732b] | 603 | return order;
|
---|
| 604 |
|
---|
| 605 | ++order;
|
---|
| 606 | } while (order < CHT_MAX_ORDER);
|
---|
| 607 |
|
---|
| 608 | return order;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[b9cb911] | 611 | /** Destroys a CHT successfully created via cht_create().
|
---|
| 612 | *
|
---|
| 613 | * Waits for all outstanding concurrent operations to complete and
|
---|
| 614 | * frees internal allocated resources. The table is however not cleared
|
---|
| 615 | * and items already present in the table (if any) are leaked.
|
---|
| 616 | */
|
---|
[7ef2249] | 617 | void cht_destroy(cht_t *h)
|
---|
[3ac5086] | 618 | {
|
---|
| 619 | cht_destroy_unsafe(h);
|
---|
| 620 |
|
---|
| 621 | /* You must clear the table of items. Otherwise cht_destroy will leak. */
|
---|
| 622 | ASSERT(atomic_get(&h->item_cnt) == 0);
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | /** Destroys a successfully created CHT but does no error checking. */
|
---|
| 626 | void cht_destroy_unsafe(cht_t *h)
|
---|
[7ef2249] | 627 | {
|
---|
[14c9aa6] | 628 | /* Wait for resize to complete. */
|
---|
| 629 | while (0 < atomic_get(&h->resize_reqs)) {
|
---|
| 630 | rcu_barrier();
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | /* Wait for all remove_callback()s to complete. */
|
---|
| 634 | rcu_barrier();
|
---|
| 635 |
|
---|
| 636 | free(h->b);
|
---|
[205832b] | 637 | h->b = NULL;
|
---|
[b9cb911] | 638 | }
|
---|
| 639 |
|
---|
| 640 | /** Returns the first item equal to the search key or NULL if not found.
|
---|
| 641 | *
|
---|
| 642 | * The call must be enclosed in a rcu_read_lock() unlock() pair. The
|
---|
| 643 | * returned item is guaranteed to be allocated until rcu_read_unlock()
|
---|
| 644 | * although the item may be concurrently removed from the table by another
|
---|
| 645 | * cpu.
|
---|
| 646 | *
|
---|
| 647 | * Further items matching the key may be retrieved via cht_find_next().
|
---|
| 648 | *
|
---|
| 649 | * cht_find() sees the effects of any completed cht_remove(), cht_insert().
|
---|
| 650 | * If a concurrent remove or insert had not yet completed cht_find() may
|
---|
| 651 | * or may not see the effects of it (eg it may find an item being removed).
|
---|
| 652 | *
|
---|
| 653 | * @param h CHT to operate on.
|
---|
| 654 | * @param key Search key as defined by cht_ops_t.key_equal() and .key_hash().
|
---|
| 655 | * @return First item equal to the key or NULL if such an item does not exist.
|
---|
| 656 | */
|
---|
[7ef2249] | 657 | cht_link_t *cht_find(cht_t *h, void *key)
|
---|
| 658 | {
|
---|
[14c9aa6] | 659 | /* Make the most recent changes to the table visible. */
|
---|
[7ef2249] | 660 | read_barrier();
|
---|
| 661 | return cht_find_lazy(h, key);
|
---|
| 662 | }
|
---|
| 663 |
|
---|
[b9cb911] | 664 | /** Returns the first item equal to the search key or NULL if not found.
|
---|
| 665 | *
|
---|
| 666 | * Unlike cht_find(), cht_find_lazy() may not see the effects of
|
---|
| 667 | * cht_remove() or cht_insert() even though they have already completed.
|
---|
| 668 | * It may take a couple of milliseconds for those changes to propagate
|
---|
| 669 | * and become visible to cht_find_lazy(). On the other hand, cht_find_lazy()
|
---|
| 670 | * operates a bit faster than cht_find().
|
---|
| 671 | *
|
---|
| 672 | * See cht_find() for more details.
|
---|
| 673 | */
|
---|
[7ef2249] | 674 | cht_link_t *cht_find_lazy(cht_t *h, void *key)
|
---|
[5e5cef3] | 675 | {
|
---|
| 676 | return find_lazy(h, key);
|
---|
| 677 | }
|
---|
| 678 |
|
---|
[b9cb911] | 679 | /** Finds the first item equal to the search key. */
|
---|
[5e5cef3] | 680 | static inline cht_link_t *find_lazy(cht_t *h, void *key)
|
---|
[7ef2249] | 681 | {
|
---|
| 682 | ASSERT(h);
|
---|
[b9cb911] | 683 | /* See docs to cht_find() and cht_find_lazy(). */
|
---|
[7ef2249] | 684 | ASSERT(rcu_read_locked());
|
---|
| 685 |
|
---|
[6eaed07] | 686 | size_t hash = calc_key_hash(h, key);
|
---|
[7ef2249] | 687 |
|
---|
| 688 | cht_buckets_t *b = rcu_access(h->b);
|
---|
| 689 | size_t idx = calc_bucket_idx(hash, b->order);
|
---|
[3bb732b] | 690 | /*
|
---|
| 691 | * No need for access_once. b->head[idx] will point to an allocated node
|
---|
| 692 | * even if marked invalid until we exit rcu read section.
|
---|
| 693 | */
|
---|
[7ef2249] | 694 | marked_ptr_t head = b->head[idx];
|
---|
| 695 |
|
---|
[b9cb911] | 696 | /* Undergoing a resize - take the slow path. */
|
---|
[7ef2249] | 697 | if (N_INVALID == get_mark(head))
|
---|
| 698 | return find_resizing(h, key, hash, head, idx);
|
---|
| 699 |
|
---|
| 700 | return search_bucket(h, head, key, hash);
|
---|
| 701 | }
|
---|
| 702 |
|
---|
[b9cb911] | 703 | /** Returns the next item matching \a item.
|
---|
| 704 | *
|
---|
| 705 | * Must be enclosed in a rcu_read_lock()/unlock() pair. Effects of
|
---|
| 706 | * completed cht_remove(), cht_insert() are guaranteed to be visible
|
---|
| 707 | * to cht_find_next().
|
---|
| 708 | *
|
---|
| 709 | * See cht_find() for more details.
|
---|
| 710 | */
|
---|
[14c9aa6] | 711 | cht_link_t *cht_find_next(cht_t *h, const cht_link_t *item)
|
---|
| 712 | {
|
---|
| 713 | /* Make the most recent changes to the table visible. */
|
---|
| 714 | read_barrier();
|
---|
| 715 | return cht_find_next_lazy(h, item);
|
---|
| 716 | }
|
---|
| 717 |
|
---|
[b9cb911] | 718 | /** Returns the next item matching \a item.
|
---|
| 719 | *
|
---|
| 720 | * Must be enclosed in a rcu_read_lock()/unlock() pair. Effects of
|
---|
| 721 | * completed cht_remove(), cht_insert() may or may not be visible
|
---|
| 722 | * to cht_find_next_lazy().
|
---|
| 723 | *
|
---|
| 724 | * See cht_find_lazy() for more details.
|
---|
| 725 | */
|
---|
[14c9aa6] | 726 | cht_link_t *cht_find_next_lazy(cht_t *h, const cht_link_t *item)
|
---|
| 727 | {
|
---|
| 728 | ASSERT(h);
|
---|
| 729 | ASSERT(rcu_read_locked());
|
---|
| 730 | ASSERT(item);
|
---|
| 731 |
|
---|
[6eaed07] | 732 | return find_duplicate(h, item, calc_node_hash(h, item), get_next(item->link));
|
---|
[14c9aa6] | 733 | }
|
---|
[7ef2249] | 734 |
|
---|
[b9cb911] | 735 | /** Searches the bucket at head for key using search_hash. */
|
---|
[5e5cef3] | 736 | static inline cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key,
|
---|
[7ef2249] | 737 | size_t search_hash)
|
---|
| 738 | {
|
---|
[6eaed07] | 739 | /*
|
---|
| 740 | * It is safe to access nodes even outside of this bucket (eg when
|
---|
| 741 | * splitting the bucket). The resizer makes sure that any node we
|
---|
| 742 | * may find by following the next pointers is allocated.
|
---|
| 743 | */
|
---|
| 744 |
|
---|
[205832b] | 745 | cht_link_t *cur = NULL;
|
---|
[6eaed07] | 746 | marked_ptr_t prev = head;
|
---|
| 747 |
|
---|
| 748 | try_again:
|
---|
| 749 | /* Filter out items with different hashes. */
|
---|
| 750 | do {
|
---|
| 751 | cur = get_next(prev);
|
---|
| 752 | ASSERT(cur);
|
---|
| 753 | prev = cur->link;
|
---|
| 754 | } while (node_hash(h, cur) < search_hash);
|
---|
[7ef2249] | 755 |
|
---|
[6eaed07] | 756 | /*
|
---|
| 757 | * Only search for an item with an equal key if cur is not the sentinel
|
---|
| 758 | * node or a node with a different hash.
|
---|
| 759 | */
|
---|
| 760 | while (node_hash(h, cur) == search_hash) {
|
---|
[5e5cef3] | 761 | if (h->op->key_equal(key, cur)) {
|
---|
| 762 | if (!(N_DELETED & get_mark(cur->link)))
|
---|
[7ef2249] | 763 | return cur;
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | cur = get_next(cur->link);
|
---|
[6eaed07] | 767 | ASSERT(cur);
|
---|
| 768 | }
|
---|
| 769 |
|
---|
| 770 | /*
|
---|
| 771 | * In the unlikely case that we have encountered a node whose cached
|
---|
| 772 | * hash has been overwritten due to a pending rcu_call for it, skip
|
---|
| 773 | * the node and try again.
|
---|
| 774 | */
|
---|
| 775 | if (node_hash(h, cur) == h->invalid_hash) {
|
---|
| 776 | prev = cur->link;
|
---|
| 777 | goto try_again;
|
---|
[7ef2249] | 778 | }
|
---|
| 779 |
|
---|
[205832b] | 780 | return NULL;
|
---|
[7ef2249] | 781 | }
|
---|
| 782 |
|
---|
[b9cb911] | 783 | /** Searches for the key while the table is undergoing a resize. */
|
---|
[7ef2249] | 784 | static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash,
|
---|
| 785 | marked_ptr_t old_head, size_t old_idx)
|
---|
| 786 | {
|
---|
| 787 | ASSERT(N_INVALID == get_mark(old_head));
|
---|
| 788 | ASSERT(h->new_b);
|
---|
| 789 |
|
---|
| 790 | size_t new_idx = calc_bucket_idx(hash, h->new_b->order);
|
---|
| 791 | marked_ptr_t new_head = h->new_b->head[new_idx];
|
---|
| 792 | marked_ptr_t search_head = new_head;
|
---|
| 793 |
|
---|
| 794 | /* Growing. */
|
---|
| 795 | if (h->b->order < h->new_b->order) {
|
---|
| 796 | /*
|
---|
| 797 | * Old bucket head is invalid, so it must have been already
|
---|
| 798 | * moved. Make the new head visible if still not visible, ie
|
---|
| 799 | * invalid.
|
---|
| 800 | */
|
---|
| 801 | if (N_INVALID == get_mark(new_head)) {
|
---|
| 802 | /*
|
---|
| 803 | * We should be searching a newly added bucket but the old
|
---|
| 804 | * moved bucket has not yet been split (its marked invalid)
|
---|
| 805 | * or we have not yet seen the split.
|
---|
| 806 | */
|
---|
| 807 | if (grow_idx(old_idx) != new_idx) {
|
---|
| 808 | /*
|
---|
| 809 | * Search the moved bucket. It is guaranteed to contain
|
---|
| 810 | * items of the newly added bucket that were present
|
---|
| 811 | * before the moved bucket was split.
|
---|
| 812 | */
|
---|
| 813 | new_head = h->new_b->head[grow_idx(old_idx)];
|
---|
| 814 | }
|
---|
| 815 |
|
---|
| 816 | /* new_head is now the moved bucket, either valid or invalid. */
|
---|
| 817 |
|
---|
| 818 | /*
|
---|
| 819 | * The old bucket was definitely moved to new_head but the
|
---|
| 820 | * change of new_head had not yet propagated to this cpu.
|
---|
| 821 | */
|
---|
| 822 | if (N_INVALID == get_mark(new_head)) {
|
---|
| 823 | /*
|
---|
| 824 | * We could issue a read_barrier() and make the now valid
|
---|
| 825 | * moved bucket head new_head visible, but instead fall back
|
---|
| 826 | * on using the old bucket. Although the old bucket head is
|
---|
| 827 | * invalid, it points to a node that is allocated and in the
|
---|
| 828 | * right bucket. Before the node can be freed, it must be
|
---|
| 829 | * unlinked from the head (or another item after that item
|
---|
| 830 | * modified the new_head) and a grace period must elapse.
|
---|
| 831 | * As a result had the node been already freed the grace
|
---|
| 832 | * period preceeding the free() would make the unlink and
|
---|
| 833 | * any changes to new_head visible. Therefore, it is safe
|
---|
| 834 | * to use the node pointed to from the old bucket head.
|
---|
| 835 | */
|
---|
| 836 |
|
---|
| 837 | search_head = old_head;
|
---|
| 838 | } else {
|
---|
| 839 | search_head = new_head;
|
---|
| 840 | }
|
---|
| 841 | }
|
---|
| 842 |
|
---|
| 843 | return search_bucket(h, search_head, key, hash);
|
---|
| 844 | } else if (h->b->order > h->new_b->order) {
|
---|
| 845 | /* Shrinking. */
|
---|
| 846 |
|
---|
| 847 | /* Index of the bucket in the old table that was moved. */
|
---|
| 848 | size_t move_src_idx = grow_idx(new_idx);
|
---|
| 849 | marked_ptr_t moved_old_head = h->b->head[move_src_idx];
|
---|
| 850 |
|
---|
| 851 | /*
|
---|
| 852 | * h->b->head[move_src_idx] had already been moved to new_head
|
---|
| 853 | * but the change to new_head had not yet propagated to us.
|
---|
| 854 | */
|
---|
| 855 | if (N_INVALID == get_mark(new_head)) {
|
---|
| 856 | /*
|
---|
| 857 | * new_head is definitely valid and we could make it visible
|
---|
| 858 | * to this cpu with a read_barrier(). Instead, use the bucket
|
---|
| 859 | * in the old table that was moved even though it is now marked
|
---|
| 860 | * as invalid. The node it points to must be allocated because
|
---|
| 861 | * a grace period would have to elapse before it could be freed;
|
---|
| 862 | * and the grace period would make the now valid new_head
|
---|
| 863 | * visible to all cpus.
|
---|
| 864 | *
|
---|
| 865 | * Note that move_src_idx may not be the same as old_idx.
|
---|
| 866 | * If move_src_idx != old_idx then old_idx is the bucket
|
---|
| 867 | * in the old table that is not moved but instead it is
|
---|
| 868 | * appended to the moved bucket, ie it is added at the tail
|
---|
| 869 | * of new_head. In that case an invalid old_head notes that
|
---|
| 870 | * it had already been merged into (the moved) new_head.
|
---|
| 871 | * We will try to search that bucket first because it
|
---|
| 872 | * may contain some newly added nodes after the bucket
|
---|
| 873 | * join. Moreover, the bucket joining link may already be
|
---|
| 874 | * visible even if new_head is not. Therefore, if we're
|
---|
| 875 | * lucky we'll find the item via moved_old_head. In any
|
---|
| 876 | * case, we'll retry in proper old_head if not found.
|
---|
| 877 | */
|
---|
| 878 | search_head = moved_old_head;
|
---|
| 879 | }
|
---|
| 880 |
|
---|
| 881 | cht_link_t *ret = search_bucket(h, search_head, key, hash);
|
---|
| 882 |
|
---|
| 883 | if (ret)
|
---|
| 884 | return ret;
|
---|
| 885 | /*
|
---|
| 886 | * Bucket old_head was already joined with moved_old_head
|
---|
| 887 | * in the new table but we have not yet seen change of the
|
---|
| 888 | * joining link (or the item is not in the table).
|
---|
| 889 | */
|
---|
[6eaed07] | 890 | if (move_src_idx != old_idx && get_next(old_head) != &sentinel) {
|
---|
[7ef2249] | 891 | /*
|
---|
| 892 | * Note that old_head (the bucket to be merged into new_head)
|
---|
| 893 | * points to an allocated join node (if non-null) even if marked
|
---|
| 894 | * invalid. Before the resizer lets join nodes to be unlinked
|
---|
[205832b] | 895 | * (and freed) it sets old_head to NULL and waits for a grace period.
|
---|
[7ef2249] | 896 | * So either the invalid old_head points to join node; or old_head
|
---|
| 897 | * is null and we would have seen a completed bucket join while
|
---|
| 898 | * traversing search_head.
|
---|
| 899 | */
|
---|
| 900 | ASSERT(N_JOIN & get_mark(get_next(old_head)->link));
|
---|
| 901 | return search_bucket(h, old_head, key, hash);
|
---|
| 902 | }
|
---|
| 903 |
|
---|
[205832b] | 904 | return NULL;
|
---|
[7ef2249] | 905 | } else {
|
---|
| 906 | /*
|
---|
| 907 | * Resize is almost done. The resizer is waiting to make
|
---|
| 908 | * sure all cpus see that the new table replaced the old one.
|
---|
| 909 | */
|
---|
| 910 | ASSERT(h->b->order == h->new_b->order);
|
---|
| 911 | /*
|
---|
| 912 | * The resizer must ensure all new bucket heads are visible before
|
---|
| 913 | * replacing the old table.
|
---|
| 914 | */
|
---|
| 915 | ASSERT(N_NORMAL == get_mark(new_head));
|
---|
| 916 | return search_bucket(h, new_head, key, hash);
|
---|
| 917 | }
|
---|
| 918 | }
|
---|
| 919 |
|
---|
[b9cb911] | 920 | /** Inserts an item. Succeeds even if an equal item is already present. */
|
---|
[7ef2249] | 921 | void cht_insert(cht_t *h, cht_link_t *item)
|
---|
| 922 | {
|
---|
[04d66804] | 923 | insert_impl(h, item, NULL);
|
---|
[7ef2249] | 924 | }
|
---|
| 925 |
|
---|
[b9cb911] | 926 | /** Inserts a unique item. Returns false if an equal item was already present.
|
---|
| 927 | *
|
---|
| 928 | * Use this function to atomically check if an equal/duplicate item had
|
---|
| 929 | * not yet been inserted into the table and to insert this item into the
|
---|
| 930 | * table.
|
---|
| 931 | *
|
---|
[991d2d8] | 932 | * The following is @e NOT thread-safe, so do not use:
|
---|
| 933 | * @code
|
---|
[b9cb911] | 934 | * if (!cht_find(h, key)) {
|
---|
| 935 | * // A concurrent insert here may go unnoticed by cht_find() above.
|
---|
| 936 | * item = malloc(..);
|
---|
| 937 | * cht_insert(h, item);
|
---|
| 938 | * // Now we may have two items with equal search keys.
|
---|
| 939 | * }
|
---|
[991d2d8] | 940 | * @endcode
|
---|
[b9cb911] | 941 | *
|
---|
| 942 | * Replace such code with:
|
---|
[991d2d8] | 943 | * @code
|
---|
[b9cb911] | 944 | * item = malloc(..);
|
---|
[04d66804] | 945 | * if (!cht_insert_unique(h, item, &dup_item)) {
|
---|
| 946 | * // Whoops, someone beat us to it - an equal item 'dup_item'
|
---|
| 947 | * // had already been inserted.
|
---|
[b9cb911] | 948 | * free(item);
|
---|
| 949 | * } else {
|
---|
| 950 | * // Successfully inserted the item and we are guaranteed that
|
---|
| 951 | * // there are no other equal items.
|
---|
| 952 | * }
|
---|
[991d2d8] | 953 | * @endcode
|
---|
[b9cb911] | 954 | *
|
---|
| 955 | */
|
---|
[04d66804] | 956 | bool cht_insert_unique(cht_t *h, cht_link_t *item, cht_link_t **dup_item)
|
---|
[7ef2249] | 957 | {
|
---|
[04d66804] | 958 | ASSERT(rcu_read_locked());
|
---|
| 959 | ASSERT(dup_item);
|
---|
| 960 | return insert_impl(h, item, dup_item);
|
---|
[7ef2249] | 961 | }
|
---|
| 962 |
|
---|
[04d66804] | 963 | /** Inserts the item into the table and checks for duplicates if dup_item. */
|
---|
| 964 | static bool insert_impl(cht_t *h, cht_link_t *item, cht_link_t **dup_item)
|
---|
[7ef2249] | 965 | {
|
---|
| 966 | rcu_read_lock();
|
---|
| 967 |
|
---|
[3bb732b] | 968 | cht_buckets_t *b = rcu_access(h->b);
|
---|
[6eaed07] | 969 | memoize_node_hash(h, item);
|
---|
[3bb732b] | 970 | size_t hash = node_hash(h, item);
|
---|
| 971 | size_t idx = calc_bucket_idx(hash, b->order);
|
---|
| 972 | marked_ptr_t *phead = &b->head[idx];
|
---|
| 973 |
|
---|
| 974 | bool resizing = false;
|
---|
[5e5cef3] | 975 | bool inserted = false;
|
---|
[3bb732b] | 976 |
|
---|
| 977 | do {
|
---|
| 978 | walk_mode_t walk_mode = WM_NORMAL;
|
---|
| 979 | bool join_finishing;
|
---|
| 980 |
|
---|
| 981 | resizing = resizing || (N_NORMAL != get_mark(*phead));
|
---|
| 982 |
|
---|
| 983 | /* The table is resizing. Get the correct bucket head. */
|
---|
| 984 | if (resizing) {
|
---|
[14c9aa6] | 985 | upd_resizing_head(h, hash, &phead, &join_finishing, &walk_mode);
|
---|
[3bb732b] | 986 | }
|
---|
| 987 |
|
---|
| 988 | wnd_t wnd = {
|
---|
| 989 | .ppred = phead,
|
---|
| 990 | .cur = get_next(*phead),
|
---|
[205832b] | 991 | .last = NULL
|
---|
[3bb732b] | 992 | };
|
---|
| 993 |
|
---|
| 994 | if (!find_wnd_and_gc(h, hash, walk_mode, &wnd, &resizing)) {
|
---|
| 995 | /* Could not GC a node; or detected an unexpected resize. */
|
---|
| 996 | continue;
|
---|
| 997 | }
|
---|
| 998 |
|
---|
[04d66804] | 999 | if (dup_item && has_duplicate(h, item, hash, wnd.cur, dup_item)) {
|
---|
[3bb732b] | 1000 | rcu_read_unlock();
|
---|
| 1001 | return false;
|
---|
| 1002 | }
|
---|
| 1003 |
|
---|
[14c9aa6] | 1004 | inserted = insert_at(item, &wnd, walk_mode, &resizing);
|
---|
[3bb732b] | 1005 | } while (!inserted);
|
---|
| 1006 |
|
---|
[7ef2249] | 1007 | rcu_read_unlock();
|
---|
[14c9aa6] | 1008 |
|
---|
| 1009 | item_inserted(h);
|
---|
[3bb732b] | 1010 | return true;
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
[b9cb911] | 1013 | /** Inserts item between wnd.ppred and wnd.cur.
|
---|
| 1014 | *
|
---|
| 1015 | * @param item Item to link to wnd.ppred and wnd.cur.
|
---|
| 1016 | * @param wnd The item will be inserted before wnd.cur. Wnd.ppred
|
---|
| 1017 | * must be N_NORMAL.
|
---|
| 1018 | * @param walk_mode
|
---|
| 1019 | * @param resizing Set to true only if the table is undergoing resize
|
---|
| 1020 | * and it was not expected (ie walk_mode == WM_NORMAL).
|
---|
| 1021 | * @return True if the item was successfully linked to wnd.ppred. False
|
---|
| 1022 | * if whole insert operation must be retried because the predecessor
|
---|
| 1023 | * of wnd.cur has changed.
|
---|
| 1024 | */
|
---|
[5e5cef3] | 1025 | inline static bool insert_at(cht_link_t *item, const wnd_t *wnd,
|
---|
| 1026 | walk_mode_t walk_mode, bool *resizing)
|
---|
[3bb732b] | 1027 | {
|
---|
| 1028 | marked_ptr_t ret;
|
---|
| 1029 |
|
---|
| 1030 | if (walk_mode == WM_NORMAL) {
|
---|
| 1031 | item->link = make_link(wnd->cur, N_NORMAL);
|
---|
| 1032 | /* Initialize the item before adding it to a bucket. */
|
---|
| 1033 | memory_barrier();
|
---|
| 1034 |
|
---|
| 1035 | /* Link a clean/normal predecessor to the item. */
|
---|
| 1036 | ret = cas_link(wnd->ppred, wnd->cur, N_NORMAL, item, N_NORMAL);
|
---|
| 1037 |
|
---|
| 1038 | if (ret == make_link(wnd->cur, N_NORMAL)) {
|
---|
| 1039 | return true;
|
---|
| 1040 | } else {
|
---|
[14c9aa6] | 1041 | /* This includes an invalid head but not a const head. */
|
---|
[3bb732b] | 1042 | *resizing = ((N_JOIN_FOLLOWS | N_JOIN) & get_mark(ret));
|
---|
| 1043 | return false;
|
---|
| 1044 | }
|
---|
| 1045 | } else if (walk_mode == WM_MOVE_JOIN_FOLLOWS) {
|
---|
| 1046 | /* Move JOIN_FOLLOWS mark but filter out the DELETED mark. */
|
---|
| 1047 | mark_t jf_mark = get_mark(*wnd->ppred) & N_JOIN_FOLLOWS;
|
---|
| 1048 | item->link = make_link(wnd->cur, jf_mark);
|
---|
| 1049 | /* Initialize the item before adding it to a bucket. */
|
---|
| 1050 | memory_barrier();
|
---|
| 1051 |
|
---|
| 1052 | /* Link the not-deleted predecessor to the item. Move its JF mark. */
|
---|
| 1053 | ret = cas_link(wnd->ppred, wnd->cur, jf_mark, item, N_NORMAL);
|
---|
| 1054 |
|
---|
| 1055 | return ret == make_link(wnd->cur, jf_mark);
|
---|
| 1056 | } else {
|
---|
| 1057 | ASSERT(walk_mode == WM_LEAVE_JOIN);
|
---|
| 1058 |
|
---|
| 1059 | item->link = make_link(wnd->cur, N_NORMAL);
|
---|
| 1060 | /* Initialize the item before adding it to a bucket. */
|
---|
| 1061 | memory_barrier();
|
---|
| 1062 |
|
---|
| 1063 | mark_t pred_mark = get_mark(*wnd->ppred);
|
---|
| 1064 | /* If the predecessor is a join node it may be marked deleted.*/
|
---|
| 1065 | mark_t exp_pred_mark = (N_JOIN & pred_mark) ? pred_mark : N_NORMAL;
|
---|
| 1066 |
|
---|
| 1067 | ret = cas_link(wnd->ppred, wnd->cur, exp_pred_mark, item, exp_pred_mark);
|
---|
| 1068 | return ret == make_link(wnd->cur, exp_pred_mark);
|
---|
| 1069 | }
|
---|
| 1070 | }
|
---|
| 1071 |
|
---|
[04d66804] | 1072 | /** Returns true if the chain starting at cur has an item equal to \a item.
|
---|
[b9cb911] | 1073 | *
|
---|
| 1074 | * @param h CHT to operate on.
|
---|
| 1075 | * @param item Item whose duplicates the function looks for.
|
---|
| 1076 | * @param hash Hash of \a item.
|
---|
[04d66804] | 1077 | * @param[in] cur The first node with a hash greater to or equal to item's hash.
|
---|
| 1078 | * @param[out] dup_item The first duplicate item encountered.
|
---|
[b9cb911] | 1079 | * @return True if a non-deleted item equal to \a item exists in the table.
|
---|
| 1080 | */
|
---|
[04d66804] | 1081 | static inline bool has_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
|
---|
| 1082 | cht_link_t *cur, cht_link_t **dup_item)
|
---|
[3bb732b] | 1083 | {
|
---|
[04d66804] | 1084 | ASSERT(cur);
|
---|
| 1085 | ASSERT(cur == &sentinel || hash <= node_hash(h, cur)
|
---|
| 1086 | || node_hash(h, cur) == h->invalid_hash);
|
---|
[3bb732b] | 1087 |
|
---|
[04d66804] | 1088 | /* hash < node_hash(h, cur) */
|
---|
| 1089 | if (hash != node_hash(h, cur) && h->invalid_hash != node_hash(h, cur))
|
---|
[3bb732b] | 1090 | return false;
|
---|
| 1091 |
|
---|
| 1092 | /*
|
---|
| 1093 | * Load the most recent node marks. Otherwise we might pronounce a
|
---|
| 1094 | * logically deleted node for a duplicate of the item just because
|
---|
| 1095 | * the deleted node's DEL mark had not yet propagated to this cpu.
|
---|
| 1096 | */
|
---|
| 1097 | read_barrier();
|
---|
[04d66804] | 1098 |
|
---|
| 1099 | *dup_item = find_duplicate(h, item, hash, cur);
|
---|
| 1100 | return NULL != *dup_item;
|
---|
[14c9aa6] | 1101 | }
|
---|
| 1102 |
|
---|
[b9cb911] | 1103 | /** Returns an item that is equal to \a item starting in a chain at \a start. */
|
---|
[14c9aa6] | 1104 | static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
|
---|
| 1105 | cht_link_t *start)
|
---|
| 1106 | {
|
---|
[6eaed07] | 1107 | ASSERT(hash <= node_hash(h, start) || h->invalid_hash == node_hash(h, start));
|
---|
[3bb732b] | 1108 |
|
---|
[14c9aa6] | 1109 | cht_link_t *cur = start;
|
---|
[3bb732b] | 1110 |
|
---|
[6eaed07] | 1111 | try_again:
|
---|
| 1112 | ASSERT(cur);
|
---|
| 1113 |
|
---|
| 1114 | while (node_hash(h, cur) == hash) {
|
---|
| 1115 | ASSERT(cur != &sentinel);
|
---|
| 1116 |
|
---|
[3bb732b] | 1117 | bool deleted = (N_DELETED & get_mark(cur->link));
|
---|
| 1118 |
|
---|
| 1119 | /* Skip logically deleted nodes. */
|
---|
| 1120 | if (!deleted && h->op->equal(item, cur))
|
---|
[14c9aa6] | 1121 | return cur;
|
---|
[3bb732b] | 1122 |
|
---|
| 1123 | cur = get_next(cur->link);
|
---|
[6eaed07] | 1124 | ASSERT(cur);
|
---|
[14c9aa6] | 1125 | }
|
---|
[6eaed07] | 1126 |
|
---|
[b9cb911] | 1127 | /* Skip logically deleted nodes with rcu_call() in progress. */
|
---|
[6eaed07] | 1128 | if (h->invalid_hash == node_hash(h, cur)) {
|
---|
| 1129 | cur = get_next(cur->link);
|
---|
| 1130 | goto try_again;
|
---|
| 1131 | }
|
---|
[3bb732b] | 1132 |
|
---|
[205832b] | 1133 | return NULL;
|
---|
[7ef2249] | 1134 | }
|
---|
| 1135 |
|
---|
[b9cb911] | 1136 | /** Removes all items matching the search key. Returns the number of items removed.*/
|
---|
[7ef2249] | 1137 | size_t cht_remove_key(cht_t *h, void *key)
|
---|
| 1138 | {
|
---|
| 1139 | ASSERT(h);
|
---|
| 1140 |
|
---|
[6eaed07] | 1141 | size_t hash = calc_key_hash(h, key);
|
---|
[7ef2249] | 1142 | size_t removed = 0;
|
---|
| 1143 |
|
---|
| 1144 | while (remove_pred(h, hash, h->op->key_equal, key))
|
---|
| 1145 | ++removed;
|
---|
| 1146 |
|
---|
| 1147 | return removed;
|
---|
| 1148 | }
|
---|
| 1149 |
|
---|
[b9cb911] | 1150 | /** Removes a specific item from the table.
|
---|
| 1151 | *
|
---|
| 1152 | * The called must hold rcu read lock.
|
---|
| 1153 | *
|
---|
| 1154 | * @param item Item presumably present in the table and to be removed.
|
---|
| 1155 | * @return True if the item was removed successfully; or false if it had
|
---|
| 1156 | * already been deleted.
|
---|
| 1157 | */
|
---|
[7ef2249] | 1158 | bool cht_remove_item(cht_t *h, cht_link_t *item)
|
---|
| 1159 | {
|
---|
| 1160 | ASSERT(h);
|
---|
| 1161 | ASSERT(item);
|
---|
[b9cb911] | 1162 | /* Otherwise a concurrent cht_remove_key might free the item. */
|
---|
| 1163 | ASSERT(rcu_read_locked());
|
---|
[7ef2249] | 1164 |
|
---|
| 1165 | /*
|
---|
| 1166 | * Even though we know the node we want to delete we must unlink it
|
---|
| 1167 | * from the correct bucket and from a clean/normal predecessor. Therefore,
|
---|
| 1168 | * we search for it again from the beginning of the correct bucket.
|
---|
| 1169 | */
|
---|
[6eaed07] | 1170 | size_t hash = calc_node_hash(h, item);
|
---|
[7ef2249] | 1171 | return remove_pred(h, hash, same_node_pred, item);
|
---|
| 1172 | }
|
---|
| 1173 |
|
---|
[b9cb911] | 1174 | /** Removes an item equal to pred_arg according to the predicate pred. */
|
---|
[7ef2249] | 1175 | static bool remove_pred(cht_t *h, size_t hash, equal_pred_t pred, void *pred_arg)
|
---|
| 1176 | {
|
---|
| 1177 | rcu_read_lock();
|
---|
| 1178 |
|
---|
| 1179 | bool resizing = false;
|
---|
| 1180 | bool deleted = false;
|
---|
| 1181 | bool deleted_but_gc = false;
|
---|
| 1182 |
|
---|
| 1183 | cht_buckets_t *b = rcu_access(h->b);
|
---|
| 1184 | size_t idx = calc_bucket_idx(hash, b->order);
|
---|
| 1185 | marked_ptr_t *phead = &b->head[idx];
|
---|
| 1186 |
|
---|
| 1187 | do {
|
---|
| 1188 | walk_mode_t walk_mode = WM_NORMAL;
|
---|
| 1189 | bool join_finishing = false;
|
---|
| 1190 |
|
---|
| 1191 | resizing = resizing || (N_NORMAL != get_mark(*phead));
|
---|
| 1192 |
|
---|
| 1193 | /* The table is resizing. Get the correct bucket head. */
|
---|
| 1194 | if (resizing) {
|
---|
[14c9aa6] | 1195 | upd_resizing_head(h, hash, &phead, &join_finishing, &walk_mode);
|
---|
[7ef2249] | 1196 | }
|
---|
| 1197 |
|
---|
| 1198 | wnd_t wnd = {
|
---|
| 1199 | .ppred = phead,
|
---|
| 1200 | .cur = get_next(*phead),
|
---|
[205832b] | 1201 | .last = NULL
|
---|
[7ef2249] | 1202 | };
|
---|
| 1203 |
|
---|
| 1204 | if (!find_wnd_and_gc_pred(
|
---|
| 1205 | h, hash, walk_mode, pred, pred_arg, &wnd, &resizing)) {
|
---|
| 1206 | /* Could not GC a node; or detected an unexpected resize. */
|
---|
| 1207 | continue;
|
---|
| 1208 | }
|
---|
| 1209 |
|
---|
| 1210 | /*
|
---|
| 1211 | * The item lookup is affected by a bucket join but effects of
|
---|
| 1212 | * the bucket join have not been seen while searching for the item.
|
---|
| 1213 | */
|
---|
| 1214 | if (join_finishing && !join_completed(h, &wnd)) {
|
---|
| 1215 | /*
|
---|
| 1216 | * Bucket was appended at the end of another but the next
|
---|
| 1217 | * ptr linking them together was not visible on this cpu.
|
---|
| 1218 | * join_completed() makes this appended bucket visible.
|
---|
| 1219 | */
|
---|
| 1220 | continue;
|
---|
| 1221 | }
|
---|
| 1222 |
|
---|
| 1223 | /* Already deleted, but delete_at() requested one GC pass. */
|
---|
| 1224 | if (deleted_but_gc)
|
---|
| 1225 | break;
|
---|
| 1226 |
|
---|
[6eaed07] | 1227 | bool found = (wnd.cur != &sentinel && pred(pred_arg, wnd.cur));
|
---|
[7ef2249] | 1228 |
|
---|
| 1229 | if (!found) {
|
---|
| 1230 | rcu_read_unlock();
|
---|
| 1231 | return false;
|
---|
| 1232 | }
|
---|
| 1233 |
|
---|
[14c9aa6] | 1234 | deleted = delete_at(h, &wnd, walk_mode, &deleted_but_gc, &resizing);
|
---|
[7ef2249] | 1235 | } while (!deleted || deleted_but_gc);
|
---|
| 1236 |
|
---|
| 1237 | rcu_read_unlock();
|
---|
| 1238 | return true;
|
---|
| 1239 | }
|
---|
| 1240 |
|
---|
[b9cb911] | 1241 | /** Unlinks wnd.cur from wnd.ppred and schedules a deferred free for the item.
|
---|
| 1242 | *
|
---|
| 1243 | * Ignores nodes marked N_JOIN if walk mode is WM_LEAVE_JOIN.
|
---|
| 1244 | *
|
---|
| 1245 | * @param h CHT to operate on.
|
---|
| 1246 | * @param wnd Points to the item to delete and its N_NORMAL predecessor.
|
---|
| 1247 | * @param walk_mode Bucket chaing walk mode.
|
---|
| 1248 | * @param deleted_but_gc Set to true if the item had been logically deleted,
|
---|
| 1249 | * but a garbage collecting walk of the bucket is in order for
|
---|
| 1250 | * it to be fully unlinked.
|
---|
| 1251 | * @param resizing Set to true if the table is undergoing an unexpected
|
---|
| 1252 | * resize (ie walk_mode == WM_NORMAL).
|
---|
| 1253 | * @return False if the wnd.ppred changed in the meantime and the whole
|
---|
| 1254 | * delete operation must be retried.
|
---|
| 1255 | */
|
---|
[5e5cef3] | 1256 | static inline bool delete_at(cht_t *h, wnd_t *wnd, walk_mode_t walk_mode,
|
---|
[7ef2249] | 1257 | bool *deleted_but_gc, bool *resizing)
|
---|
| 1258 | {
|
---|
[6eaed07] | 1259 | ASSERT(wnd->cur && wnd->cur != &sentinel);
|
---|
[7ef2249] | 1260 |
|
---|
| 1261 | *deleted_but_gc = false;
|
---|
| 1262 |
|
---|
| 1263 | if (!mark_deleted(wnd->cur, walk_mode, resizing)) {
|
---|
| 1264 | /* Already deleted, or unexpectedly marked as JOIN/JOIN_FOLLOWS. */
|
---|
| 1265 | return false;
|
---|
| 1266 | }
|
---|
| 1267 |
|
---|
| 1268 | /* Marked deleted. Unlink from the bucket. */
|
---|
| 1269 |
|
---|
| 1270 | /* Never unlink join nodes. */
|
---|
| 1271 | if (walk_mode == WM_LEAVE_JOIN && (N_JOIN & get_mark(wnd->cur->link)))
|
---|
| 1272 | return true;
|
---|
| 1273 |
|
---|
[14c9aa6] | 1274 | cas_order_barrier();
|
---|
| 1275 |
|
---|
[7ef2249] | 1276 | if (unlink_from_pred(wnd, walk_mode, resizing)) {
|
---|
| 1277 | free_later(h, wnd->cur);
|
---|
| 1278 | } else {
|
---|
| 1279 | *deleted_but_gc = true;
|
---|
| 1280 | }
|
---|
| 1281 |
|
---|
| 1282 | return true;
|
---|
| 1283 | }
|
---|
| 1284 |
|
---|
[b9cb911] | 1285 | /** Marks cur logically deleted. Returns false to request a retry. */
|
---|
[5e5cef3] | 1286 | static inline bool mark_deleted(cht_link_t *cur, walk_mode_t walk_mode,
|
---|
| 1287 | bool *resizing)
|
---|
[7ef2249] | 1288 | {
|
---|
[6eaed07] | 1289 | ASSERT(cur && cur != &sentinel);
|
---|
[7ef2249] | 1290 |
|
---|
| 1291 | /*
|
---|
| 1292 | * Btw, we could loop here if the cas fails but let's not complicate
|
---|
| 1293 | * things and let's retry from the head of the bucket.
|
---|
| 1294 | */
|
---|
| 1295 |
|
---|
| 1296 | cht_link_t *next = get_next(cur->link);
|
---|
| 1297 |
|
---|
| 1298 | if (walk_mode == WM_NORMAL) {
|
---|
| 1299 | /* Only mark clean/normal nodes - JF/JN is used only during resize. */
|
---|
[14c9aa6] | 1300 | marked_ptr_t ret = cas_link(&cur->link, next, N_NORMAL, next, N_DELETED);
|
---|
[7ef2249] | 1301 |
|
---|
[14c9aa6] | 1302 | if (ret != make_link(next, N_NORMAL)) {
|
---|
| 1303 | *resizing = (N_JOIN | N_JOIN_FOLLOWS) & get_mark(ret);
|
---|
[7ef2249] | 1304 | return false;
|
---|
| 1305 | }
|
---|
| 1306 | } else {
|
---|
| 1307 | ASSERT(N_JOIN == N_JOIN_FOLLOWS);
|
---|
| 1308 |
|
---|
| 1309 | /* Keep the N_JOIN/N_JOIN_FOLLOWS mark but strip N_DELETED. */
|
---|
| 1310 | mark_t cur_mark = get_mark(cur->link) & N_JOIN_FOLLOWS;
|
---|
| 1311 |
|
---|
[14c9aa6] | 1312 | marked_ptr_t ret =
|
---|
| 1313 | cas_link(&cur->link, next, cur_mark, next, cur_mark | N_DELETED);
|
---|
[7ef2249] | 1314 |
|
---|
[14c9aa6] | 1315 | if (ret != make_link(next, cur_mark))
|
---|
[7ef2249] | 1316 | return false;
|
---|
| 1317 | }
|
---|
| 1318 |
|
---|
| 1319 | return true;
|
---|
| 1320 | }
|
---|
| 1321 |
|
---|
[b9cb911] | 1322 | /** Unlinks wnd.cur from wnd.ppred. Returns false if it should be retried. */
|
---|
[5e5cef3] | 1323 | static inline bool unlink_from_pred(wnd_t *wnd, walk_mode_t walk_mode,
|
---|
| 1324 | bool *resizing)
|
---|
[7ef2249] | 1325 | {
|
---|
[6eaed07] | 1326 | ASSERT(wnd->cur != &sentinel);
|
---|
[7ef2249] | 1327 | ASSERT(wnd->cur && (N_DELETED & get_mark(wnd->cur->link)));
|
---|
| 1328 |
|
---|
| 1329 | cht_link_t *next = get_next(wnd->cur->link);
|
---|
| 1330 |
|
---|
| 1331 | if (walk_mode == WM_LEAVE_JOIN) {
|
---|
| 1332 | /* Never try to unlink join nodes. */
|
---|
| 1333 | ASSERT(!(N_JOIN & get_mark(wnd->cur->link)));
|
---|
| 1334 |
|
---|
| 1335 | mark_t pred_mark = get_mark(*wnd->ppred);
|
---|
[14c9aa6] | 1336 | /* Succeed only if the predecessor is clean/normal or a join node. */
|
---|
[7ef2249] | 1337 | mark_t exp_pred_mark = (N_JOIN & pred_mark) ? pred_mark : N_NORMAL;
|
---|
| 1338 |
|
---|
| 1339 | marked_ptr_t pred_link = make_link(wnd->cur, exp_pred_mark);
|
---|
| 1340 | marked_ptr_t next_link = make_link(next, exp_pred_mark);
|
---|
| 1341 |
|
---|
[14c9aa6] | 1342 | if (pred_link != _cas_link(wnd->ppred, pred_link, next_link))
|
---|
[7ef2249] | 1343 | return false;
|
---|
| 1344 | } else {
|
---|
| 1345 | ASSERT(walk_mode == WM_MOVE_JOIN_FOLLOWS || walk_mode == WM_NORMAL);
|
---|
| 1346 | /* Move the JF mark if set. Clear DEL mark. */
|
---|
| 1347 | mark_t cur_mark = N_JOIN_FOLLOWS & get_mark(wnd->cur->link);
|
---|
| 1348 |
|
---|
| 1349 | /* The predecessor must be clean/normal. */
|
---|
| 1350 | marked_ptr_t pred_link = make_link(wnd->cur, N_NORMAL);
|
---|
| 1351 | /* Link to cur's successor keeping/copying cur's JF mark. */
|
---|
| 1352 | marked_ptr_t next_link = make_link(next, cur_mark);
|
---|
| 1353 |
|
---|
[14c9aa6] | 1354 | marked_ptr_t ret = _cas_link(wnd->ppred, pred_link, next_link);
|
---|
[7ef2249] | 1355 |
|
---|
| 1356 | if (pred_link != ret) {
|
---|
| 1357 | /* If we're not resizing the table there are no JF/JN nodes. */
|
---|
| 1358 | *resizing = (walk_mode == WM_NORMAL)
|
---|
| 1359 | && (N_JOIN_FOLLOWS & get_mark(ret));
|
---|
| 1360 | return false;
|
---|
| 1361 | }
|
---|
| 1362 | }
|
---|
| 1363 |
|
---|
| 1364 | return true;
|
---|
| 1365 | }
|
---|
| 1366 |
|
---|
[b9cb911] | 1367 | /** Finds the first non-deleted item equal to \a pred_arg according to \a pred.
|
---|
| 1368 | *
|
---|
| 1369 | * The function returns the candidate item in \a wnd. Logically deleted
|
---|
| 1370 | * nodes are garbage collected so the predecessor will most likely not
|
---|
| 1371 | * be marked as deleted.
|
---|
| 1372 | *
|
---|
| 1373 | * Unlike find_wnd_and_gc(), this function never returns a node that
|
---|
| 1374 | * is known to have already been marked N_DELETED.
|
---|
| 1375 | *
|
---|
| 1376 | * Any logically deleted nodes (ie those marked N_DELETED) are garbage
|
---|
| 1377 | * collected, ie free in the background via rcu_call (except for join-nodes
|
---|
| 1378 | * if walk_mode == WM_LEAVE_JOIN).
|
---|
| 1379 | *
|
---|
| 1380 | * @param h CHT to operate on.
|
---|
| 1381 | * @param hash Hash the search for.
|
---|
| 1382 | * @param walk_mode Bucket chain walk mode.
|
---|
| 1383 | * @param pred Predicate used to find an item equal to pred_arg.
|
---|
| 1384 | * @param pred_arg Argument to pass to the equality predicate \a pred.
|
---|
| 1385 | * @param[in,out] wnd The search starts with wnd.cur. If the desired
|
---|
| 1386 | * item is found wnd.cur will point to it.
|
---|
| 1387 | * @param resizing Set to true if the table is resizing but it was not
|
---|
| 1388 | * expected (ie walk_mode == WM_NORMAL).
|
---|
| 1389 | * @return False if the operation has to be retried. True otherwise
|
---|
| 1390 | * (even if an equal item had not been found).
|
---|
| 1391 | */
|
---|
[7ef2249] | 1392 | static bool find_wnd_and_gc_pred(cht_t *h, size_t hash, walk_mode_t walk_mode,
|
---|
| 1393 | equal_pred_t pred, void *pred_arg, wnd_t *wnd, bool *resizing)
|
---|
| 1394 | {
|
---|
[6eaed07] | 1395 | ASSERT(wnd->cur);
|
---|
| 1396 |
|
---|
| 1397 | if (wnd->cur == &sentinel)
|
---|
[7ef2249] | 1398 | return true;
|
---|
| 1399 |
|
---|
| 1400 | /*
|
---|
| 1401 | * A read barrier is not needed here to bring up the most recent
|
---|
| 1402 | * node marks (esp the N_DELETED). At worst we'll try to delete
|
---|
| 1403 | * an already deleted node; fail in delete_at(); and retry.
|
---|
| 1404 | */
|
---|
| 1405 |
|
---|
[6eaed07] | 1406 | size_t cur_hash;
|
---|
| 1407 |
|
---|
| 1408 | try_again:
|
---|
| 1409 | cur_hash = node_hash(h, wnd->cur);
|
---|
[7ef2249] | 1410 |
|
---|
| 1411 | while (cur_hash <= hash) {
|
---|
[6eaed07] | 1412 | ASSERT(wnd->cur && wnd->cur != &sentinel);
|
---|
| 1413 |
|
---|
[7ef2249] | 1414 | /* GC any deleted nodes on the way. */
|
---|
| 1415 | if (N_DELETED & get_mark(wnd->cur->link)) {
|
---|
| 1416 | if (!gc_deleted_node(h, walk_mode, wnd, resizing)) {
|
---|
| 1417 | /* Retry from the head of a bucket. */
|
---|
| 1418 | return false;
|
---|
| 1419 | }
|
---|
| 1420 | } else {
|
---|
| 1421 | /* Is this the node we were looking for? */
|
---|
| 1422 | if (cur_hash == hash && pred(pred_arg, wnd->cur))
|
---|
| 1423 | return true;
|
---|
| 1424 |
|
---|
| 1425 | next_wnd(wnd);
|
---|
| 1426 | }
|
---|
| 1427 |
|
---|
| 1428 | cur_hash = node_hash(h, wnd->cur);
|
---|
| 1429 | }
|
---|
| 1430 |
|
---|
[6eaed07] | 1431 | if (cur_hash == h->invalid_hash) {
|
---|
| 1432 | next_wnd(wnd);
|
---|
| 1433 | ASSERT(wnd->cur);
|
---|
| 1434 | goto try_again;
|
---|
| 1435 | }
|
---|
| 1436 |
|
---|
[7ef2249] | 1437 | /* The searched for node is not in the current bucket. */
|
---|
| 1438 | return true;
|
---|
| 1439 | }
|
---|
| 1440 |
|
---|
[b9cb911] | 1441 | /** Find the first item (deleted or not) with a hash greater or equal to \a hash.
|
---|
| 1442 | *
|
---|
| 1443 | * The function returns the first item with a hash that is greater or
|
---|
| 1444 | * equal to \a hash in \a wnd. Moreover it garbage collects logically
|
---|
| 1445 | * deleted node that have not yet been unlinked and freed. Therefore,
|
---|
| 1446 | * the returned node's predecessor will most likely be N_NORMAL.
|
---|
| 1447 | *
|
---|
| 1448 | * Unlike find_wnd_and_gc_pred(), this function may return a node
|
---|
| 1449 | * that is known to had been marked N_DELETED.
|
---|
| 1450 | *
|
---|
| 1451 | * @param h CHT to operate on.
|
---|
| 1452 | * @param hash Hash of the item to find.
|
---|
| 1453 | * @param walk_mode Bucket chain walk mode.
|
---|
| 1454 | * @param[in,out] wnd wnd.cur denotes the first node of the chain. If the
|
---|
| 1455 | * the operation is successful, \a wnd points to the desired
|
---|
| 1456 | * item.
|
---|
| 1457 | * @param resizing Set to true if a table resize was detected but walk_mode
|
---|
| 1458 | * suggested the table was not undergoing a resize.
|
---|
| 1459 | * @return False indicates the operation must be retried. True otherwise
|
---|
| 1460 | * (even if an item with exactly the same has was not found).
|
---|
| 1461 | */
|
---|
[7ef2249] | 1462 | static bool find_wnd_and_gc(cht_t *h, size_t hash, walk_mode_t walk_mode,
|
---|
| 1463 | wnd_t *wnd, bool *resizing)
|
---|
| 1464 | {
|
---|
[6eaed07] | 1465 | try_again:
|
---|
| 1466 | ASSERT(wnd->cur);
|
---|
| 1467 |
|
---|
| 1468 | while (node_hash(h, wnd->cur) < hash) {
|
---|
[7ef2249] | 1469 | /* GC any deleted nodes along the way to our desired node. */
|
---|
| 1470 | if (N_DELETED & get_mark(wnd->cur->link)) {
|
---|
| 1471 | if (!gc_deleted_node(h, walk_mode, wnd, resizing)) {
|
---|
| 1472 | /* Failed to remove the garbage node. Retry. */
|
---|
| 1473 | return false;
|
---|
| 1474 | }
|
---|
| 1475 | } else {
|
---|
| 1476 | next_wnd(wnd);
|
---|
| 1477 | }
|
---|
[6eaed07] | 1478 |
|
---|
| 1479 | ASSERT(wnd->cur);
|
---|
[7ef2249] | 1480 | }
|
---|
| 1481 |
|
---|
[6eaed07] | 1482 | if (node_hash(h, wnd->cur) == h->invalid_hash) {
|
---|
| 1483 | next_wnd(wnd);
|
---|
| 1484 | goto try_again;
|
---|
| 1485 | }
|
---|
| 1486 |
|
---|
[205832b] | 1487 | /* wnd->cur may be NULL or even marked N_DELETED. */
|
---|
[7ef2249] | 1488 | return true;
|
---|
| 1489 | }
|
---|
| 1490 |
|
---|
[b9cb911] | 1491 | /** Garbage collects the N_DELETED node at \a wnd skipping join nodes. */
|
---|
[7ef2249] | 1492 | static bool gc_deleted_node(cht_t *h, walk_mode_t walk_mode, wnd_t *wnd,
|
---|
| 1493 | bool *resizing)
|
---|
| 1494 | {
|
---|
| 1495 | ASSERT(N_DELETED & get_mark(wnd->cur->link));
|
---|
| 1496 |
|
---|
| 1497 | /* Skip deleted JOIN nodes. */
|
---|
| 1498 | if (walk_mode == WM_LEAVE_JOIN && (N_JOIN & get_mark(wnd->cur->link))) {
|
---|
| 1499 | next_wnd(wnd);
|
---|
| 1500 | } else {
|
---|
| 1501 | /* Ordinary deleted node or a deleted JOIN_FOLLOWS. */
|
---|
| 1502 | ASSERT(walk_mode != WM_LEAVE_JOIN
|
---|
| 1503 | || !((N_JOIN | N_JOIN_FOLLOWS) & get_mark(wnd->cur->link)));
|
---|
| 1504 |
|
---|
| 1505 | /* Unlink an ordinary deleted node, move JOIN_FOLLOWS mark. */
|
---|
| 1506 | if (!unlink_from_pred(wnd, walk_mode, resizing)) {
|
---|
| 1507 | /* Retry. The predecessor was deleted, invalid, const, join_follows. */
|
---|
| 1508 | return false;
|
---|
| 1509 | }
|
---|
| 1510 |
|
---|
| 1511 | free_later(h, wnd->cur);
|
---|
| 1512 |
|
---|
| 1513 | /* Leave ppred as is. */
|
---|
| 1514 | wnd->last = wnd->cur;
|
---|
| 1515 | wnd->cur = get_next(wnd->cur->link);
|
---|
| 1516 | }
|
---|
| 1517 |
|
---|
| 1518 | return true;
|
---|
| 1519 | }
|
---|
| 1520 |
|
---|
[b9cb911] | 1521 | /** Returns true if a bucket join had already completed.
|
---|
| 1522 | *
|
---|
| 1523 | * May only be called if upd_resizing_head() indicates a bucket join
|
---|
| 1524 | * may be in progress.
|
---|
| 1525 | *
|
---|
| 1526 | * If it returns false, the search must be retried in order to guarantee
|
---|
| 1527 | * all item that should have been encountered have been seen.
|
---|
| 1528 | */
|
---|
[7ef2249] | 1529 | static bool join_completed(cht_t *h, const wnd_t *wnd)
|
---|
| 1530 | {
|
---|
| 1531 | /*
|
---|
| 1532 | * The table is shrinking and the searched for item is in a bucket
|
---|
| 1533 | * appended to another. Check that the link joining these two buckets
|
---|
| 1534 | * is visible and if not, make it visible to this cpu.
|
---|
| 1535 | */
|
---|
| 1536 |
|
---|
| 1537 | /*
|
---|
| 1538 | * Resizer ensures h->b->order stays the same for the duration of this
|
---|
| 1539 | * func. We got here because there was an alternative head to search.
|
---|
| 1540 | * The resizer waits for all preexisting readers to finish after
|
---|
| 1541 | * it
|
---|
| 1542 | */
|
---|
| 1543 | ASSERT(h->b->order > h->new_b->order);
|
---|
[6eaed07] | 1544 | ASSERT(wnd->cur);
|
---|
[7ef2249] | 1545 |
|
---|
| 1546 | /* Either we did not need the joining link or we have already followed it.*/
|
---|
[6eaed07] | 1547 | if (wnd->cur != &sentinel)
|
---|
[7ef2249] | 1548 | return true;
|
---|
| 1549 |
|
---|
| 1550 | /* We have reached the end of a bucket. */
|
---|
| 1551 |
|
---|
[6eaed07] | 1552 | if (wnd->last != &sentinel) {
|
---|
[7ef2249] | 1553 | size_t last_seen_hash = node_hash(h, wnd->last);
|
---|
[6eaed07] | 1554 |
|
---|
| 1555 | if (last_seen_hash == h->invalid_hash) {
|
---|
| 1556 | last_seen_hash = calc_node_hash(h, wnd->last);
|
---|
| 1557 | }
|
---|
| 1558 |
|
---|
[7ef2249] | 1559 | size_t last_old_idx = calc_bucket_idx(last_seen_hash, h->b->order);
|
---|
| 1560 | size_t move_src_idx = grow_idx(shrink_idx(last_old_idx));
|
---|
| 1561 |
|
---|
| 1562 | /*
|
---|
[6eaed07] | 1563 | * Last node seen was in the joining bucket - if the searched
|
---|
| 1564 | * for node is there we will find it.
|
---|
[7ef2249] | 1565 | */
|
---|
| 1566 | if (move_src_idx != last_old_idx)
|
---|
| 1567 | return true;
|
---|
| 1568 | }
|
---|
| 1569 |
|
---|
| 1570 | /*
|
---|
| 1571 | * Reached the end of the bucket but no nodes from the joining bucket
|
---|
| 1572 | * were seen. There should have at least been a JOIN node so we have
|
---|
| 1573 | * definitely not seen (and followed) the joining link. Make the link
|
---|
| 1574 | * visible and retry.
|
---|
| 1575 | */
|
---|
| 1576 | read_barrier();
|
---|
| 1577 | return false;
|
---|
| 1578 | }
|
---|
| 1579 |
|
---|
[b9cb911] | 1580 | /** When resizing returns the bucket head to start the search with in \a phead.
|
---|
| 1581 | *
|
---|
| 1582 | * If a resize had been detected (eg cht_t.b.head[idx] is marked immutable).
|
---|
| 1583 | * upd_resizing_head() moves the bucket for \a hash from the old head
|
---|
| 1584 | * to the new head. Moreover, it splits or joins buckets as necessary.
|
---|
| 1585 | *
|
---|
| 1586 | * @param h CHT to operate on.
|
---|
| 1587 | * @param hash Hash of an item whose chain we would like to traverse.
|
---|
| 1588 | * @param[out] phead Head of the bucket to search for \a hash.
|
---|
| 1589 | * @param[out] join_finishing Set to true if a bucket join might be
|
---|
| 1590 | * in progress and the bucket may have to traversed again
|
---|
| 1591 | * as indicated by join_completed().
|
---|
| 1592 | * @param[out] walk_mode Specifies how to interpret node marks.
|
---|
| 1593 | */
|
---|
[7ef2249] | 1594 | static void upd_resizing_head(cht_t *h, size_t hash, marked_ptr_t **phead,
|
---|
[14c9aa6] | 1595 | bool *join_finishing, walk_mode_t *walk_mode)
|
---|
[7ef2249] | 1596 | {
|
---|
| 1597 | cht_buckets_t *b = rcu_access(h->b);
|
---|
| 1598 | size_t old_idx = calc_bucket_idx(hash, b->order);
|
---|
| 1599 | size_t new_idx = calc_bucket_idx(hash, h->new_b->order);
|
---|
| 1600 |
|
---|
| 1601 | marked_ptr_t *pold_head = &b->head[old_idx];
|
---|
| 1602 | marked_ptr_t *pnew_head = &h->new_b->head[new_idx];
|
---|
| 1603 |
|
---|
| 1604 | /* In any case, use the bucket in the new table. */
|
---|
| 1605 | *phead = pnew_head;
|
---|
| 1606 |
|
---|
| 1607 | /* Growing the table. */
|
---|
| 1608 | if (b->order < h->new_b->order) {
|
---|
| 1609 | size_t move_dest_idx = grow_idx(old_idx);
|
---|
| 1610 | marked_ptr_t *pmoved_head = &h->new_b->head[move_dest_idx];
|
---|
| 1611 |
|
---|
| 1612 | /* Complete moving the bucket from the old to the new table. */
|
---|
[14c9aa6] | 1613 | help_head_move(pold_head, pmoved_head);
|
---|
[7ef2249] | 1614 |
|
---|
| 1615 | /* The hash belongs to the moved bucket. */
|
---|
| 1616 | if (move_dest_idx == new_idx) {
|
---|
[14c9aa6] | 1617 | ASSERT(pmoved_head == pnew_head);
|
---|
[7ef2249] | 1618 | /*
|
---|
| 1619 | * move_head() makes the new head of the moved bucket visible.
|
---|
| 1620 | * The new head may be marked with a JOIN_FOLLOWS
|
---|
| 1621 | */
|
---|
[14c9aa6] | 1622 | ASSERT(!(N_CONST & get_mark(*pmoved_head)));
|
---|
[7ef2249] | 1623 | *walk_mode = WM_MOVE_JOIN_FOLLOWS;
|
---|
| 1624 | } else {
|
---|
[14c9aa6] | 1625 | ASSERT(pmoved_head != pnew_head);
|
---|
[7ef2249] | 1626 | /*
|
---|
| 1627 | * The hash belongs to the bucket that is the result of splitting
|
---|
| 1628 | * the old/moved bucket, ie the bucket that contains the second
|
---|
| 1629 | * half of the split/old/moved bucket.
|
---|
| 1630 | */
|
---|
| 1631 |
|
---|
| 1632 | /* The moved bucket has not yet been split. */
|
---|
| 1633 | if (N_NORMAL != get_mark(*pnew_head)) {
|
---|
[3bb732b] | 1634 | size_t split_hash = calc_split_hash(new_idx, h->new_b->order);
|
---|
[14c9aa6] | 1635 | split_bucket(h, pmoved_head, pnew_head, split_hash);
|
---|
[7ef2249] | 1636 | /*
|
---|
| 1637 | * split_bucket() makes the new head visible. No
|
---|
| 1638 | * JOIN_FOLLOWS in this part of split bucket.
|
---|
| 1639 | */
|
---|
| 1640 | ASSERT(N_NORMAL == get_mark(*pnew_head));
|
---|
| 1641 | }
|
---|
| 1642 |
|
---|
| 1643 | *walk_mode = WM_LEAVE_JOIN;
|
---|
| 1644 | }
|
---|
| 1645 | } else if (h->new_b->order < b->order ) {
|
---|
| 1646 | /* Shrinking the table. */
|
---|
| 1647 |
|
---|
| 1648 | size_t move_src_idx = grow_idx(new_idx);
|
---|
| 1649 |
|
---|
| 1650 | /*
|
---|
| 1651 | * Complete moving the bucket from the old to the new table.
|
---|
| 1652 | * Makes a valid pnew_head visible if already moved.
|
---|
| 1653 | */
|
---|
[14c9aa6] | 1654 | help_head_move(&b->head[move_src_idx], pnew_head);
|
---|
[7ef2249] | 1655 |
|
---|
| 1656 | /* Hash belongs to the bucket to be joined with the moved bucket. */
|
---|
| 1657 | if (move_src_idx != old_idx) {
|
---|
| 1658 | /* Bucket join not yet completed. */
|
---|
| 1659 | if (N_INVALID != get_mark(*pold_head)) {
|
---|
[3bb732b] | 1660 | size_t split_hash = calc_split_hash(old_idx, b->order);
|
---|
[14c9aa6] | 1661 | join_buckets(h, pold_head, pnew_head, split_hash);
|
---|
[7ef2249] | 1662 | }
|
---|
| 1663 |
|
---|
[6eaed07] | 1664 | /*
|
---|
| 1665 | * The resizer sets pold_head to &sentinel when all cpus are
|
---|
| 1666 | * guaranteed to see the bucket join.
|
---|
| 1667 | */
|
---|
| 1668 | *join_finishing = (&sentinel != get_next(*pold_head));
|
---|
[7ef2249] | 1669 | }
|
---|
| 1670 |
|
---|
| 1671 | /* move_head() or join_buckets() makes it so or makes the mark visible.*/
|
---|
| 1672 | ASSERT(N_INVALID == get_mark(*pold_head));
|
---|
| 1673 | /* move_head() makes it visible. No JOIN_FOLLOWS used when shrinking. */
|
---|
| 1674 | ASSERT(N_NORMAL == get_mark(*pnew_head));
|
---|
| 1675 |
|
---|
| 1676 | *walk_mode = WM_LEAVE_JOIN;
|
---|
| 1677 | } else {
|
---|
| 1678 | /*
|
---|
| 1679 | * Final stage of resize. The resizer is waiting for all
|
---|
| 1680 | * readers to notice that the old table had been replaced.
|
---|
| 1681 | */
|
---|
| 1682 | ASSERT(b == h->new_b);
|
---|
| 1683 | *walk_mode = WM_NORMAL;
|
---|
| 1684 | }
|
---|
| 1685 | }
|
---|
| 1686 |
|
---|
| 1687 |
|
---|
[3bb732b] | 1688 | #if 0
|
---|
[7ef2249] | 1689 | static void move_head(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head)
|
---|
| 1690 | {
|
---|
| 1691 | start_head_move(psrc_head);
|
---|
[14c9aa6] | 1692 | cas_order_barrier();
|
---|
[7ef2249] | 1693 | complete_head_move(psrc_head, pdest_head);
|
---|
| 1694 | }
|
---|
[3bb732b] | 1695 | #endif
|
---|
[7ef2249] | 1696 |
|
---|
[b9cb911] | 1697 | /** Moves an immutable head \a psrc_head of cht_t.b to \a pdest_head of cht_t.new_b.
|
---|
| 1698 | *
|
---|
| 1699 | * The function guarantees the move will be visible on this cpu once
|
---|
| 1700 | * it completes. In particular, *pdest_head will not be N_INVALID.
|
---|
| 1701 | *
|
---|
| 1702 | * Unlike complete_head_move(), help_head_move() checks if the head had already
|
---|
| 1703 | * been moved and tries to avoid moving the bucket heads if possible.
|
---|
| 1704 | */
|
---|
[5e5cef3] | 1705 | static inline void help_head_move(marked_ptr_t *psrc_head,
|
---|
| 1706 | marked_ptr_t *pdest_head)
|
---|
[7ef2249] | 1707 | {
|
---|
| 1708 | /* Head move has to in progress already when calling this func. */
|
---|
| 1709 | ASSERT(N_CONST & get_mark(*psrc_head));
|
---|
| 1710 |
|
---|
| 1711 | /* Head already moved. */
|
---|
| 1712 | if (N_INVALID == get_mark(*psrc_head)) {
|
---|
| 1713 | /* Effects of the head move have not yet propagated to this cpu. */
|
---|
| 1714 | if (N_INVALID == get_mark(*pdest_head)) {
|
---|
| 1715 | /* Make the move visible on this cpu. */
|
---|
| 1716 | read_barrier();
|
---|
| 1717 | }
|
---|
| 1718 | } else {
|
---|
| 1719 | complete_head_move(psrc_head, pdest_head);
|
---|
| 1720 | }
|
---|
[14c9aa6] | 1721 |
|
---|
| 1722 | ASSERT(!(N_CONST & get_mark(*pdest_head)));
|
---|
[7ef2249] | 1723 | }
|
---|
| 1724 |
|
---|
[b9cb911] | 1725 | /** Initiates the move of the old head \a psrc_head.
|
---|
| 1726 | *
|
---|
| 1727 | * The move may be completed with help_head_move().
|
---|
| 1728 | */
|
---|
[7ef2249] | 1729 | static void start_head_move(marked_ptr_t *psrc_head)
|
---|
| 1730 | {
|
---|
| 1731 | /* Mark src head immutable. */
|
---|
| 1732 | mark_const(psrc_head);
|
---|
| 1733 | }
|
---|
| 1734 |
|
---|
[b9cb911] | 1735 | /** Marks the head immutable. */
|
---|
[7ef2249] | 1736 | static void mark_const(marked_ptr_t *psrc_head)
|
---|
| 1737 | {
|
---|
| 1738 | marked_ptr_t ret, src_link;
|
---|
| 1739 |
|
---|
| 1740 | /* Mark src head immutable. */
|
---|
| 1741 | do {
|
---|
| 1742 | cht_link_t *next = get_next(*psrc_head);
|
---|
| 1743 | src_link = make_link(next, N_NORMAL);
|
---|
| 1744 |
|
---|
| 1745 | /* Mark the normal/clean src link immutable/const. */
|
---|
| 1746 | ret = cas_link(psrc_head, next, N_NORMAL, next, N_CONST);
|
---|
| 1747 | } while(ret != src_link && !(N_CONST & get_mark(ret)));
|
---|
| 1748 | }
|
---|
| 1749 |
|
---|
[b9cb911] | 1750 | /** Completes moving head psrc_head to pdest_head (started by start_head_move()).*/
|
---|
[7ef2249] | 1751 | static void complete_head_move(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head)
|
---|
| 1752 | {
|
---|
| 1753 | ASSERT(N_JOIN_FOLLOWS != get_mark(*psrc_head));
|
---|
| 1754 | ASSERT(N_CONST & get_mark(*psrc_head));
|
---|
| 1755 |
|
---|
| 1756 | cht_link_t *next = get_next(*psrc_head);
|
---|
[14c9aa6] | 1757 | marked_ptr_t ret;
|
---|
| 1758 |
|
---|
[6eaed07] | 1759 | ret = cas_link(pdest_head, &sentinel, N_INVALID, next, N_NORMAL);
|
---|
| 1760 | ASSERT(ret == make_link(&sentinel, N_INVALID) || (N_NORMAL == get_mark(ret)));
|
---|
[14c9aa6] | 1761 | cas_order_barrier();
|
---|
| 1762 |
|
---|
| 1763 | ret = cas_link(psrc_head, next, N_CONST, next, N_INVALID);
|
---|
| 1764 | ASSERT(ret == make_link(next, N_CONST) || (N_INVALID == get_mark(ret)));
|
---|
| 1765 | cas_order_barrier();
|
---|
[7ef2249] | 1766 | }
|
---|
| 1767 |
|
---|
[b9cb911] | 1768 | /** Splits the bucket at psrc_head and links to the remainder from pdest_head.
|
---|
| 1769 | *
|
---|
| 1770 | * Items with hashes greater or equal to \a split_hash are moved to bucket
|
---|
| 1771 | * with head at \a pdest_head.
|
---|
| 1772 | *
|
---|
| 1773 | * @param h CHT to operate on.
|
---|
| 1774 | * @param psrc_head Head of the bucket to split (in cht_t.new_b).
|
---|
| 1775 | * @param pdest_head Head of the bucket that points to the second part
|
---|
| 1776 | * of the split bucket in psrc_head. (in cht_t.new_b)
|
---|
| 1777 | * @param split_hash Hash of the first possible item in the remainder of
|
---|
| 1778 | * psrc_head, ie the smallest hash pdest_head is allowed
|
---|
| 1779 | * to point to..
|
---|
| 1780 | */
|
---|
[7ef2249] | 1781 | static void split_bucket(cht_t *h, marked_ptr_t *psrc_head,
|
---|
| 1782 | marked_ptr_t *pdest_head, size_t split_hash)
|
---|
| 1783 | {
|
---|
| 1784 | /* Already split. */
|
---|
| 1785 | if (N_NORMAL == get_mark(*pdest_head))
|
---|
| 1786 | return;
|
---|
| 1787 |
|
---|
| 1788 | /*
|
---|
| 1789 | * L == Last node of the first part of the split bucket. That part
|
---|
| 1790 | * remains in the original/src bucket.
|
---|
| 1791 | * F == First node of the second part of the split bucket. That part
|
---|
| 1792 | * will be referenced from the dest bucket head.
|
---|
| 1793 | *
|
---|
| 1794 | * We want to first mark a clean L as JF so that updaters unaware of
|
---|
| 1795 | * the split (or table resize):
|
---|
| 1796 | * - do not insert a new node between L and F
|
---|
| 1797 | * - do not unlink L (that is why it has to be clean/normal)
|
---|
| 1798 | * - do not unlink F
|
---|
| 1799 | *
|
---|
| 1800 | * Then we can safely mark F as JN even if it has been marked deleted.
|
---|
| 1801 | * Once F is marked as JN updaters aware of table resize will not
|
---|
| 1802 | * attempt to unlink it (JN will have two predecessors - we cannot
|
---|
| 1803 | * safely unlink from both at the same time). Updaters unaware of
|
---|
| 1804 | * ongoing resize can reach F only via L and that node is already
|
---|
| 1805 | * marked JF, so they won't unlink F.
|
---|
| 1806 | *
|
---|
| 1807 | * Last, link the new/dest head to F.
|
---|
| 1808 | *
|
---|
| 1809 | *
|
---|
| 1810 | * 0) ,-- split_hash, first hash of the dest bucket
|
---|
| 1811 | * v
|
---|
| 1812 | * [src_head | N] -> .. -> [L] -> [F]
|
---|
| 1813 | * [dest_head | Inv]
|
---|
| 1814 | *
|
---|
| 1815 | * 1) ,-- split_hash
|
---|
| 1816 | * v
|
---|
| 1817 | * [src_head | N] -> .. -> [JF] -> [F]
|
---|
| 1818 | * [dest_head | Inv]
|
---|
| 1819 | *
|
---|
| 1820 | * 2) ,-- split_hash
|
---|
| 1821 | * v
|
---|
| 1822 | * [src_head | N] -> .. -> [JF] -> [JN]
|
---|
| 1823 | * [dest_head | Inv]
|
---|
| 1824 | *
|
---|
[991d2d8] | 1825 | * 3) ,-- split_hash
|
---|
[7ef2249] | 1826 | * v
|
---|
| 1827 | * [src_head | N] -> .. -> [JF] -> [JN]
|
---|
| 1828 | * ^
|
---|
| 1829 | * [dest_head | N] -----------------'
|
---|
| 1830 | */
|
---|
| 1831 | wnd_t wnd;
|
---|
| 1832 |
|
---|
[3bb732b] | 1833 | rcu_read_lock();
|
---|
| 1834 |
|
---|
[7ef2249] | 1835 | /* Mark the last node of the first part of the split bucket as JF. */
|
---|
| 1836 | mark_join_follows(h, psrc_head, split_hash, &wnd);
|
---|
[14c9aa6] | 1837 | cas_order_barrier();
|
---|
[7ef2249] | 1838 |
|
---|
| 1839 | /* There are nodes in the dest bucket, ie the second part of the split. */
|
---|
[6eaed07] | 1840 | if (wnd.cur != &sentinel) {
|
---|
[7ef2249] | 1841 | /*
|
---|
| 1842 | * Mark the first node of the dest bucket as a join node so
|
---|
| 1843 | * updaters do not attempt to unlink it if it is deleted.
|
---|
| 1844 | */
|
---|
| 1845 | mark_join_node(wnd.cur);
|
---|
[14c9aa6] | 1846 | cas_order_barrier();
|
---|
[7ef2249] | 1847 | } else {
|
---|
| 1848 | /*
|
---|
| 1849 | * Second part of the split bucket is empty. There are no nodes
|
---|
| 1850 | * to mark as JOIN nodes and there never will be.
|
---|
| 1851 | */
|
---|
| 1852 | }
|
---|
| 1853 |
|
---|
| 1854 | /* Link the dest head to the second part of the split. */
|
---|
[6eaed07] | 1855 | marked_ptr_t ret =
|
---|
| 1856 | cas_link(pdest_head, &sentinel, N_INVALID, wnd.cur, N_NORMAL);
|
---|
| 1857 | ASSERT(ret == make_link(&sentinel, N_INVALID) || (N_NORMAL == get_mark(ret)));
|
---|
[14c9aa6] | 1858 | cas_order_barrier();
|
---|
[3bb732b] | 1859 |
|
---|
| 1860 | rcu_read_unlock();
|
---|
[7ef2249] | 1861 | }
|
---|
| 1862 |
|
---|
[b9cb911] | 1863 | /** Finds and marks the last node of psrc_head w/ hash less than split_hash.
|
---|
| 1864 | *
|
---|
| 1865 | * Finds a node in psrc_head with the greatest hash that is strictly less
|
---|
| 1866 | * than split_hash and marks it with N_JOIN_FOLLOWS.
|
---|
| 1867 | *
|
---|
| 1868 | * Returns a window pointing to that node.
|
---|
| 1869 | *
|
---|
| 1870 | * Any logically deleted nodes along the way are
|
---|
| 1871 | * garbage collected; therefore, the predecessor node (if any) will most
|
---|
| 1872 | * likely not be marked N_DELETED.
|
---|
| 1873 | *
|
---|
| 1874 | * @param h CHT to operate on.
|
---|
| 1875 | * @param psrc_head Bucket head.
|
---|
| 1876 | * @param split_hash The smallest hash a join node (ie the node following
|
---|
| 1877 | * the desired join-follows node) may have.
|
---|
| 1878 | * @param[out] wnd Points to the node marked with N_JOIN_FOLLOWS.
|
---|
| 1879 | */
|
---|
[7ef2249] | 1880 | static void mark_join_follows(cht_t *h, marked_ptr_t *psrc_head,
|
---|
| 1881 | size_t split_hash, wnd_t *wnd)
|
---|
| 1882 | {
|
---|
| 1883 | /* See comment in split_bucket(). */
|
---|
| 1884 |
|
---|
| 1885 | bool done;
|
---|
| 1886 | do {
|
---|
[14c9aa6] | 1887 | bool resizing = false;
|
---|
[7ef2249] | 1888 | wnd->ppred = psrc_head;
|
---|
| 1889 | wnd->cur = get_next(*psrc_head);
|
---|
| 1890 |
|
---|
| 1891 | /*
|
---|
| 1892 | * Find the split window, ie the last node of the first part of
|
---|
| 1893 | * the split bucket and the its successor - the first node of
|
---|
| 1894 | * the second part of the split bucket. Retry if GC failed.
|
---|
| 1895 | */
|
---|
[14c9aa6] | 1896 | if (!find_wnd_and_gc(h, split_hash, WM_MOVE_JOIN_FOLLOWS, wnd, &resizing))
|
---|
[7ef2249] | 1897 | continue;
|
---|
| 1898 |
|
---|
[14c9aa6] | 1899 | /* Must not report that the table is resizing if WM_MOVE_JOIN_FOLLOWS.*/
|
---|
| 1900 | ASSERT(!resizing);
|
---|
[7ef2249] | 1901 | /*
|
---|
| 1902 | * Mark the last node of the first half of the split bucket
|
---|
| 1903 | * that a join node follows. It must be clean/normal.
|
---|
| 1904 | */
|
---|
| 1905 | marked_ptr_t ret
|
---|
| 1906 | = cas_link(wnd->ppred, wnd->cur, N_NORMAL, wnd->cur, N_JOIN_FOLLOWS);
|
---|
| 1907 |
|
---|
[14c9aa6] | 1908 | /*
|
---|
| 1909 | * Successfully marked as a JF node or already marked that way (even
|
---|
| 1910 | * if also marked deleted - unlinking the node will move the JF mark).
|
---|
| 1911 | */
|
---|
[7ef2249] | 1912 | done = (ret == make_link(wnd->cur, N_NORMAL))
|
---|
| 1913 | || (N_JOIN_FOLLOWS & get_mark(ret));
|
---|
| 1914 | } while (!done);
|
---|
| 1915 | }
|
---|
| 1916 |
|
---|
[b9cb911] | 1917 | /** Marks join_node with N_JOIN. */
|
---|
[7ef2249] | 1918 | static void mark_join_node(cht_link_t *join_node)
|
---|
| 1919 | {
|
---|
| 1920 | /* See comment in split_bucket(). */
|
---|
| 1921 |
|
---|
| 1922 | bool done;
|
---|
| 1923 | do {
|
---|
[14c9aa6] | 1924 | cht_link_t *next = get_next(join_node->link);
|
---|
| 1925 | mark_t mark = get_mark(join_node->link);
|
---|
[7ef2249] | 1926 |
|
---|
| 1927 | /*
|
---|
| 1928 | * May already be marked as deleted, but it won't be unlinked
|
---|
| 1929 | * because its predecessor is marked with JOIN_FOLLOWS or CONST.
|
---|
| 1930 | */
|
---|
| 1931 | marked_ptr_t ret
|
---|
| 1932 | = cas_link(&join_node->link, next, mark, next, mark | N_JOIN);
|
---|
| 1933 |
|
---|
| 1934 | /* Successfully marked or already marked as a join node. */
|
---|
| 1935 | done = (ret == make_link(next, mark))
|
---|
| 1936 | || (N_JOIN & get_mark(ret));
|
---|
| 1937 | } while(!done);
|
---|
| 1938 | }
|
---|
| 1939 |
|
---|
[b9cb911] | 1940 | /** Appends the bucket at psrc_head to the bucket at pdest_head.
|
---|
| 1941 | *
|
---|
| 1942 | * @param h CHT to operate on.
|
---|
| 1943 | * @param psrc_head Bucket to merge with pdest_head.
|
---|
| 1944 | * @param pdest_head Bucket to be joined by psrc_head.
|
---|
| 1945 | * @param split_hash The smallest hash psrc_head may contain.
|
---|
| 1946 | */
|
---|
[7ef2249] | 1947 | static void join_buckets(cht_t *h, marked_ptr_t *psrc_head,
|
---|
[3bb732b] | 1948 | marked_ptr_t *pdest_head, size_t split_hash)
|
---|
[7ef2249] | 1949 | {
|
---|
| 1950 | /* Buckets already joined. */
|
---|
| 1951 | if (N_INVALID == get_mark(*psrc_head))
|
---|
| 1952 | return;
|
---|
| 1953 | /*
|
---|
| 1954 | * F == First node of psrc_head, ie the bucket we want to append
|
---|
| 1955 | * to (ie join with) the bucket starting at pdest_head.
|
---|
| 1956 | * L == Last node of pdest_head, ie the bucket that psrc_head will
|
---|
| 1957 | * be appended to.
|
---|
| 1958 | *
|
---|
| 1959 | * (1) We first mark psrc_head immutable to signal that a join is
|
---|
| 1960 | * in progress and so that updaters unaware of the join (or table
|
---|
| 1961 | * resize):
|
---|
| 1962 | * - do not insert new nodes between the head psrc_head and F
|
---|
| 1963 | * - do not unlink F (it may already be marked deleted)
|
---|
| 1964 | *
|
---|
| 1965 | * (2) Next, F is marked as a join node. Updaters aware of table resize
|
---|
| 1966 | * will not attempt to unlink it. We cannot safely/atomically unlink
|
---|
| 1967 | * the join node because it will be pointed to from two different
|
---|
| 1968 | * buckets. Updaters unaware of resize will fail to unlink the join
|
---|
| 1969 | * node due to the head being marked immutable.
|
---|
| 1970 | *
|
---|
| 1971 | * (3) Then the tail of the bucket at pdest_head is linked to the join
|
---|
| 1972 | * node. From now on, nodes in both buckets can be found via pdest_head.
|
---|
| 1973 | *
|
---|
| 1974 | * (4) Last, mark immutable psrc_head as invalid. It signals updaters
|
---|
| 1975 | * that the join is complete and they can insert new nodes (originally
|
---|
| 1976 | * destined for psrc_head) into pdest_head.
|
---|
| 1977 | *
|
---|
| 1978 | * Note that pdest_head keeps pointing at the join node. This allows
|
---|
| 1979 | * lookups and updaters to determine if they should see a link between
|
---|
| 1980 | * the tail L and F when searching for nodes originally in psrc_head
|
---|
| 1981 | * via pdest_head. If they reach the tail of pdest_head without
|
---|
| 1982 | * encountering any nodes of psrc_head, either there were no nodes
|
---|
| 1983 | * in psrc_head to begin with or the link between L and F did not
|
---|
| 1984 | * yet propagate to their cpus. If psrc_head was empty, it remains
|
---|
| 1985 | * NULL. Otherwise psrc_head points to a join node (it will not be
|
---|
| 1986 | * unlinked until table resize completes) and updaters/lookups
|
---|
| 1987 | * should issue a read_barrier() to make the link [L]->[JN] visible.
|
---|
| 1988 | *
|
---|
| 1989 | * 0) ,-- split_hash, first hash of the src bucket
|
---|
| 1990 | * v
|
---|
| 1991 | * [dest_head | N]-> .. -> [L]
|
---|
| 1992 | * [src_head | N]--> [F] -> ..
|
---|
| 1993 | * ^
|
---|
| 1994 | * ` split_hash, first hash of the src bucket
|
---|
| 1995 | *
|
---|
| 1996 | * 1) ,-- split_hash
|
---|
| 1997 | * v
|
---|
| 1998 | * [dest_head | N]-> .. -> [L]
|
---|
| 1999 | * [src_head | C]--> [F] -> ..
|
---|
| 2000 | *
|
---|
| 2001 | * 2) ,-- split_hash
|
---|
| 2002 | * v
|
---|
| 2003 | * [dest_head | N]-> .. -> [L]
|
---|
| 2004 | * [src_head | C]--> [JN] -> ..
|
---|
| 2005 | *
|
---|
| 2006 | * 3) ,-- split_hash
|
---|
| 2007 | * v
|
---|
| 2008 | * [dest_head | N]-> .. -> [L] --+
|
---|
| 2009 | * v
|
---|
| 2010 | * [src_head | C]-------------> [JN] -> ..
|
---|
| 2011 | *
|
---|
| 2012 | * 4) ,-- split_hash
|
---|
| 2013 | * v
|
---|
| 2014 | * [dest_head | N]-> .. -> [L] --+
|
---|
| 2015 | * v
|
---|
| 2016 | * [src_head | Inv]-----------> [JN] -> ..
|
---|
| 2017 | */
|
---|
| 2018 |
|
---|
[3bb732b] | 2019 | rcu_read_lock();
|
---|
| 2020 |
|
---|
[14c9aa6] | 2021 | /* Mark src_head immutable - signals updaters that bucket join started. */
|
---|
[7ef2249] | 2022 | mark_const(psrc_head);
|
---|
[14c9aa6] | 2023 | cas_order_barrier();
|
---|
[7ef2249] | 2024 |
|
---|
| 2025 | cht_link_t *join_node = get_next(*psrc_head);
|
---|
| 2026 |
|
---|
[6eaed07] | 2027 | if (join_node != &sentinel) {
|
---|
[7ef2249] | 2028 | mark_join_node(join_node);
|
---|
[14c9aa6] | 2029 | cas_order_barrier();
|
---|
[7ef2249] | 2030 |
|
---|
| 2031 | link_to_join_node(h, pdest_head, join_node, split_hash);
|
---|
[14c9aa6] | 2032 | cas_order_barrier();
|
---|
[7ef2249] | 2033 | }
|
---|
| 2034 |
|
---|
[14c9aa6] | 2035 | marked_ptr_t ret =
|
---|
| 2036 | cas_link(psrc_head, join_node, N_CONST, join_node, N_INVALID);
|
---|
| 2037 | ASSERT(ret == make_link(join_node, N_CONST) || (N_INVALID == get_mark(ret)));
|
---|
| 2038 | cas_order_barrier();
|
---|
[3bb732b] | 2039 |
|
---|
| 2040 | rcu_read_unlock();
|
---|
[7ef2249] | 2041 | }
|
---|
| 2042 |
|
---|
[b9cb911] | 2043 | /** Links the tail of pdest_head to join_node.
|
---|
| 2044 | *
|
---|
| 2045 | * @param h CHT to operate on.
|
---|
| 2046 | * @param pdest_head Head of the bucket whose tail is to be linked to join_node.
|
---|
| 2047 | * @param join_node A node marked N_JOIN with a hash greater or equal to
|
---|
| 2048 | * split_hash.
|
---|
| 2049 | * @param split_hash The least hash that is greater than the hash of any items
|
---|
| 2050 | * (originally) in pdest_head.
|
---|
| 2051 | */
|
---|
[7ef2249] | 2052 | static void link_to_join_node(cht_t *h, marked_ptr_t *pdest_head,
|
---|
| 2053 | cht_link_t *join_node, size_t split_hash)
|
---|
| 2054 | {
|
---|
| 2055 | bool done;
|
---|
| 2056 | do {
|
---|
| 2057 | wnd_t wnd = {
|
---|
| 2058 | .ppred = pdest_head,
|
---|
| 2059 | .cur = get_next(*pdest_head)
|
---|
| 2060 | };
|
---|
| 2061 |
|
---|
[14c9aa6] | 2062 | bool resizing = false;
|
---|
[7ef2249] | 2063 |
|
---|
[14c9aa6] | 2064 | if (!find_wnd_and_gc(h, split_hash, WM_LEAVE_JOIN, &wnd, &resizing))
|
---|
[7ef2249] | 2065 | continue;
|
---|
| 2066 |
|
---|
[14c9aa6] | 2067 | ASSERT(!resizing);
|
---|
| 2068 |
|
---|
[6eaed07] | 2069 | if (wnd.cur != &sentinel) {
|
---|
[7ef2249] | 2070 | /* Must be from the new appended bucket. */
|
---|
[6eaed07] | 2071 | ASSERT(split_hash <= node_hash(h, wnd.cur)
|
---|
| 2072 | || h->invalid_hash == node_hash(h, wnd.cur));
|
---|
[7ef2249] | 2073 | return;
|
---|
| 2074 | }
|
---|
| 2075 |
|
---|
| 2076 | /* Reached the tail of pdest_head - link it to the join node. */
|
---|
[6eaed07] | 2077 | marked_ptr_t ret =
|
---|
| 2078 | cas_link(wnd.ppred, &sentinel, N_NORMAL, join_node, N_NORMAL);
|
---|
[7ef2249] | 2079 |
|
---|
[6eaed07] | 2080 | done = (ret == make_link(&sentinel, N_NORMAL));
|
---|
[7ef2249] | 2081 | } while (!done);
|
---|
| 2082 | }
|
---|
| 2083 |
|
---|
[b9cb911] | 2084 | /** Instructs RCU to free the item once all preexisting references are dropped.
|
---|
| 2085 | *
|
---|
| 2086 | * The item is freed via op->remove_callback().
|
---|
| 2087 | */
|
---|
[7ef2249] | 2088 | static void free_later(cht_t *h, cht_link_t *item)
|
---|
| 2089 | {
|
---|
[6eaed07] | 2090 | ASSERT(item != &sentinel);
|
---|
| 2091 |
|
---|
[7ef2249] | 2092 | /*
|
---|
| 2093 | * remove_callback only works as rcu_func_t because rcu_link is the first
|
---|
| 2094 | * field in cht_link_t.
|
---|
| 2095 | */
|
---|
| 2096 | rcu_call(&item->rcu_link, (rcu_func_t)h->op->remove_callback);
|
---|
| 2097 |
|
---|
[3bb732b] | 2098 | item_removed(h);
|
---|
[7ef2249] | 2099 | }
|
---|
| 2100 |
|
---|
[b9cb911] | 2101 | /** Notes that an item had been unlinked from the table and shrinks it if needed.
|
---|
| 2102 | *
|
---|
| 2103 | * If the number of items in the table drops below 1/4 of the maximum
|
---|
| 2104 | * allowed load the table is shrunk in the background.
|
---|
| 2105 | */
|
---|
[5e5cef3] | 2106 | static inline void item_removed(cht_t *h)
|
---|
[7ef2249] | 2107 | {
|
---|
[14c9aa6] | 2108 | size_t items = (size_t) atomic_predec(&h->item_cnt);
|
---|
| 2109 | size_t bucket_cnt = (1 << h->b->order);
|
---|
| 2110 |
|
---|
[0b7bcb8] | 2111 | bool need_shrink = (items == h->max_load * bucket_cnt / 4);
|
---|
| 2112 | bool missed_shrink = (items == h->max_load * bucket_cnt / 8);
|
---|
[14c9aa6] | 2113 |
|
---|
| 2114 | if ((need_shrink || missed_shrink) && h->b->order > h->min_order) {
|
---|
| 2115 | atomic_count_t resize_reqs = atomic_preinc(&h->resize_reqs);
|
---|
| 2116 | /* The first resize request. Start the resizer. */
|
---|
| 2117 | if (1 == resize_reqs) {
|
---|
| 2118 | workq_global_enqueue_noblock(&h->resize_work, resize_table);
|
---|
| 2119 | }
|
---|
| 2120 | }
|
---|
[7ef2249] | 2121 | }
|
---|
| 2122 |
|
---|
[b9cb911] | 2123 | /** Notes an item had been inserted and grows the table if needed.
|
---|
| 2124 | *
|
---|
| 2125 | * The table is resized in the background.
|
---|
| 2126 | */
|
---|
[5e5cef3] | 2127 | static inline void item_inserted(cht_t *h)
|
---|
[7ef2249] | 2128 | {
|
---|
[14c9aa6] | 2129 | size_t items = (size_t) atomic_preinc(&h->item_cnt);
|
---|
| 2130 | size_t bucket_cnt = (1 << h->b->order);
|
---|
| 2131 |
|
---|
[0b7bcb8] | 2132 | bool need_grow = (items == h->max_load * bucket_cnt);
|
---|
| 2133 | bool missed_grow = (items == 2 * h->max_load * bucket_cnt);
|
---|
[14c9aa6] | 2134 |
|
---|
| 2135 | if ((need_grow || missed_grow) && h->b->order < CHT_MAX_ORDER) {
|
---|
| 2136 | atomic_count_t resize_reqs = atomic_preinc(&h->resize_reqs);
|
---|
| 2137 | /* The first resize request. Start the resizer. */
|
---|
| 2138 | if (1 == resize_reqs) {
|
---|
| 2139 | workq_global_enqueue_noblock(&h->resize_work, resize_table);
|
---|
| 2140 | }
|
---|
| 2141 | }
|
---|
[3bb732b] | 2142 | }
|
---|
| 2143 |
|
---|
[b9cb911] | 2144 | /** Resize request handler. Invoked on the system work queue. */
|
---|
[14c9aa6] | 2145 | static void resize_table(work_t *arg)
|
---|
[3bb732b] | 2146 | {
|
---|
[14c9aa6] | 2147 | cht_t *h = member_to_inst(arg, cht_t, resize_work);
|
---|
[7ef2249] | 2148 |
|
---|
[14c9aa6] | 2149 | #ifdef CONFIG_DEBUG
|
---|
[3bb732b] | 2150 | ASSERT(h->b);
|
---|
[14c9aa6] | 2151 | /* Make resize_reqs visible. */
|
---|
[3bb732b] | 2152 | read_barrier();
|
---|
[14c9aa6] | 2153 | ASSERT(0 < atomic_get(&h->resize_reqs));
|
---|
| 2154 | #endif
|
---|
| 2155 |
|
---|
| 2156 | bool done;
|
---|
[3bb732b] | 2157 | do {
|
---|
[14c9aa6] | 2158 | /* Load the most recent h->item_cnt. */
|
---|
| 2159 | read_barrier();
|
---|
| 2160 | size_t cur_items = (size_t) atomic_get(&h->item_cnt);
|
---|
[3bb732b] | 2161 | size_t bucket_cnt = (1 << h->b->order);
|
---|
[0b7bcb8] | 2162 | size_t max_items = h->max_load * bucket_cnt;
|
---|
[3bb732b] | 2163 |
|
---|
[14c9aa6] | 2164 | if (cur_items >= max_items && h->b->order < CHT_MAX_ORDER) {
|
---|
[3bb732b] | 2165 | grow_table(h);
|
---|
[14c9aa6] | 2166 | } else if (cur_items <= max_items / 4 && h->b->order > h->min_order) {
|
---|
[3bb732b] | 2167 | shrink_table(h);
|
---|
[14c9aa6] | 2168 | } else {
|
---|
| 2169 | /* Table is just the right size. */
|
---|
| 2170 | atomic_count_t reqs = atomic_predec(&h->resize_reqs);
|
---|
| 2171 | done = (reqs == 0);
|
---|
[3bb732b] | 2172 | }
|
---|
[14c9aa6] | 2173 | } while (!done);
|
---|
[3bb732b] | 2174 | }
|
---|
| 2175 |
|
---|
[b9cb911] | 2176 | /** Increases the number of buckets two-fold. Blocks until done. */
|
---|
[3bb732b] | 2177 | static void grow_table(cht_t *h)
|
---|
| 2178 | {
|
---|
| 2179 | if (h->b->order >= CHT_MAX_ORDER)
|
---|
| 2180 | return;
|
---|
[7ef2249] | 2181 |
|
---|
[c28413a9] | 2182 | h->new_b = alloc_buckets(h->b->order + 1, true, false);
|
---|
[3bb732b] | 2183 |
|
---|
| 2184 | /* Failed to alloc a new table - try next time the resizer is run. */
|
---|
| 2185 | if (!h->new_b)
|
---|
| 2186 | return;
|
---|
| 2187 |
|
---|
| 2188 | /* Wait for all readers and updaters to see the initialized new table. */
|
---|
| 2189 | rcu_synchronize();
|
---|
| 2190 | size_t old_bucket_cnt = (1 << h->b->order);
|
---|
[7ef2249] | 2191 |
|
---|
[3bb732b] | 2192 | /*
|
---|
| 2193 | * Give updaters a chance to help out with the resize. Do the minimum
|
---|
| 2194 | * work needed to announce a resize is in progress, ie start moving heads.
|
---|
| 2195 | */
|
---|
| 2196 | for (size_t idx = 0; idx < old_bucket_cnt; ++idx) {
|
---|
| 2197 | start_head_move(&h->b->head[idx]);
|
---|
[7ef2249] | 2198 | }
|
---|
| 2199 |
|
---|
[14c9aa6] | 2200 | /* Order start_head_move() wrt complete_head_move(). */
|
---|
| 2201 | cas_order_barrier();
|
---|
| 2202 |
|
---|
[3bb732b] | 2203 | /* Complete moving heads and split any buckets not yet split by updaters. */
|
---|
| 2204 | for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
|
---|
| 2205 | marked_ptr_t *move_dest_head = &h->new_b->head[grow_idx(old_idx)];
|
---|
| 2206 | marked_ptr_t *move_src_head = &h->b->head[old_idx];
|
---|
| 2207 |
|
---|
| 2208 | /* Head move not yet completed. */
|
---|
| 2209 | if (N_INVALID != get_mark(*move_src_head)) {
|
---|
| 2210 | complete_head_move(move_src_head, move_dest_head);
|
---|
| 2211 | }
|
---|
| 2212 |
|
---|
| 2213 | size_t split_idx = grow_to_split_idx(old_idx);
|
---|
| 2214 | size_t split_hash = calc_split_hash(split_idx, h->new_b->order);
|
---|
| 2215 | marked_ptr_t *split_dest_head = &h->new_b->head[split_idx];
|
---|
| 2216 |
|
---|
| 2217 | split_bucket(h, move_dest_head, split_dest_head, split_hash);
|
---|
| 2218 | }
|
---|
| 2219 |
|
---|
| 2220 | /*
|
---|
| 2221 | * Wait for all updaters to notice the new heads. Once everyone sees
|
---|
| 2222 | * the invalid old bucket heads they will know a resize is in progress
|
---|
| 2223 | * and updaters will modify the correct new buckets.
|
---|
| 2224 | */
|
---|
| 2225 | rcu_synchronize();
|
---|
| 2226 |
|
---|
| 2227 | /* Clear the JOIN_FOLLOWS mark and remove the link between the split buckets.*/
|
---|
| 2228 | for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
|
---|
| 2229 | size_t new_idx = grow_idx(old_idx);
|
---|
| 2230 |
|
---|
[14c9aa6] | 2231 | cleanup_join_follows(h, &h->new_b->head[new_idx]);
|
---|
[3bb732b] | 2232 | }
|
---|
[14c9aa6] | 2233 |
|
---|
[3bb732b] | 2234 | /*
|
---|
| 2235 | * Wait for everyone to notice that buckets were split, ie link connecting
|
---|
| 2236 | * the join follows and join node has been cut.
|
---|
| 2237 | */
|
---|
| 2238 | rcu_synchronize();
|
---|
| 2239 |
|
---|
| 2240 | /* Clear the JOIN mark and GC any deleted join nodes. */
|
---|
| 2241 | for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
|
---|
| 2242 | size_t new_idx = grow_to_split_idx(old_idx);
|
---|
| 2243 |
|
---|
[14c9aa6] | 2244 | cleanup_join_node(h, &h->new_b->head[new_idx]);
|
---|
[3bb732b] | 2245 | }
|
---|
[14c9aa6] | 2246 |
|
---|
[3bb732b] | 2247 | /* Wait for everyone to see that the table is clear of any resize marks. */
|
---|
| 2248 | rcu_synchronize();
|
---|
| 2249 |
|
---|
| 2250 | cht_buckets_t *old_b = h->b;
|
---|
| 2251 | rcu_assign(h->b, h->new_b);
|
---|
[14c9aa6] | 2252 |
|
---|
[3bb732b] | 2253 | /* Wait for everyone to start using the new table. */
|
---|
| 2254 | rcu_synchronize();
|
---|
| 2255 |
|
---|
| 2256 | free(old_b);
|
---|
| 2257 |
|
---|
| 2258 | /* Not needed; just for increased readability. */
|
---|
[205832b] | 2259 | h->new_b = NULL;
|
---|
[3bb732b] | 2260 | }
|
---|
| 2261 |
|
---|
[b9cb911] | 2262 | /** Halfs the number of buckets. Blocks until done. */
|
---|
[3bb732b] | 2263 | static void shrink_table(cht_t *h)
|
---|
| 2264 | {
|
---|
[14c9aa6] | 2265 | if (h->b->order <= h->min_order)
|
---|
[3bb732b] | 2266 | return;
|
---|
| 2267 |
|
---|
[c28413a9] | 2268 | h->new_b = alloc_buckets(h->b->order - 1, true, false);
|
---|
[3bb732b] | 2269 |
|
---|
| 2270 | /* Failed to alloc a new table - try next time the resizer is run. */
|
---|
| 2271 | if (!h->new_b)
|
---|
| 2272 | return;
|
---|
| 2273 |
|
---|
| 2274 | /* Wait for all readers and updaters to see the initialized new table. */
|
---|
| 2275 | rcu_synchronize();
|
---|
| 2276 |
|
---|
| 2277 | size_t old_bucket_cnt = (1 << h->b->order);
|
---|
| 2278 |
|
---|
| 2279 | /*
|
---|
| 2280 | * Give updaters a chance to help out with the resize. Do the minimum
|
---|
| 2281 | * work needed to announce a resize is in progress, ie start moving heads.
|
---|
| 2282 | */
|
---|
| 2283 | for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
|
---|
| 2284 | size_t new_idx = shrink_idx(old_idx);
|
---|
| 2285 |
|
---|
| 2286 | /* This bucket should be moved. */
|
---|
| 2287 | if (grow_idx(new_idx) == old_idx) {
|
---|
| 2288 | start_head_move(&h->b->head[old_idx]);
|
---|
| 2289 | } else {
|
---|
| 2290 | /* This bucket should join the moved bucket once the move is done.*/
|
---|
| 2291 | }
|
---|
| 2292 | }
|
---|
| 2293 |
|
---|
[14c9aa6] | 2294 | /* Order start_head_move() wrt to complete_head_move(). */
|
---|
| 2295 | cas_order_barrier();
|
---|
| 2296 |
|
---|
[3bb732b] | 2297 | /* Complete moving heads and join buckets with the moved buckets. */
|
---|
| 2298 | for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
|
---|
| 2299 | size_t new_idx = shrink_idx(old_idx);
|
---|
[14c9aa6] | 2300 | size_t move_src_idx = grow_idx(new_idx);
|
---|
[3bb732b] | 2301 |
|
---|
| 2302 | /* This bucket should be moved. */
|
---|
[14c9aa6] | 2303 | if (move_src_idx == old_idx) {
|
---|
[3bb732b] | 2304 | /* Head move not yet completed. */
|
---|
| 2305 | if (N_INVALID != get_mark(h->b->head[old_idx])) {
|
---|
| 2306 | complete_head_move(&h->b->head[old_idx], &h->new_b->head[new_idx]);
|
---|
| 2307 | }
|
---|
| 2308 | } else {
|
---|
| 2309 | /* This bucket should join the moved bucket. */
|
---|
| 2310 | size_t split_hash = calc_split_hash(old_idx, h->b->order);
|
---|
| 2311 | join_buckets(h, &h->b->head[old_idx], &h->new_b->head[new_idx],
|
---|
| 2312 | split_hash);
|
---|
| 2313 | }
|
---|
| 2314 | }
|
---|
| 2315 |
|
---|
| 2316 | /*
|
---|
| 2317 | * Wait for all updaters to notice the new heads. Once everyone sees
|
---|
| 2318 | * the invalid old bucket heads they will know a resize is in progress
|
---|
| 2319 | * and updaters will modify the correct new buckets.
|
---|
| 2320 | */
|
---|
| 2321 | rcu_synchronize();
|
---|
| 2322 |
|
---|
| 2323 | /* Let everyone know joins are complete and fully visible. */
|
---|
| 2324 | for (size_t old_idx = 0; old_idx < old_bucket_cnt; ++old_idx) {
|
---|
| 2325 | size_t move_src_idx = grow_idx(shrink_idx(old_idx));
|
---|
| 2326 |
|
---|
| 2327 | /* Set the invalid joinee head to NULL. */
|
---|
| 2328 | if (old_idx != move_src_idx) {
|
---|
[14c9aa6] | 2329 | ASSERT(N_INVALID == get_mark(h->b->head[old_idx]));
|
---|
[3bb732b] | 2330 |
|
---|
[6eaed07] | 2331 | if (&sentinel != get_next(h->b->head[old_idx]))
|
---|
| 2332 | h->b->head[old_idx] = make_link(&sentinel, N_INVALID);
|
---|
[3bb732b] | 2333 | }
|
---|
| 2334 | }
|
---|
| 2335 |
|
---|
| 2336 | /* todo comment join node vs reset joinee head*/
|
---|
| 2337 | rcu_synchronize();
|
---|
| 2338 |
|
---|
| 2339 | size_t new_bucket_cnt = (1 << h->new_b->order);
|
---|
| 2340 |
|
---|
| 2341 | /* Clear the JOIN mark and GC any deleted join nodes. */
|
---|
| 2342 | for (size_t new_idx = 0; new_idx < new_bucket_cnt; ++new_idx) {
|
---|
[14c9aa6] | 2343 | cleanup_join_node(h, &h->new_b->head[new_idx]);
|
---|
[3bb732b] | 2344 | }
|
---|
| 2345 |
|
---|
| 2346 | /* Wait for everyone to see that the table is clear of any resize marks. */
|
---|
| 2347 | rcu_synchronize();
|
---|
| 2348 |
|
---|
| 2349 | cht_buckets_t *old_b = h->b;
|
---|
| 2350 | rcu_assign(h->b, h->new_b);
|
---|
| 2351 |
|
---|
| 2352 | /* Wait for everyone to start using the new table. */
|
---|
| 2353 | rcu_synchronize();
|
---|
| 2354 |
|
---|
| 2355 | free(old_b);
|
---|
| 2356 |
|
---|
| 2357 | /* Not needed; just for increased readability. */
|
---|
[205832b] | 2358 | h->new_b = NULL;
|
---|
[3bb732b] | 2359 | }
|
---|
| 2360 |
|
---|
[b9cb911] | 2361 | /** Finds and clears the N_JOIN mark from a node in new_head (if present). */
|
---|
[3bb732b] | 2362 | static void cleanup_join_node(cht_t *h, marked_ptr_t *new_head)
|
---|
| 2363 | {
|
---|
| 2364 | rcu_read_lock();
|
---|
| 2365 |
|
---|
| 2366 | cht_link_t *cur = get_next(*new_head);
|
---|
| 2367 |
|
---|
[6eaed07] | 2368 | while (cur != &sentinel) {
|
---|
[3bb732b] | 2369 | /* Clear the join node's JN mark - even if it is marked as deleted. */
|
---|
| 2370 | if (N_JOIN & get_mark(cur->link)) {
|
---|
| 2371 | clear_join_and_gc(h, cur, new_head);
|
---|
| 2372 | break;
|
---|
| 2373 | }
|
---|
| 2374 |
|
---|
| 2375 | cur = get_next(cur->link);
|
---|
| 2376 | }
|
---|
| 2377 |
|
---|
| 2378 | rcu_read_unlock();
|
---|
| 2379 | }
|
---|
| 2380 |
|
---|
[b9cb911] | 2381 | /** Clears the join_node's N_JOIN mark frees it if marked N_DELETED as well. */
|
---|
[3bb732b] | 2382 | static void clear_join_and_gc(cht_t *h, cht_link_t *join_node,
|
---|
| 2383 | marked_ptr_t *new_head)
|
---|
| 2384 | {
|
---|
[6eaed07] | 2385 | ASSERT(join_node != &sentinel);
|
---|
[3bb732b] | 2386 | ASSERT(join_node && (N_JOIN & get_mark(join_node->link)));
|
---|
| 2387 |
|
---|
| 2388 | bool done;
|
---|
| 2389 |
|
---|
| 2390 | /* Clear the JN mark. */
|
---|
| 2391 | do {
|
---|
| 2392 | marked_ptr_t jn_link = join_node->link;
|
---|
| 2393 | cht_link_t *next = get_next(jn_link);
|
---|
| 2394 | /* Clear the JOIN mark but keep the DEL mark if present. */
|
---|
| 2395 | mark_t cleared_mark = get_mark(jn_link) & N_DELETED;
|
---|
| 2396 |
|
---|
| 2397 | marked_ptr_t ret =
|
---|
| 2398 | _cas_link(&join_node->link, jn_link, make_link(next, cleared_mark));
|
---|
| 2399 |
|
---|
| 2400 | /* Done if the mark was cleared. Retry if a new node was inserted. */
|
---|
| 2401 | done = (ret == jn_link);
|
---|
[14c9aa6] | 2402 | ASSERT(ret == jn_link || (get_mark(ret) & N_JOIN));
|
---|
[3bb732b] | 2403 | } while (!done);
|
---|
| 2404 |
|
---|
| 2405 | if (!(N_DELETED & get_mark(join_node->link)))
|
---|
| 2406 | return;
|
---|
| 2407 |
|
---|
| 2408 | /* The join node had been marked as deleted - GC it. */
|
---|
[14c9aa6] | 2409 |
|
---|
| 2410 | /* Clear the JOIN mark before trying to unlink the deleted join node.*/
|
---|
| 2411 | cas_order_barrier();
|
---|
[3bb732b] | 2412 |
|
---|
| 2413 | size_t jn_hash = node_hash(h, join_node);
|
---|
| 2414 | do {
|
---|
[14c9aa6] | 2415 | bool resizing = false;
|
---|
[3bb732b] | 2416 |
|
---|
| 2417 | wnd_t wnd = {
|
---|
| 2418 | .ppred = new_head,
|
---|
| 2419 | .cur = get_next(*new_head)
|
---|
| 2420 | };
|
---|
| 2421 |
|
---|
| 2422 | done = find_wnd_and_gc_pred(h, jn_hash, WM_NORMAL, same_node_pred,
|
---|
| 2423 | join_node, &wnd, &resizing);
|
---|
| 2424 |
|
---|
| 2425 | ASSERT(!resizing);
|
---|
| 2426 | } while (!done);
|
---|
| 2427 | }
|
---|
| 2428 |
|
---|
[b9cb911] | 2429 | /** Finds a non-deleted node with N_JOIN_FOLLOWS and clears the mark. */
|
---|
[3bb732b] | 2430 | static void cleanup_join_follows(cht_t *h, marked_ptr_t *new_head)
|
---|
| 2431 | {
|
---|
| 2432 | ASSERT(new_head);
|
---|
| 2433 |
|
---|
| 2434 | rcu_read_lock();
|
---|
| 2435 |
|
---|
| 2436 | wnd_t wnd = {
|
---|
[205832b] | 2437 | .ppred = NULL,
|
---|
| 2438 | .cur = NULL
|
---|
[3bb732b] | 2439 | };
|
---|
| 2440 | marked_ptr_t *cur_link = new_head;
|
---|
| 2441 |
|
---|
| 2442 | /*
|
---|
| 2443 | * Find the non-deleted node with a JF mark and clear the JF mark.
|
---|
| 2444 | * The JF node may be deleted and/or the mark moved to its neighbors
|
---|
| 2445 | * at any time. Therefore, we GC deleted nodes until we find the JF
|
---|
| 2446 | * node in order to remove stale/deleted JF nodes left behind eg by
|
---|
| 2447 | * delayed threads that did not yet get a chance to unlink the deleted
|
---|
| 2448 | * JF node and move its mark.
|
---|
| 2449 | *
|
---|
| 2450 | * Note that the head may be marked JF (but never DELETED).
|
---|
| 2451 | */
|
---|
| 2452 | while (true) {
|
---|
| 2453 | bool is_jf_node = N_JOIN_FOLLOWS & get_mark(*cur_link);
|
---|
| 2454 |
|
---|
| 2455 | /* GC any deleted nodes on the way - even deleted JOIN_FOLLOWS. */
|
---|
| 2456 | if (N_DELETED & get_mark(*cur_link)) {
|
---|
| 2457 | ASSERT(cur_link != new_head);
|
---|
[6eaed07] | 2458 | ASSERT(wnd.ppred && wnd.cur && wnd.cur != &sentinel);
|
---|
[3bb732b] | 2459 | ASSERT(cur_link == &wnd.cur->link);
|
---|
| 2460 |
|
---|
| 2461 | bool dummy;
|
---|
| 2462 | bool deleted = gc_deleted_node(h, WM_MOVE_JOIN_FOLLOWS, &wnd, &dummy);
|
---|
| 2463 |
|
---|
| 2464 | /* Failed to GC or collected a deleted JOIN_FOLLOWS. */
|
---|
| 2465 | if (!deleted || is_jf_node) {
|
---|
| 2466 | /* Retry from the head of the bucket. */
|
---|
| 2467 | cur_link = new_head;
|
---|
| 2468 | continue;
|
---|
| 2469 | }
|
---|
| 2470 | } else {
|
---|
| 2471 | /* Found a non-deleted JF. Clear its JF mark. */
|
---|
| 2472 | if (is_jf_node) {
|
---|
| 2473 | cht_link_t *next = get_next(*cur_link);
|
---|
[6eaed07] | 2474 | marked_ptr_t ret =
|
---|
| 2475 | cas_link(cur_link, next, N_JOIN_FOLLOWS, &sentinel, N_NORMAL);
|
---|
[14c9aa6] | 2476 |
|
---|
[6eaed07] | 2477 | ASSERT(next == &sentinel
|
---|
| 2478 | || ((N_JOIN | N_JOIN_FOLLOWS) & get_mark(ret)));
|
---|
[3bb732b] | 2479 |
|
---|
| 2480 | /* Successfully cleared the JF mark of a non-deleted node. */
|
---|
| 2481 | if (ret == make_link(next, N_JOIN_FOLLOWS)) {
|
---|
| 2482 | break;
|
---|
| 2483 | } else {
|
---|
| 2484 | /*
|
---|
| 2485 | * The JF node had been deleted or a new node inserted
|
---|
| 2486 | * right after it. Retry from the head.
|
---|
| 2487 | */
|
---|
| 2488 | cur_link = new_head;
|
---|
| 2489 | continue;
|
---|
| 2490 | }
|
---|
| 2491 | } else {
|
---|
| 2492 | wnd.ppred = cur_link;
|
---|
| 2493 | wnd.cur = get_next(*cur_link);
|
---|
| 2494 | }
|
---|
| 2495 | }
|
---|
| 2496 |
|
---|
| 2497 | /* We must encounter a JF node before we reach the end of the bucket. */
|
---|
[6eaed07] | 2498 | ASSERT(wnd.cur && wnd.cur != &sentinel);
|
---|
[3bb732b] | 2499 | cur_link = &wnd.cur->link;
|
---|
| 2500 | }
|
---|
| 2501 |
|
---|
| 2502 | rcu_read_unlock();
|
---|
| 2503 | }
|
---|
| 2504 |
|
---|
[b9cb911] | 2505 | /** Returns the first possible hash following a bucket split point.
|
---|
| 2506 | *
|
---|
| 2507 | * In other words the returned hash is the smallest possible hash
|
---|
| 2508 | * the remainder of the split bucket may contain.
|
---|
| 2509 | */
|
---|
[5e5cef3] | 2510 | static inline size_t calc_split_hash(size_t split_idx, size_t order)
|
---|
[3bb732b] | 2511 | {
|
---|
| 2512 | ASSERT(1 <= order && order <= 8 * sizeof(size_t));
|
---|
| 2513 | return split_idx << (8 * sizeof(size_t) - order);
|
---|
| 2514 | }
|
---|
| 2515 |
|
---|
[b9cb911] | 2516 | /** Returns the bucket head index given the table size order and item hash. */
|
---|
[5e5cef3] | 2517 | static inline size_t calc_bucket_idx(size_t hash, size_t order)
|
---|
[3bb732b] | 2518 | {
|
---|
| 2519 | ASSERT(1 <= order && order <= 8 * sizeof(size_t));
|
---|
| 2520 | return hash >> (8 * sizeof(size_t) - order);
|
---|
| 2521 | }
|
---|
| 2522 |
|
---|
[b9cb911] | 2523 | /** Returns the bucket index of destination*/
|
---|
[5e5cef3] | 2524 | static inline size_t grow_to_split_idx(size_t old_idx)
|
---|
[3bb732b] | 2525 | {
|
---|
| 2526 | return grow_idx(old_idx) | 1;
|
---|
| 2527 | }
|
---|
| 2528 |
|
---|
[b9cb911] | 2529 | /** Returns the destination index of a bucket head when the table is growing. */
|
---|
[5e5cef3] | 2530 | static inline size_t grow_idx(size_t idx)
|
---|
[3bb732b] | 2531 | {
|
---|
| 2532 | return idx << 1;
|
---|
| 2533 | }
|
---|
| 2534 |
|
---|
[b9cb911] | 2535 | /** Returns the destination index of a bucket head when the table is shrinking.*/
|
---|
[5e5cef3] | 2536 | static inline size_t shrink_idx(size_t idx)
|
---|
[3bb732b] | 2537 | {
|
---|
| 2538 | return idx >> 1;
|
---|
[7ef2249] | 2539 | }
|
---|
| 2540 |
|
---|
[b9cb911] | 2541 | /** Returns a mixed hash of the search key.*/
|
---|
[6eaed07] | 2542 | static inline size_t calc_key_hash(cht_t *h, void *key)
|
---|
[7ef2249] | 2543 | {
|
---|
[85d31de9] | 2544 | /* Mimic calc_node_hash. */
|
---|
[30c0826] | 2545 | return hash_mix(h->op->key_hash(key)) & ~(size_t)1;
|
---|
[7ef2249] | 2546 | }
|
---|
| 2547 |
|
---|
[b9cb911] | 2548 | /** Returns a memoized mixed hash of the item. */
|
---|
[5e5cef3] | 2549 | static inline size_t node_hash(cht_t *h, const cht_link_t *item)
|
---|
[7ef2249] | 2550 | {
|
---|
[6eaed07] | 2551 | ASSERT(item->hash == h->invalid_hash
|
---|
| 2552 | || item->hash == sentinel.hash
|
---|
| 2553 | || item->hash == calc_node_hash(h, item));
|
---|
| 2554 |
|
---|
| 2555 | return item->hash;
|
---|
[7ef2249] | 2556 | }
|
---|
| 2557 |
|
---|
[b9cb911] | 2558 | /** Calculates and mixed the hash of the item. */
|
---|
[6eaed07] | 2559 | static inline size_t calc_node_hash(cht_t *h, const cht_link_t *item)
|
---|
| 2560 | {
|
---|
| 2561 | ASSERT(item != &sentinel);
|
---|
| 2562 | /*
|
---|
| 2563 | * Clear the lowest order bit in order for sentinel's node hash
|
---|
| 2564 | * to be the greatest possible.
|
---|
| 2565 | */
|
---|
[30c0826] | 2566 | return hash_mix(h->op->hash(item)) & ~(size_t)1;
|
---|
[6eaed07] | 2567 | }
|
---|
| 2568 |
|
---|
[b9cb911] | 2569 | /** Computes and memoizes the hash of the item. */
|
---|
[6eaed07] | 2570 | static inline void memoize_node_hash(cht_t *h, cht_link_t *item)
|
---|
| 2571 | {
|
---|
| 2572 | item->hash = calc_node_hash(h, item);
|
---|
| 2573 | }
|
---|
[7ef2249] | 2574 |
|
---|
[b9cb911] | 2575 | /** Packs the next pointer address and the mark into a single pointer. */
|
---|
[5e5cef3] | 2576 | static inline marked_ptr_t make_link(const cht_link_t *next, mark_t mark)
|
---|
[7ef2249] | 2577 | {
|
---|
| 2578 | marked_ptr_t ptr = (marked_ptr_t) next;
|
---|
| 2579 |
|
---|
| 2580 | ASSERT(!(ptr & N_MARK_MASK));
|
---|
| 2581 | ASSERT(!((unsigned)mark & ~N_MARK_MASK));
|
---|
| 2582 |
|
---|
| 2583 | return ptr | mark;
|
---|
| 2584 | }
|
---|
| 2585 |
|
---|
[b9cb911] | 2586 | /** Strips any marks from the next item link and returns the next item's address.*/
|
---|
[5e5cef3] | 2587 | static inline cht_link_t * get_next(marked_ptr_t link)
|
---|
[7ef2249] | 2588 | {
|
---|
| 2589 | return (cht_link_t*)(link & ~N_MARK_MASK);
|
---|
| 2590 | }
|
---|
| 2591 |
|
---|
[b9cb911] | 2592 | /** Returns the current node's mark stored in the next item link. */
|
---|
[5e5cef3] | 2593 | static inline mark_t get_mark(marked_ptr_t link)
|
---|
[7ef2249] | 2594 | {
|
---|
| 2595 | return (mark_t)(link & N_MARK_MASK);
|
---|
| 2596 | }
|
---|
| 2597 |
|
---|
[b9cb911] | 2598 | /** Moves the window by one item so that is points to the next item. */
|
---|
[5e5cef3] | 2599 | static inline void next_wnd(wnd_t *wnd)
|
---|
[7ef2249] | 2600 | {
|
---|
| 2601 | ASSERT(wnd);
|
---|
| 2602 | ASSERT(wnd->cur);
|
---|
| 2603 |
|
---|
| 2604 | wnd->last = wnd->cur;
|
---|
| 2605 | wnd->ppred = &wnd->cur->link;
|
---|
| 2606 | wnd->cur = get_next(wnd->cur->link);
|
---|
| 2607 | }
|
---|
| 2608 |
|
---|
[b9cb911] | 2609 | /** Predicate that matches only exactly the same node. */
|
---|
[7ef2249] | 2610 | static bool same_node_pred(void *node, const cht_link_t *item2)
|
---|
| 2611 | {
|
---|
| 2612 | const cht_link_t *item1 = (const cht_link_t*) node;
|
---|
| 2613 | return item1 == item2;
|
---|
| 2614 | }
|
---|
| 2615 |
|
---|
[b9cb911] | 2616 | /** Compare-and-swaps a next item link. */
|
---|
[5e5cef3] | 2617 | static inline marked_ptr_t cas_link(marked_ptr_t *link, const cht_link_t *cur_next,
|
---|
[3bb732b] | 2618 | mark_t cur_mark, const cht_link_t *new_next, mark_t new_mark)
|
---|
[7ef2249] | 2619 | {
|
---|
| 2620 | return _cas_link(link, make_link(cur_next, cur_mark),
|
---|
| 2621 | make_link(new_next, new_mark));
|
---|
| 2622 | }
|
---|
| 2623 |
|
---|
[b9cb911] | 2624 | /** Compare-and-swaps a next item link. */
|
---|
[5e5cef3] | 2625 | static inline marked_ptr_t _cas_link(marked_ptr_t *link, marked_ptr_t cur,
|
---|
[7ef2249] | 2626 | marked_ptr_t new)
|
---|
| 2627 | {
|
---|
[6eaed07] | 2628 | ASSERT(link != &sentinel.link);
|
---|
[3bb732b] | 2629 | /*
|
---|
| 2630 | * cas(x) on the same location x on one cpu must be ordered, but do not
|
---|
| 2631 | * have to be ordered wrt to other cas(y) to a different location y
|
---|
| 2632 | * on the same cpu.
|
---|
| 2633 | *
|
---|
| 2634 | * cas(x) must act as a write barrier on x, ie if cas(x) succeeds
|
---|
| 2635 | * and is observed by another cpu, then all cpus must be able to
|
---|
| 2636 | * make the effects of cas(x) visible just by issuing a load barrier.
|
---|
| 2637 | * For example:
|
---|
| 2638 | * cpu1 cpu2 cpu3
|
---|
| 2639 | * cas(x, 0 -> 1), succeeds
|
---|
| 2640 | * cas(x, 0 -> 1), fails
|
---|
[c8fccf5] | 2641 | * MB, to order load of x in cas and store to y
|
---|
[3bb732b] | 2642 | * y = 7
|
---|
| 2643 | * sees y == 7
|
---|
| 2644 | * loadMB must be enough to make cas(x) on cpu3 visible to cpu1, ie x == 1.
|
---|
| 2645 | *
|
---|
| 2646 | * If cas() did not work this way:
|
---|
[c8fccf5] | 2647 | * a) our head move protocol would not be correct.
|
---|
| 2648 | * b) freeing an item linked to a moved head after another item was
|
---|
[3bb732b] | 2649 | * inserted in front of it, would require more than one grace period.
|
---|
[c8fccf5] | 2650 | *
|
---|
| 2651 | * Ad (a): In the following example, cpu1 starts moving old_head
|
---|
| 2652 | * to new_head, cpu2 completes the move and cpu3 notices cpu2
|
---|
| 2653 | * completed the move before cpu1 gets a chance to notice cpu2
|
---|
| 2654 | * had already completed the move. Our requirements for cas()
|
---|
| 2655 | * assume cpu3 will see a valid and mutable value in new_head
|
---|
| 2656 | * after issuing a load memory barrier once it has determined
|
---|
| 2657 | * the old_head's value had been successfully moved to new_head
|
---|
| 2658 | * (because it sees old_head marked invalid).
|
---|
| 2659 | *
|
---|
| 2660 | * cpu1 cpu2 cpu3
|
---|
| 2661 | * cas(old_head, <addr, N>, <addr, Const>), succeeds
|
---|
| 2662 | * cas-order-barrier
|
---|
| 2663 | * // Move from old_head to new_head started, now the interesting stuff:
|
---|
| 2664 | * cas(new_head, <0, Inv>, <addr, N>), succeeds
|
---|
| 2665 | *
|
---|
| 2666 | * cas(new_head, <0, Inv>, <addr, N>), but fails
|
---|
| 2667 | * cas-order-barrier
|
---|
| 2668 | * cas(old_head, <addr, Const>, <addr, Inv>), succeeds
|
---|
| 2669 | *
|
---|
| 2670 | * Sees old_head marked Inv (by cpu2)
|
---|
| 2671 | * load-MB
|
---|
| 2672 | * assert(new_head == <addr, N>)
|
---|
| 2673 | *
|
---|
| 2674 | * cas-order-barrier
|
---|
| 2675 | *
|
---|
| 2676 | * Even though cpu1 did not yet issue a cas-order-barrier, cpu1's store
|
---|
| 2677 | * to new_head (successful cas()) must be made visible to cpu3 with
|
---|
| 2678 | * a load memory barrier if cpu1's store to new_head is visible
|
---|
| 2679 | * on another cpu (cpu2) and that cpu's (cpu2's) store to old_head
|
---|
| 2680 | * is already visible to cpu3. *
|
---|
[3bb732b] | 2681 | */
|
---|
[c8fccf5] | 2682 | void *expected = (void*)cur;
|
---|
| 2683 |
|
---|
| 2684 | /*
|
---|
| 2685 | * Use the acquire-release model, although we could probably
|
---|
| 2686 | * get away even with the relaxed memory model due to our use
|
---|
| 2687 | * of explicit memory barriers.
|
---|
| 2688 | */
|
---|
| 2689 | __atomic_compare_exchange_n((void**)link, &expected, (void *)new, false,
|
---|
| 2690 | __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
|
---|
| 2691 |
|
---|
| 2692 | return (marked_ptr_t) expected;
|
---|
[14c9aa6] | 2693 | }
|
---|
| 2694 |
|
---|
[b9cb911] | 2695 | /** Orders compare-and-swaps to different memory locations. */
|
---|
[5e5cef3] | 2696 | static inline void cas_order_barrier(void)
|
---|
[14c9aa6] | 2697 | {
|
---|
| 2698 | /* Make sure CAS to different memory locations are ordered. */
|
---|
| 2699 | write_barrier();
|
---|
[7ef2249] | 2700 | }
|
---|
| 2701 |
|
---|
[14c9aa6] | 2702 |
|
---|
[7ef2249] | 2703 | /** @}
|
---|
| 2704 | */
|
---|