Complete removal of urcu-signal flavor
[urcu.git] / src / rculfhash.c
CommitLineData
acdb82a2
MJ
1// SPDX-FileCopyrightText: 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2// SPDX-FileCopyrightText: 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
5e28c532 6/*
1475579c 7 * Userspace RCU library - Lock-Free Resizable RCU Hash Table
5e28c532
MD
8 */
9
e753ff5a
MD
10/*
11 * Based on the following articles:
12 * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
13 * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
14 * - Michael, M. M. High performance dynamic lock-free hash tables
15 * and list-based sets. In Proceedings of the fourteenth annual ACM
16 * symposium on Parallel algorithms and architectures, ACM Press,
17 * (2002), 73-82.
18 *
1475579c 19 * Some specificities of this Lock-Free Resizable RCU Hash Table
e753ff5a
MD
20 * implementation:
21 *
22 * - RCU read-side critical section allows readers to perform hash
1f67ba50
MD
23 * table lookups, as well as traversals, and use the returned objects
24 * safely by allowing memory reclaim to take place only after a grace
25 * period.
e753ff5a
MD
26 * - Add and remove operations are lock-free, and do not need to
27 * allocate memory. They need to be executed within RCU read-side
28 * critical section to ensure the objects they read are valid and to
29 * deal with the cmpxchg ABA problem.
30 * - add and add_unique operations are supported. add_unique checks if
1f67ba50
MD
31 * the node key already exists in the hash table. It ensures not to
32 * populate a duplicate key if the node key already exists in the hash
33 * table.
34 * - The resize operation executes concurrently with
35 * add/add_unique/add_replace/remove/lookup/traversal.
e753ff5a
MD
36 * - Hash table nodes are contained within a split-ordered list. This
37 * list is ordered by incrementing reversed-bits-hash value.
1ee8f000 38 * - An index of bucket nodes is kept. These bucket nodes are the hash
1f67ba50
MD
39 * table "buckets". These buckets are internal nodes that allow to
40 * perform a fast hash lookup, similarly to a skip list. These
41 * buckets are chained together in the split-ordered list, which
42 * allows recursive expansion by inserting new buckets between the
43 * existing buckets. The split-ordered list allows adding new buckets
44 * between existing buckets as the table needs to grow.
45 * - The resize operation for small tables only allows expanding the
46 * hash table. It is triggered automatically by detecting long chains
47 * in the add operation.
1475579c
MD
48 * - The resize operation for larger tables (and available through an
49 * API) allows both expanding and shrinking the hash table.
4c42f1b8 50 * - Split-counters are used to keep track of the number of
1475579c 51 * nodes within the hash table for automatic resize triggering.
e753ff5a 52 * - Resize operation initiated by long chain detection is executed by a
d0ec0ed2 53 * worker thread, which keeps lock-freedom of add and remove.
e753ff5a
MD
54 * - Resize operations are protected by a mutex.
55 * - The removal operation is split in two parts: first, a "removed"
56 * flag is set in the next pointer within the node to remove. Then,
57 * a "garbage collection" is performed in the bucket containing the
58 * removed node (from the start of the bucket up to the removed node).
59 * All encountered nodes with "removed" flag set in their next
60 * pointers are removed from the linked-list. If the cmpxchg used for
61 * removal fails (due to concurrent garbage-collection or concurrent
62 * add), we retry from the beginning of the bucket. This ensures that
63 * the node with "removed" flag set is removed from the hash table
64 * (not visible to lookups anymore) before the RCU read-side critical
65 * section held across removal ends. Furthermore, this ensures that
66 * the node with "removed" flag set is removed from the linked-list
5c4ca589
MD
67 * before its memory is reclaimed. After setting the "removal" flag,
68 * only the thread which removal is the first to set the "removal
69 * owner" flag (with an xchg) into a node's next pointer is considered
70 * to have succeeded its removal (and thus owns the node to reclaim).
71 * Because we garbage-collect starting from an invariant node (the
72 * start-of-bucket bucket node) up to the "removed" node (or find a
73 * reverse-hash that is higher), we are sure that a successful
74 * traversal of the chain leads to a chain that is present in the
1f67ba50 75 * linked-list (the start node is never removed) and that it does not
5c4ca589
MD
76 * contain the "removed" node anymore, even if concurrent delete/add
77 * operations are changing the structure of the list concurrently.
1f67ba50
MD
78 * - The add operations perform garbage collection of buckets if they
79 * encounter nodes with removed flag set in the bucket where they want
80 * to add their new node. This ensures lock-freedom of add operation by
29e669f6
MD
81 * helping the remover unlink nodes from the list rather than to wait
82 * for it do to so.
1f67ba50
MD
83 * - There are three memory backends for the hash table buckets: the
84 * "order table", the "chunks", and the "mmap".
85 * - These bucket containers contain a compact version of the hash table
86 * nodes.
87 * - The RCU "order table":
88 * - has a first level table indexed by log2(hash index) which is
89 * copied and expanded by the resize operation. This order table
90 * allows finding the "bucket node" tables.
91 * - There is one bucket node table per hash index order. The size of
92 * each bucket node table is half the number of hashes contained in
93 * this order (except for order 0).
94 * - The RCU "chunks" is best suited for close interaction with a page
95 * allocator. It uses a linear array as index to "chunks" containing
96 * each the same number of buckets.
97 * - The RCU "mmap" memory backend uses a single memory map to hold
98 * all buckets.
5f177b1c 99 * - synchronize_rcu is used to garbage-collect the old bucket node table.
93d46c39 100 *
7f949215 101 * Ordering Guarantees:
0f5543cb 102 *
7f949215
MD
103 * To discuss these guarantees, we first define "read" operation as any
104 * of the the basic cds_lfht_lookup, cds_lfht_next_duplicate,
105 * cds_lfht_first, cds_lfht_next operation, as well as
67ecffc0 106 * cds_lfht_add_unique (failure).
7f949215
MD
107 *
108 * We define "read traversal" operation as any of the following
109 * group of operations
0f5543cb 110 * - cds_lfht_lookup followed by iteration with cds_lfht_next_duplicate
7f949215
MD
111 * (and/or cds_lfht_next, although less common).
112 * - cds_lfht_add_unique (failure) followed by iteration with
113 * cds_lfht_next_duplicate (and/or cds_lfht_next, although less
114 * common).
115 * - cds_lfht_first followed iteration with cds_lfht_next (and/or
116 * cds_lfht_next_duplicate, although less common).
0f5543cb 117 *
bf09adc7 118 * We define "write" operations as any of cds_lfht_add, cds_lfht_replace,
7f949215
MD
119 * cds_lfht_add_unique (success), cds_lfht_add_replace, cds_lfht_del.
120 *
121 * When cds_lfht_add_unique succeeds (returns the node passed as
122 * parameter), it acts as a "write" operation. When cds_lfht_add_unique
123 * fails (returns a node different from the one passed as parameter), it
124 * acts as a "read" operation. A cds_lfht_add_unique failure is a
125 * cds_lfht_lookup "read" operation, therefore, any ordering guarantee
126 * referring to "lookup" imply any of "lookup" or cds_lfht_add_unique
127 * (failure).
128 *
129 * We define "prior" and "later" node as nodes observable by reads and
130 * read traversals respectively before and after a write or sequence of
131 * write operations.
132 *
133 * Hash-table operations are often cascaded, for example, the pointer
134 * returned by a cds_lfht_lookup() might be passed to a cds_lfht_next(),
135 * whose return value might in turn be passed to another hash-table
136 * operation. This entire cascaded series of operations must be enclosed
137 * by a pair of matching rcu_read_lock() and rcu_read_unlock()
138 * operations.
139 *
140 * The following ordering guarantees are offered by this hash table:
141 *
142 * A.1) "read" after "write": if there is ordering between a write and a
143 * later read, then the read is guaranteed to see the write or some
144 * later write.
145 * A.2) "read traversal" after "write": given that there is dependency
146 * ordering between reads in a "read traversal", if there is
147 * ordering between a write and the first read of the traversal,
148 * then the "read traversal" is guaranteed to see the write or
149 * some later write.
150 * B.1) "write" after "read": if there is ordering between a read and a
151 * later write, then the read will never see the write.
152 * B.2) "write" after "read traversal": given that there is dependency
153 * ordering between reads in a "read traversal", if there is
154 * ordering between the last read of the traversal and a later
155 * write, then the "read traversal" will never see the write.
156 * C) "write" while "read traversal": if a write occurs during a "read
157 * traversal", the traversal may, or may not, see the write.
158 * D.1) "write" after "write": if there is ordering between a write and
159 * a later write, then the later write is guaranteed to see the
160 * effects of the first write.
161 * D.2) Concurrent "write" pairs: The system will assign an arbitrary
162 * order to any pair of concurrent conflicting writes.
163 * Non-conflicting writes (for example, to different keys) are
164 * unordered.
165 * E) If a grace period separates a "del" or "replace" operation
166 * and a subsequent operation, then that subsequent operation is
167 * guaranteed not to see the removed item.
168 * F) Uniqueness guarantee: given a hash table that does not contain
169 * duplicate items for a given key, there will only be one item in
170 * the hash table after an arbitrary sequence of add_unique and/or
171 * add_replace operations. Note, however, that a pair of
172 * concurrent read operations might well access two different items
173 * with that key.
174 * G.1) If a pair of lookups for a given key are ordered (e.g. by a
175 * memory barrier), then the second lookup will return the same
176 * node as the previous lookup, or some later node.
177 * G.2) A "read traversal" that starts after the end of a prior "read
178 * traversal" (ordered by memory barriers) is guaranteed to see the
179 * same nodes as the previous traversal, or some later nodes.
180 * G.3) Concurrent "read" pairs: concurrent reads are unordered. For
181 * example, if a pair of reads to the same key run concurrently
182 * with an insertion of that same key, the reads remain unordered
183 * regardless of their return values. In other words, you cannot
184 * rely on the values returned by the reads to deduce ordering.
185 *
186 * Progress guarantees:
187 *
188 * * Reads are wait-free. These operations always move forward in the
189 * hash table linked list, and this list has no loop.
190 * * Writes are lock-free. Any retry loop performed by a write operation
191 * is triggered by progress made within another update operation.
0f5543cb 192 *
1ee8f000 193 * Bucket node tables:
93d46c39 194 *
1ee8f000
LJ
195 * hash table hash table the last all bucket node tables
196 * order size bucket node 0 1 2 3 4 5 6(index)
93d46c39
LJ
197 * table size
198 * 0 1 1 1
199 * 1 2 1 1 1
200 * 2 4 2 1 1 2
201 * 3 8 4 1 1 2 4
202 * 4 16 8 1 1 2 4 8
203 * 5 32 16 1 1 2 4 8 16
204 * 6 64 32 1 1 2 4 8 16 32
205 *
1ee8f000 206 * When growing/shrinking, we only focus on the last bucket node table
93d46c39
LJ
207 * which size is (!order ? 1 : (1 << (order -1))).
208 *
209 * Example for growing/shrinking:
1ee8f000
LJ
210 * grow hash table from order 5 to 6: init the index=6 bucket node table
211 * shrink hash table from order 6 to 5: fini the index=6 bucket node table
93d46c39 212 *
1475579c 213 * A bit of ascii art explanation:
67ecffc0 214 *
1f67ba50
MD
215 * The order index is the off-by-one compared to the actual power of 2
216 * because we use index 0 to deal with the 0 special-case.
67ecffc0 217 *
1475579c 218 * This shows the nodes for a small table ordered by reversed bits:
67ecffc0 219 *
1475579c
MD
220 * bits reverse
221 * 0 000 000
222 * 4 100 001
223 * 2 010 010
224 * 6 110 011
225 * 1 001 100
226 * 5 101 101
227 * 3 011 110
228 * 7 111 111
67ecffc0
MD
229 *
230 * This shows the nodes in order of non-reversed bits, linked by
1475579c 231 * reversed-bit order.
67ecffc0 232 *
1475579c
MD
233 * order bits reverse
234 * 0 0 000 000
0adc36a8
LJ
235 * 1 | 1 001 100 <-
236 * 2 | | 2 010 010 <- |
f6fdd688 237 * | | | 3 011 110 | <- |
1475579c
MD
238 * 3 -> | | | 4 100 001 | |
239 * -> | | 5 101 101 |
240 * -> | 6 110 011
241 * -> 7 111 111
e753ff5a
MD
242 */
243
2ed95849
MD
244#define _LGPL_SOURCE
245#include <stdlib.h>
e0ba718a 246#include <errno.h>
e0ba718a 247#include <stdio.h>
abc490a1 248#include <stdint.h>
f000907d 249#include <string.h>
125f41db 250#include <sched.h>
95747f9e 251#include <unistd.h>
e0ba718a 252
a47dd11c 253#include "compat-getcpu.h"
01477510 254#include <urcu/assert.h>
6cd23d47
MD
255#include <urcu/pointer.h>
256#include <urcu/call-rcu.h>
257#include <urcu/flavor.h>
a42cc659
MD
258#include <urcu/arch.h>
259#include <urcu/uatomic.h>
a42cc659 260#include <urcu/compiler.h>
abc490a1 261#include <urcu/rculfhash.h>
5e28c532 262#include <stdio.h>
464a1ec9 263#include <pthread.h>
d0ec0ed2 264#include <signal.h>
0d0409b1 265#include "rculfhash-internal.h"
d0ec0ed2
MD
266#include "workqueue.h"
267#include "urcu-die.h"
83e334d0 268#include "urcu-utils.h"
5cfe81b7 269#include "compat-smp.h"
44395fb7 270
f8994aee 271/*
4c42f1b8 272 * Split-counters lazily update the global counter each 1024
f8994aee
MD
273 * addition/removal. It automatically keeps track of resize required.
274 * We use the bucket length as indicator for need to expand for small
ffa11a18 275 * tables and machines lacking per-cpu data support.
f8994aee
MD
276 */
277#define COUNT_COMMIT_ORDER 10
4ddbb355 278#define DEFAULT_SPLIT_COUNT_MASK 0xFUL
6ea6bc67
MD
279#define CHAIN_LEN_TARGET 1
280#define CHAIN_LEN_RESIZE_THRESHOLD 3
2ed95849 281
cd95516d 282/*
76a73da8 283 * Define the minimum table size.
cd95516d 284 */
d0d8f9aa
LJ
285#define MIN_TABLE_ORDER 0
286#define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER)
cd95516d 287
b7d619b0 288/*
1ee8f000 289 * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink.
b7d619b0 290 */
6083a889
MD
291#define MIN_PARTITION_PER_THREAD_ORDER 12
292#define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
b7d619b0 293
d95bd160
MD
294/*
295 * The removed flag needs to be updated atomically with the pointer.
48ed1c18 296 * It indicates that no node must attach to the node scheduled for
b198f0fd 297 * removal, and that node garbage collection must be performed.
1ee8f000 298 * The bucket flag does not require to be updated atomically with the
d95bd160 299 * pointer, but it is added as a pointer low bit flag to save space.
1f67ba50
MD
300 * The "removal owner" flag is used to detect which of the "del"
301 * operation that has set the "removed flag" gets to return the removed
302 * node to its caller. Note that the replace operation does not need to
303 * iteract with the "removal owner" flag, because it validates that
304 * the "removed" flag is not set before performing its cmpxchg.
d95bd160 305 */
d37166c6 306#define REMOVED_FLAG (1UL << 0)
1ee8f000 307#define BUCKET_FLAG (1UL << 1)
db00ccc3
MD
308#define REMOVAL_OWNER_FLAG (1UL << 2)
309#define FLAGS_MASK ((1UL << 3) - 1)
d37166c6 310
bb7b2f26 311/* Value of the end pointer. Should not interact with flags. */
f9c80341 312#define END_VALUE NULL
bb7b2f26 313
7f52427b
MD
314/*
315 * ht_items_count: Split-counters counting the number of node addition
316 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
317 * is set at hash table creation.
318 *
319 * These are free-running counters, never reset to zero. They count the
320 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
321 * operations to update the global counter. We choose a power-of-2 value
322 * for the trigger to deal with 32 or 64-bit overflow of the counter.
323 */
df44348d 324struct ht_items_count {
860d07e8 325 unsigned long add, del;
df44348d
MD
326} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
327
7f52427b 328/*
d0ec0ed2 329 * resize_work: Contains arguments passed to worker thread
7f52427b
MD
330 * responsible for performing lazy resize.
331 */
d0ec0ed2
MD
332struct resize_work {
333 struct urcu_work work;
14044b37 334 struct cds_lfht *ht;
abc490a1 335};
2ed95849 336
7f52427b
MD
337/*
338 * partition_resize_work: Contains arguments passed to worker threads
339 * executing the hash table resize on partitions of the hash table
340 * assigned to each processor's worker thread.
341 */
b7d619b0 342struct partition_resize_work {
1af6e26e 343 pthread_t thread_id;
b7d619b0
MD
344 struct cds_lfht *ht;
345 unsigned long i, start, len;
346 void (*fct)(struct cds_lfht *ht, unsigned long i,
347 unsigned long start, unsigned long len);
348};
349
d0ec0ed2 350static struct urcu_workqueue *cds_lfht_workqueue;
d0ec0ed2
MD
351
352/*
353 * Mutex ensuring mutual exclusion between workqueue initialization and
354 * fork handlers. cds_lfht_fork_mutex nests inside call_rcu_mutex.
355 */
356static pthread_mutex_t cds_lfht_fork_mutex = PTHREAD_MUTEX_INITIALIZER;
357
358static struct urcu_atfork cds_lfht_atfork;
359
360/*
361 * atfork handler nesting counters. Handle being registered to many urcu
362 * flavors, thus being possibly invoked more than once in the
363 * pthread_atfork list of callbacks.
364 */
365static int cds_lfht_workqueue_atfork_nesting;
366
b047e7a7 367static void __attribute__((destructor)) cds_lfht_exit(void);
d0ec0ed2 368static void cds_lfht_init_worker(const struct rcu_flavor_struct *flavor);
d0ec0ed2 369
d7c76f85
MD
370#ifdef CONFIG_CDS_LFHT_ITER_DEBUG
371
372static
373void cds_lfht_iter_debug_set_ht(struct cds_lfht *ht, struct cds_lfht_iter *iter)
374{
375 iter->lfht = ht;
376}
377
01477510 378#define cds_lfht_iter_debug_assert(...) urcu_posix_assert(__VA_ARGS__)
d7c76f85
MD
379
380#else
381
382static
70469b43
MJ
383void cds_lfht_iter_debug_set_ht(struct cds_lfht *ht __attribute__((unused)),
384 struct cds_lfht_iter *iter __attribute__((unused)))
d7c76f85
MD
385{
386}
387
388#define cds_lfht_iter_debug_assert(...)
389
390#endif
391
abc490a1
MD
392/*
393 * Algorithm to reverse bits in a word by lookup table, extended to
394 * 64-bit words.
f9830efd 395 * Source:
abc490a1 396 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
f9830efd 397 * Originally from Public Domain.
abc490a1
MD
398 */
399
67ecffc0 400static const uint8_t BitReverseTable256[256] =
2ed95849 401{
abc490a1
MD
402#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
403#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
404#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
405 R6(0), R6(2), R6(1), R6(3)
406};
407#undef R2
408#undef R4
409#undef R6
2ed95849 410
abc490a1
MD
411static
412uint8_t bit_reverse_u8(uint8_t v)
413{
414 return BitReverseTable256[v];
415}
ab7d5fc6 416
95bc7fb9
MD
417#if (CAA_BITS_PER_LONG == 32)
418static
abc490a1
MD
419uint32_t bit_reverse_u32(uint32_t v)
420{
67ecffc0
MD
421 return ((uint32_t) bit_reverse_u8(v) << 24) |
422 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
423 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
abc490a1 424 ((uint32_t) bit_reverse_u8(v >> 24));
2ed95849 425}
95bc7fb9
MD
426#else
427static
abc490a1 428uint64_t bit_reverse_u64(uint64_t v)
2ed95849 429{
67ecffc0
MD
430 return ((uint64_t) bit_reverse_u8(v) << 56) |
431 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
abc490a1
MD
432 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
433 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
67ecffc0
MD
434 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
435 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
abc490a1
MD
436 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
437 ((uint64_t) bit_reverse_u8(v >> 56));
438}
95bc7fb9 439#endif
abc490a1
MD
440
441static
442unsigned long bit_reverse_ulong(unsigned long v)
443{
444#if (CAA_BITS_PER_LONG == 32)
445 return bit_reverse_u32(v);
446#else
447 return bit_reverse_u64(v);
448#endif
449}
450
f9830efd 451/*
24365af7
MD
452 * fls: returns the position of the most significant bit.
453 * Returns 0 if no bit is set, else returns the position of the most
454 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
f9830efd 455 */
0b1e236d 456#if defined(URCU_ARCH_X86)
24365af7
MD
457static inline
458unsigned int fls_u32(uint32_t x)
f9830efd 459{
24365af7
MD
460 int r;
461
e1789ce2 462 __asm__ ("bsrl %1,%0\n\t"
24365af7
MD
463 "jnz 1f\n\t"
464 "movl $-1,%0\n\t"
465 "1:\n\t"
466 : "=r" (r) : "rm" (x));
467 return r + 1;
468}
469#define HAS_FLS_U32
470#endif
471
0b1e236d 472#if defined(URCU_ARCH_AMD64)
24365af7
MD
473static inline
474unsigned int fls_u64(uint64_t x)
475{
476 long r;
477
e1789ce2 478 __asm__ ("bsrq %1,%0\n\t"
24365af7
MD
479 "jnz 1f\n\t"
480 "movq $-1,%0\n\t"
481 "1:\n\t"
482 : "=r" (r) : "rm" (x));
483 return r + 1;
484}
485#define HAS_FLS_U64
486#endif
487
488#ifndef HAS_FLS_U64
489static __attribute__((unused))
490unsigned int fls_u64(uint64_t x)
491{
492 unsigned int r = 64;
493
494 if (!x)
495 return 0;
496
497 if (!(x & 0xFFFFFFFF00000000ULL)) {
498 x <<= 32;
499 r -= 32;
500 }
501 if (!(x & 0xFFFF000000000000ULL)) {
502 x <<= 16;
503 r -= 16;
504 }
505 if (!(x & 0xFF00000000000000ULL)) {
506 x <<= 8;
507 r -= 8;
508 }
509 if (!(x & 0xF000000000000000ULL)) {
510 x <<= 4;
511 r -= 4;
512 }
513 if (!(x & 0xC000000000000000ULL)) {
514 x <<= 2;
515 r -= 2;
516 }
517 if (!(x & 0x8000000000000000ULL)) {
518 x <<= 1;
519 r -= 1;
520 }
521 return r;
522}
523#endif
524
525#ifndef HAS_FLS_U32
526static __attribute__((unused))
527unsigned int fls_u32(uint32_t x)
528{
529 unsigned int r = 32;
f9830efd 530
24365af7
MD
531 if (!x)
532 return 0;
533 if (!(x & 0xFFFF0000U)) {
534 x <<= 16;
535 r -= 16;
536 }
537 if (!(x & 0xFF000000U)) {
538 x <<= 8;
539 r -= 8;
540 }
541 if (!(x & 0xF0000000U)) {
542 x <<= 4;
543 r -= 4;
544 }
545 if (!(x & 0xC0000000U)) {
546 x <<= 2;
547 r -= 2;
548 }
549 if (!(x & 0x80000000U)) {
550 x <<= 1;
551 r -= 1;
552 }
553 return r;
554}
555#endif
556
5bc6b66f 557unsigned int cds_lfht_fls_ulong(unsigned long x)
f9830efd 558{
6887cc5e 559#if (CAA_BITS_PER_LONG == 32)
24365af7
MD
560 return fls_u32(x);
561#else
562 return fls_u64(x);
563#endif
564}
f9830efd 565
920f8ef6
LJ
566/*
567 * Return the minimum order for which x <= (1UL << order).
568 * Return -1 if x is 0.
569 */
61c3fb60 570static
5bc6b66f 571int cds_lfht_get_count_order_u32(uint32_t x)
24365af7 572{
920f8ef6
LJ
573 if (!x)
574 return -1;
24365af7 575
920f8ef6 576 return fls_u32(x - 1);
24365af7
MD
577}
578
920f8ef6
LJ
579/*
580 * Return the minimum order for which x <= (1UL << order).
581 * Return -1 if x is 0.
582 */
5bc6b66f 583int cds_lfht_get_count_order_ulong(unsigned long x)
24365af7 584{
920f8ef6
LJ
585 if (!x)
586 return -1;
24365af7 587
5bc6b66f 588 return cds_lfht_fls_ulong(x - 1);
f9830efd
MD
589}
590
591static
ab65b890 592void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth);
f9830efd 593
f8994aee 594static
4105056a 595void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
f8994aee
MD
596 unsigned long count);
597
5ffcaeef
MD
598static void mutex_lock(pthread_mutex_t *mutex)
599{
600 int ret;
601
602#ifndef DISTRUST_SIGNALS_EXTREME
603 ret = pthread_mutex_lock(mutex);
604 if (ret)
605 urcu_die(ret);
606#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
607 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
608 if (ret != EBUSY && ret != EINTR)
609 urcu_die(ret);
610 if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) {
601922a8 611 uatomic_store(&URCU_TLS(rcu_reader).need_mb, 0, CMM_SEQ_CST);
5ffcaeef
MD
612 }
613 (void) poll(NULL, 0, 10);
614 }
615#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
616}
617
618static void mutex_unlock(pthread_mutex_t *mutex)
619{
620 int ret;
621
622 ret = pthread_mutex_unlock(mutex);
623 if (ret)
624 urcu_die(ret);
625}
626
df44348d 627static long nr_cpus_mask = -1;
4c42f1b8 628static long split_count_mask = -1;
e53ab1eb 629static int split_count_order = -1;
4c42f1b8
LJ
630
631static void ht_init_nr_cpus_mask(void)
632{
633 long maxcpus;
634
5cfe81b7 635 maxcpus = get_possible_cpus_array_len();
4c42f1b8
LJ
636 if (maxcpus <= 0) {
637 nr_cpus_mask = -2;
638 return;
639 }
640 /*
641 * round up number of CPUs to next power of two, so we
642 * can use & for modulo.
643 */
5bc6b66f 644 maxcpus = 1UL << cds_lfht_get_count_order_ulong(maxcpus);
4c42f1b8
LJ
645 nr_cpus_mask = maxcpus - 1;
646}
df44348d
MD
647
648static
5afadd12 649void alloc_split_items_count(struct cds_lfht *ht)
df44348d 650{
4c42f1b8
LJ
651 if (nr_cpus_mask == -1) {
652 ht_init_nr_cpus_mask();
4ddbb355
LJ
653 if (nr_cpus_mask < 0)
654 split_count_mask = DEFAULT_SPLIT_COUNT_MASK;
655 else
656 split_count_mask = nr_cpus_mask;
e53ab1eb
MD
657 split_count_order =
658 cds_lfht_get_count_order_ulong(split_count_mask + 1);
df44348d 659 }
4c42f1b8 660
01477510 661 urcu_posix_assert(split_count_mask >= 0);
5afadd12
LJ
662
663 if (ht->flags & CDS_LFHT_ACCOUNTING) {
95bc7fb9
MD
664 ht->split_count = calloc(split_count_mask + 1,
665 sizeof(struct ht_items_count));
01477510 666 urcu_posix_assert(ht->split_count);
5afadd12
LJ
667 } else {
668 ht->split_count = NULL;
669 }
df44348d
MD
670}
671
672static
5afadd12 673void free_split_items_count(struct cds_lfht *ht)
df44348d 674{
5afadd12 675 poison_free(ht->split_count);
df44348d
MD
676}
677
678static
14360f1c 679int ht_get_split_count_index(unsigned long hash)
df44348d
MD
680{
681 int cpu;
682
01477510 683 urcu_posix_assert(split_count_mask >= 0);
a47dd11c 684 cpu = urcu_sched_getcpu();
8ed51e04 685 if (caa_unlikely(cpu < 0))
14360f1c 686 return hash & split_count_mask;
df44348d 687 else
4c42f1b8 688 return cpu & split_count_mask;
df44348d
MD
689}
690
691static
14360f1c 692void ht_count_add(struct cds_lfht *ht, unsigned long size, unsigned long hash)
df44348d 693{
83e334d0 694 unsigned long split_count, count;
4c42f1b8 695 int index;
df44348d 696
8ed51e04 697 if (caa_unlikely(!ht->split_count))
3171717f 698 return;
14360f1c 699 index = ht_get_split_count_index(hash);
4c42f1b8 700 split_count = uatomic_add_return(&ht->split_count[index].add, 1);
314558bf
MD
701 if (caa_likely(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))
702 return;
703 /* Only if number of add multiple of 1UL << COUNT_COMMIT_ORDER */
704
705 dbg_printf("add split count %lu\n", split_count);
706 count = uatomic_add_return(&ht->count,
707 1UL << COUNT_COMMIT_ORDER);
4c299dcb 708 if (caa_likely(count & (count - 1)))
314558bf
MD
709 return;
710 /* Only if global count is power of 2 */
711
712 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) < size)
713 return;
83e334d0 714 dbg_printf("add set global %lu\n", count);
314558bf
MD
715 cds_lfht_resize_lazy_count(ht, size,
716 count >> (CHAIN_LEN_TARGET - 1));
df44348d
MD
717}
718
719static
14360f1c 720void ht_count_del(struct cds_lfht *ht, unsigned long size, unsigned long hash)
df44348d 721{
83e334d0 722 unsigned long split_count, count;
4c42f1b8 723 int index;
df44348d 724
8ed51e04 725 if (caa_unlikely(!ht->split_count))
3171717f 726 return;
14360f1c 727 index = ht_get_split_count_index(hash);
4c42f1b8 728 split_count = uatomic_add_return(&ht->split_count[index].del, 1);
314558bf
MD
729 if (caa_likely(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))
730 return;
731 /* Only if number of deletes multiple of 1UL << COUNT_COMMIT_ORDER */
732
733 dbg_printf("del split count %lu\n", split_count);
734 count = uatomic_add_return(&ht->count,
735 -(1UL << COUNT_COMMIT_ORDER));
4c299dcb 736 if (caa_likely(count & (count - 1)))
314558bf
MD
737 return;
738 /* Only if global count is power of 2 */
739
740 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) >= size)
741 return;
59b6b14f 742 dbg_printf("del set global %lu\n", count);
314558bf
MD
743 /*
744 * Don't shrink table if the number of nodes is below a
745 * certain threshold.
746 */
747 if (count < (1UL << COUNT_COMMIT_ORDER) * (split_count_mask + 1))
748 return;
749 cds_lfht_resize_lazy_count(ht, size,
750 count >> (CHAIN_LEN_TARGET - 1));
df44348d
MD
751}
752
f9830efd 753static
4105056a 754void check_resize(struct cds_lfht *ht, unsigned long size, uint32_t chain_len)
f9830efd 755{
f8994aee
MD
756 unsigned long count;
757
b8af5011
MD
758 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
759 return;
f8994aee
MD
760 count = uatomic_read(&ht->count);
761 /*
762 * Use bucket-local length for small table expand and for
763 * environments lacking per-cpu data support.
764 */
e53ab1eb 765 if (count >= (1UL << (COUNT_COMMIT_ORDER + split_count_order)))
f8994aee 766 return;
24365af7 767 if (chain_len > 100)
f0c29ed7 768 dbg_printf("WARNING: large chain length: %u.\n",
24365af7 769 chain_len);
e53ab1eb
MD
770 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD) {
771 int growth;
772
773 /*
774 * Ideal growth calculated based on chain length.
775 */
776 growth = cds_lfht_get_count_order_u32(chain_len
777 - (CHAIN_LEN_TARGET - 1));
778 if ((ht->flags & CDS_LFHT_ACCOUNTING)
779 && (size << growth)
780 >= (1UL << (COUNT_COMMIT_ORDER
781 + split_count_order))) {
782 /*
783 * If ideal growth expands the hash table size
784 * beyond the "small hash table" sizes, use the
785 * maximum small hash table size to attempt
786 * expanding the hash table. This only applies
787 * when node accounting is available, otherwise
788 * the chain length is used to expand the hash
789 * table in every case.
790 */
791 growth = COUNT_COMMIT_ORDER + split_count_order
792 - cds_lfht_get_count_order_ulong(size);
793 if (growth <= 0)
794 return;
795 }
796 cds_lfht_resize_lazy_grow(ht, size, growth);
797 }
f9830efd
MD
798}
799
abc490a1 800static
14044b37 801struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
abc490a1 802{
14044b37 803 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
abc490a1
MD
804}
805
806static
afa5940d 807int is_removed(const struct cds_lfht_node *node)
abc490a1 808{
d37166c6 809 return ((unsigned long) node) & REMOVED_FLAG;
abc490a1
MD
810}
811
f5596c94 812static
1ee8f000 813int is_bucket(struct cds_lfht_node *node)
f5596c94 814{
1ee8f000 815 return ((unsigned long) node) & BUCKET_FLAG;
f5596c94
MD
816}
817
818static
1ee8f000 819struct cds_lfht_node *flag_bucket(struct cds_lfht_node *node)
f5596c94 820{
1ee8f000 821 return (struct cds_lfht_node *) (((unsigned long) node) | BUCKET_FLAG);
f5596c94 822}
bb7b2f26 823
db00ccc3
MD
824static
825int is_removal_owner(struct cds_lfht_node *node)
826{
827 return ((unsigned long) node) & REMOVAL_OWNER_FLAG;
828}
829
4c10e9af
MD
830static
831struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
832{
833 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
834}
835
db00ccc3
MD
836static
837struct cds_lfht_node *flag_removal_owner(struct cds_lfht_node *node)
838{
839 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVAL_OWNER_FLAG);
840}
841
71bb3aca
MD
842static
843struct cds_lfht_node *flag_removed_or_removal_owner(struct cds_lfht_node *node)
844{
845 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG | REMOVAL_OWNER_FLAG);
846}
847
bb7b2f26
MD
848static
849struct cds_lfht_node *get_end(void)
850{
851 return (struct cds_lfht_node *) END_VALUE;
852}
853
854static
855int is_end(struct cds_lfht_node *node)
856{
857 return clear_flag(node) == (struct cds_lfht_node *) END_VALUE;
858}
859
abc490a1 860static
ab65b890
LJ
861unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr,
862 unsigned long v)
abc490a1
MD
863{
864 unsigned long old1, old2;
865
866 old1 = uatomic_read(ptr);
867 do {
868 old2 = old1;
601922a8
OD
869 if (old2 >= v) {
870 cmm_smp_mb();
f9830efd 871 return old2;
601922a8 872 }
abc490a1 873 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
ab65b890 874 return old2;
abc490a1
MD
875}
876
48f1b16d
LJ
877static
878void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
879{
0b6aa001 880 return ht->mm->alloc_bucket_table(ht, order);
48f1b16d
LJ
881}
882
883/*
884 * cds_lfht_free_bucket_table() should be called with decreasing order.
885 * When cds_lfht_free_bucket_table(0) is called, it means the whole
886 * lfht is destroyed.
887 */
888static
889void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
890{
0b6aa001 891 return ht->mm->free_bucket_table(ht, order);
48f1b16d
LJ
892}
893
9d72a73f
LJ
894static inline
895struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
f4a9cc0b 896{
0b6aa001 897 return ht->bucket_at(ht, index);
f4a9cc0b
LJ
898}
899
9d72a73f
LJ
900static inline
901struct cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size,
902 unsigned long hash)
903{
01477510 904 urcu_posix_assert(size > 0);
9d72a73f
LJ
905 return bucket_at(ht, hash & (size - 1));
906}
907
273399de
MD
908/*
909 * Remove all logically deleted nodes from a bucket up to a certain node key.
910 */
911static
1ee8f000 912void _cds_lfht_gc_bucket(struct cds_lfht_node *bucket, struct cds_lfht_node *node)
273399de 913{
14044b37 914 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
273399de 915
01477510
FD
916 urcu_posix_assert(!is_bucket(bucket));
917 urcu_posix_assert(!is_removed(bucket));
918 urcu_posix_assert(!is_removal_owner(bucket));
919 urcu_posix_assert(!is_bucket(node));
920 urcu_posix_assert(!is_removed(node));
921 urcu_posix_assert(!is_removal_owner(node));
273399de 922 for (;;) {
1ee8f000
LJ
923 iter_prev = bucket;
924 /* We can always skip the bucket node initially */
04db56f8 925 iter = rcu_dereference(iter_prev->next);
01477510
FD
926 urcu_posix_assert(!is_removed(iter));
927 urcu_posix_assert(!is_removal_owner(iter));
928 urcu_posix_assert(iter_prev->reverse_hash <= node->reverse_hash);
bd4db153 929 /*
1ee8f000 930 * We should never be called with bucket (start of chain)
bd4db153
MD
931 * and logically removed node (end of path compression
932 * marker) being the actual same node. This would be a
933 * bug in the algorithm implementation.
934 */
01477510 935 urcu_posix_assert(bucket != node);
273399de 936 for (;;) {
8ed51e04 937 if (caa_unlikely(is_end(iter)))
f9c80341 938 return;
04db56f8 939 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
f9c80341 940 return;
04db56f8 941 next = rcu_dereference(clear_flag(iter)->next);
8ed51e04 942 if (caa_likely(is_removed(next)))
273399de 943 break;
b453eae1 944 iter_prev = clear_flag(iter);
273399de
MD
945 iter = next;
946 }
01477510
FD
947 urcu_posix_assert(!is_removed(iter));
948 urcu_posix_assert(!is_removal_owner(iter));
1ee8f000
LJ
949 if (is_bucket(iter))
950 new_next = flag_bucket(clear_flag(next));
f5596c94
MD
951 else
952 new_next = clear_flag(next);
04db56f8 953 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
273399de
MD
954 }
955}
956
9357c415
MD
957static
958int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size,
959 struct cds_lfht_node *old_node,
3fb86f26 960 struct cds_lfht_node *old_next,
9357c415
MD
961 struct cds_lfht_node *new_node)
962{
04db56f8 963 struct cds_lfht_node *bucket, *ret_next;
9357c415
MD
964
965 if (!old_node) /* Return -ENOENT if asked to replace NULL node */
7801dadd 966 return -ENOENT;
9357c415 967
01477510
FD
968 urcu_posix_assert(!is_removed(old_node));
969 urcu_posix_assert(!is_removal_owner(old_node));
970 urcu_posix_assert(!is_bucket(old_node));
971 urcu_posix_assert(!is_removed(new_node));
972 urcu_posix_assert(!is_removal_owner(new_node));
973 urcu_posix_assert(!is_bucket(new_node));
974 urcu_posix_assert(new_node != old_node);
3fb86f26 975 for (;;) {
9357c415 976 /* Insert after node to be replaced */
9357c415
MD
977 if (is_removed(old_next)) {
978 /*
979 * Too late, the old node has been removed under us
980 * between lookup and replace. Fail.
981 */
7801dadd 982 return -ENOENT;
9357c415 983 }
01477510
FD
984 urcu_posix_assert(old_next == clear_flag(old_next));
985 urcu_posix_assert(new_node != old_next);
71bb3aca
MD
986 /*
987 * REMOVAL_OWNER flag is _NEVER_ set before the REMOVED
988 * flag. It is either set atomically at the same time
989 * (replace) or after (del).
990 */
01477510 991 urcu_posix_assert(!is_removal_owner(old_next));
feda2722 992 new_node->next = old_next;
9357c415
MD
993 /*
994 * Here is the whole trick for lock-free replace: we add
995 * the replacement node _after_ the node we want to
996 * replace by atomically setting its next pointer at the
997 * same time we set its removal flag. Given that
998 * the lookups/get next use an iterator aware of the
999 * next pointer, they will either skip the old node due
1000 * to the removal flag and see the new node, or use
1001 * the old node, but will not see the new one.
db00ccc3
MD
1002 * This is a replacement of a node with another node
1003 * that has the same value: we are therefore not
71bb3aca
MD
1004 * removing a value from the hash table. We set both the
1005 * REMOVED and REMOVAL_OWNER flags atomically so we own
1006 * the node after successful cmpxchg.
9357c415 1007 */
04db56f8 1008 ret_next = uatomic_cmpxchg(&old_node->next,
71bb3aca 1009 old_next, flag_removed_or_removal_owner(new_node));
3fb86f26 1010 if (ret_next == old_next)
7801dadd 1011 break; /* We performed the replacement. */
3fb86f26
LJ
1012 old_next = ret_next;
1013 }
9357c415 1014
9357c415
MD
1015 /*
1016 * Ensure that the old node is not visible to readers anymore:
1017 * lookup for the node, and remove it (along with any other
1018 * logically removed node) if found.
1019 */
04db56f8
LJ
1020 bucket = lookup_bucket(ht, size, bit_reverse_ulong(old_node->reverse_hash));
1021 _cds_lfht_gc_bucket(bucket, new_node);
7801dadd 1022
01477510 1023 urcu_posix_assert(is_removed(CMM_LOAD_SHARED(old_node->next)));
7801dadd 1024 return 0;
9357c415
MD
1025}
1026
83beee94
MD
1027/*
1028 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
1029 * mode. A NULL unique_ret allows creation of duplicate keys.
1030 */
abc490a1 1031static
83beee94 1032void _cds_lfht_add(struct cds_lfht *ht,
91a75cc5 1033 unsigned long hash,
0422d92c 1034 cds_lfht_match_fct match,
996ff57c 1035 const void *key,
83beee94
MD
1036 unsigned long size,
1037 struct cds_lfht_node *node,
1038 struct cds_lfht_iter *unique_ret,
1ee8f000 1039 int bucket_flag)
abc490a1 1040{
14044b37 1041 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
960c9e4f 1042 *return_node;
04db56f8 1043 struct cds_lfht_node *bucket;
abc490a1 1044
01477510
FD
1045 urcu_posix_assert(!is_bucket(node));
1046 urcu_posix_assert(!is_removed(node));
1047 urcu_posix_assert(!is_removal_owner(node));
91a75cc5 1048 bucket = lookup_bucket(ht, size, hash);
abc490a1 1049 for (;;) {
adc0de68 1050 uint32_t chain_len = 0;
abc490a1 1051
11519af6
MD
1052 /*
1053 * iter_prev points to the non-removed node prior to the
1054 * insert location.
11519af6 1055 */
04db56f8 1056 iter_prev = bucket;
1ee8f000 1057 /* We can always skip the bucket node initially */
04db56f8 1058 iter = rcu_dereference(iter_prev->next);
01477510 1059 urcu_posix_assert(iter_prev->reverse_hash <= node->reverse_hash);
abc490a1 1060 for (;;) {
8ed51e04 1061 if (caa_unlikely(is_end(iter)))
273399de 1062 goto insert;
04db56f8 1063 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
273399de 1064 goto insert;
238cc06e 1065
1ee8f000
LJ
1066 /* bucket node is the first node of the identical-hash-value chain */
1067 if (bucket_flag && clear_flag(iter)->reverse_hash == node->reverse_hash)
194fdbd1 1068 goto insert;
238cc06e 1069
04db56f8 1070 next = rcu_dereference(clear_flag(iter)->next);
8ed51e04 1071 if (caa_unlikely(is_removed(next)))
9dba85be 1072 goto gc_node;
238cc06e
LJ
1073
1074 /* uniquely add */
83beee94 1075 if (unique_ret
1ee8f000 1076 && !is_bucket(next)
04db56f8 1077 && clear_flag(iter)->reverse_hash == node->reverse_hash) {
d7c76f85
MD
1078 struct cds_lfht_iter d_iter = {
1079 .node = node,
1080 .next = iter,
1081#ifdef CONFIG_CDS_LFHT_ITER_DEBUG
1082 .lfht = ht,
1083#endif
1084 };
238cc06e
LJ
1085
1086 /*
1087 * uniquely adding inserts the node as the first
1088 * node of the identical-hash-value node chain.
1089 *
1090 * This semantic ensures no duplicated keys
1091 * should ever be observable in the table
1f67ba50
MD
1092 * (including traversing the table node by
1093 * node by forward iterations)
238cc06e 1094 */
04db56f8 1095 cds_lfht_next_duplicate(ht, match, key, &d_iter);
238cc06e
LJ
1096 if (!d_iter.node)
1097 goto insert;
1098
1099 *unique_ret = d_iter;
83beee94 1100 return;
48ed1c18 1101 }
238cc06e 1102
11519af6 1103 /* Only account for identical reverse hash once */
04db56f8 1104 if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash
1ee8f000 1105 && !is_bucket(next))
4105056a 1106 check_resize(ht, size, ++chain_len);
11519af6 1107 iter_prev = clear_flag(iter);
273399de 1108 iter = next;
abc490a1 1109 }
48ed1c18 1110
273399de 1111 insert:
01477510
FD
1112 urcu_posix_assert(node != clear_flag(iter));
1113 urcu_posix_assert(!is_removed(iter_prev));
1114 urcu_posix_assert(!is_removal_owner(iter_prev));
1115 urcu_posix_assert(!is_removed(iter));
1116 urcu_posix_assert(!is_removal_owner(iter));
1117 urcu_posix_assert(iter_prev != node);
1ee8f000 1118 if (!bucket_flag)
04db56f8 1119 node->next = clear_flag(iter);
f9c80341 1120 else
1ee8f000
LJ
1121 node->next = flag_bucket(clear_flag(iter));
1122 if (is_bucket(iter))
1123 new_node = flag_bucket(node);
f5596c94
MD
1124 else
1125 new_node = node;
04db56f8 1126 if (uatomic_cmpxchg(&iter_prev->next, iter,
48ed1c18 1127 new_node) != iter) {
273399de 1128 continue; /* retry */
48ed1c18 1129 } else {
83beee94 1130 return_node = node;
960c9e4f 1131 goto end;
48ed1c18
MD
1132 }
1133
9dba85be 1134 gc_node:
01477510
FD
1135 urcu_posix_assert(!is_removed(iter));
1136 urcu_posix_assert(!is_removal_owner(iter));
1ee8f000
LJ
1137 if (is_bucket(iter))
1138 new_next = flag_bucket(clear_flag(next));
f5596c94
MD
1139 else
1140 new_next = clear_flag(next);
04db56f8 1141 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
273399de 1142 /* retry */
464a1ec9 1143 }
9357c415 1144end:
83beee94
MD
1145 if (unique_ret) {
1146 unique_ret->node = return_node;
1147 /* unique_ret->next left unset, never used. */
1148 }
abc490a1 1149}
464a1ec9 1150
abc490a1 1151static
860d07e8 1152int _cds_lfht_del(struct cds_lfht *ht, unsigned long size,
b65ec430 1153 struct cds_lfht_node *node)
abc490a1 1154{
db00ccc3 1155 struct cds_lfht_node *bucket, *next;
601922a8 1156 struct cds_lfht_node **node_next;
5e28c532 1157
9357c415 1158 if (!node) /* Return -ENOENT if asked to delete NULL node */
743f9143 1159 return -ENOENT;
9357c415 1160
7ec59d3b 1161 /* logically delete the node */
01477510
FD
1162 urcu_posix_assert(!is_bucket(node));
1163 urcu_posix_assert(!is_removed(node));
1164 urcu_posix_assert(!is_removal_owner(node));
48ed1c18 1165
db00ccc3
MD
1166 /*
1167 * We are first checking if the node had previously been
1168 * logically removed (this check is not atomic with setting the
1169 * logical removal flag). Return -ENOENT if the node had
1170 * previously been removed.
1171 */
a85eff52 1172 next = CMM_LOAD_SHARED(node->next); /* next is not dereferenced */
db00ccc3
MD
1173 if (caa_unlikely(is_removed(next)))
1174 return -ENOENT;
01477510 1175 urcu_posix_assert(!is_bucket(next));
196f4fab
MD
1176 /*
1177 * The del operation semantic guarantees a full memory barrier
1178 * before the uatomic_or atomic commit of the deletion flag.
601922a8 1179 *
db00ccc3
MD
1180 * We set the REMOVED_FLAG unconditionally. Note that there may
1181 * be more than one concurrent thread setting this flag.
1182 * Knowing which wins the race will be known after the garbage
1183 * collection phase, stay tuned!
601922a8
OD
1184 *
1185 * NOTE: The node_next variable is present to avoid breaking
1186 * strict-aliasing rules.
db00ccc3 1187 */
601922a8
OD
1188 node_next = &node->next;
1189 uatomic_or_mo(node_next, REMOVED_FLAG, CMM_RELEASE);
1190
7ec59d3b 1191 /* We performed the (logical) deletion. */
7ec59d3b
MD
1192
1193 /*
1194 * Ensure that the node is not visible to readers anymore: lookup for
273399de
MD
1195 * the node, and remove it (along with any other logically removed node)
1196 * if found.
11519af6 1197 */
04db56f8
LJ
1198 bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash));
1199 _cds_lfht_gc_bucket(bucket, node);
743f9143 1200
01477510 1201 urcu_posix_assert(is_removed(CMM_LOAD_SHARED(node->next)));
db00ccc3
MD
1202 /*
1203 * Last phase: atomically exchange node->next with a version
1204 * having "REMOVAL_OWNER_FLAG" set. If the returned node->next
1205 * pointer did _not_ have "REMOVAL_OWNER_FLAG" set, we now own
1206 * the node and win the removal race.
1207 * It is interesting to note that all "add" paths are forbidden
1208 * to change the next pointer starting from the point where the
1209 * REMOVED_FLAG is set, so here using a read, followed by a
1210 * xchg() suffice to guarantee that the xchg() will ever only
1211 * set the "REMOVAL_OWNER_FLAG" (or change nothing if the flag
1212 * was already set).
1213 */
1214 if (!is_removal_owner(uatomic_xchg(&node->next,
601922a8 1215 flag_removal_owner(uatomic_load(&node->next, CMM_RELAXED)))))
db00ccc3
MD
1216 return 0;
1217 else
1218 return -ENOENT;
abc490a1 1219}
2ed95849 1220
b7d619b0
MD
1221static
1222void *partition_resize_thread(void *arg)
1223{
1224 struct partition_resize_work *work = arg;
1225
7b17c13e 1226 work->ht->flavor->register_thread();
b7d619b0 1227 work->fct(work->ht, work->i, work->start, work->len);
7b17c13e 1228 work->ht->flavor->unregister_thread();
b7d619b0
MD
1229 return NULL;
1230}
1231
1232static
1233void partition_resize_helper(struct cds_lfht *ht, unsigned long i,
1234 unsigned long len,
1235 void (*fct)(struct cds_lfht *ht, unsigned long i,
1236 unsigned long start, unsigned long len))
1237{
e54ec2f5 1238 unsigned long partition_len, start = 0;
b7d619b0 1239 struct partition_resize_work *work;
83e334d0
MJ
1240 int ret;
1241 unsigned long thread, nr_threads;
ea3a28a3 1242 sigset_t newmask, oldmask;
b7d619b0 1243
01477510 1244 urcu_posix_assert(nr_cpus_mask != -1);
d7f3ba4c
EW
1245 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD)
1246 goto fallback;
1247
6083a889
MD
1248 /*
1249 * Note: nr_cpus_mask + 1 is always power of 2.
1250 * We spawn just the number of threads we need to satisfy the minimum
1251 * partition size, up to the number of CPUs in the system.
1252 */
91452a6a 1253 if (nr_cpus_mask > 0) {
83e334d0 1254 nr_threads = min_t(unsigned long, nr_cpus_mask + 1,
91452a6a
MD
1255 len >> MIN_PARTITION_PER_THREAD_ORDER);
1256 } else {
1257 nr_threads = 1;
1258 }
5bc6b66f 1259 partition_len = len >> cds_lfht_get_count_order_ulong(nr_threads);
6083a889 1260 work = calloc(nr_threads, sizeof(*work));
7c75d498
EW
1261 if (!work) {
1262 dbg_printf("error allocating for resize, single-threading\n");
1263 goto fallback;
1264 }
ea3a28a3
MD
1265
1266 ret = sigfillset(&newmask);
1267 urcu_posix_assert(!ret);
1268 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
1269 urcu_posix_assert(!ret);
1270
6083a889
MD
1271 for (thread = 0; thread < nr_threads; thread++) {
1272 work[thread].ht = ht;
1273 work[thread].i = i;
1274 work[thread].len = partition_len;
1275 work[thread].start = thread * partition_len;
1276 work[thread].fct = fct;
b047e7a7
MD
1277 ret = pthread_create(&(work[thread].thread_id),
1278 ht->caller_resize_attr ? &ht->resize_attr : NULL,
6083a889 1279 partition_resize_thread, &work[thread]);
e54ec2f5
EW
1280 if (ret == EAGAIN) {
1281 /*
1282 * Out of resources: wait and join the threads
1283 * we've created, then handle leftovers.
1284 */
1285 dbg_printf("error spawning for resize, single-threading\n");
1286 start = work[thread].start;
1287 len -= start;
1288 nr_threads = thread;
1289 break;
1290 }
01477510 1291 urcu_posix_assert(!ret);
b7d619b0 1292 }
ea3a28a3
MD
1293
1294 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
1295 urcu_posix_assert(!ret);
1296
6083a889 1297 for (thread = 0; thread < nr_threads; thread++) {
1af6e26e 1298 ret = pthread_join(work[thread].thread_id, NULL);
01477510 1299 urcu_posix_assert(!ret);
b7d619b0
MD
1300 }
1301 free(work);
e54ec2f5
EW
1302
1303 /*
1304 * A pthread_create failure above will either lead in us having
1305 * no threads to join or starting at a non-zero offset,
1306 * fallback to single thread processing of leftovers.
1307 */
1308 if (start == 0 && nr_threads > 0)
1309 return;
7c75d498 1310fallback:
e54ec2f5 1311 fct(ht, i, start, len);
b7d619b0
MD
1312}
1313
e8de508e
MD
1314/*
1315 * Holding RCU read lock to protect _cds_lfht_add against memory
d0ec0ed2 1316 * reclaim that could be performed by other worker threads (ABA
e8de508e 1317 * problem).
9ee0fc9a 1318 *
b7d619b0 1319 * When we reach a certain length, we can split this population phase over
9ee0fc9a
MD
1320 * many worker threads, based on the number of CPUs available in the system.
1321 * This should therefore take care of not having the expand lagging behind too
1322 * many concurrent insertion threads by using the scheduler's ability to
1ee8f000 1323 * schedule bucket node population fairly with insertions.
e8de508e 1324 */
4105056a 1325static
b7d619b0
MD
1326void init_table_populate_partition(struct cds_lfht *ht, unsigned long i,
1327 unsigned long start, unsigned long len)
4105056a 1328{
9d72a73f 1329 unsigned long j, size = 1UL << (i - 1);
4105056a 1330
01477510 1331 urcu_posix_assert(i > MIN_TABLE_ORDER);
7b17c13e 1332 ht->flavor->read_lock();
9d72a73f
LJ
1333 for (j = size + start; j < size + start + len; j++) {
1334 struct cds_lfht_node *new_node = bucket_at(ht, j);
1335
01477510 1336 urcu_posix_assert(j >= size && j < (size << 1));
9d72a73f
LJ
1337 dbg_printf("init populate: order %lu index %lu hash %lu\n",
1338 i, j, j);
1339 new_node->reverse_hash = bit_reverse_ulong(j);
91a75cc5 1340 _cds_lfht_add(ht, j, NULL, NULL, size, new_node, NULL, 1);
4105056a 1341 }
7b17c13e 1342 ht->flavor->read_unlock();
b7d619b0
MD
1343}
1344
1345static
1346void init_table_populate(struct cds_lfht *ht, unsigned long i,
1347 unsigned long len)
1348{
b7d619b0 1349 partition_resize_helper(ht, i, len, init_table_populate_partition);
4105056a
MD
1350}
1351
abc490a1 1352static
4105056a 1353void init_table(struct cds_lfht *ht,
93d46c39 1354 unsigned long first_order, unsigned long last_order)
24365af7 1355{
93d46c39 1356 unsigned long i;
24365af7 1357
93d46c39
LJ
1358 dbg_printf("init table: first_order %lu last_order %lu\n",
1359 first_order, last_order);
01477510 1360 urcu_posix_assert(first_order > MIN_TABLE_ORDER);
93d46c39 1361 for (i = first_order; i <= last_order; i++) {
4105056a 1362 unsigned long len;
24365af7 1363
4f6e90b7 1364 len = 1UL << (i - 1);
f0c29ed7 1365 dbg_printf("init order %lu len: %lu\n", i, len);
4d676753
MD
1366
1367 /* Stop expand if the resize target changes under us */
7b3893e4 1368 if (CMM_LOAD_SHARED(ht->resize_target) < (1UL << i))
4d676753
MD
1369 break;
1370
48f1b16d 1371 cds_lfht_alloc_bucket_table(ht, i);
4105056a 1372
4105056a 1373 /*
1ee8f000
LJ
1374 * Set all bucket nodes reverse hash values for a level and
1375 * link all bucket nodes into the table.
4105056a 1376 */
dc1da8f6 1377 init_table_populate(ht, i, len);
4105056a 1378
f9c80341
MD
1379 /*
1380 * Update table size.
601922a8
OD
1381 *
1382 * Populate data before RCU size.
f9c80341 1383 */
601922a8 1384 uatomic_store(&ht->size, 1UL << i, CMM_RELEASE);
f9c80341 1385
4f6e90b7 1386 dbg_printf("init new size: %lu\n", 1UL << i);
4105056a
MD
1387 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1388 break;
1389 }
1390}
1391
e8de508e
MD
1392/*
1393 * Holding RCU read lock to protect _cds_lfht_remove against memory
d0ec0ed2 1394 * reclaim that could be performed by other worker threads (ABA
e8de508e
MD
1395 * problem).
1396 * For a single level, we logically remove and garbage collect each node.
1397 *
1398 * As a design choice, we perform logical removal and garbage collection on a
1399 * node-per-node basis to simplify this algorithm. We also assume keeping good
1400 * cache locality of the operation would overweight possible performance gain
1401 * that could be achieved by batching garbage collection for multiple levels.
1402 * However, this would have to be justified by benchmarks.
1403 *
1404 * Concurrent removal and add operations are helping us perform garbage
1405 * collection of logically removed nodes. We guarantee that all logically
d0ec0ed2
MD
1406 * removed nodes have been garbage-collected (unlinked) before work
1407 * enqueue is invoked to free a hole level of bucket nodes (after a
1408 * grace period).
e8de508e 1409 *
1f67ba50
MD
1410 * Logical removal and garbage collection can therefore be done in batch
1411 * or on a node-per-node basis, as long as the guarantee above holds.
9ee0fc9a 1412 *
b7d619b0
MD
1413 * When we reach a certain length, we can split this removal over many worker
1414 * threads, based on the number of CPUs available in the system. This should
1415 * take care of not letting resize process lag behind too many concurrent
9ee0fc9a 1416 * updater threads actively inserting into the hash table.
e8de508e 1417 */
4105056a 1418static
b7d619b0
MD
1419void remove_table_partition(struct cds_lfht *ht, unsigned long i,
1420 unsigned long start, unsigned long len)
4105056a 1421{
9d72a73f 1422 unsigned long j, size = 1UL << (i - 1);
4105056a 1423
01477510 1424 urcu_posix_assert(i > MIN_TABLE_ORDER);
7b17c13e 1425 ht->flavor->read_lock();
9d72a73f 1426 for (j = size + start; j < size + start + len; j++) {
2e2ce1e9
LJ
1427 struct cds_lfht_node *fini_bucket = bucket_at(ht, j);
1428 struct cds_lfht_node *parent_bucket = bucket_at(ht, j - size);
601922a8 1429 struct cds_lfht_node **fini_bucket_next;
9d72a73f 1430
01477510 1431 urcu_posix_assert(j >= size && j < (size << 1));
9d72a73f
LJ
1432 dbg_printf("remove entry: order %lu index %lu hash %lu\n",
1433 i, j, j);
601922a8
OD
1434 /* Set the REMOVED_FLAG to freeze the ->next for gc.
1435 *
1436 * NOTE: The fini_bucket_next variable is present to
1437 * avoid breaking strict-aliasing rules.
1438 */
1439 fini_bucket_next = &fini_bucket->next;
1440 uatomic_or(fini_bucket_next, REMOVED_FLAG);
2e2ce1e9 1441 _cds_lfht_gc_bucket(parent_bucket, fini_bucket);
abc490a1 1442 }
7b17c13e 1443 ht->flavor->read_unlock();
b7d619b0
MD
1444}
1445
1446static
1447void remove_table(struct cds_lfht *ht, unsigned long i, unsigned long len)
1448{
b7d619b0 1449 partition_resize_helper(ht, i, len, remove_table_partition);
2ed95849
MD
1450}
1451
61adb337
MD
1452/*
1453 * fini_table() is never called for first_order == 0, which is why
1454 * free_by_rcu_order == 0 can be used as criterion to know if free must
1455 * be called.
1456 */
1475579c 1457static
4105056a 1458void fini_table(struct cds_lfht *ht,
93d46c39 1459 unsigned long first_order, unsigned long last_order)
1475579c 1460{
83e334d0 1461 unsigned long free_by_rcu_order = 0, i;
1475579c 1462
93d46c39
LJ
1463 dbg_printf("fini table: first_order %lu last_order %lu\n",
1464 first_order, last_order);
01477510 1465 urcu_posix_assert(first_order > MIN_TABLE_ORDER);
93d46c39 1466 for (i = last_order; i >= first_order; i--) {
4105056a 1467 unsigned long len;
1475579c 1468
4f6e90b7 1469 len = 1UL << (i - 1);
e15df1cc 1470 dbg_printf("fini order %ld len: %lu\n", i, len);
4105056a 1471
4d676753 1472 /* Stop shrink if the resize target changes under us */
7b3893e4 1473 if (CMM_LOAD_SHARED(ht->resize_target) > (1UL << (i - 1)))
4d676753
MD
1474 break;
1475
1476 cmm_smp_wmb(); /* populate data before RCU size */
7b3893e4 1477 CMM_STORE_SHARED(ht->size, 1UL << (i - 1));
4d676753
MD
1478
1479 /*
1480 * We need to wait for all add operations to reach Q.S. (and
1481 * thus use the new table for lookups) before we can start
1ee8f000 1482 * releasing the old bucket nodes. Otherwise their lookup will
4d676753
MD
1483 * return a logically removed node as insert position.
1484 */
7b17c13e 1485 ht->flavor->update_synchronize_rcu();
48f1b16d
LJ
1486 if (free_by_rcu_order)
1487 cds_lfht_free_bucket_table(ht, free_by_rcu_order);
4d676753 1488
21263e21 1489 /*
1ee8f000
LJ
1490 * Set "removed" flag in bucket nodes about to be removed.
1491 * Unlink all now-logically-removed bucket node pointers.
4105056a
MD
1492 * Concurrent add/remove operation are helping us doing
1493 * the gc.
21263e21 1494 */
4105056a
MD
1495 remove_table(ht, i, len);
1496
48f1b16d 1497 free_by_rcu_order = i;
4105056a
MD
1498
1499 dbg_printf("fini new size: %lu\n", 1UL << i);
1475579c
MD
1500 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1501 break;
1502 }
0d14ceb2 1503
48f1b16d 1504 if (free_by_rcu_order) {
7b17c13e 1505 ht->flavor->update_synchronize_rcu();
48f1b16d 1506 cds_lfht_free_bucket_table(ht, free_by_rcu_order);
0d14ceb2 1507 }
1475579c
MD
1508}
1509
83e334d0
MJ
1510/*
1511 * Never called with size < 1.
1512 */
ff0d69de 1513static
1ee8f000 1514void cds_lfht_create_bucket(struct cds_lfht *ht, unsigned long size)
ff0d69de 1515{
04db56f8 1516 struct cds_lfht_node *prev, *node;
9d72a73f 1517 unsigned long order, len, i;
83e334d0 1518 int bucket_order;
ff0d69de 1519
48f1b16d 1520 cds_lfht_alloc_bucket_table(ht, 0);
ff0d69de 1521
9d72a73f
LJ
1522 dbg_printf("create bucket: order 0 index 0 hash 0\n");
1523 node = bucket_at(ht, 0);
1524 node->next = flag_bucket(get_end());
1525 node->reverse_hash = 0;
ff0d69de 1526
83e334d0 1527 bucket_order = cds_lfht_get_count_order_ulong(size);
01477510 1528 urcu_posix_assert(bucket_order >= 0);
83e334d0
MJ
1529
1530 for (order = 1; order < (unsigned long) bucket_order + 1; order++) {
ff0d69de 1531 len = 1UL << (order - 1);
48f1b16d 1532 cds_lfht_alloc_bucket_table(ht, order);
ff0d69de 1533
9d72a73f
LJ
1534 for (i = 0; i < len; i++) {
1535 /*
1536 * Now, we are trying to init the node with the
1537 * hash=(len+i) (which is also a bucket with the
1538 * index=(len+i)) and insert it into the hash table,
1539 * so this node has to be inserted after the bucket
1540 * with the index=(len+i)&(len-1)=i. And because there
1541 * is no other non-bucket node nor bucket node with
1542 * larger index/hash inserted, so the bucket node
1543 * being inserted should be inserted directly linked
1544 * after the bucket node with index=i.
1545 */
1546 prev = bucket_at(ht, i);
1547 node = bucket_at(ht, len + i);
ff0d69de 1548
1ee8f000 1549 dbg_printf("create bucket: order %lu index %lu hash %lu\n",
9d72a73f
LJ
1550 order, len + i, len + i);
1551 node->reverse_hash = bit_reverse_ulong(len + i);
1552
1553 /* insert after prev */
01477510 1554 urcu_posix_assert(is_bucket(prev->next));
ff0d69de 1555 node->next = prev->next;
1ee8f000 1556 prev->next = flag_bucket(node);
ff0d69de
LJ
1557 }
1558 }
1559}
1560
99ab1528
MJ
1561#if (CAA_BITS_PER_LONG > 32)
1562/*
1563 * For 64-bit architectures, with max number of buckets small enough not to
1564 * use the entire 64-bit memory mapping space (and allowing a fair number of
1565 * hash table instances), use the mmap allocator, which is faster. Otherwise,
1566 * fallback to the order allocator.
1567 */
1568static
1569const struct cds_lfht_mm_type *get_mm_type(unsigned long max_nr_buckets)
1570{
1571 if (max_nr_buckets && max_nr_buckets <= (1ULL << 32))
1572 return &cds_lfht_mm_mmap;
1573 else
1574 return &cds_lfht_mm_order;
1575}
1576#else
1577/*
1578 * For 32-bit architectures, use the order allocator.
1579 */
1580static
70469b43
MJ
1581const struct cds_lfht_mm_type *get_mm_type(
1582 unsigned long max_nr_buckets __attribute__((unused)))
99ab1528
MJ
1583{
1584 return &cds_lfht_mm_order;
1585}
1586#endif
1587
4c10e9af
MD
1588void cds_lfht_node_init_deleted(struct cds_lfht_node *node)
1589{
1590 cds_lfht_node_init(node);
1591 node->next = flag_removed(NULL);
1592}
1593
0422d92c 1594struct cds_lfht *_cds_lfht_new(unsigned long init_size,
0722081a 1595 unsigned long min_nr_alloc_buckets,
747d725c 1596 unsigned long max_nr_buckets,
b8af5011 1597 int flags,
0b6aa001 1598 const struct cds_lfht_mm_type *mm,
7b17c13e 1599 const struct rcu_flavor_struct *flavor,
b7d619b0 1600 pthread_attr_t *attr)
abc490a1 1601{
14044b37 1602 struct cds_lfht *ht;
24365af7 1603 unsigned long order;
abc490a1 1604
0722081a
LJ
1605 /* min_nr_alloc_buckets must be power of two */
1606 if (!min_nr_alloc_buckets || (min_nr_alloc_buckets & (min_nr_alloc_buckets - 1)))
5488222b 1607 return NULL;
747d725c 1608
8129be4e 1609 /* init_size must be power of two */
5488222b 1610 if (!init_size || (init_size & (init_size - 1)))
8129be4e 1611 return NULL;
747d725c 1612
c1888f3a
MD
1613 /*
1614 * Memory management plugin default.
1615 */
99ab1528
MJ
1616 if (!mm)
1617 mm = get_mm_type(max_nr_buckets);
c1888f3a 1618
0b6aa001
LJ
1619 /* max_nr_buckets == 0 for order based mm means infinite */
1620 if (mm == &cds_lfht_mm_order && !max_nr_buckets)
747d725c
LJ
1621 max_nr_buckets = 1UL << (MAX_TABLE_ORDER - 1);
1622
1623 /* max_nr_buckets must be power of two */
1624 if (!max_nr_buckets || (max_nr_buckets & (max_nr_buckets - 1)))
1625 return NULL;
1626
d0ec0ed2
MD
1627 if (flags & CDS_LFHT_AUTO_RESIZE)
1628 cds_lfht_init_worker(flavor);
1629
0722081a 1630 min_nr_alloc_buckets = max(min_nr_alloc_buckets, MIN_TABLE_SIZE);
d0d8f9aa 1631 init_size = max(init_size, MIN_TABLE_SIZE);
747d725c
LJ
1632 max_nr_buckets = max(max_nr_buckets, min_nr_alloc_buckets);
1633 init_size = min(init_size, max_nr_buckets);
0b6aa001
LJ
1634
1635 ht = mm->alloc_cds_lfht(min_nr_alloc_buckets, max_nr_buckets);
01477510
FD
1636 urcu_posix_assert(ht);
1637 urcu_posix_assert(ht->mm == mm);
1638 urcu_posix_assert(ht->bucket_at == mm->bucket_at);
0b6aa001 1639
b5d6b20f 1640 ht->flags = flags;
7b17c13e 1641 ht->flavor = flavor;
b047e7a7
MD
1642 ht->caller_resize_attr = attr;
1643 if (attr)
1644 ht->resize_attr = *attr;
5afadd12 1645 alloc_split_items_count(ht);
abc490a1
MD
1646 /* this mutex should not nest in read-side C.S. */
1647 pthread_mutex_init(&ht->resize_mutex, NULL);
5bc6b66f 1648 order = cds_lfht_get_count_order_ulong(init_size);
7b3893e4 1649 ht->resize_target = 1UL << order;
1ee8f000 1650 cds_lfht_create_bucket(ht, 1UL << order);
7b3893e4 1651 ht->size = 1UL << order;
abc490a1
MD
1652 return ht;
1653}
1654
6f554439 1655void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
996ff57c 1656 cds_lfht_match_fct match, const void *key,
6f554439 1657 struct cds_lfht_iter *iter)
2ed95849 1658{
04db56f8 1659 struct cds_lfht_node *node, *next, *bucket;
0422d92c 1660 unsigned long reverse_hash, size;
2ed95849 1661
d7c76f85
MD
1662 cds_lfht_iter_debug_set_ht(ht, iter);
1663
abc490a1 1664 reverse_hash = bit_reverse_ulong(hash);
464a1ec9 1665
601922a8
OD
1666 /*
1667 * Use load acquire instead of rcu_dereference because there is no
1668 * dependency between the table size and the dereference of the bucket
1669 * content.
1670 *
1671 * This acquire is paired with the store release in init_table().
1672 */
1673 size = uatomic_load(&ht->size, CMM_ACQUIRE);
04db56f8 1674 bucket = lookup_bucket(ht, size, hash);
1ee8f000 1675 /* We can always skip the bucket node initially */
04db56f8 1676 node = rcu_dereference(bucket->next);
bb7b2f26 1677 node = clear_flag(node);
2ed95849 1678 for (;;) {
8ed51e04 1679 if (caa_unlikely(is_end(node))) {
96ad1112 1680 node = next = NULL;
abc490a1 1681 break;
bb7b2f26 1682 }
04db56f8 1683 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
96ad1112 1684 node = next = NULL;
abc490a1 1685 break;
2ed95849 1686 }
04db56f8 1687 next = rcu_dereference(node->next);
01477510 1688 urcu_posix_assert(node == clear_flag(node));
8ed51e04 1689 if (caa_likely(!is_removed(next))
1ee8f000 1690 && !is_bucket(next)
04db56f8 1691 && node->reverse_hash == reverse_hash
0422d92c 1692 && caa_likely(match(node, key))) {
273399de 1693 break;
2ed95849 1694 }
1b81fe1a 1695 node = clear_flag(next);
2ed95849 1696 }
01477510 1697 urcu_posix_assert(!node || !is_bucket(CMM_LOAD_SHARED(node->next)));
adc0de68
MD
1698 iter->node = node;
1699 iter->next = next;
abc490a1 1700}
e0ba718a 1701
70469b43
MJ
1702void cds_lfht_next_duplicate(struct cds_lfht *ht __attribute__((unused)),
1703 cds_lfht_match_fct match,
996ff57c 1704 const void *key, struct cds_lfht_iter *iter)
a481e5ff 1705{
adc0de68 1706 struct cds_lfht_node *node, *next;
a481e5ff 1707 unsigned long reverse_hash;
a481e5ff 1708
d7c76f85 1709 cds_lfht_iter_debug_assert(ht == iter->lfht);
adc0de68 1710 node = iter->node;
04db56f8 1711 reverse_hash = node->reverse_hash;
adc0de68 1712 next = iter->next;
a481e5ff
MD
1713 node = clear_flag(next);
1714
1715 for (;;) {
8ed51e04 1716 if (caa_unlikely(is_end(node))) {
96ad1112 1717 node = next = NULL;
a481e5ff 1718 break;
bb7b2f26 1719 }
04db56f8 1720 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
96ad1112 1721 node = next = NULL;
a481e5ff
MD
1722 break;
1723 }
04db56f8 1724 next = rcu_dereference(node->next);
8ed51e04 1725 if (caa_likely(!is_removed(next))
1ee8f000 1726 && !is_bucket(next)
04db56f8 1727 && caa_likely(match(node, key))) {
a481e5ff
MD
1728 break;
1729 }
1730 node = clear_flag(next);
1731 }
601922a8 1732 urcu_posix_assert(!node || !is_bucket(uatomic_load(&node->next, CMM_RELAXED)));
adc0de68
MD
1733 iter->node = node;
1734 iter->next = next;
a481e5ff
MD
1735}
1736
70469b43
MJ
1737void cds_lfht_next(struct cds_lfht *ht __attribute__((unused)),
1738 struct cds_lfht_iter *iter)
4e9b9fbf
MD
1739{
1740 struct cds_lfht_node *node, *next;
1741
d7c76f85 1742 cds_lfht_iter_debug_assert(ht == iter->lfht);
853395e1 1743 node = clear_flag(iter->next);
4e9b9fbf 1744 for (;;) {
8ed51e04 1745 if (caa_unlikely(is_end(node))) {
4e9b9fbf
MD
1746 node = next = NULL;
1747 break;
1748 }
04db56f8 1749 next = rcu_dereference(node->next);
8ed51e04 1750 if (caa_likely(!is_removed(next))
1ee8f000 1751 && !is_bucket(next)) {
4e9b9fbf
MD
1752 break;
1753 }
1754 node = clear_flag(next);
1755 }
601922a8 1756 urcu_posix_assert(!node || !is_bucket(uatomic_load(&node->next, CMM_RELAXED)));
4e9b9fbf
MD
1757 iter->node = node;
1758 iter->next = next;
1759}
1760
1761void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1762{
d7c76f85 1763 cds_lfht_iter_debug_set_ht(ht, iter);
4e9b9fbf 1764 /*
1ee8f000 1765 * Get next after first bucket node. The first bucket node is the
4e9b9fbf
MD
1766 * first node of the linked list.
1767 */
601922a8 1768 iter->next = uatomic_load(&bucket_at(ht, 0)->next, CMM_CONSUME);
4e9b9fbf
MD
1769 cds_lfht_next(ht, iter);
1770}
1771
0422d92c
MD
1772void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
1773 struct cds_lfht_node *node)
abc490a1 1774{
0422d92c 1775 unsigned long size;
ab7d5fc6 1776
709bacf9 1777 node->reverse_hash = bit_reverse_ulong(hash);
601922a8 1778 size = uatomic_load(&ht->size, CMM_ACQUIRE);
91a75cc5 1779 _cds_lfht_add(ht, hash, NULL, NULL, size, node, NULL, 0);
14360f1c 1780 ht_count_add(ht, size, hash);
3eca1b8c
MD
1781}
1782
14044b37 1783struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
6f554439 1784 unsigned long hash,
0422d92c 1785 cds_lfht_match_fct match,
996ff57c 1786 const void *key,
48ed1c18 1787 struct cds_lfht_node *node)
3eca1b8c 1788{
0422d92c 1789 unsigned long size;
83beee94 1790 struct cds_lfht_iter iter;
3eca1b8c 1791
709bacf9 1792 node->reverse_hash = bit_reverse_ulong(hash);
601922a8 1793 size = uatomic_load(&ht->size, CMM_ACQUIRE);
91a75cc5 1794 _cds_lfht_add(ht, hash, match, key, size, node, &iter, 0);
83beee94 1795 if (iter.node == node)
14360f1c 1796 ht_count_add(ht, size, hash);
83beee94 1797 return iter.node;
2ed95849
MD
1798}
1799
9357c415 1800struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
6f554439 1801 unsigned long hash,
0422d92c 1802 cds_lfht_match_fct match,
996ff57c 1803 const void *key,
48ed1c18
MD
1804 struct cds_lfht_node *node)
1805{
0422d92c 1806 unsigned long size;
83beee94 1807 struct cds_lfht_iter iter;
48ed1c18 1808
709bacf9 1809 node->reverse_hash = bit_reverse_ulong(hash);
601922a8 1810 size = uatomic_load(&ht->size, CMM_ACQUIRE);
83beee94 1811 for (;;) {
91a75cc5 1812 _cds_lfht_add(ht, hash, match, key, size, node, &iter, 0);
83beee94 1813 if (iter.node == node) {
14360f1c 1814 ht_count_add(ht, size, hash);
83beee94
MD
1815 return NULL;
1816 }
1817
1818 if (!_cds_lfht_replace(ht, size, iter.node, iter.next, node))
1819 return iter.node;
1820 }
48ed1c18
MD
1821}
1822
2e79c445
MD
1823int cds_lfht_replace(struct cds_lfht *ht,
1824 struct cds_lfht_iter *old_iter,
1825 unsigned long hash,
1826 cds_lfht_match_fct match,
1827 const void *key,
9357c415
MD
1828 struct cds_lfht_node *new_node)
1829{
1830 unsigned long size;
1831
709bacf9 1832 new_node->reverse_hash = bit_reverse_ulong(hash);
2e79c445
MD
1833 if (!old_iter->node)
1834 return -ENOENT;
1835 if (caa_unlikely(old_iter->node->reverse_hash != new_node->reverse_hash))
1836 return -EINVAL;
1837 if (caa_unlikely(!match(old_iter->node, key)))
1838 return -EINVAL;
601922a8 1839 size = uatomic_load(&ht->size, CMM_ACQUIRE);
9357c415
MD
1840 return _cds_lfht_replace(ht, size, old_iter->node, old_iter->next,
1841 new_node);
1842}
1843
bc8c3c74 1844int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node)
2ed95849 1845{
95bc7fb9 1846 unsigned long size;
df44348d 1847 int ret;
abc490a1 1848
601922a8 1849 size = uatomic_load(&ht->size, CMM_ACQUIRE);
bc8c3c74 1850 ret = _cds_lfht_del(ht, size, node);
14360f1c 1851 if (!ret) {
95bc7fb9
MD
1852 unsigned long hash;
1853
bc8c3c74 1854 hash = bit_reverse_ulong(node->reverse_hash);
14360f1c
LJ
1855 ht_count_del(ht, size, hash);
1856 }
df44348d 1857 return ret;
2ed95849 1858}
ab7d5fc6 1859
afa5940d 1860int cds_lfht_is_node_deleted(const struct cds_lfht_node *node)
df55172a 1861{
a85eff52 1862 return is_removed(CMM_LOAD_SHARED(node->next));
df55172a
MD
1863}
1864
b047e7a7
MD
1865static
1866bool cds_lfht_is_empty(struct cds_lfht *ht)
1867{
1868 struct cds_lfht_node *node, *next;
1869 bool empty = true;
1870 bool was_online;
1871
1872 was_online = ht->flavor->read_ongoing();
1873 if (!was_online) {
1874 ht->flavor->thread_online();
1875 ht->flavor->read_lock();
1876 }
1877 /* Check that the table is empty */
1878 node = bucket_at(ht, 0);
1879 do {
1880 next = rcu_dereference(node->next);
1881 if (!is_bucket(next)) {
1882 empty = false;
1883 break;
1884 }
1885 node = clear_flag(next);
1886 } while (!is_end(node));
1887 if (!was_online) {
1888 ht->flavor->read_unlock();
1889 ht->flavor->thread_offline();
1890 }
1891 return empty;
1892}
1893
abc490a1 1894static
1ee8f000 1895int cds_lfht_delete_bucket(struct cds_lfht *ht)
674f7a69 1896{
14044b37 1897 struct cds_lfht_node *node;
4105056a 1898 unsigned long order, i, size;
674f7a69 1899
abc490a1 1900 /* Check that the table is empty */
9d72a73f 1901 node = bucket_at(ht, 0);
abc490a1 1902 do {
04db56f8 1903 node = clear_flag(node)->next;
1ee8f000 1904 if (!is_bucket(node))
abc490a1 1905 return -EPERM;
01477510
FD
1906 urcu_posix_assert(!is_removed(node));
1907 urcu_posix_assert(!is_removal_owner(node));
bb7b2f26 1908 } while (!is_end(node));
4105056a
MD
1909 /*
1910 * size accessed without rcu_dereference because hash table is
1911 * being destroyed.
1912 */
7b3893e4 1913 size = ht->size;
1f67ba50 1914 /* Internal sanity check: all nodes left should be buckets */
48f1b16d
LJ
1915 for (i = 0; i < size; i++) {
1916 node = bucket_at(ht, i);
1917 dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n",
1918 i, i, bit_reverse_ulong(node->reverse_hash));
01477510 1919 urcu_posix_assert(is_bucket(node->next));
48f1b16d 1920 }
24365af7 1921
5bc6b66f 1922 for (order = cds_lfht_get_count_order_ulong(size); (long)order >= 0; order--)
48f1b16d 1923 cds_lfht_free_bucket_table(ht, order);
5488222b 1924
abc490a1 1925 return 0;
674f7a69
MD
1926}
1927
b047e7a7
MD
1928static
1929void do_auto_resize_destroy_cb(struct urcu_work *work)
1930{
1931 struct cds_lfht *ht = caa_container_of(work, struct cds_lfht, destroy_work);
1932 int ret;
1933
1934 ht->flavor->register_thread();
1935 ret = cds_lfht_delete_bucket(ht);
1936 if (ret)
a51018da 1937 urcu_die(-ret);
b047e7a7
MD
1938 free_split_items_count(ht);
1939 ret = pthread_mutex_destroy(&ht->resize_mutex);
1940 if (ret)
1941 urcu_die(ret);
1942 ht->flavor->unregister_thread();
1943 poison_free(ht);
1944}
1945
674f7a69
MD
1946/*
1947 * Should only be called when no more concurrent readers nor writers can
1948 * possibly access the table.
1949 */
b7d619b0 1950int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr)
674f7a69 1951{
d0ec0ed2
MD
1952 int ret;
1953
1954 if (ht->flags & CDS_LFHT_AUTO_RESIZE) {
b047e7a7
MD
1955 /*
1956 * Perform error-checking for emptiness before queuing
1957 * work, so we can return error to the caller. This runs
1958 * concurrently with ongoing resize.
1959 */
1960 if (!cds_lfht_is_empty(ht))
1961 return -EPERM;
d0ec0ed2 1962 /* Cancel ongoing resize operations. */
601922a8 1963 uatomic_store(&ht->in_progress_destroy, 1, CMM_RELAXED);
b047e7a7
MD
1964 if (attr) {
1965 *attr = ht->caller_resize_attr;
1966 ht->caller_resize_attr = NULL;
1967 }
1968 /*
1969 * Queue destroy work after prior queued resize
1970 * operations. Given there are no concurrent writers
1971 * accessing the hash table at this point, no resize
1972 * operations can be queued after this destroy work.
1973 */
1974 urcu_workqueue_queue_work(cds_lfht_workqueue,
1975 &ht->destroy_work, do_auto_resize_destroy_cb);
1976 return 0;
10e68472 1977 }
1ee8f000 1978 ret = cds_lfht_delete_bucket(ht);
abc490a1
MD
1979 if (ret)
1980 return ret;
5afadd12 1981 free_split_items_count(ht);
b7d619b0 1982 if (attr)
b047e7a7 1983 *attr = ht->caller_resize_attr;
59629f09
MD
1984 ret = pthread_mutex_destroy(&ht->resize_mutex);
1985 if (ret)
1986 ret = -EBUSY;
98808fb1 1987 poison_free(ht);
5e28c532 1988 return ret;
674f7a69
MD
1989}
1990
14044b37 1991void cds_lfht_count_nodes(struct cds_lfht *ht,
d933dd0e 1992 long *approx_before,
273399de 1993 unsigned long *count,
d933dd0e 1994 long *approx_after)
273399de 1995{
14044b37 1996 struct cds_lfht_node *node, *next;
caf3653d 1997 unsigned long nr_bucket = 0, nr_removed = 0;
273399de 1998
7ed7682f 1999 *approx_before = 0;
5afadd12 2000 if (ht->split_count) {
973e5e1b
MD
2001 int i;
2002
4c42f1b8
LJ
2003 for (i = 0; i < split_count_mask + 1; i++) {
2004 *approx_before += uatomic_read(&ht->split_count[i].add);
2005 *approx_before -= uatomic_read(&ht->split_count[i].del);
973e5e1b
MD
2006 }
2007 }
2008
273399de 2009 *count = 0;
273399de 2010
1ee8f000 2011 /* Count non-bucket nodes in the table */
9d72a73f 2012 node = bucket_at(ht, 0);
273399de 2013 do {
04db56f8 2014 next = rcu_dereference(node->next);
b198f0fd 2015 if (is_removed(next)) {
1ee8f000 2016 if (!is_bucket(next))
caf3653d 2017 (nr_removed)++;
973e5e1b 2018 else
1ee8f000
LJ
2019 (nr_bucket)++;
2020 } else if (!is_bucket(next))
273399de 2021 (*count)++;
24365af7 2022 else
1ee8f000 2023 (nr_bucket)++;
273399de 2024 node = clear_flag(next);
bb7b2f26 2025 } while (!is_end(node));
caf3653d 2026 dbg_printf("number of logically removed nodes: %lu\n", nr_removed);
1ee8f000 2027 dbg_printf("number of bucket nodes: %lu\n", nr_bucket);
7ed7682f 2028 *approx_after = 0;
5afadd12 2029 if (ht->split_count) {
973e5e1b
MD
2030 int i;
2031
4c42f1b8
LJ
2032 for (i = 0; i < split_count_mask + 1; i++) {
2033 *approx_after += uatomic_read(&ht->split_count[i].add);
2034 *approx_after -= uatomic_read(&ht->split_count[i].del);
973e5e1b
MD
2035 }
2036 }
273399de
MD
2037}
2038
1475579c 2039/* called with resize mutex held */
abc490a1 2040static
4105056a 2041void _do_cds_lfht_grow(struct cds_lfht *ht,
1475579c 2042 unsigned long old_size, unsigned long new_size)
abc490a1 2043{
1475579c 2044 unsigned long old_order, new_order;
1475579c 2045
5bc6b66f
MD
2046 old_order = cds_lfht_get_count_order_ulong(old_size);
2047 new_order = cds_lfht_get_count_order_ulong(new_size);
1a401918
LJ
2048 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
2049 old_size, old_order, new_size, new_order);
01477510 2050 urcu_posix_assert(new_size > old_size);
93d46c39 2051 init_table(ht, old_order + 1, new_order);
abc490a1
MD
2052}
2053
2054/* called with resize mutex held */
2055static
4105056a 2056void _do_cds_lfht_shrink(struct cds_lfht *ht,
1475579c 2057 unsigned long old_size, unsigned long new_size)
464a1ec9 2058{
1475579c 2059 unsigned long old_order, new_order;
464a1ec9 2060
d0d8f9aa 2061 new_size = max(new_size, MIN_TABLE_SIZE);
5bc6b66f
MD
2062 old_order = cds_lfht_get_count_order_ulong(old_size);
2063 new_order = cds_lfht_get_count_order_ulong(new_size);
1a401918
LJ
2064 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
2065 old_size, old_order, new_size, new_order);
01477510 2066 urcu_posix_assert(new_size < old_size);
1475579c 2067
1ee8f000 2068 /* Remove and unlink all bucket nodes to remove. */
93d46c39 2069 fini_table(ht, new_order + 1, old_order);
464a1ec9
MD
2070}
2071
1475579c
MD
2072
2073/* called with resize mutex held */
2074static
2075void _do_cds_lfht_resize(struct cds_lfht *ht)
2076{
2077 unsigned long new_size, old_size;
4105056a
MD
2078
2079 /*
2080 * Resize table, re-do if the target size has changed under us.
2081 */
2082 do {
601922a8 2083 if (uatomic_load(&ht->in_progress_destroy, CMM_RELAXED))
d2be3620 2084 break;
601922a8
OD
2085
2086 uatomic_store(&ht->resize_initiated, 1, CMM_RELAXED);
2087
7b3893e4 2088 old_size = ht->size;
601922a8 2089 new_size = uatomic_load(&ht->resize_target, CMM_RELAXED);
4105056a
MD
2090 if (old_size < new_size)
2091 _do_cds_lfht_grow(ht, old_size, new_size);
2092 else if (old_size > new_size)
2093 _do_cds_lfht_shrink(ht, old_size, new_size);
601922a8
OD
2094
2095 uatomic_store(&ht->resize_initiated, 0, CMM_RELAXED);
4105056a
MD
2096 /* write resize_initiated before read resize_target */
2097 cmm_smp_mb();
601922a8 2098 } while (ht->size != uatomic_load(&ht->resize_target, CMM_RELAXED));
1475579c
MD
2099}
2100
abc490a1 2101static
ab65b890 2102unsigned long resize_target_grow(struct cds_lfht *ht, unsigned long new_size)
464a1ec9 2103{
7b3893e4 2104 return _uatomic_xchg_monotonic_increase(&ht->resize_target, new_size);
464a1ec9
MD
2105}
2106
1475579c 2107static
4105056a 2108void resize_target_update_count(struct cds_lfht *ht,
b8af5011 2109 unsigned long count)
1475579c 2110{
d0d8f9aa 2111 count = max(count, MIN_TABLE_SIZE);
747d725c 2112 count = min(count, ht->max_nr_buckets);
7b3893e4 2113 uatomic_set(&ht->resize_target, count);
1475579c
MD
2114}
2115
2116void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size)
464a1ec9 2117{
10e68472 2118 resize_target_update_count(ht, new_size);
601922a8
OD
2119
2120 /*
2121 * Set flags has early as possible even in contention case.
2122 */
2123 uatomic_store(&ht->resize_initiated, 1, CMM_RELAXED);
2124
5ffcaeef 2125 mutex_lock(&ht->resize_mutex);
1475579c 2126 _do_cds_lfht_resize(ht);
5ffcaeef 2127 mutex_unlock(&ht->resize_mutex);
abc490a1 2128}
464a1ec9 2129
abc490a1 2130static
d0ec0ed2 2131void do_resize_cb(struct urcu_work *work)
abc490a1 2132{
d0ec0ed2
MD
2133 struct resize_work *resize_work =
2134 caa_container_of(work, struct resize_work, work);
2135 struct cds_lfht *ht = resize_work->ht;
abc490a1 2136
d0ec0ed2 2137 ht->flavor->register_thread();
5ffcaeef 2138 mutex_lock(&ht->resize_mutex);
14044b37 2139 _do_cds_lfht_resize(ht);
5ffcaeef 2140 mutex_unlock(&ht->resize_mutex);
d0ec0ed2 2141 ht->flavor->unregister_thread();
98808fb1 2142 poison_free(work);
464a1ec9
MD
2143}
2144
abc490a1 2145static
f1f119ee 2146void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht)
ab7d5fc6 2147{
d0ec0ed2 2148 struct resize_work *work;
abc490a1 2149
601922a8
OD
2150 /*
2151 * Store to resize_target is before read resize_initiated as guaranteed
2152 * by either cmpxchg or _uatomic_xchg_monotonic_increase.
2153 */
2154 if (!uatomic_load(&ht->resize_initiated, CMM_RELAXED)) {
2155 if (uatomic_load(&ht->in_progress_destroy, CMM_RELAXED)) {
59290e9d 2156 return;
ed35e6d8 2157 }
f9830efd 2158 work = malloc(sizeof(*work));
741f378e
MD
2159 if (work == NULL) {
2160 dbg_printf("error allocating resize work, bailing out\n");
741f378e
MD
2161 return;
2162 }
f9830efd 2163 work->ht = ht;
d0ec0ed2
MD
2164 urcu_workqueue_queue_work(cds_lfht_workqueue,
2165 &work->work, do_resize_cb);
601922a8 2166 uatomic_store(&ht->resize_initiated, 1, CMM_RELAXED);
f9830efd 2167 }
ab7d5fc6 2168}
3171717f 2169
f1f119ee
LJ
2170static
2171void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth)
2172{
2173 unsigned long target_size = size << growth;
2174
747d725c 2175 target_size = min(target_size, ht->max_nr_buckets);
f1f119ee
LJ
2176 if (resize_target_grow(ht, target_size) >= target_size)
2177 return;
2178
2179 __cds_lfht_resize_lazy_launch(ht);
2180}
2181
89bb121d
LJ
2182/*
2183 * We favor grow operations over shrink. A shrink operation never occurs
2184 * if a grow operation is queued for lazy execution. A grow operation
2185 * cancels any pending shrink lazy execution.
2186 */
3171717f 2187static
4105056a 2188void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
3171717f
MD
2189 unsigned long count)
2190{
b8af5011
MD
2191 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
2192 return;
d0d8f9aa 2193 count = max(count, MIN_TABLE_SIZE);
747d725c 2194 count = min(count, ht->max_nr_buckets);
89bb121d
LJ
2195 if (count == size)
2196 return; /* Already the right size, no resize needed */
2197 if (count > size) { /* lazy grow */
2198 if (resize_target_grow(ht, count) >= count)
2199 return;
2200 } else { /* lazy shrink */
2201 for (;;) {
2202 unsigned long s;
2203
7b3893e4 2204 s = uatomic_cmpxchg(&ht->resize_target, size, count);
89bb121d
LJ
2205 if (s == size)
2206 break; /* no resize needed */
2207 if (s > size)
2208 return; /* growing is/(was just) in progress */
2209 if (s <= count)
2210 return; /* some other thread do shrink */
2211 size = s;
2212 }
2213 }
f1f119ee 2214 __cds_lfht_resize_lazy_launch(ht);
3171717f 2215}
d0ec0ed2 2216
70469b43 2217static void cds_lfht_before_fork(void *priv __attribute__((unused)))
d0ec0ed2
MD
2218{
2219 if (cds_lfht_workqueue_atfork_nesting++)
2220 return;
2221 mutex_lock(&cds_lfht_fork_mutex);
2222 if (!cds_lfht_workqueue)
2223 return;
2224 urcu_workqueue_pause_worker(cds_lfht_workqueue);
2225}
2226
70469b43 2227static void cds_lfht_after_fork_parent(void *priv __attribute__((unused)))
d0ec0ed2
MD
2228{
2229 if (--cds_lfht_workqueue_atfork_nesting)
2230 return;
2231 if (!cds_lfht_workqueue)
2232 goto end;
2233 urcu_workqueue_resume_worker(cds_lfht_workqueue);
2234end:
2235 mutex_unlock(&cds_lfht_fork_mutex);
2236}
2237
70469b43 2238static void cds_lfht_after_fork_child(void *priv __attribute__((unused)))
d0ec0ed2
MD
2239{
2240 if (--cds_lfht_workqueue_atfork_nesting)
2241 return;
2242 if (!cds_lfht_workqueue)
2243 goto end;
2244 urcu_workqueue_create_worker(cds_lfht_workqueue);
2245end:
2246 mutex_unlock(&cds_lfht_fork_mutex);
2247}
2248
2249static struct urcu_atfork cds_lfht_atfork = {
2250 .before_fork = cds_lfht_before_fork,
2251 .after_fork_parent = cds_lfht_after_fork_parent,
2252 .after_fork_child = cds_lfht_after_fork_child,
2253};
2254
d0ec0ed2
MD
2255static void cds_lfht_init_worker(const struct rcu_flavor_struct *flavor)
2256{
2257 flavor->register_rculfhash_atfork(&cds_lfht_atfork);
2258
2259 mutex_lock(&cds_lfht_fork_mutex);
b047e7a7
MD
2260 if (!cds_lfht_workqueue)
2261 cds_lfht_workqueue = urcu_workqueue_create(0, -1, NULL,
2262 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
d0ec0ed2
MD
2263 mutex_unlock(&cds_lfht_fork_mutex);
2264}
2265
b047e7a7 2266static void cds_lfht_exit(void)
d0ec0ed2
MD
2267{
2268 mutex_lock(&cds_lfht_fork_mutex);
b047e7a7
MD
2269 if (cds_lfht_workqueue) {
2270 urcu_workqueue_flush_queued_work(cds_lfht_workqueue);
2271 urcu_workqueue_destroy(cds_lfht_workqueue);
2272 cds_lfht_workqueue = NULL;
2273 }
d0ec0ed2 2274 mutex_unlock(&cds_lfht_fork_mutex);
d0ec0ed2 2275}
This page took 0.197449 seconds and 4 git commands to generate.