Changeset 1b20da0 in mainline for kernel/generic/src/adt/cht.c


Ignore:
Timestamp:
2018-02-28T17:52:03Z (7 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3061bc1
Parents:
df6ded8
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:26:03)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:52:03)
Message:

style: Remove trailing whitespace on non-empty lines, in certain file types.

Command used: tools/srepl '\([^[:space:]]\)\s\+$' '\1' -- *.c *.h *.py *.sh *.s *.S *.ag

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/adt/cht.c

    rdf6ded8 r1b20da0  
    3535 * @file
    3636 * @brief Scalable resizable concurrent lock-free hash table.
    37  * 
     37 *
    3838 * CHT is a concurrent hash table that is scalable resizable and lock-free.
    3939 * resizable = the number of buckets of the table increases or decreases
     
    4848 * fraction of updates (insert/remove) increases. Other data structures
    4949 * significantly outperform CHT if the fraction of updates exceeds ~40%.
    50  * 
     50 *
    5151 * CHT tolerates hardware exceptions and may be accessed from exception
    5252 * handlers as long as the underlying RCU implementation is exception safe.
    53  * 
     53 *
    5454 * @par Caveats
    55  * 
     55 *
    5656 * 0) Never assume an item is still in the table.
    5757 * The table may be accessed concurrently; therefore, other threads may
     
    5959 * in the table if cht_find() just returned it to you. Similarly, an
    6060 * item may have already been inserted by the time cht_find() returns NULL.
    61  * 
     61 *
    6262 * 1) Always use RCU read locks when searching the table.
    6363 * Holding an RCU lock guarantees that an item found in the table remains
    6464 * valid (eg is not freed) even if the item was removed from the table
    6565 * in the meantime by another thread.
    66  * 
     66 *
    6767 * 2) Never update values in place.
    6868 * Do not update items in the table in place, ie directly. The changes
    6969 * will not propagate to other readers (on other cpus) immediately or even
    7070 * correctly. Some readers may then encounter items that have only some
    71  * of their fields changed or are completely inconsistent. 
    72  * 
    73  * Instead consider inserting an updated/changed copy of the item and 
     71 * of their fields changed or are completely inconsistent.
     72 *
     73 * Instead consider inserting an updated/changed copy of the item and
    7474 * removing the original item. Or contact the maintainer to provide
    7575 * you with a function that atomically replaces an item with a copy.
    76  * 
     76 *
    7777 * 3) Use cht_insert_unique() instead of checking for duplicates with cht_find()
    7878 * The following code is prone to race conditions:
     
    8484 * @endcode
    8585 * See cht_insert_unique() on how to correctly fix this.
    86  * 
     86 *
    8787 *
    8888 * @par Semantics
    89  * 
     89 *
    9090 * Lazy readers = cht_find_lazy(), cht_find_next_lazy()
    9191 * Readers = lazy readers, cht_find(), cht_find_next()
    92  * Updates = cht_insert(), cht_insert_unique(), cht_remove_key(), 
     92 * Updates = cht_insert(), cht_insert_unique(), cht_remove_key(),
    9393 *     cht_remove_item()
    94  * 
    95  * Readers (but not lazy readers) are guaranteed to see the effects 
    96  * of @e completed updates. In other words, if cht_find() is invoked 
    97  * after a cht_insert() @e returned eg on another cpu, cht_find() is 
    98  * guaranteed to see the inserted item. 
    99  * 
     94 *
     95 * Readers (but not lazy readers) are guaranteed to see the effects
     96 * of @e completed updates. In other words, if cht_find() is invoked
     97 * after a cht_insert() @e returned eg on another cpu, cht_find() is
     98 * guaranteed to see the inserted item.
     99 *
    100100 * Similarly, updates see the effects of @e completed updates. For example,
    101  * issuing cht_remove() after a cht_insert() for that key returned (even 
     101 * issuing cht_remove() after a cht_insert() for that key returned (even
    102102 * on another cpu) is guaranteed to remove the inserted item.
    103  * 
     103 *
    104104 * Reading or updating the table concurrently with other updates
    105105 * always returns consistent data and never corrupts the table.
    106106 * However the effects of concurrent updates may or may not be
    107107 * visible to all other concurrent readers or updaters. Eg, not
    108  * all readers may see that an item has already been inserted 
    109  * if cht_insert() has not yet returned. 
    110  * 
     108 * all readers may see that an item has already been inserted
     109 * if cht_insert() has not yet returned.
     110 *
    111111 * Lazy readers are guaranteed to eventually see updates but it
    112112 * may take some time (possibly milliseconds) after the update
    113113 * completes for the change to propagate to lazy readers on all
    114114 * cpus.
    115  * 
     115 *
    116116 * @par Implementation
    117  * 
     117 *
    118118 * Collisions in CHT are resolved with chaining. The number of buckets
    119  * is always a power of 2. Each bucket is represented with a single linked 
    120  * lock-free list [1]. Items in buckets are sorted by their mixed hashes 
    121  * in ascending order. All buckets are terminated with a single global 
    122  * sentinel node whose mixed hash value is the greatest possible. 
     119 * is always a power of 2. Each bucket is represented with a single linked
     120 * lock-free list [1]. Items in buckets are sorted by their mixed hashes
     121 * in ascending order. All buckets are terminated with a single global
     122 * sentinel node whose mixed hash value is the greatest possible.
    123123 *
    124124 * CHT with 2^k buckets uses the k most significant bits of a hash value
     
    127127 * hash function does not produce uniform hashes, hash values are
    128128 * mixed first so that the top bits of a mixed hash change even if hash
    129  * values differ only in the least significant bits. The mixed hash 
    130  * values are cached in cht_link.hash (which is overwritten once the 
     129 * values differ only in the least significant bits. The mixed hash
     130 * values are cached in cht_link.hash (which is overwritten once the
    131131 * item is scheduled for removal via rcu_call).
    132  * 
     132 *
    133133 * A new item is inserted before all other existing items in the bucket
    134134 * with the same hash value as the newly inserted item (a la the original
    135  * lock-free list [2]). Placing new items at the start of a same-hash 
    136  * sequence of items (eg duplicates) allows us to easily check for duplicates 
    137  * in cht_insert_unique(). The function can first check that there are 
    138  * no duplicates of the newly inserted item amongst the items with the 
    139  * same hash as the new item. If there were no duplicates the new item 
    140  * is linked before the same-hash items. Inserting a duplicate while 
    141  * the function is checking for duplicates is detected as a change of 
    142  * the link to the first checked same-hash item (and the search for 
     135 * lock-free list [2]). Placing new items at the start of a same-hash
     136 * sequence of items (eg duplicates) allows us to easily check for duplicates
     137 * in cht_insert_unique(). The function can first check that there are
     138 * no duplicates of the newly inserted item amongst the items with the
     139 * same hash as the new item. If there were no duplicates the new item
     140 * is linked before the same-hash items. Inserting a duplicate while
     141 * the function is checking for duplicates is detected as a change of
     142 * the link to the first checked same-hash item (and the search for
    143143 * duplicates can be restarted).
    144  * 
     144 *
    145145 * @par Table resize algorithm
    146  * 
     146 *
    147147 * Table resize is based on [3] and [5]. First, a new bucket head array
    148148 * is allocated and initialized. Second, old bucket heads are moved
    149  * to the new bucket head array with the protocol mentioned in [5]. 
     149 * to the new bucket head array with the protocol mentioned in [5].
    150150 * At this point updaters start using the new bucket heads. Third,
    151151 * buckets are split (or joined) so that the table can make use of
    152152 * the extra bucket head slots in the new array (or stop wasting space
    153153 * with the unnecessary extra slots in the old array). Splitting
    154  * or joining buckets employs a custom protocol. Last, the new array 
     154 * or joining buckets employs a custom protocol. Last, the new array
    155155 * replaces the original bucket array.
    156  * 
     156 *
    157157 * A single background work item (of the system work queue) guides
    158158 * resizing of the table. If an updater detects that the bucket it
     
    160160 * or it needs to be split/joined), it helps out and completes the
    161161 * head move or the bucket split/join.
    162  * 
    163  * The table always grows or shrinks by a factor of 2. Because items 
    164  * are assigned a bucket based on the top k bits of their mixed hash 
    165  * values, when growing the table each bucket is split into two buckets 
    166  * and all items of the two new buckets come from the single bucket in the 
     162 *
     163 * The table always grows or shrinks by a factor of 2. Because items
     164 * are assigned a bucket based on the top k bits of their mixed hash
     165 * values, when growing the table each bucket is split into two buckets
     166 * and all items of the two new buckets come from the single bucket in the
    167167 * original table. Ie items from separate buckets in the original table
    168  * never intermix in the new buckets. Moreover 
    169  * since the buckets are sorted by their mixed hash values the items 
    170  * at the beginning of the old bucket will end up in the first new 
     168 * never intermix in the new buckets. Moreover
     169 * since the buckets are sorted by their mixed hash values the items
     170 * at the beginning of the old bucket will end up in the first new
    171171 * bucket while all the remaining items of the old bucket will end up
    172  * in the second new bucket. Therefore, there is a single point where 
    173  * to split the linked list of the old bucket into two correctly sorted 
     172 * in the second new bucket. Therefore, there is a single point where
     173 * to split the linked list of the old bucket into two correctly sorted
    174174 * linked lists of the new buckets:
    175175 *                            .- bucket split
    176  *                            | 
    177  *             <-- first -->  v  <-- second --> 
     176 *                            |
     177 *             <-- first -->  v  <-- second -->
    178178 *   [old] --> [00b] -> [01b] -> [10b] -> [11b] -> sentinel
    179  *              ^                 ^   
    180  *   [new0] -- -+                 | 
     179 *              ^                 ^
     180 *   [new0] -- -+                 |
    181181 *   [new1] -- -- -- -- -- -- -- -+
    182  * 
     182 *
    183183 * Resize in greater detail:
    184  * 
    185  * a) First, a resizer (a single background system work queue item 
    186  * in charge of resizing the table) allocates and initializes a new 
    187  * bucket head array. New bucket heads are pointed to the sentinel 
    188  * and marked Invalid (in the lower order bits of the pointer to the 
     184 *
     185 * a) First, a resizer (a single background system work queue item
     186 * in charge of resizing the table) allocates and initializes a new
     187 * bucket head array. New bucket heads are pointed to the sentinel
     188 * and marked Invalid (in the lower order bits of the pointer to the
    189189 * next item, ie the sentinel in this case):
    190  * 
     190 *
    191191 *   [old, N] --> [00b] -> [01b] -> [10b] -> [11b] -> sentinel
    192192 *                                                    ^ ^
    193193 *   [new0, Inv] -------------------------------------+ |
    194194 *   [new1, Inv] ---------------------------------------+
    195  * 
    196  * 
    197  * b) Second, the resizer starts moving old bucket heads with the following 
    198  * lock-free protocol (from [5]) where cas(variable, expected_val, new_val) 
     195 *
     196 *
     197 * b) Second, the resizer starts moving old bucket heads with the following
     198 * lock-free protocol (from [5]) where cas(variable, expected_val, new_val)
    199199 * is short for compare-and-swap:
    200  * 
     200 *
    201201 *   old head     new0 head      transition to next state
    202202 *   --------     ---------      ------------------------
    203203 *   addr, N      sentinel, Inv  cas(old, (addr, N), (addr, Const))
    204  *                               .. mark the old head as immutable, so that 
    205  *                                  updaters do not relink it to other nodes 
     204 *                               .. mark the old head as immutable, so that
     205 *                                  updaters do not relink it to other nodes
    206206 *                                  until the head move is done.
    207207 *   addr, Const  sentinel, Inv  cas(new0, (sentinel, Inv), (addr, N))
    208  *                               .. move the address to the new head and mark 
     208 *                               .. move the address to the new head and mark
    209209 *                                  the new head normal so updaters can start
    210210 *                                  using it.
     
    213213 *                                  the head move is done.
    214214 *   addr, Inv    addr, N
    215  * 
     215 *
    216216 * Notice that concurrent updaters may step in at any point and correctly
    217217 * complete the head move without disrupting the resizer. At worst, the
    218  * resizer or other concurrent updaters will attempt a number of CAS() that 
     218 * resizer or other concurrent updaters will attempt a number of CAS() that
    219219 * will correctly fail.
    220  * 
     220 *
    221221 *   [old, Inv] -> [00b] -> [01b] -> [10b] -> [11b] -> sentinel
    222222 *                 ^                                   ^
    223223 *   [new0, N] ----+                                   |
    224224 *   [new1, Inv] --------------------------------------+
    225  * 
    226  * 
    227  * c) Third, buckets are split if the table is growing; or joined if 
    228  * shrinking (by the resizer or updaters depending on whoever accesses 
     225 *
     226 *
     227 * c) Third, buckets are split if the table is growing; or joined if
     228 * shrinking (by the resizer or updaters depending on whoever accesses
    229229 * the bucket first). See split_bucket() and join_buckets() for details.
    230  * 
     230 *
    231231 *  1) Mark the last item of new0 with JOIN_FOLLOWS:
    232232 *   [old, Inv] -> [00b] -> [01b, JF] -> [10b] -> [11b] -> sentinel
     
    234234 *   [new0, N] ----+                                       |
    235235 *   [new1, Inv] ------------------------------------------+
    236  * 
     236 *
    237237 *  2) Mark the first item of new1 with JOIN_NODE:
    238238 *   [old, Inv] -> [00b] -> [01b, JF] -> [10b, JN] -> [11b] -> sentinel
     
    240240 *   [new0, N] ----+                                           |
    241241 *   [new1, Inv] ----------------------------------------------+
    242  * 
     242 *
    243243 *  3) Point new1 to the join-node and mark new1 NORMAL.
    244244 *   [old, Inv] -> [00b] -> [01b, JF] -> [10b, JN] -> [11b] -> sentinel
     
    246246 *   [new0, N] ----+                     |
    247247 *   [new1, N] --------------------------+
    248  * 
    249  * 
    250  * d) Fourth, the resizer cleans up extra marks added during bucket 
     248 *
     249 *
     250 * d) Fourth, the resizer cleans up extra marks added during bucket
    251251 * splits/joins but only when it is sure all updaters are accessing
    252252 * the table via the new bucket heads only (ie it is certain there
    253  * are no delayed updaters unaware of the resize and accessing the 
     253 * are no delayed updaters unaware of the resize and accessing the
    254254 * table via the old bucket head).
    255  * 
     255 *
    256256 *   [old, Inv] ---+
    257257 *                 v
     
    259259 *                                      v
    260260 *   [new1, N] --> [10b, N] -> [11b] -> sentinel
    261  * 
    262  * 
     261 *
     262 *
    263263 * e) Last, the resizer publishes the new bucket head array for everyone
    264264 * to see and use. This signals the end of the resize and the old bucket
    265  * array is freed. 
    266  * 
    267  * 
     265 * array is freed.
     266 *
     267 *
    268268 * To understand details of how the table is resized, read [1, 3, 5]
    269269 * and comments in join_buckets(), split_bucket().
    270  * 
    271  * 
    272  * [1] High performance dynamic lock-free hash tables and list-based sets, 
     270 *
     271 *
     272 * [1] High performance dynamic lock-free hash tables and list-based sets,
    273273 *     Michael, 2002
    274274 *     http://www.research.ibm.com/people/m/michael/spaa-2002.pdf
     
    311311#define CHT_MIN_BUCKET_CNT (1 << CHT_MIN_ORDER)
    312312/* Does not have to be a power of 2. */
    313 #define CHT_MAX_LOAD 2 
     313#define CHT_MAX_LOAD 2
    314314
    315315typedef cht_ptr_t marked_ptr_t;
    316316typedef bool (*equal_pred_t)(void *arg, const cht_link_t *item);
    317317
    318 /** The following mark items and bucket heads. 
    319  * 
     318/** The following mark items and bucket heads.
     319 *
    320320 * They are stored in the two low order bits of the next item pointers.
    321321 * Some marks may be combined. Some marks share the same binary value and
     
    327327        N_NORMAL = 0,
    328328        /** Logically deleted item that might have already been unlinked.
    329          * 
    330          * May be combined with N_JOIN and N_JOIN_FOLLOWS. Applicable only 
    331          * to items; never to bucket heads. 
    332          * 
    333          * Once marked deleted an item remains marked deleted.   
     329         *
     330         * May be combined with N_JOIN and N_JOIN_FOLLOWS. Applicable only
     331         * to items; never to bucket heads.
     332         *
     333         * Once marked deleted an item remains marked deleted.
    334334         */
    335335        N_DELETED = 1,
    336         /** Immutable bucket head. 
    337          * 
    338          * The bucket is being moved or joined with another and its (old) head 
     336        /** Immutable bucket head.
     337         *
     338         * The bucket is being moved or joined with another and its (old) head
    339339         * must not be modified.
    340          * 
     340         *
    341341         * May be combined with N_INVALID. Applicable only to old bucket heads,
    342342         * ie cht_t.b and not cht_t.new_b.
    343343         */
    344344        N_CONST = 1,
    345         /** Invalid bucket head. The bucket head must not be modified. 
    346          * 
     345        /** Invalid bucket head. The bucket head must not be modified.
     346         *
    347347         * Old bucket heads (ie cht_t.b) are marked invalid if they have
    348348         * already been moved to cht_t.new_b or if the bucket had already
     
    354354        N_INVALID = 3,
    355355        /** The item is a join node, ie joining two buckets
    356          * 
     356         *
    357357         * A join node is either the first node of the second part of
    358358         * a bucket to be split; or it is the first node of the bucket
    359359         * to be merged into/appended to/joined with another bucket.
    360          * 
    361          * May be combined with N_DELETED. Applicable only to items, never 
     360         *
     361         * May be combined with N_DELETED. Applicable only to items, never
    362362         * to bucket heads.
    363          * 
     363         *
    364364         * Join nodes are referred to from two different buckets and may,
    365365         * therefore, not be safely/atomically unlinked from both buckets.
     
    369369         */
    370370        N_JOIN = 2,
    371         /** The next node is a join node and will soon be marked so. 
    372          * 
     371        /** The next node is a join node and will soon be marked so.
     372         *
    373373         * A join-follows node is the last node of the first part of bucket
    374374         * that is to be split, ie it is the last node that will remain
    375375         * in the same bucket after splitting it.
    376          * 
     376         *
    377377         * May be combined with N_DELETED. Applicable to items as well
    378          * as to bucket heads of the bucket to be split (but only in cht_t.new_b). 
     378         * as to bucket heads of the bucket to be split (but only in cht_t.new_b).
    379379         */
    380380        N_JOIN_FOLLOWS = 2,
     
    413413
    414414static size_t size_to_order(size_t bucket_cnt, size_t min_order);
    415 static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid, 
     415static cht_buckets_t *alloc_buckets(size_t order, bool set_invalid,
    416416        bool can_block);
    417417static inline cht_link_t *find_lazy(cht_t *h, void *key);
    418 static cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key, 
     418static cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key,
    419419        size_t search_hash);
    420 static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash, 
     420static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash,
    421421        marked_ptr_t old_head, size_t old_idx);
    422422static bool insert_impl(cht_t *h, cht_link_t *item, cht_link_t **dup_item);
    423423static bool insert_at(cht_link_t *item, const wnd_t *wnd, walk_mode_t walk_mode,
    424424        bool *resizing);
    425 static bool has_duplicate(cht_t *h, const cht_link_t *item, size_t hash, 
     425static bool has_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
    426426        cht_link_t *cur, cht_link_t **dup_item);
    427 static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash, 
     427static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
    428428        cht_link_t *start);
    429429static bool remove_pred(cht_t *h, size_t hash, equal_pred_t pred, void *pred_arg);
    430 static bool delete_at(cht_t *h, wnd_t *wnd, walk_mode_t walk_mode, 
     430static bool delete_at(cht_t *h, wnd_t *wnd, walk_mode_t walk_mode,
    431431        bool *deleted_but_gc, bool *resizing);
    432432static bool mark_deleted(cht_link_t *cur, walk_mode_t walk_mode, bool *resizing);
    433433static bool unlink_from_pred(wnd_t *wnd, walk_mode_t walk_mode, bool *resizing);
    434 static bool find_wnd_and_gc_pred(cht_t *h, size_t hash, walk_mode_t walk_mode, 
     434static bool find_wnd_and_gc_pred(cht_t *h, size_t hash, walk_mode_t walk_mode,
    435435        equal_pred_t pred, void *pred_arg, wnd_t *wnd, bool *resizing);
    436 static bool find_wnd_and_gc(cht_t *h, size_t hash, walk_mode_t walk_mode, 
     436static bool find_wnd_and_gc(cht_t *h, size_t hash, walk_mode_t walk_mode,
    437437        wnd_t *wnd, bool *resizing);
    438438static bool gc_deleted_node(cht_t *h, walk_mode_t walk_mode, wnd_t *wnd,
    439439        bool *resizing);
    440440static bool join_completed(cht_t *h, const wnd_t *wnd);
    441 static void upd_resizing_head(cht_t *h, size_t hash, marked_ptr_t **phead, 
     441static void upd_resizing_head(cht_t *h, size_t hash, marked_ptr_t **phead,
    442442        bool *join_finishing,  walk_mode_t *walk_mode);
    443443static void item_removed(cht_t *h);
     
    448448static void mark_const(marked_ptr_t *psrc_head);
    449449static void complete_head_move(marked_ptr_t *psrc_head, marked_ptr_t *pdest_head);
    450 static void split_bucket(cht_t *h, marked_ptr_t *psrc_head, 
     450static void split_bucket(cht_t *h, marked_ptr_t *psrc_head,
    451451        marked_ptr_t *pdest_head, size_t split_hash);
    452 static void mark_join_follows(cht_t *h, marked_ptr_t *psrc_head, 
     452static void mark_join_follows(cht_t *h, marked_ptr_t *psrc_head,
    453453        size_t split_hash, wnd_t *wnd);
    454454static void mark_join_node(cht_link_t *join_node);
    455 static void join_buckets(cht_t *h, marked_ptr_t *psrc_head, 
     455static void join_buckets(cht_t *h, marked_ptr_t *psrc_head,
    456456        marked_ptr_t *pdest_head, size_t split_hash);
    457 static void link_to_join_node(cht_t *h, marked_ptr_t *pdest_head, 
     457static void link_to_join_node(cht_t *h, marked_ptr_t *pdest_head,
    458458        cht_link_t *join_node, size_t split_hash);
    459459static void resize_table(work_t *arg);
     
    461461static void shrink_table(cht_t *h);
    462462static void cleanup_join_node(cht_t *h, marked_ptr_t *new_head);
    463 static void clear_join_and_gc(cht_t *h, cht_link_t *join_node, 
     463static void clear_join_and_gc(cht_t *h, cht_link_t *join_node,
    464464        marked_ptr_t *new_head);
    465465static void cleanup_join_follows(cht_t *h, marked_ptr_t *new_head);
     
    478478static size_t grow_idx(size_t idx);
    479479static size_t shrink_idx(size_t idx);
    480 static marked_ptr_t cas_link(marked_ptr_t *link, const cht_link_t *cur_next, 
     480static marked_ptr_t cas_link(marked_ptr_t *link, const cht_link_t *cur_next,
    481481        mark_t cur_mark, const cht_link_t *new_next, mark_t new_mark);
    482 static marked_ptr_t _cas_link(marked_ptr_t *link, marked_ptr_t cur, 
     482static marked_ptr_t _cas_link(marked_ptr_t *link, marked_ptr_t cur,
    483483        marked_ptr_t new);
    484484static void cas_order_barrier(void);
     
    490490
    491491/** Creates a concurrent hash table.
    492  * 
     492 *
    493493 * @param h         Valid pointer to a cht_t instance.
    494494 * @param op        Item specific operations. All operations are compulsory.
     
    497497bool cht_create_simple(cht_t *h, cht_ops_t *op)
    498498{
    499         return cht_create(h, 0, 0, 0, false, op); 
     499        return cht_create(h, 0, 0, 0, false, op);
    500500}
    501501
    502502/** Creates a concurrent hash table.
    503  * 
     503 *
    504504 * @param h         Valid pointer to a cht_t instance.
    505505 * @param init_size The initial number of buckets the table should contain.
     
    513513 *                  before the table grows.
    514514 * @param can_block If true creating the table blocks until enough memory
    515  *                  is available (possibly indefinitely). Otherwise, 
     515 *                  is available (possibly indefinitely). Otherwise,
    516516 *                  table creation does not block and returns immediately
    517  *                  even if not enough memory is available. 
     517 *                  even if not enough memory is available.
    518518 * @param op        Item specific operations. All operations are compulsory.
    519519 * @return True if successfully created the table. False otherwise.
    520520 */
    521 bool cht_create(cht_t *h, size_t init_size, size_t min_size, size_t max_load, 
     521bool cht_create(cht_t *h, size_t init_size, size_t min_size, size_t max_load,
    522522        bool can_block, cht_ops_t *op)
    523523{
     
    551551        }
    552552       
    553         /* 
     553        /*
    554554         * Cached item hashes are stored in item->rcu_link.func. Once the item
    555555         * is deleted rcu_link.func will contain the value of invalid_hash.
     
    564564
    565565/** Allocates and initializes 2^order buckets.
    566  * 
     566 *
    567567 * All bucket heads are initialized to point to the sentinel node.
    568  * 
     568 *
    569569 * @param order       The number of buckets to allocate is 2^order.
    570570 * @param set_invalid Bucket heads are marked invalid if true; otherwise
    571571 *                    they are marked N_NORMAL.
    572572 * @param can_block   If true memory allocation blocks until enough memory
    573  *                    is available (possibly indefinitely). Otherwise, 
    574  *                    memory allocation does not block. 
     573 *                    is available (possibly indefinitely). Otherwise,
     574 *                    memory allocation does not block.
    575575 * @return Newly allocated and initialized buckets or NULL if not enough memory.
    576576 */
     
    578578{
    579579        size_t bucket_cnt = (1 << order);
    580         size_t bytes = 
     580        size_t bytes =
    581581                sizeof(cht_buckets_t) + (bucket_cnt - 1) * sizeof(marked_ptr_t);
    582582        cht_buckets_t *b = malloc(bytes, can_block ? 0 : FRAME_ATOMIC);
     
    587587        b->order = order;
    588588       
    589         marked_ptr_t head_link = set_invalid 
    590                 ? make_link(&sentinel, N_INVALID) 
     589        marked_ptr_t head_link = set_invalid
     590                ? make_link(&sentinel, N_INVALID)
    591591                : make_link(&sentinel, N_NORMAL);
    592592       
     
    615615
    616616/** Destroys a CHT successfully created via cht_create().
    617  * 
     617 *
    618618 * Waits for all outstanding concurrent operations to complete and
    619619 * frees internal allocated resources. The table is however not cleared
     
    644644
    645645/** Returns the first item equal to the search key or NULL if not found.
    646  * 
    647  * The call must be enclosed in a rcu_read_lock() unlock() pair. The 
     646 *
     647 * The call must be enclosed in a rcu_read_lock() unlock() pair. The
    648648 * returned item is guaranteed to be allocated until rcu_read_unlock()
    649649 * although the item may be concurrently removed from the table by another
    650650 * cpu.
    651  * 
     651 *
    652652 * Further items matching the key may be retrieved via cht_find_next().
    653  * 
     653 *
    654654 * cht_find() sees the effects of any completed cht_remove(), cht_insert().
    655655 * If a concurrent remove or insert had not yet completed cht_find() may
    656656 * or may not see the effects of it (eg it may find an item being removed).
    657  * 
     657 *
    658658 * @param h   CHT to operate on.
    659659 * @param key Search key as defined by cht_ops_t.key_equal() and .key_hash().
     
    668668
    669669/** Returns the first item equal to the search key or NULL if not found.
    670  * 
    671  * Unlike cht_find(), cht_find_lazy() may not see the effects of 
     670 *
     671 * Unlike cht_find(), cht_find_lazy() may not see the effects of
    672672 * cht_remove() or cht_insert() even though they have already completed.
    673673 * It may take a couple of milliseconds for those changes to propagate
    674  * and become visible to cht_find_lazy(). On the other hand, cht_find_lazy() 
     674 * and become visible to cht_find_lazy(). On the other hand, cht_find_lazy()
    675675 * operates a bit faster than cht_find().
    676  * 
     676 *
    677677 * See cht_find() for more details.
    678678 */
     
    693693        cht_buckets_t *b = rcu_access(h->b);
    694694        size_t idx = calc_bucket_idx(hash, b->order);
    695         /* 
    696          * No need for access_once. b->head[idx] will point to an allocated node 
     695        /*
     696         * No need for access_once. b->head[idx] will point to an allocated node
    697697         * even if marked invalid until we exit rcu read section.
    698698         */
     
    706706}
    707707
    708 /** Returns the next item matching \a item. 
    709  * 
    710  * Must be enclosed in a rcu_read_lock()/unlock() pair. Effects of 
     708/** Returns the next item matching \a item.
     709 *
     710 * Must be enclosed in a rcu_read_lock()/unlock() pair. Effects of
    711711 * completed cht_remove(), cht_insert() are guaranteed to be visible
    712712 * to cht_find_next().
    713  * 
    714  * See cht_find() for more details. 
     713 *
     714 * See cht_find() for more details.
    715715 */
    716716cht_link_t *cht_find_next(cht_t *h, const cht_link_t *item)
     
    721721}
    722722
    723 /** Returns the next item matching \a item. 
    724  * 
    725  * Must be enclosed in a rcu_read_lock()/unlock() pair. Effects of 
     723/** Returns the next item matching \a item.
     724 *
     725 * Must be enclosed in a rcu_read_lock()/unlock() pair. Effects of
    726726 * completed cht_remove(), cht_insert() may or may not be visible
    727727 * to cht_find_next_lazy().
    728  * 
    729  * See cht_find_lazy() for more details. 
     728 *
     729 * See cht_find_lazy() for more details.
    730730 */
    731731cht_link_t *cht_find_next_lazy(cht_t *h, const cht_link_t *item)
     
    739739
    740740/** Searches the bucket at head for key using search_hash. */
    741 static inline cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key, 
     741static inline cht_link_t *search_bucket(cht_t *h, marked_ptr_t head, void *key,
    742742        size_t search_hash)
    743743{
    744         /* 
     744        /*
    745745         * It is safe to access nodes even outside of this bucket (eg when
    746          * splitting the bucket). The resizer makes sure that any node we 
     746         * splitting the bucket). The resizer makes sure that any node we
    747747         * may find by following the next pointers is allocated.
    748748         */
     
    759759        } while (node_hash(h, cur) < search_hash);
    760760       
    761         /* 
     761        /*
    762762         * Only search for an item with an equal key if cur is not the sentinel
    763          * node or a node with a different hash. 
     763         * node or a node with a different hash.
    764764         */
    765765        while (node_hash(h, cur) == search_hash) {
     
    771771                cur = get_next(cur->link);
    772772                assert(cur);
    773         } 
    774        
    775         /* 
     773        }
     774       
     775        /*
    776776         * In the unlikely case that we have encountered a node whose cached
    777777         * hash has been overwritten due to a pending rcu_call for it, skip
     
    787787
    788788/** Searches for the key while the table is undergoing a resize. */
    789 static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash, 
     789static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash,
    790790        marked_ptr_t old_head, size_t old_idx)
    791791{
    792         assert(N_INVALID == get_mark(old_head)); 
     792        assert(N_INVALID == get_mark(old_head));
    793793        assert(h->new_b);
    794794       
     
    799799        /* Growing. */
    800800        if (h->b->order < h->new_b->order) {
    801                 /* 
     801                /*
    802802                 * Old bucket head is invalid, so it must have been already
    803803                 * moved. Make the new head visible if still not visible, ie
     
    805805                 */
    806806                if (N_INVALID == get_mark(new_head)) {
    807                         /* 
     807                        /*
    808808                         * We should be searching a newly added bucket but the old
    809                          * moved bucket has not yet been split (its marked invalid) 
    810                          * or we have not yet seen the split. 
     809                         * moved bucket has not yet been split (its marked invalid)
     810                         * or we have not yet seen the split.
    811811                         */
    812812                        if (grow_idx(old_idx) != new_idx) {
    813                                 /* 
     813                                /*
    814814                                 * Search the moved bucket. It is guaranteed to contain
    815815                                 * items of the newly added bucket that were present
     
    821821                        /* new_head is now the moved bucket, either valid or invalid. */
    822822                       
    823                         /* 
     823                        /*
    824824                         * The old bucket was definitely moved to new_head but the
    825825                         * change of new_head had not yet propagated to this cpu.
     
    829829                                 * We could issue a read_barrier() and make the now valid
    830830                                 * moved bucket head new_head visible, but instead fall back
    831                                  * on using the old bucket. Although the old bucket head is 
    832                                  * invalid, it points to a node that is allocated and in the 
     831                                 * on using the old bucket. Although the old bucket head is
     832                                 * invalid, it points to a node that is allocated and in the
    833833                                 * right bucket. Before the node can be freed, it must be
    834834                                 * unlinked from the head (or another item after that item
    835                                  * modified the new_head) and a grace period must elapse. 
     835                                 * modified the new_head) and a grace period must elapse.
    836836                                 * As a result had the node been already freed the grace
    837837                                 * period preceeding the free() would make the unlink and
     
    855855               
    856856                /*
    857                  * h->b->head[move_src_idx] had already been moved to new_head 
     857                 * h->b->head[move_src_idx] had already been moved to new_head
    858858                 * but the change to new_head had not yet propagated to us.
    859859                 */
    860860                if (N_INVALID == get_mark(new_head)) {
    861861                        /*
    862                          * new_head is definitely valid and we could make it visible 
    863                          * to this cpu with a read_barrier(). Instead, use the bucket 
    864                          * in the old table that was moved even though it is now marked 
     862                         * new_head is definitely valid and we could make it visible
     863                         * to this cpu with a read_barrier(). Instead, use the bucket
     864                         * in the old table that was moved even though it is now marked
    865865                         * as invalid. The node it points to must be allocated because
    866866                         * a grace period would have to elapse before it could be freed;
    867                          * and the grace period would make the now valid new_head 
    868                          * visible to all cpus. 
    869                          * 
     867                         * and the grace period would make the now valid new_head
     868                         * visible to all cpus.
     869                         *
    870870                         * Note that move_src_idx may not be the same as old_idx.
    871871                         * If move_src_idx != old_idx then old_idx is the bucket
     
    873873                         * appended to the moved bucket, ie it is added at the tail
    874874                         * of new_head. In that case an invalid old_head notes that
    875                          * it had already been merged into (the moved) new_head. 
     875                         * it had already been merged into (the moved) new_head.
    876876                         * We will try to search that bucket first because it
    877                          * may contain some newly added nodes after the bucket 
    878                          * join. Moreover, the bucket joining link may already be 
     877                         * may contain some newly added nodes after the bucket
     878                         * join. Moreover, the bucket joining link may already be
    879879                         * visible even if new_head is not. Therefore, if we're
    880880                         * lucky we'll find the item via moved_old_head. In any
     
    895895                if (move_src_idx != old_idx && get_next(old_head) != &sentinel) {
    896896                        /*
    897                          * Note that old_head (the bucket to be merged into new_head) 
    898                          * points to an allocated join node (if non-null) even if marked 
     897                         * Note that old_head (the bucket to be merged into new_head)
     898                         * points to an allocated join node (if non-null) even if marked
    899899                         * invalid. Before the resizer lets join nodes to be unlinked
    900900                         * (and freed) it sets old_head to NULL and waits for a grace period.
     
    909909                return NULL;
    910910        } else {
    911                 /* 
     911                /*
    912912                 * Resize is almost done. The resizer is waiting to make
    913913                 * sure all cpus see that the new table replaced the old one.
    914914                 */
    915915                assert(h->b->order == h->new_b->order);
    916                 /* 
     916                /*
    917917                 * The resizer must ensure all new bucket heads are visible before
    918918                 * replacing the old table.
     
    929929}
    930930
    931 /** Inserts a unique item. Returns false if an equal item was already present. 
    932  * 
     931/** Inserts a unique item. Returns false if an equal item was already present.
     932 *
    933933 * Use this function to atomically check if an equal/duplicate item had
    934  * not yet been inserted into the table and to insert this item into the 
     934 * not yet been inserted into the table and to insert this item into the
    935935 * table.
    936  * 
     936 *
    937937 * The following is @e NOT thread-safe, so do not use:
    938938 * @code
     
    944944 * }
    945945 * @endcode
    946  * 
     946 *
    947947 * Replace such code with:
    948948 * @code
     
    951951 *     // Whoops, someone beat us to it - an equal item 'dup_item'
    952952 *     // had already been inserted.
    953  *     free(item); 
     953 *     free(item);
    954954 * } else {
    955955 *     // Successfully inserted the item and we are guaranteed that
     
    957957 * }
    958958 * @endcode
    959  * 
     959 *
    960960 */
    961961bool cht_insert_unique(cht_t *h, cht_link_t *item, cht_link_t **dup_item)
     
    10071007                }
    10081008               
    1009                 inserted = insert_at(item, &wnd, walk_mode, &resizing);         
     1009                inserted = insert_at(item, &wnd, walk_mode, &resizing);
    10101010        } while (!inserted);
    10111011       
     
    10161016}
    10171017
    1018 /** Inserts item between wnd.ppred and wnd.cur. 
    1019  * 
     1018/** Inserts item between wnd.ppred and wnd.cur.
     1019 *
    10201020 * @param item      Item to link to wnd.ppred and wnd.cur.
    10211021 * @param wnd       The item will be inserted before wnd.cur. Wnd.ppred
    10221022 *                  must be N_NORMAL.
    1023  * @param walk_mode 
    1024  * @param resizing  Set to true only if the table is undergoing resize 
     1023 * @param walk_mode
     1024 * @param resizing  Set to true only if the table is undergoing resize
    10251025 *         and it was not expected (ie walk_mode == WM_NORMAL).
    10261026 * @return True if the item was successfully linked to wnd.ppred. False
     
    10281028 *         of wnd.cur has changed.
    10291029 */
    1030 inline static bool insert_at(cht_link_t *item, const wnd_t *wnd, 
     1030inline static bool insert_at(cht_link_t *item, const wnd_t *wnd,
    10311031        walk_mode_t walk_mode, bool *resizing)
    10321032{
     
    10761076
    10771077/** Returns true if the chain starting at cur has an item equal to \a item.
    1078  * 
     1078 *
    10791079 * @param h    CHT to operate on.
    10801080 * @param item Item whose duplicates the function looks for.
     
    10841084 * @return True if a non-deleted item equal to \a item exists in the table.
    10851085 */
    1086 static inline bool has_duplicate(cht_t *h, const cht_link_t *item, size_t hash, 
     1086static inline bool has_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
    10871087        cht_link_t *cur, cht_link_t **dup_item)
    10881088{
     
    10951095                return false;
    10961096
    1097         /* 
    1098          * Load the most recent node marks. Otherwise we might pronounce a 
    1099          * logically deleted node for a duplicate of the item just because 
     1097        /*
     1098         * Load the most recent node marks. Otherwise we might pronounce a
     1099         * logically deleted node for a duplicate of the item just because
    11001100         * the deleted node's DEL mark had not yet propagated to this cpu.
    11011101         */
     
    11071107
    11081108/** Returns an item that is equal to \a item starting in a chain at \a start. */
    1109 static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash, 
     1109static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash,
    11101110        cht_link_t *start)
    11111111{
     
    11141114        cht_link_t *cur = start;
    11151115       
    1116 try_again:     
     1116try_again:
    11171117        assert(cur);
    11181118
     
    11281128                cur = get_next(cur->link);
    11291129                assert(cur);
    1130         } 
     1130        }
    11311131
    11321132        /* Skip logically deleted nodes with rcu_call() in progress. */
     
    11471147        size_t removed = 0;
    11481148       
    1149         while (remove_pred(h, hash, h->op->key_equal, key)) 
     1149        while (remove_pred(h, hash, h->op->key_equal, key))
    11501150                ++removed;
    11511151       
     
    11531153}
    11541154
    1155 /** Removes a specific item from the table. 
    1156  * 
    1157  * The called must hold rcu read lock. 
    1158  * 
     1155/** Removes a specific item from the table.
     1156 *
     1157 * The called must hold rcu read lock.
     1158 *
    11591159 * @param item Item presumably present in the table and to be removed.
    11601160 * @return True if the item was removed successfully; or false if it had
    1161  *     already been deleted. 
     1161 *     already been deleted.
    11621162 */
    11631163bool cht_remove_item(cht_t *h, cht_link_t *item)
     
    11681168        assert(rcu_read_locked());
    11691169
    1170         /* 
     1170        /*
    11711171         * Even though we know the node we want to delete we must unlink it
    1172          * from the correct bucket and from a clean/normal predecessor. Therefore, 
     1172         * from the correct bucket and from a clean/normal predecessor. Therefore,
    11731173         * we search for it again from the beginning of the correct bucket.
    11741174         */
     
    12131213                }
    12141214               
    1215                 /* 
     1215                /*
    12161216                 * The item lookup is affected by a bucket join but effects of
    12171217                 * the bucket join have not been seen while searching for the item.
    12181218                 */
    12191219                if (join_finishing && !join_completed(h, &wnd)) {
    1220                         /* 
    1221                          * Bucket was appended at the end of another but the next 
    1222                          * ptr linking them together was not visible on this cpu. 
     1220                        /*
     1221                         * Bucket was appended at the end of another but the next
     1222                         * ptr linking them together was not visible on this cpu.
    12231223                         * join_completed() makes this appended bucket visible.
    12241224                         */
     
    12371237                }
    12381238               
    1239                 deleted = delete_at(h, &wnd, walk_mode, &deleted_but_gc, &resizing);           
     1239                deleted = delete_at(h, &wnd, walk_mode, &deleted_but_gc, &resizing);
    12401240        } while (!deleted || deleted_but_gc);
    12411241       
     
    12451245
    12461246/** Unlinks wnd.cur from wnd.ppred and schedules a deferred free for the item.
    1247  * 
     1247 *
    12481248 * Ignores nodes marked N_JOIN if walk mode is WM_LEAVE_JOIN.
    1249  * 
     1249 *
    12501250 * @param h   CHT to operate on.
    12511251 * @param wnd Points to the item to delete and its N_NORMAL predecessor.
    12521252 * @param walk_mode Bucket chaing walk mode.
    1253  * @param deleted_but_gc Set to true if the item had been logically deleted, 
     1253 * @param deleted_but_gc Set to true if the item had been logically deleted,
    12541254 *         but a garbage collecting walk of the bucket is in order for
    1255  *         it to be fully unlinked.         
     1255 *         it to be fully unlinked.
    12561256 * @param resizing Set to true if the table is undergoing an unexpected
    12571257 *         resize (ie walk_mode == WM_NORMAL).
     
    12591259 *         delete operation must be retried.
    12601260 */
    1261 static inline bool delete_at(cht_t *h, wnd_t *wnd, walk_mode_t walk_mode, 
     1261static inline bool delete_at(cht_t *h, wnd_t *wnd, walk_mode_t walk_mode,
    12621262        bool *deleted_but_gc, bool *resizing)
    12631263{
     
    12891289
    12901290/** Marks cur logically deleted. Returns false to request a retry. */
    1291 static inline bool mark_deleted(cht_link_t *cur, walk_mode_t walk_mode, 
     1291static inline bool mark_deleted(cht_link_t *cur, walk_mode_t walk_mode,
    12921292        bool *resizing)
    12931293{
    12941294        assert(cur && cur != &sentinel);
    12951295       
    1296         /* 
     1296        /*
    12971297         * Btw, we could loop here if the cas fails but let's not complicate
    1298          * things and let's retry from the head of the bucket. 
     1298         * things and let's retry from the head of the bucket.
    12991299         */
    13001300       
     
    13151315                mark_t cur_mark = get_mark(cur->link) & N_JOIN_FOLLOWS;
    13161316               
    1317                 marked_ptr_t ret = 
     1317                marked_ptr_t ret =
    13181318                        cas_link(&cur->link, next, cur_mark, next, cur_mark | N_DELETED);
    13191319               
    13201320                if (ret != make_link(next, cur_mark))
    13211321                        return false;
    1322         } 
     1322        }
    13231323       
    13241324        return true;
     
    13261326
    13271327/** Unlinks wnd.cur from wnd.ppred. Returns false if it should be retried. */
    1328 static inline bool unlink_from_pred(wnd_t *wnd, walk_mode_t walk_mode, 
     1328static inline bool unlink_from_pred(wnd_t *wnd, walk_mode_t walk_mode,
    13291329        bool *resizing)
    13301330{
     
    13551355                marked_ptr_t pred_link = make_link(wnd->cur, N_NORMAL);
    13561356                /* Link to cur's successor keeping/copying cur's JF mark. */
    1357                 marked_ptr_t next_link = make_link(next, cur_mark);             
     1357                marked_ptr_t next_link = make_link(next, cur_mark);
    13581358               
    13591359                marked_ptr_t ret = _cas_link(wnd->ppred, pred_link, next_link);
     
    13611361                if (pred_link != ret) {
    13621362                        /* If we're not resizing the table there are no JF/JN nodes. */
    1363                         *resizing = (walk_mode == WM_NORMAL) 
     1363                        *resizing = (walk_mode == WM_NORMAL)
    13641364                                && (N_JOIN_FOLLOWS & get_mark(ret));
    13651365                        return false;
     
    13711371
    13721372/** Finds the first non-deleted item equal to \a pred_arg according to \a pred.
    1373  * 
     1373 *
    13741374 * The function returns the candidate item in \a wnd. Logically deleted
    13751375 * nodes are garbage collected so the predecessor will most likely not
    1376  * be marked as deleted. 
    1377  * 
     1376 * be marked as deleted.
     1377 *
    13781378 * Unlike find_wnd_and_gc(), this function never returns a node that
    13791379 * is known to have already been marked N_DELETED.
     
    13821382 * collected, ie free in the background via rcu_call (except for join-nodes
    13831383 * if walk_mode == WM_LEAVE_JOIN).
    1384  * 
     1384 *
    13851385 * @param h         CHT to operate on.
    13861386 * @param hash      Hash the search for.
     
    13921392 * @param resizing  Set to true if the table is resizing but it was not
    13931393 *                  expected (ie walk_mode == WM_NORMAL).
    1394  * @return False if the operation has to be retried. True otherwise 
     1394 * @return False if the operation has to be retried. True otherwise
    13951395 *        (even if an equal item had not been found).
    13961396 */
    1397 static bool find_wnd_and_gc_pred(cht_t *h, size_t hash, walk_mode_t walk_mode, 
     1397static bool find_wnd_and_gc_pred(cht_t *h, size_t hash, walk_mode_t walk_mode,
    13981398        equal_pred_t pred, void *pred_arg, wnd_t *wnd, bool *resizing)
    13991399{
     
    14031403                return true;
    14041404       
    1405         /* 
    1406          * A read barrier is not needed here to bring up the most recent 
     1405        /*
     1406         * A read barrier is not needed here to bring up the most recent
    14071407         * node marks (esp the N_DELETED). At worst we'll try to delete
    14081408         * an already deleted node; fail in delete_at(); and retry.
     
    14111411        size_t cur_hash;
    14121412
    1413 try_again:     
     1413try_again:
    14141414        cur_hash = node_hash(h, wnd->cur);
    14151415               
     
    14451445
    14461446/** Find the first item (deleted or not) with a hash greater or equal to \a hash.
    1447  * 
    1448  * The function returns the first item with a hash that is greater or 
     1447 *
     1448 * The function returns the first item with a hash that is greater or
    14491449 * equal to \a hash in \a wnd. Moreover it garbage collects logically
    14501450 * deleted node that have not yet been unlinked and freed. Therefore,
    14511451 * the returned node's predecessor will most likely be N_NORMAL.
    1452  * 
     1452 *
    14531453 * Unlike find_wnd_and_gc_pred(), this function may return a node
    14541454 * that is known to had been marked N_DELETED.
    1455  * 
     1455 *
    14561456 * @param h         CHT to operate on.
    14571457 * @param hash      Hash of the item to find.
    14581458 * @param walk_mode Bucket chain walk mode.
    1459  * @param[in,out] wnd wnd.cur denotes the first node of the chain. If the 
    1460  *                  the operation is successful, \a wnd points to the desired 
     1459 * @param[in,out] wnd wnd.cur denotes the first node of the chain. If the
     1460 *                  the operation is successful, \a wnd points to the desired
    14611461 *                  item.
    14621462 * @param resizing  Set to true if a table resize was detected but walk_mode
    14631463 *                  suggested the table was not undergoing a resize.
    1464  * @return False indicates the operation must be retried. True otherwise 
     1464 * @return False indicates the operation must be retried. True otherwise
    14651465 *       (even if an item with exactly the same has was not found).
    14661466 */
    1467 static bool find_wnd_and_gc(cht_t *h, size_t hash, walk_mode_t walk_mode, 
     1467static bool find_wnd_and_gc(cht_t *h, size_t hash, walk_mode_t walk_mode,
    14681468        wnd_t *wnd, bool *resizing)
    14691469{
     
    15051505        } else {
    15061506                /* Ordinary deleted node or a deleted JOIN_FOLLOWS. */
    1507                 assert(walk_mode != WM_LEAVE_JOIN 
     1507                assert(walk_mode != WM_LEAVE_JOIN
    15081508                        || !((N_JOIN | N_JOIN_FOLLOWS) & get_mark(wnd->cur->link)));
    15091509
     
    15251525
    15261526/** Returns true if a bucket join had already completed.
    1527  * 
    1528  * May only be called if upd_resizing_head() indicates a bucket join 
     1527 *
     1528 * May only be called if upd_resizing_head() indicates a bucket join
    15291529 * may be in progress.
    1530  * 
     1530 *
    15311531 * If it returns false, the search must be retried in order to guarantee
    15321532 * all item that should have been encountered have been seen.
     
    15341534static bool join_completed(cht_t *h, const wnd_t *wnd)
    15351535{
    1536         /* 
    1537          * The table is shrinking and the searched for item is in a bucket 
    1538          * appended to another. Check that the link joining these two buckets 
     1536        /*
     1537         * The table is shrinking and the searched for item is in a bucket
     1538         * appended to another. Check that the link joining these two buckets
    15391539         * is visible and if not, make it visible to this cpu.
    15401540         */
    15411541       
    1542         /* 
    1543          * Resizer ensures h->b->order stays the same for the duration of this 
     1542        /*
     1543         * Resizer ensures h->b->order stays the same for the duration of this
    15441544         * func. We got here because there was an alternative head to search.
    15451545         * The resizer waits for all preexisting readers to finish after
    1546          * it 
     1546         * it
    15471547         */
    15481548        assert(h->b->order > h->new_b->order);
     
    15651565                size_t move_src_idx = grow_idx(shrink_idx(last_old_idx));
    15661566               
    1567                 /* 
    1568                  * Last node seen was in the joining bucket - if the searched 
    1569                  * for node is there we will find it. 
     1567                /*
     1568                 * Last node seen was in the joining bucket - if the searched
     1569                 * for node is there we will find it.
    15701570                 */
    1571                 if (move_src_idx != last_old_idx) 
     1571                if (move_src_idx != last_old_idx)
    15721572                        return true;
    15731573        }
    15741574       
    1575         /* 
     1575        /*
    15761576         * Reached the end of the bucket but no nodes from the joining bucket
    15771577         * were seen. There should have at least been a JOIN node so we have
     
    15841584
    15851585/** When resizing returns the bucket head to start the search with in \a phead.
    1586  * 
     1586 *
    15871587 * If a resize had been detected (eg cht_t.b.head[idx] is marked immutable).
    15881588 * upd_resizing_head() moves the bucket for \a hash from the old head
    15891589 * to the new head. Moreover, it splits or joins buckets as necessary.
    1590  * 
     1590 *
    15911591 * @param h     CHT to operate on.
    15921592 * @param hash  Hash of an item whose chain we would like to traverse.
     
    15951595 *              in progress and the bucket may have to traversed again
    15961596 *              as indicated by join_completed().
    1597  * @param[out] walk_mode Specifies how to interpret node marks. 
    1598  */
    1599 static void upd_resizing_head(cht_t *h, size_t hash, marked_ptr_t **phead, 
     1597 * @param[out] walk_mode Specifies how to interpret node marks.
     1598 */
     1599static void upd_resizing_head(cht_t *h, size_t hash, marked_ptr_t **phead,
    16001600        bool *join_finishing,  walk_mode_t *walk_mode)
    16011601{
     
    16211621                if (move_dest_idx == new_idx) {
    16221622                        assert(pmoved_head == pnew_head);
    1623                         /* 
    1624                          * move_head() makes the new head of the moved bucket visible. 
     1623                        /*
     1624                         * move_head() makes the new head of the moved bucket visible.
    16251625                         * The new head may be marked with a JOIN_FOLLOWS
    16261626                         */
     
    16291629                } else {
    16301630                        assert(pmoved_head != pnew_head);
    1631                         /* 
    1632                          * The hash belongs to the bucket that is the result of splitting 
     1631                        /*
     1632                         * The hash belongs to the bucket that is the result of splitting
    16331633                         * the old/moved bucket, ie the bucket that contains the second
    16341634                         * half of the split/old/moved bucket.
     
    16391639                                size_t split_hash = calc_split_hash(new_idx, h->new_b->order);
    16401640                                split_bucket(h, pmoved_head, pnew_head, split_hash);
    1641                                 /* 
    1642                                  * split_bucket() makes the new head visible. No 
     1641                                /*
     1642                                 * split_bucket() makes the new head visible. No
    16431643                                 * JOIN_FOLLOWS in this part of split bucket.
    16441644                                 */
     
    16531653                size_t move_src_idx = grow_idx(new_idx);
    16541654               
    1655                 /* 
    1656                  * Complete moving the bucket from the old to the new table. 
     1655                /*
     1656                 * Complete moving the bucket from the old to the new table.
    16571657                 * Makes a valid pnew_head visible if already moved.
    16581658                 */
     
    16671667                        }
    16681668                       
    1669                         /* 
     1669                        /*
    16701670                         * The resizer sets pold_head to &sentinel when all cpus are
    16711671                         * guaranteed to see the bucket join.
     
    16811681                *walk_mode = WM_LEAVE_JOIN;
    16821682        } else {
    1683                 /* 
    1684                  * Final stage of resize. The resizer is waiting for all 
     1683                /*
     1684                 * Final stage of resize. The resizer is waiting for all
    16851685                 * readers to notice that the old table had been replaced.
    16861686                 */
     
    17001700#endif
    17011701
    1702 /** Moves an immutable head \a psrc_head of cht_t.b to \a pdest_head of cht_t.new_b. 
    1703  * 
     1702/** Moves an immutable head \a psrc_head of cht_t.b to \a pdest_head of cht_t.new_b.
     1703 *
    17041704 * The function guarantees the move will be visible on this cpu once
    17051705 * it completes. In particular, *pdest_head will not be N_INVALID.
    1706  * 
     1706 *
    17071707 * Unlike complete_head_move(), help_head_move() checks if the head had already
    17081708 * been moved and tries to avoid moving the bucket heads if possible.
    17091709 */
    1710 static inline void help_head_move(marked_ptr_t *psrc_head, 
     1710static inline void help_head_move(marked_ptr_t *psrc_head,
    17111711        marked_ptr_t *pdest_head)
    17121712{
     
    17291729
    17301730/** Initiates the move of the old head \a psrc_head.
    1731  * 
    1732  * The move may be completed with help_head_move(). 
     1731 *
     1732 * The move may be completed with help_head_move().
    17331733 */
    17341734static void start_head_move(marked_ptr_t *psrc_head)
     
    17661766        cas_order_barrier();
    17671767       
    1768         DBG(ret = ) 
    1769                 cas_link(psrc_head, next, N_CONST, next, N_INVALID);   
     1768        DBG(ret = )
     1769                cas_link(psrc_head, next, N_CONST, next, N_INVALID);
    17701770        assert(ret == make_link(next, N_CONST) || (N_INVALID == get_mark(ret)));
    17711771        cas_order_barrier();
     
    17731773
    17741774/** Splits the bucket at psrc_head and links to the remainder from pdest_head.
    1775  * 
     1775 *
    17761776 * Items with hashes greater or equal to \a split_hash are moved to bucket
    1777  * with head at \a pdest_head. 
    1778  * 
     1777 * with head at \a pdest_head.
     1778 *
    17791779 * @param h           CHT to operate on.
    17801780 * @param psrc_head   Head of the bucket to split (in cht_t.new_b).
    17811781 * @param pdest_head  Head of the bucket that points to the second part
    17821782 *                    of the split bucket in psrc_head. (in cht_t.new_b)
    1783  * @param split_hash  Hash of the first possible item in the remainder of 
     1783 * @param split_hash  Hash of the first possible item in the remainder of
    17841784 *                    psrc_head, ie the smallest hash pdest_head is allowed
    17851785 *                    to point to..
    17861786 */
    1787 static void split_bucket(cht_t *h, marked_ptr_t *psrc_head, 
     1787static void split_bucket(cht_t *h, marked_ptr_t *psrc_head,
    17881788        marked_ptr_t *pdest_head, size_t split_hash)
    17891789{
     
    17941794        /*
    17951795         * L == Last node of the first part of the split bucket. That part
    1796          *      remains in the original/src bucket. 
     1796         *      remains in the original/src bucket.
    17971797         * F == First node of the second part of the split bucket. That part
    17981798         *      will be referenced from the dest bucket head.
    17991799         *
    1800          * We want to first mark a clean L as JF so that updaters unaware of 
     1800         * We want to first mark a clean L as JF so that updaters unaware of
    18011801         * the split (or table resize):
    18021802         * - do not insert a new node between L and F
     
    18041804         * - do not unlink F
    18051805         *
    1806          * Then we can safely mark F as JN even if it has been marked deleted. 
    1807          * Once F is marked as JN updaters aware of table resize will not 
     1806         * Then we can safely mark F as JN even if it has been marked deleted.
     1807         * Once F is marked as JN updaters aware of table resize will not
    18081808         * attempt to unlink it (JN will have two predecessors - we cannot
    1809          * safely unlink from both at the same time). Updaters unaware of 
    1810          * ongoing resize can reach F only via L and that node is already 
     1809         * safely unlink from both at the same time). Updaters unaware of
     1810         * ongoing resize can reach F only via L and that node is already
    18111811         * marked JF, so they won't unlink F.
    1812          * 
     1812         *
    18131813         * Last, link the new/dest head to F.
    1814          * 
    1815          * 
    1816          * 0)                           ,-- split_hash, first hash of the dest bucket 
    1817          *                              v 
     1814         *
     1815         *
     1816         * 0)                           ,-- split_hash, first hash of the dest bucket
     1817         *                              v
    18181818         *  [src_head | N] -> .. -> [L] -> [F]
    18191819         *  [dest_head | Inv]
    1820          * 
     1820         *
    18211821         * 1)                             ,-- split_hash
    1822          *                                v 
     1822         *                                v
    18231823         *  [src_head | N] -> .. -> [JF] -> [F]
    18241824         *  [dest_head | Inv]
    1825          * 
     1825         *
    18261826         * 2)                             ,-- split_hash
    1827          *                                v 
     1827         *                                v
    18281828         *  [src_head | N] -> .. -> [JF] -> [JN]
    18291829         *  [dest_head | Inv]
    1830          * 
     1830         *
    18311831         * 3)                             ,-- split_hash
    1832          *                                v 
     1832         *                                v
    18331833         *  [src_head | N] -> .. -> [JF] -> [JN]
    18341834         *                                   ^
     
    18451845        /* There are nodes in the dest bucket, ie the second part of the split. */
    18461846        if (wnd.cur != &sentinel) {
    1847                 /* 
    1848                  * Mark the first node of the dest bucket as a join node so 
    1849                  * updaters do not attempt to unlink it if it is deleted. 
     1847                /*
     1848                 * Mark the first node of the dest bucket as a join node so
     1849                 * updaters do not attempt to unlink it if it is deleted.
    18501850                 */
    18511851                mark_join_node(wnd.cur);
    18521852                cas_order_barrier();
    18531853        } else {
    1854                 /* 
     1854                /*
    18551855                 * Second part of the split bucket is empty. There are no nodes
    18561856                 * to mark as JOIN nodes and there never will be.
     
    18681868
    18691869/** Finds and marks the last node of psrc_head w/ hash less than split_hash.
    1870  * 
    1871  * Finds a node in psrc_head with the greatest hash that is strictly less 
    1872  * than split_hash and marks it with N_JOIN_FOLLOWS. 
    1873  * 
    1874  * Returns a window pointing to that node. 
    1875  * 
    1876  * Any logically deleted nodes along the way are 
    1877  * garbage collected; therefore, the predecessor node (if any) will most 
     1870 *
     1871 * Finds a node in psrc_head with the greatest hash that is strictly less
     1872 * than split_hash and marks it with N_JOIN_FOLLOWS.
     1873 *
     1874 * Returns a window pointing to that node.
     1875 *
     1876 * Any logically deleted nodes along the way are
     1877 * garbage collected; therefore, the predecessor node (if any) will most
    18781878 * likely not be marked N_DELETED.
    1879  * 
     1879 *
    18801880 * @param h          CHT to operate on.
    18811881 * @param psrc_head  Bucket head.
     
    18841884 * @param[out] wnd   Points to the node marked with N_JOIN_FOLLOWS.
    18851885 */
    1886 static void mark_join_follows(cht_t *h, marked_ptr_t *psrc_head, 
     1886static void mark_join_follows(cht_t *h, marked_ptr_t *psrc_head,
    18871887        size_t split_hash, wnd_t *wnd)
    18881888{
     
    18961896                wnd->cur = get_next(*psrc_head);
    18971897               
    1898                 /* 
     1898                /*
    18991899                 * Find the split window, ie the last node of the first part of
    19001900                 * the split bucket and the its successor - the first node of
    1901                  * the second part of the split bucket. Retry if GC failed. 
     1901                 * the second part of the split bucket. Retry if GC failed.
    19021902                 */
    19031903                if (!find_wnd_and_gc(h, split_hash, WM_MOVE_JOIN_FOLLOWS, wnd, &resizing))
     
    19061906                /* Must not report that the table is resizing if WM_MOVE_JOIN_FOLLOWS.*/
    19071907                assert(!resizing);
    1908                 /* 
    1909                  * Mark the last node of the first half of the split bucket 
     1908                /*
     1909                 * Mark the last node of the first half of the split bucket
    19101910                 * that a join node follows. It must be clean/normal.
    19111911                 */
     
    19131913                        = cas_link(wnd->ppred, wnd->cur, N_NORMAL, wnd->cur, N_JOIN_FOLLOWS);
    19141914
    1915                 /* 
    1916                  * Successfully marked as a JF node or already marked that way (even 
    1917                  * if also marked deleted - unlinking the node will move the JF mark). 
     1915                /*
     1916                 * Successfully marked as a JF node or already marked that way (even
     1917                 * if also marked deleted - unlinking the node will move the JF mark).
    19181918                 */
    19191919                done = (ret == make_link(wnd->cur, N_NORMAL))
     
    19321932                mark_t mark = get_mark(join_node->link);
    19331933               
    1934                 /* 
    1935                  * May already be marked as deleted, but it won't be unlinked 
     1934                /*
     1935                 * May already be marked as deleted, but it won't be unlinked
    19361936                 * because its predecessor is marked with JOIN_FOLLOWS or CONST.
    19371937                 */
    1938                 marked_ptr_t ret 
     1938                marked_ptr_t ret
    19391939                        = cas_link(&join_node->link, next, mark, next, mark | N_JOIN);
    19401940               
     
    19461946
    19471947/** Appends the bucket at psrc_head to the bucket at pdest_head.
    1948  * 
     1948 *
    19491949 * @param h          CHT to operate on.
    19501950 * @param psrc_head  Bucket to merge with pdest_head.
     
    19521952 * @param split_hash The smallest hash psrc_head may contain.
    19531953 */
    1954 static void join_buckets(cht_t *h, marked_ptr_t *psrc_head, 
     1954static void join_buckets(cht_t *h, marked_ptr_t *psrc_head,
    19551955        marked_ptr_t *pdest_head, size_t split_hash)
    19561956{
     
    19591959                return;
    19601960        /*
    1961          * F == First node of psrc_head, ie the bucket we want to append 
     1961         * F == First node of psrc_head, ie the bucket we want to append
    19621962         *      to (ie join with) the bucket starting at pdest_head.
    19631963         * L == Last node of pdest_head, ie the bucket that psrc_head will
    1964          *      be appended to. 
    1965          *
    1966          * (1) We first mark psrc_head immutable to signal that a join is 
    1967          * in progress and so that updaters unaware of the join (or table 
     1964         *      be appended to.
     1965         *
     1966         * (1) We first mark psrc_head immutable to signal that a join is
     1967         * in progress and so that updaters unaware of the join (or table
    19681968         * resize):
    19691969         * - do not insert new nodes between the head psrc_head and F
    19701970         * - do not unlink F (it may already be marked deleted)
    1971          * 
     1971         *
    19721972         * (2) Next, F is marked as a join node. Updaters aware of table resize
    1973          * will not attempt to unlink it. We cannot safely/atomically unlink 
    1974          * the join node because it will be pointed to from two different 
     1973         * will not attempt to unlink it. We cannot safely/atomically unlink
     1974         * the join node because it will be pointed to from two different
    19751975         * buckets. Updaters unaware of resize will fail to unlink the join
    19761976         * node due to the head being marked immutable.
     
    19781978         * (3) Then the tail of the bucket at pdest_head is linked to the join
    19791979         * node. From now on, nodes in both buckets can be found via pdest_head.
    1980          * 
     1980         *
    19811981         * (4) Last, mark immutable psrc_head as invalid. It signals updaters
    19821982         * that the join is complete and they can insert new nodes (originally
    1983          * destined for psrc_head) into pdest_head. 
    1984          * 
     1983         * destined for psrc_head) into pdest_head.
     1984         *
    19851985         * Note that pdest_head keeps pointing at the join node. This allows
    19861986         * lookups and updaters to determine if they should see a link between
    19871987         * the tail L and F when searching for nodes originally in psrc_head
    1988          * via pdest_head. If they reach the tail of pdest_head without 
     1988         * via pdest_head. If they reach the tail of pdest_head without
    19891989         * encountering any nodes of psrc_head, either there were no nodes
    19901990         * in psrc_head to begin with or the link between L and F did not
    19911991         * yet propagate to their cpus. If psrc_head was empty, it remains
    1992          * NULL. Otherwise psrc_head points to a join node (it will not be 
     1992         * NULL. Otherwise psrc_head points to a join node (it will not be
    19931993         * unlinked until table resize completes) and updaters/lookups
    19941994         * should issue a read_barrier() to make the link [L]->[JN] visible.
    1995          * 
    1996          * 0)                           ,-- split_hash, first hash of the src bucket 
    1997          *                              v 
     1995         *
     1996         * 0)                           ,-- split_hash, first hash of the src bucket
     1997         *                              v
    19981998         *  [dest_head | N]-> .. -> [L]
    1999          *  [src_head | N]--> [F] -> .. 
     1999         *  [src_head | N]--> [F] -> ..
    20002000         *  ^
    20012001         *  ` split_hash, first hash of the src bucket
    2002          * 
     2002         *
    20032003         * 1)                            ,-- split_hash
    2004          *                               v 
     2004         *                               v
    20052005         *  [dest_head | N]-> .. -> [L]
    2006          *  [src_head | C]--> [F] -> .. 
    2007          * 
     2006         *  [src_head | C]--> [F] -> ..
     2007         *
    20082008         * 2)                            ,-- split_hash
    2009          *                               v 
     2009         *                               v
    20102010         *  [dest_head | N]-> .. -> [L]
    2011          *  [src_head | C]--> [JN] -> .. 
    2012          * 
     2011         *  [src_head | C]--> [JN] -> ..
     2012         *
    20132013         * 3)                            ,-- split_hash
    2014          *                               v 
     2014         *                               v
    20152015         *  [dest_head | N]-> .. -> [L] --+
    20162016         *                                v
    2017          *  [src_head | C]-------------> [JN] -> .. 
    2018          * 
     2017         *  [src_head | C]-------------> [JN] -> ..
     2018         *
    20192019         * 4)                            ,-- split_hash
    2020          *                               v 
     2020         *                               v
    20212021         *  [dest_head | N]-> .. -> [L] --+
    20222022         *                                v
    2023          *  [src_head | Inv]-----------> [JN] -> .. 
     2023         *  [src_head | Inv]-----------> [JN] -> ..
    20242024         */
    20252025       
     
    20382038                link_to_join_node(h, pdest_head, join_node, split_hash);
    20392039                cas_order_barrier();
    2040         } 
     2040        }
    20412041       
    20422042        DBG(marked_ptr_t ret = )
     
    20492049
    20502050/** Links the tail of pdest_head to join_node.
    2051  * 
     2051 *
    20522052 * @param h          CHT to operate on.
    20532053 * @param pdest_head Head of the bucket whose tail is to be linked to join_node.
     
    20572057 *                   (originally) in pdest_head.
    20582058 */
    2059 static void link_to_join_node(cht_t *h, marked_ptr_t *pdest_head, 
     2059static void link_to_join_node(cht_t *h, marked_ptr_t *pdest_head,
    20602060        cht_link_t *join_node, size_t split_hash)
    20612061{
     
    20772077                if (wnd.cur != &sentinel) {
    20782078                        /* Must be from the new appended bucket. */
    2079                         assert(split_hash <= node_hash(h, wnd.cur) 
     2079                        assert(split_hash <= node_hash(h, wnd.cur)
    20802080                                || h->invalid_hash == node_hash(h, wnd.cur));
    20812081                        return;
     
    20832083               
    20842084                /* Reached the tail of pdest_head - link it to the join node. */
    2085                 marked_ptr_t ret = 
     2085                marked_ptr_t ret =
    20862086                        cas_link(wnd.ppred, &sentinel, N_NORMAL, join_node, N_NORMAL);
    20872087               
     
    20902090}
    20912091
    2092 /** Instructs RCU to free the item once all preexisting references are dropped. 
    2093  * 
     2092/** Instructs RCU to free the item once all preexisting references are dropped.
     2093 *
    20942094 * The item is freed via op->remove_callback().
    20952095 */
     
    20982098        assert(item != &sentinel);
    20992099       
    2100         /* 
     2100        /*
    21012101         * remove_callback only works as rcu_func_t because rcu_link is the first
    21022102         * field in cht_link_t.
     
    21082108
    21092109/** Notes that an item had been unlinked from the table and shrinks it if needed.
    2110  * 
    2111  * If the number of items in the table drops below 1/4 of the maximum 
     2110 *
     2111 * If the number of items in the table drops below 1/4 of the maximum
    21122112 * allowed load the table is shrunk in the background.
    21132113 */
     
    21292129}
    21302130
    2131 /** Notes an item had been inserted and grows the table if needed. 
    2132  * 
     2131/** Notes an item had been inserted and grows the table if needed.
     2132 *
    21332133 * The table is resized in the background.
    21342134 */
     
    21922192
    21932193        /* Failed to alloc a new table - try next time the resizer is run. */
    2194         if (!h->new_b) 
     2194        if (!h->new_b)
    21952195                return;
    21962196
     
    21992199        size_t old_bucket_cnt = (1 << h->b->order);
    22002200       
    2201         /* 
    2202          * Give updaters a chance to help out with the resize. Do the minimum 
     2201        /*
     2202         * Give updaters a chance to help out with the resize. Do the minimum
    22032203         * work needed to announce a resize is in progress, ie start moving heads.
    22042204         */
     
    22272227        }
    22282228       
    2229         /* 
     2229        /*
    22302230         * Wait for all updaters to notice the new heads. Once everyone sees
    22312231         * the invalid old bucket heads they will know a resize is in progress
    2232          * and updaters will modify the correct new buckets. 
     2232         * and updaters will modify the correct new buckets.
    22332233         */
    22342234        rcu_synchronize();
     
    22412241        }
    22422242
    2243         /* 
     2243        /*
    22442244         * Wait for everyone to notice that buckets were split, ie link connecting
    2245          * the join follows and join node has been cut. 
     2245         * the join follows and join node has been cut.
    22462246         */
    22472247        rcu_synchronize();
     
    22782278
    22792279        /* Failed to alloc a new table - try next time the resizer is run. */
    2280         if (!h->new_b) 
     2280        if (!h->new_b)
    22812281                return;
    22822282
     
    22862286        size_t old_bucket_cnt = (1 << h->b->order);
    22872287       
    2288         /* 
    2289          * Give updaters a chance to help out with the resize. Do the minimum 
     2288        /*
     2289         * Give updaters a chance to help out with the resize. Do the minimum
    22902290         * work needed to announce a resize is in progress, ie start moving heads.
    22912291         */
     
    23182318                        /* This bucket should join the moved bucket. */
    23192319                        size_t split_hash = calc_split_hash(old_idx, h->b->order);
    2320                         join_buckets(h, &h->b->head[old_idx], &h->new_b->head[new_idx], 
     2320                        join_buckets(h, &h->b->head[old_idx], &h->new_b->head[new_idx],
    23212321                                split_hash);
    23222322                }
    23232323        }
    23242324       
    2325         /* 
     2325        /*
    23262326         * Wait for all updaters to notice the new heads. Once everyone sees
    23272327         * the invalid old bucket heads they will know a resize is in progress
    2328          * and updaters will modify the correct new buckets. 
     2328         * and updaters will modify the correct new buckets.
    23292329         */
    23302330        rcu_synchronize();
     
    23892389
    23902390/** Clears the join_node's N_JOIN mark frees it if marked N_DELETED as well. */
    2391 static void clear_join_and_gc(cht_t *h, cht_link_t *join_node, 
     2391static void clear_join_and_gc(cht_t *h, cht_link_t *join_node,
    23922392        marked_ptr_t *new_head)
    23932393{
     
    24042404                mark_t cleared_mark = get_mark(jn_link) & N_DELETED;
    24052405
    2406                 marked_ptr_t ret = 
     2406                marked_ptr_t ret =
    24072407                        _cas_link(&join_node->link, jn_link, make_link(next, cleared_mark));
    24082408
     
    24292429                };
    24302430               
    2431                 done = find_wnd_and_gc_pred(h, jn_hash, WM_NORMAL, same_node_pred, 
     2431                done = find_wnd_and_gc_pred(h, jn_hash, WM_NORMAL, same_node_pred,
    24322432                        join_node, &wnd, &resizing);
    24332433               
     
    24522452         * Find the non-deleted node with a JF mark and clear the JF mark.
    24532453         * The JF node may be deleted and/or the mark moved to its neighbors
    2454          * at any time. Therefore, we GC deleted nodes until we find the JF 
    2455          * node in order to remove stale/deleted JF nodes left behind eg by 
    2456          * delayed threads that did not yet get a chance to unlink the deleted 
    2457          * JF node and move its mark. 
    2458          * 
     2454         * at any time. Therefore, we GC deleted nodes until we find the JF
     2455         * node in order to remove stale/deleted JF nodes left behind eg by
     2456         * delayed threads that did not yet get a chance to unlink the deleted
     2457         * JF node and move its mark.
     2458         *
    24592459         * Note that the head may be marked JF (but never DELETED).
    24602460         */
     
    24812481                        if (is_jf_node) {
    24822482                                cht_link_t *next = get_next(*cur_link);
    2483                                 marked_ptr_t ret = 
     2483                                marked_ptr_t ret =
    24842484                                        cas_link(cur_link, next, N_JOIN_FOLLOWS, &sentinel, N_NORMAL);
    24852485                               
    2486                                 assert(next == &sentinel 
     2486                                assert(next == &sentinel
    24872487                                        || ((N_JOIN | N_JOIN_FOLLOWS) & get_mark(ret)));
    24882488
     
    24912491                                        break;
    24922492                                } else {
    2493                                         /* 
    2494                                          * The JF node had been deleted or a new node inserted 
     2493                                        /*
     2494                                         * The JF node had been deleted or a new node inserted
    24952495                                         * right after it. Retry from the head.
    24962496                                         */
     
    25002500                        } else {
    25012501                                wnd.ppred = cur_link;
    2502                                 wnd.cur = get_next(*cur_link);                         
     2502                                wnd.cur = get_next(*cur_link);
    25032503                        }
    25042504                }
     
    25122512}
    25132513
    2514 /** Returns the first possible hash following a bucket split point. 
    2515  * 
     2514/** Returns the first possible hash following a bucket split point.
     2515 *
    25162516 * In other words the returned hash is the smallest possible hash
    25172517 * the remainder of the split bucket may contain.
     
    25582558static inline size_t node_hash(cht_t *h, const cht_link_t *item)
    25592559{
    2560         assert(item->hash == h->invalid_hash 
     2560        assert(item->hash == h->invalid_hash
    25612561                || item->hash == sentinel.hash
    25622562                || item->hash == calc_node_hash(h, item));
     
    25692569{
    25702570        assert(item != &sentinel);
    2571         /* 
     2571        /*
    25722572         * Clear the lowest order bit in order for sentinel's node hash
    25732573         * to be the greatest possible.
     
    26242624
    26252625/** Compare-and-swaps a next item link. */
    2626 static inline marked_ptr_t cas_link(marked_ptr_t *link, const cht_link_t *cur_next, 
     2626static inline marked_ptr_t cas_link(marked_ptr_t *link, const cht_link_t *cur_next,
    26272627        mark_t cur_mark, const cht_link_t *new_next, mark_t new_mark)
    26282628{
    2629         return _cas_link(link, make_link(cur_next, cur_mark), 
     2629        return _cas_link(link, make_link(cur_next, cur_mark),
    26302630                make_link(new_next, new_mark));
    26312631}
    26322632
    26332633/** Compare-and-swaps a next item link. */
    2634 static inline marked_ptr_t _cas_link(marked_ptr_t *link, marked_ptr_t cur, 
     2634static inline marked_ptr_t _cas_link(marked_ptr_t *link, marked_ptr_t cur,
    26352635        marked_ptr_t new)
    26362636{
     
    26402640         * have to be ordered wrt to other cas(y) to a different location y
    26412641         * on the same cpu.
    2642          * 
    2643          * cas(x) must act as a write barrier on x, ie if cas(x) succeeds 
    2644          * and is observed by another cpu, then all cpus must be able to 
     2642         *
     2643         * cas(x) must act as a write barrier on x, ie if cas(x) succeeds
     2644         * and is observed by another cpu, then all cpus must be able to
    26452645         * make the effects of cas(x) visible just by issuing a load barrier.
    26462646         * For example:
    26472647         * cpu1         cpu2            cpu3
    2648          *                              cas(x, 0 -> 1), succeeds 
     2648         *                              cas(x, 0 -> 1), succeeds
    26492649         *              cas(x, 0 -> 1), fails
    26502650         *              MB, to order load of x in cas and store to y
     
    26522652         * sees y == 7
    26532653         * loadMB must be enough to make cas(x) on cpu3 visible to cpu1, ie x == 1.
    2654          * 
     2654         *
    26552655         * If cas() did not work this way:
    26562656         * a) our head move protocol would not be correct.
    26572657         * b) freeing an item linked to a moved head after another item was
    26582658         *   inserted in front of it, would require more than one grace period.
    2659          * 
     2659         *
    26602660         * Ad (a): In the following example, cpu1 starts moving old_head
    26612661         * to new_head, cpu2 completes the move and cpu3 notices cpu2
    26622662         * completed the move before cpu1 gets a chance to notice cpu2
    2663          * had already completed the move. Our requirements for cas() 
    2664          * assume cpu3 will see a valid and mutable value in new_head 
    2665          * after issuing a load memory barrier once it has determined 
    2666          * the old_head's value had been successfully moved to new_head 
     2663         * had already completed the move. Our requirements for cas()
     2664         * assume cpu3 will see a valid and mutable value in new_head
     2665         * after issuing a load memory barrier once it has determined
     2666         * the old_head's value had been successfully moved to new_head
    26672667         * (because it sees old_head marked invalid).
    2668          * 
     2668         *
    26692669         *  cpu1             cpu2             cpu3
    26702670         *   cas(old_head, <addr, N>, <addr, Const>), succeeds
     
    26722672         *   // Move from old_head to new_head started, now the interesting stuff:
    26732673         *   cas(new_head, <0, Inv>, <addr, N>), succeeds
    2674          * 
     2674         *
    26752675         *                    cas(new_head, <0, Inv>, <addr, N>), but fails
    26762676         *                    cas-order-barrier
    26772677         *                    cas(old_head, <addr, Const>, <addr, Inv>), succeeds
    2678          *                                     
     2678         *
    26792679         *                                     Sees old_head marked Inv (by cpu2)
    26802680         *                                     load-MB
    26812681         *                                     assert(new_head == <addr, N>)
    2682          *   
     2682         *
    26832683         *   cas-order-barrier
    2684          * 
     2684         *
    26852685         * Even though cpu1 did not yet issue a cas-order-barrier, cpu1's store
    26862686         * to new_head (successful cas()) must be made visible to cpu3 with
    26872687         * a load memory barrier if cpu1's store to new_head is visible
    26882688         * on another cpu (cpu2) and that cpu's (cpu2's) store to old_head
    2689          * is already visible to cpu3.   * 
     2689         * is already visible to cpu3.   *
    26902690         */
    26912691        void *expected = (void*)cur;
    26922692       
    2693         /* 
     2693        /*
    26942694         * Use the acquire-release model, although we could probably
    26952695         * get away even with the relaxed memory model due to our use
Note: See TracChangeset for help on using the changeset viewer.