source: mainline/kernel/generic/src/adt/cht.c@ 11b285d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 11b285d was 11b285d, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Use standard signature for malloc() in kernel.

The remaining instances of blocking allocation are replaced with
a new separate function named nfmalloc (short for non-failing malloc).

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