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