Fix: work-around glibc __nptl_setxid vs clone hang
[lttng-tools.git] / src / common / hashtable / rculfhash.c
CommitLineData
fa68aa62
MD
1/*
2 * rculfhash.c
3 *
4 * Userspace RCU library - Lock-Free Resizable RCU Hash Table
5 *
6 * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
bec39940 7 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
fa68aa62
MD
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24/*
25 * Based on the following articles:
26 * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
27 * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
28 * - Michael, M. M. High performance dynamic lock-free hash tables
29 * and list-based sets. In Proceedings of the fourteenth annual ACM
30 * symposium on Parallel algorithms and architectures, ACM Press,
31 * (2002), 73-82.
32 *
33 * Some specificities of this Lock-Free Resizable RCU Hash Table
34 * implementation:
35 *
36 * - RCU read-side critical section allows readers to perform hash
37 * table lookups and use the returned objects safely by delaying
38 * memory reclaim of a grace period.
39 * - Add and remove operations are lock-free, and do not need to
40 * allocate memory. They need to be executed within RCU read-side
41 * critical section to ensure the objects they read are valid and to
42 * deal with the cmpxchg ABA problem.
43 * - add and add_unique operations are supported. add_unique checks if
44 * the node key already exists in the hash table. It ensures no key
45 * duplicata exists.
46 * - The resize operation executes concurrently with add/remove/lookup.
47 * - Hash table nodes are contained within a split-ordered list. This
48 * list is ordered by incrementing reversed-bits-hash value.
bec39940 49 * - An index of bucket nodes is kept. These bucket nodes are the hash
fa68aa62
MD
50 * table "buckets", and they are also chained together in the
51 * split-ordered list, which allows recursive expansion.
52 * - The resize operation for small tables only allows expanding the hash table.
53 * It is triggered automatically by detecting long chains in the add
54 * operation.
55 * - The resize operation for larger tables (and available through an
56 * API) allows both expanding and shrinking the hash table.
d6b18934 57 * - Split-counters are used to keep track of the number of
fa68aa62
MD
58 * nodes within the hash table for automatic resize triggering.
59 * - Resize operation initiated by long chain detection is executed by a
60 * call_rcu thread, which keeps lock-freedom of add and remove.
61 * - Resize operations are protected by a mutex.
62 * - The removal operation is split in two parts: first, a "removed"
63 * flag is set in the next pointer within the node to remove. Then,
64 * a "garbage collection" is performed in the bucket containing the
65 * removed node (from the start of the bucket up to the removed node).
66 * All encountered nodes with "removed" flag set in their next
67 * pointers are removed from the linked-list. If the cmpxchg used for
68 * removal fails (due to concurrent garbage-collection or concurrent
69 * add), we retry from the beginning of the bucket. This ensures that
70 * the node with "removed" flag set is removed from the hash table
71 * (not visible to lookups anymore) before the RCU read-side critical
72 * section held across removal ends. Furthermore, this ensures that
73 * the node with "removed" flag set is removed from the linked-list
74 * before its memory is reclaimed. Only the thread which removal
75 * successfully set the "removed" flag (with a cmpxchg) into a node's
76 * next pointer is considered to have succeeded its removal (and thus
77 * owns the node to reclaim). Because we garbage-collect starting from
bec39940 78 * an invariant node (the start-of-bucket bucket node) up to the
fa68aa62
MD
79 * "removed" node (or find a reverse-hash that is higher), we are sure
80 * that a successful traversal of the chain leads to a chain that is
81 * present in the linked-list (the start node is never removed) and
82 * that is does not contain the "removed" node anymore, even if
83 * concurrent delete/add operations are changing the structure of the
84 * list concurrently.
85 * - The add operation performs gargage collection of buckets if it
86 * encounters nodes with removed flag set in the bucket where it wants
87 * to add its new node. This ensures lock-freedom of add operation by
88 * helping the remover unlink nodes from the list rather than to wait
89 * for it do to so.
90 * - A RCU "order table" indexed by log2(hash index) is copied and
91 * expanded by the resize operation. This order table allows finding
bec39940
DG
92 * the "bucket node" tables.
93 * - There is one bucket node table per hash index order. The size of
94 * each bucket node table is half the number of hashes contained in
d6b18934 95 * this order (except for order 0).
bec39940
DG
96 * - synchronzie_rcu is used to garbage-collect the old bucket node table.
97 * - The per-order bucket node tables contain a compact version of the
fa68aa62
MD
98 * hash table nodes. These tables are invariant after they are
99 * populated into the hash table.
d6b18934 100 *
bec39940 101 * Bucket node tables:
d6b18934 102 *
bec39940
DG
103 * hash table hash table the last all bucket node tables
104 * order size bucket node 0 1 2 3 4 5 6(index)
d6b18934
DG
105 * table size
106 * 0 1 1 1
107 * 1 2 1 1 1
108 * 2 4 2 1 1 2
109 * 3 8 4 1 1 2 4
110 * 4 16 8 1 1 2 4 8
111 * 5 32 16 1 1 2 4 8 16
112 * 6 64 32 1 1 2 4 8 16 32
113 *
bec39940 114 * When growing/shrinking, we only focus on the last bucket node table
d6b18934
DG
115 * which size is (!order ? 1 : (1 << (order -1))).
116 *
117 * Example for growing/shrinking:
bec39940
DG
118 * grow hash table from order 5 to 6: init the index=6 bucket node table
119 * shrink hash table from order 6 to 5: fini the index=6 bucket node table
d6b18934 120 *
fa68aa62 121 * A bit of ascii art explanation:
bec39940
DG
122 *
123 * Order index is the off-by-one compare to the actual power of 2 because
fa68aa62 124 * we use index 0 to deal with the 0 special-case.
bec39940 125 *
fa68aa62 126 * This shows the nodes for a small table ordered by reversed bits:
bec39940 127 *
fa68aa62
MD
128 * bits reverse
129 * 0 000 000
130 * 4 100 001
131 * 2 010 010
132 * 6 110 011
133 * 1 001 100
134 * 5 101 101
135 * 3 011 110
136 * 7 111 111
bec39940
DG
137 *
138 * This shows the nodes in order of non-reversed bits, linked by
fa68aa62 139 * reversed-bit order.
bec39940 140 *
fa68aa62
MD
141 * order bits reverse
142 * 0 0 000 000
d6b18934
DG
143 * 1 | 1 001 100 <-
144 * 2 | | 2 010 010 <- |
fa68aa62 145 * | | | 3 011 110 | <- |
fa68aa62
MD
146 * 3 -> | | | 4 100 001 | |
147 * -> | | 5 101 101 |
148 * -> | 6 110 011
149 * -> 7 111 111
150 */
151
152#define _LGPL_SOURCE
153#include <stdlib.h>
154#include <errno.h>
155#include <assert.h>
156#include <stdio.h>
157#include <stdint.h>
158#include <string.h>
159
d6b18934 160#include "config.h"
fa68aa62
MD
161#include <urcu.h>
162#include <urcu-call-rcu.h>
163#include <urcu/arch.h>
164#include <urcu/uatomic.h>
165#include <urcu/compiler.h>
fa68aa62
MD
166#include <stdio.h>
167#include <pthread.h>
168
f6a9efaa 169#include "rculfhash.h"
bec39940
DG
170#include "rculfhash-internal.h"
171#include "urcu-flavor.h"
fa68aa62 172
9ef70f87
MD
173/*
174 * We need to lock pthread exit, which deadlocks __nptl_setxid in the
175 * runas clone.
176 * This work-around will be allowed to be removed when runas.c gets
177 * changed to do an exec() before issuing seteuid/setegid.
178 * See http://sourceware.org/bugzilla/show_bug.cgi?id=10184 for details.
179 */
180pthread_mutex_t lttng_libc_state_lock = PTHREAD_MUTEX_INITIALIZER;
181
fa68aa62 182/*
d6b18934 183 * Split-counters lazily update the global counter each 1024
fa68aa62
MD
184 * addition/removal. It automatically keeps track of resize required.
185 * We use the bucket length as indicator for need to expand for small
186 * tables and machines lacking per-cpu data suppport.
187 */
188#define COUNT_COMMIT_ORDER 10
d6b18934 189#define DEFAULT_SPLIT_COUNT_MASK 0xFUL
fa68aa62
MD
190#define CHAIN_LEN_TARGET 1
191#define CHAIN_LEN_RESIZE_THRESHOLD 3
192
193/*
194 * Define the minimum table size.
195 */
bec39940
DG
196#define MIN_TABLE_ORDER 0
197#define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER)
fa68aa62
MD
198
199/*
bec39940 200 * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink.
fa68aa62
MD
201 */
202#define MIN_PARTITION_PER_THREAD_ORDER 12
203#define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
204
fa68aa62
MD
205/*
206 * The removed flag needs to be updated atomically with the pointer.
207 * It indicates that no node must attach to the node scheduled for
208 * removal, and that node garbage collection must be performed.
bec39940 209 * The bucket flag does not require to be updated atomically with the
fa68aa62
MD
210 * pointer, but it is added as a pointer low bit flag to save space.
211 */
212#define REMOVED_FLAG (1UL << 0)
bec39940
DG
213#define BUCKET_FLAG (1UL << 1)
214#define REMOVAL_OWNER_FLAG (1UL << 2)
215#define FLAGS_MASK ((1UL << 3) - 1)
fa68aa62
MD
216
217/* Value of the end pointer. Should not interact with flags. */
218#define END_VALUE NULL
219
bec39940
DG
220DEFINE_RCU_FLAVOR(rcu_flavor);
221
d6b18934
DG
222/*
223 * ht_items_count: Split-counters counting the number of node addition
224 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
225 * is set at hash table creation.
226 *
227 * These are free-running counters, never reset to zero. They count the
228 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
229 * operations to update the global counter. We choose a power-of-2 value
230 * for the trigger to deal with 32 or 64-bit overflow of the counter.
231 */
fa68aa62
MD
232struct ht_items_count {
233 unsigned long add, del;
234} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
235
d6b18934
DG
236/*
237 * rcu_resize_work: Contains arguments passed to RCU worker thread
238 * responsible for performing lazy resize.
239 */
fa68aa62
MD
240struct rcu_resize_work {
241 struct rcu_head head;
242 struct cds_lfht *ht;
243};
244
d6b18934
DG
245/*
246 * partition_resize_work: Contains arguments passed to worker threads
247 * executing the hash table resize on partitions of the hash table
248 * assigned to each processor's worker thread.
249 */
fa68aa62 250struct partition_resize_work {
d6b18934 251 pthread_t thread_id;
fa68aa62
MD
252 struct cds_lfht *ht;
253 unsigned long i, start, len;
254 void (*fct)(struct cds_lfht *ht, unsigned long i,
255 unsigned long start, unsigned long len);
256};
257
fa68aa62
MD
258/*
259 * Algorithm to reverse bits in a word by lookup table, extended to
260 * 64-bit words.
261 * Source:
262 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
263 * Originally from Public Domain.
264 */
265
bec39940 266static const uint8_t BitReverseTable256[256] =
fa68aa62
MD
267{
268#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
269#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
270#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
271 R6(0), R6(2), R6(1), R6(3)
272};
273#undef R2
274#undef R4
275#undef R6
276
277static
278uint8_t bit_reverse_u8(uint8_t v)
279{
280 return BitReverseTable256[v];
281}
282
283static __attribute__((unused))
284uint32_t bit_reverse_u32(uint32_t v)
285{
bec39940
DG
286 return ((uint32_t) bit_reverse_u8(v) << 24) |
287 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
288 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
fa68aa62
MD
289 ((uint32_t) bit_reverse_u8(v >> 24));
290}
291
292static __attribute__((unused))
293uint64_t bit_reverse_u64(uint64_t v)
294{
bec39940
DG
295 return ((uint64_t) bit_reverse_u8(v) << 56) |
296 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
fa68aa62
MD
297 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
298 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
bec39940
DG
299 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
300 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
fa68aa62
MD
301 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
302 ((uint64_t) bit_reverse_u8(v >> 56));
303}
304
305static
306unsigned long bit_reverse_ulong(unsigned long v)
307{
308#if (CAA_BITS_PER_LONG == 32)
309 return bit_reverse_u32(v);
310#else
311 return bit_reverse_u64(v);
312#endif
313}
314
315/*
316 * fls: returns the position of the most significant bit.
317 * Returns 0 if no bit is set, else returns the position of the most
318 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
319 */
320#if defined(__i386) || defined(__x86_64)
321static inline
322unsigned int fls_u32(uint32_t x)
323{
324 int r;
325
326 asm("bsrl %1,%0\n\t"
327 "jnz 1f\n\t"
328 "movl $-1,%0\n\t"
329 "1:\n\t"
330 : "=r" (r) : "rm" (x));
331 return r + 1;
332}
333#define HAS_FLS_U32
334#endif
335
336#if defined(__x86_64)
337static inline
338unsigned int fls_u64(uint64_t x)
339{
340 long r;
341
342 asm("bsrq %1,%0\n\t"
343 "jnz 1f\n\t"
344 "movq $-1,%0\n\t"
345 "1:\n\t"
346 : "=r" (r) : "rm" (x));
347 return r + 1;
348}
349#define HAS_FLS_U64
350#endif
351
352#ifndef HAS_FLS_U64
353static __attribute__((unused))
354unsigned int fls_u64(uint64_t x)
355{
356 unsigned int r = 64;
357
358 if (!x)
359 return 0;
360
361 if (!(x & 0xFFFFFFFF00000000ULL)) {
362 x <<= 32;
363 r -= 32;
364 }
365 if (!(x & 0xFFFF000000000000ULL)) {
366 x <<= 16;
367 r -= 16;
368 }
369 if (!(x & 0xFF00000000000000ULL)) {
370 x <<= 8;
371 r -= 8;
372 }
373 if (!(x & 0xF000000000000000ULL)) {
374 x <<= 4;
375 r -= 4;
376 }
377 if (!(x & 0xC000000000000000ULL)) {
378 x <<= 2;
379 r -= 2;
380 }
381 if (!(x & 0x8000000000000000ULL)) {
382 x <<= 1;
383 r -= 1;
384 }
385 return r;
386}
387#endif
388
389#ifndef HAS_FLS_U32
390static __attribute__((unused))
391unsigned int fls_u32(uint32_t x)
392{
393 unsigned int r = 32;
394
395 if (!x)
396 return 0;
397 if (!(x & 0xFFFF0000U)) {
398 x <<= 16;
399 r -= 16;
400 }
401 if (!(x & 0xFF000000U)) {
402 x <<= 8;
403 r -= 8;
404 }
405 if (!(x & 0xF0000000U)) {
406 x <<= 4;
407 r -= 4;
408 }
409 if (!(x & 0xC0000000U)) {
410 x <<= 2;
411 r -= 2;
412 }
413 if (!(x & 0x80000000U)) {
414 x <<= 1;
415 r -= 1;
416 }
417 return r;
418}
419#endif
420
bec39940 421unsigned int cds_lfht_fls_ulong(unsigned long x)
fa68aa62 422{
d6b18934 423#if (CAA_BITS_PER_LONG == 32)
fa68aa62
MD
424 return fls_u32(x);
425#else
426 return fls_u64(x);
427#endif
428}
429
d6b18934
DG
430/*
431 * Return the minimum order for which x <= (1UL << order).
432 * Return -1 if x is 0.
433 */
bec39940 434int cds_lfht_get_count_order_u32(uint32_t x)
fa68aa62 435{
d6b18934
DG
436 if (!x)
437 return -1;
fa68aa62 438
d6b18934 439 return fls_u32(x - 1);
fa68aa62
MD
440}
441
d6b18934
DG
442/*
443 * Return the minimum order for which x <= (1UL << order).
444 * Return -1 if x is 0.
445 */
bec39940 446int cds_lfht_get_count_order_ulong(unsigned long x)
fa68aa62 447{
d6b18934
DG
448 if (!x)
449 return -1;
fa68aa62 450
bec39940 451 return cds_lfht_fls_ulong(x - 1);
fa68aa62
MD
452}
453
fa68aa62 454static
bec39940 455void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth);
fa68aa62 456
fa68aa62
MD
457static
458void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
459 unsigned long count);
460
461static long nr_cpus_mask = -1;
d6b18934
DG
462static long split_count_mask = -1;
463
464#if defined(HAVE_SYSCONF)
465static void ht_init_nr_cpus_mask(void)
466{
467 long maxcpus;
468
469 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
470 if (maxcpus <= 0) {
471 nr_cpus_mask = -2;
472 return;
473 }
474 /*
475 * round up number of CPUs to next power of two, so we
476 * can use & for modulo.
477 */
bec39940 478 maxcpus = 1UL << cds_lfht_get_count_order_ulong(maxcpus);
d6b18934
DG
479 nr_cpus_mask = maxcpus - 1;
480}
481#else /* #if defined(HAVE_SYSCONF) */
482static void ht_init_nr_cpus_mask(void)
483{
484 nr_cpus_mask = -2;
485}
486#endif /* #else #if defined(HAVE_SYSCONF) */
fa68aa62
MD
487
488static
d6b18934 489void alloc_split_items_count(struct cds_lfht *ht)
fa68aa62
MD
490{
491 struct ht_items_count *count;
492
d6b18934
DG
493 if (nr_cpus_mask == -1) {
494 ht_init_nr_cpus_mask();
495 if (nr_cpus_mask < 0)
496 split_count_mask = DEFAULT_SPLIT_COUNT_MASK;
497 else
498 split_count_mask = nr_cpus_mask;
fa68aa62 499 }
d6b18934
DG
500
501 assert(split_count_mask >= 0);
502
503 if (ht->flags & CDS_LFHT_ACCOUNTING) {
504 ht->split_count = calloc(split_count_mask + 1, sizeof(*count));
505 assert(ht->split_count);
506 } else {
507 ht->split_count = NULL;
fa68aa62
MD
508 }
509}
510
511static
d6b18934 512void free_split_items_count(struct cds_lfht *ht)
fa68aa62 513{
d6b18934 514 poison_free(ht->split_count);
fa68aa62
MD
515}
516
d6b18934 517#if defined(HAVE_SCHED_GETCPU)
fa68aa62 518static
d6b18934 519int ht_get_split_count_index(unsigned long hash)
fa68aa62
MD
520{
521 int cpu;
522
d6b18934 523 assert(split_count_mask >= 0);
fa68aa62 524 cpu = sched_getcpu();
6e59ae26 525 if (caa_unlikely(cpu < 0))
d6b18934 526 return hash & split_count_mask;
fa68aa62 527 else
d6b18934 528 return cpu & split_count_mask;
fa68aa62 529}
d6b18934
DG
530#else /* #if defined(HAVE_SCHED_GETCPU) */
531static
532int ht_get_split_count_index(unsigned long hash)
533{
534 return hash & split_count_mask;
535}
536#endif /* #else #if defined(HAVE_SCHED_GETCPU) */
fa68aa62
MD
537
538static
d6b18934 539void ht_count_add(struct cds_lfht *ht, unsigned long size, unsigned long hash)
fa68aa62 540{
d6b18934
DG
541 unsigned long split_count;
542 int index;
bec39940 543 long count;
fa68aa62 544
6e59ae26 545 if (caa_unlikely(!ht->split_count))
fa68aa62 546 return;
d6b18934
DG
547 index = ht_get_split_count_index(hash);
548 split_count = uatomic_add_return(&ht->split_count[index].add, 1);
bec39940
DG
549 if (caa_likely(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))
550 return;
551 /* Only if number of add multiple of 1UL << COUNT_COMMIT_ORDER */
552
553 dbg_printf("add split count %lu\n", split_count);
554 count = uatomic_add_return(&ht->count,
555 1UL << COUNT_COMMIT_ORDER);
556 if (caa_likely(count & (count - 1)))
557 return;
558 /* Only if global count is power of 2 */
559
560 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) < size)
561 return;
562 dbg_printf("add set global %ld\n", count);
563 cds_lfht_resize_lazy_count(ht, size,
564 count >> (CHAIN_LEN_TARGET - 1));
fa68aa62
MD
565}
566
567static
d6b18934 568void ht_count_del(struct cds_lfht *ht, unsigned long size, unsigned long hash)
fa68aa62 569{
d6b18934
DG
570 unsigned long split_count;
571 int index;
bec39940 572 long count;
fa68aa62 573
6e59ae26 574 if (caa_unlikely(!ht->split_count))
fa68aa62 575 return;
d6b18934
DG
576 index = ht_get_split_count_index(hash);
577 split_count = uatomic_add_return(&ht->split_count[index].del, 1);
bec39940
DG
578 if (caa_likely(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))
579 return;
580 /* Only if number of deletes multiple of 1UL << COUNT_COMMIT_ORDER */
581
582 dbg_printf("del split count %lu\n", split_count);
583 count = uatomic_add_return(&ht->count,
584 -(1UL << COUNT_COMMIT_ORDER));
585 if (caa_likely(count & (count - 1)))
586 return;
587 /* Only if global count is power of 2 */
588
589 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) >= size)
590 return;
591 dbg_printf("del set global %ld\n", count);
592 /*
593 * Don't shrink table if the number of nodes is below a
594 * certain threshold.
595 */
596 if (count < (1UL << COUNT_COMMIT_ORDER) * (split_count_mask + 1))
597 return;
598 cds_lfht_resize_lazy_count(ht, size,
599 count >> (CHAIN_LEN_TARGET - 1));
fa68aa62
MD
600}
601
fa68aa62
MD
602static
603void check_resize(struct cds_lfht *ht, unsigned long size, uint32_t chain_len)
604{
605 unsigned long count;
606
607 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
608 return;
609 count = uatomic_read(&ht->count);
610 /*
611 * Use bucket-local length for small table expand and for
612 * environments lacking per-cpu data support.
613 */
614 if (count >= (1UL << COUNT_COMMIT_ORDER))
615 return;
616 if (chain_len > 100)
617 dbg_printf("WARNING: large chain length: %u.\n",
618 chain_len);
619 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
bec39940
DG
620 cds_lfht_resize_lazy_grow(ht, size,
621 cds_lfht_get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
fa68aa62
MD
622}
623
624static
625struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
626{
627 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
628}
629
630static
631int is_removed(struct cds_lfht_node *node)
632{
633 return ((unsigned long) node) & REMOVED_FLAG;
634}
635
636static
637struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
638{
639 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
640}
641
642static
bec39940 643int is_bucket(struct cds_lfht_node *node)
fa68aa62 644{
bec39940 645 return ((unsigned long) node) & BUCKET_FLAG;
fa68aa62
MD
646}
647
648static
bec39940 649struct cds_lfht_node *flag_bucket(struct cds_lfht_node *node)
fa68aa62 650{
bec39940
DG
651 return (struct cds_lfht_node *) (((unsigned long) node) | BUCKET_FLAG);
652}
653
654static
655int is_removal_owner(struct cds_lfht_node *node)
656{
657 return ((unsigned long) node) & REMOVAL_OWNER_FLAG;
658}
659
660static
661struct cds_lfht_node *flag_removal_owner(struct cds_lfht_node *node)
662{
663 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVAL_OWNER_FLAG);
fa68aa62
MD
664}
665
666static
667struct cds_lfht_node *get_end(void)
668{
669 return (struct cds_lfht_node *) END_VALUE;
670}
671
672static
673int is_end(struct cds_lfht_node *node)
674{
675 return clear_flag(node) == (struct cds_lfht_node *) END_VALUE;
676}
677
678static
bec39940
DG
679unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr,
680 unsigned long v)
fa68aa62
MD
681{
682 unsigned long old1, old2;
683
684 old1 = uatomic_read(ptr);
685 do {
686 old2 = old1;
687 if (old2 >= v)
688 return old2;
689 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
bec39940 690 return old2;
fa68aa62
MD
691}
692
693static
bec39940 694void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
fa68aa62 695{
bec39940
DG
696 return ht->mm->alloc_bucket_table(ht, order);
697}
d6b18934 698
bec39940
DG
699/*
700 * cds_lfht_free_bucket_table() should be called with decreasing order.
701 * When cds_lfht_free_bucket_table(0) is called, it means the whole
702 * lfht is destroyed.
703 */
704static
705void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
706{
707 return ht->mm->free_bucket_table(ht, order);
708}
d6b18934 709
bec39940
DG
710static inline
711struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
712{
713 return ht->bucket_at(ht, index);
714}
715
716static inline
717struct cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size,
718 unsigned long hash)
719{
720 assert(size > 0);
721 return bucket_at(ht, hash & (size - 1));
fa68aa62
MD
722}
723
724/*
725 * Remove all logically deleted nodes from a bucket up to a certain node key.
726 */
727static
bec39940 728void _cds_lfht_gc_bucket(struct cds_lfht_node *bucket, struct cds_lfht_node *node)
fa68aa62
MD
729{
730 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
731
bec39940
DG
732 assert(!is_bucket(bucket));
733 assert(!is_removed(bucket));
734 assert(!is_bucket(node));
fa68aa62
MD
735 assert(!is_removed(node));
736 for (;;) {
bec39940
DG
737 iter_prev = bucket;
738 /* We can always skip the bucket node initially */
739 iter = rcu_dereference(iter_prev->next);
d6b18934 740 assert(!is_removed(iter));
bec39940 741 assert(iter_prev->reverse_hash <= node->reverse_hash);
fa68aa62 742 /*
bec39940 743 * We should never be called with bucket (start of chain)
fa68aa62
MD
744 * and logically removed node (end of path compression
745 * marker) being the actual same node. This would be a
746 * bug in the algorithm implementation.
747 */
bec39940 748 assert(bucket != node);
fa68aa62 749 for (;;) {
6e59ae26 750 if (caa_unlikely(is_end(iter)))
fa68aa62 751 return;
bec39940 752 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
fa68aa62 753 return;
bec39940 754 next = rcu_dereference(clear_flag(iter)->next);
6e59ae26 755 if (caa_likely(is_removed(next)))
fa68aa62
MD
756 break;
757 iter_prev = clear_flag(iter);
758 iter = next;
759 }
760 assert(!is_removed(iter));
bec39940
DG
761 if (is_bucket(iter))
762 new_next = flag_bucket(clear_flag(next));
fa68aa62
MD
763 else
764 new_next = clear_flag(next);
bec39940 765 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
fa68aa62 766 }
fa68aa62
MD
767}
768
769static
770int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size,
771 struct cds_lfht_node *old_node,
d6b18934 772 struct cds_lfht_node *old_next,
fa68aa62
MD
773 struct cds_lfht_node *new_node)
774{
bec39940 775 struct cds_lfht_node *bucket, *ret_next;
fa68aa62
MD
776
777 if (!old_node) /* Return -ENOENT if asked to replace NULL node */
d6b18934 778 return -ENOENT;
fa68aa62
MD
779
780 assert(!is_removed(old_node));
bec39940 781 assert(!is_bucket(old_node));
fa68aa62 782 assert(!is_removed(new_node));
bec39940 783 assert(!is_bucket(new_node));
fa68aa62 784 assert(new_node != old_node);
d6b18934 785 for (;;) {
fa68aa62 786 /* Insert after node to be replaced */
fa68aa62
MD
787 if (is_removed(old_next)) {
788 /*
789 * Too late, the old node has been removed under us
790 * between lookup and replace. Fail.
791 */
d6b18934 792 return -ENOENT;
fa68aa62 793 }
bec39940
DG
794 assert(old_next == clear_flag(old_next));
795 assert(new_node != old_next);
796 new_node->next = old_next;
fa68aa62
MD
797 /*
798 * Here is the whole trick for lock-free replace: we add
799 * the replacement node _after_ the node we want to
800 * replace by atomically setting its next pointer at the
801 * same time we set its removal flag. Given that
802 * the lookups/get next use an iterator aware of the
803 * next pointer, they will either skip the old node due
804 * to the removal flag and see the new node, or use
805 * the old node, but will not see the new one.
bec39940
DG
806 * This is a replacement of a node with another node
807 * that has the same value: we are therefore not
808 * removing a value from the hash table.
fa68aa62 809 */
bec39940 810 ret_next = uatomic_cmpxchg(&old_node->next,
fa68aa62 811 old_next, flag_removed(new_node));
d6b18934
DG
812 if (ret_next == old_next)
813 break; /* We performed the replacement. */
814 old_next = ret_next;
815 }
fa68aa62
MD
816
817 /*
818 * Ensure that the old node is not visible to readers anymore:
819 * lookup for the node, and remove it (along with any other
820 * logically removed node) if found.
821 */
bec39940
DG
822 bucket = lookup_bucket(ht, size, bit_reverse_ulong(old_node->reverse_hash));
823 _cds_lfht_gc_bucket(bucket, new_node);
d6b18934 824
bec39940 825 assert(is_removed(rcu_dereference(old_node->next)));
d6b18934 826 return 0;
fa68aa62
MD
827}
828
d6b18934
DG
829/*
830 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
831 * mode. A NULL unique_ret allows creation of duplicate keys.
832 */
fa68aa62 833static
d6b18934 834void _cds_lfht_add(struct cds_lfht *ht,
bec39940
DG
835 unsigned long hash,
836 cds_lfht_match_fct match,
837 const void *key,
d6b18934
DG
838 unsigned long size,
839 struct cds_lfht_node *node,
840 struct cds_lfht_iter *unique_ret,
bec39940 841 int bucket_flag)
fa68aa62
MD
842{
843 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
d6b18934 844 *return_node;
bec39940 845 struct cds_lfht_node *bucket;
fa68aa62 846
bec39940 847 assert(!is_bucket(node));
fa68aa62 848 assert(!is_removed(node));
bec39940 849 bucket = lookup_bucket(ht, size, hash);
fa68aa62
MD
850 for (;;) {
851 uint32_t chain_len = 0;
852
853 /*
854 * iter_prev points to the non-removed node prior to the
855 * insert location.
856 */
bec39940
DG
857 iter_prev = bucket;
858 /* We can always skip the bucket node initially */
859 iter = rcu_dereference(iter_prev->next);
860 assert(iter_prev->reverse_hash <= node->reverse_hash);
fa68aa62 861 for (;;) {
6e59ae26 862 if (caa_unlikely(is_end(iter)))
fa68aa62 863 goto insert;
bec39940 864 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
fa68aa62 865 goto insert;
d6b18934 866
bec39940
DG
867 /* bucket node is the first node of the identical-hash-value chain */
868 if (bucket_flag && clear_flag(iter)->reverse_hash == node->reverse_hash)
d6b18934
DG
869 goto insert;
870
bec39940 871 next = rcu_dereference(clear_flag(iter)->next);
6e59ae26 872 if (caa_unlikely(is_removed(next)))
fa68aa62 873 goto gc_node;
d6b18934
DG
874
875 /* uniquely add */
876 if (unique_ret
bec39940
DG
877 && !is_bucket(next)
878 && clear_flag(iter)->reverse_hash == node->reverse_hash) {
d6b18934
DG
879 struct cds_lfht_iter d_iter = { .node = node, .next = iter, };
880
881 /*
882 * uniquely adding inserts the node as the first
883 * node of the identical-hash-value node chain.
884 *
885 * This semantic ensures no duplicated keys
886 * should ever be observable in the table
887 * (including observe one node by one node
888 * by forward iterations)
889 */
bec39940 890 cds_lfht_next_duplicate(ht, match, key, &d_iter);
d6b18934
DG
891 if (!d_iter.node)
892 goto insert;
893
894 *unique_ret = d_iter;
895 return;
fa68aa62 896 }
d6b18934 897
fa68aa62 898 /* Only account for identical reverse hash once */
bec39940
DG
899 if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash
900 && !is_bucket(next))
fa68aa62
MD
901 check_resize(ht, size, ++chain_len);
902 iter_prev = clear_flag(iter);
903 iter = next;
904 }
905
906 insert:
907 assert(node != clear_flag(iter));
908 assert(!is_removed(iter_prev));
909 assert(!is_removed(iter));
910 assert(iter_prev != node);
bec39940
DG
911 if (!bucket_flag)
912 node->next = clear_flag(iter);
fa68aa62 913 else
bec39940
DG
914 node->next = flag_bucket(clear_flag(iter));
915 if (is_bucket(iter))
916 new_node = flag_bucket(node);
fa68aa62
MD
917 else
918 new_node = node;
bec39940 919 if (uatomic_cmpxchg(&iter_prev->next, iter,
fa68aa62
MD
920 new_node) != iter) {
921 continue; /* retry */
922 } else {
d6b18934
DG
923 return_node = node;
924 goto end;
fa68aa62
MD
925 }
926
927 gc_node:
928 assert(!is_removed(iter));
bec39940
DG
929 if (is_bucket(iter))
930 new_next = flag_bucket(clear_flag(next));
fa68aa62
MD
931 else
932 new_next = clear_flag(next);
bec39940 933 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
fa68aa62
MD
934 /* retry */
935 }
fa68aa62 936end:
d6b18934
DG
937 if (unique_ret) {
938 unique_ret->node = return_node;
939 /* unique_ret->next left unset, never used. */
940 }
fa68aa62
MD
941}
942
943static
944int _cds_lfht_del(struct cds_lfht *ht, unsigned long size,
bec39940 945 struct cds_lfht_node *node)
fa68aa62 946{
bec39940 947 struct cds_lfht_node *bucket, *next;
fa68aa62
MD
948
949 if (!node) /* Return -ENOENT if asked to delete NULL node */
d6b18934 950 return -ENOENT;
fa68aa62
MD
951
952 /* logically delete the node */
bec39940 953 assert(!is_bucket(node));
fa68aa62 954 assert(!is_removed(node));
bec39940 955 assert(!is_removal_owner(node));
fa68aa62 956
bec39940
DG
957 /*
958 * We are first checking if the node had previously been
959 * logically removed (this check is not atomic with setting the
960 * logical removal flag). Return -ENOENT if the node had
961 * previously been removed.
962 */
963 next = rcu_dereference(node->next);
964 if (caa_unlikely(is_removed(next)))
965 return -ENOENT;
966 assert(!is_bucket(next));
967 /*
968 * We set the REMOVED_FLAG unconditionally. Note that there may
969 * be more than one concurrent thread setting this flag.
970 * Knowing which wins the race will be known after the garbage
971 * collection phase, stay tuned!
972 */
973 uatomic_or(&node->next, REMOVED_FLAG);
fa68aa62 974 /* We performed the (logical) deletion. */
fa68aa62
MD
975
976 /*
977 * Ensure that the node is not visible to readers anymore: lookup for
978 * the node, and remove it (along with any other logically removed node)
979 * if found.
980 */
bec39940
DG
981 bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash));
982 _cds_lfht_gc_bucket(bucket, node);
d6b18934 983
bec39940
DG
984 assert(is_removed(rcu_dereference(node->next)));
985 /*
986 * Last phase: atomically exchange node->next with a version
987 * having "REMOVAL_OWNER_FLAG" set. If the returned node->next
988 * pointer did _not_ have "REMOVAL_OWNER_FLAG" set, we now own
989 * the node and win the removal race.
990 * It is interesting to note that all "add" paths are forbidden
991 * to change the next pointer starting from the point where the
992 * REMOVED_FLAG is set, so here using a read, followed by a
993 * xchg() suffice to guarantee that the xchg() will ever only
994 * set the "REMOVAL_OWNER_FLAG" (or change nothing if the flag
995 * was already set).
996 */
997 if (!is_removal_owner(uatomic_xchg(&node->next,
998 flag_removal_owner(node->next))))
999 return 0;
1000 else
1001 return -ENOENT;
fa68aa62
MD
1002}
1003
1004static
1005void *partition_resize_thread(void *arg)
1006{
1007 struct partition_resize_work *work = arg;
1008
bec39940 1009 work->ht->flavor->register_thread();
fa68aa62 1010 work->fct(work->ht, work->i, work->start, work->len);
bec39940 1011 work->ht->flavor->unregister_thread();
fa68aa62
MD
1012 return NULL;
1013}
1014
1015static
1016void partition_resize_helper(struct cds_lfht *ht, unsigned long i,
1017 unsigned long len,
1018 void (*fct)(struct cds_lfht *ht, unsigned long i,
1019 unsigned long start, unsigned long len))
1020{
1021 unsigned long partition_len;
1022 struct partition_resize_work *work;
1023 int thread, ret;
1024 unsigned long nr_threads;
fa68aa62
MD
1025
1026 /*
1027 * Note: nr_cpus_mask + 1 is always power of 2.
1028 * We spawn just the number of threads we need to satisfy the minimum
1029 * partition size, up to the number of CPUs in the system.
1030 */
f6a9efaa
DG
1031 if (nr_cpus_mask > 0) {
1032 nr_threads = min(nr_cpus_mask + 1,
1033 len >> MIN_PARTITION_PER_THREAD_ORDER);
1034 } else {
1035 nr_threads = 1;
1036 }
bec39940 1037 partition_len = len >> cds_lfht_get_count_order_ulong(nr_threads);
fa68aa62 1038 work = calloc(nr_threads, sizeof(*work));
fa68aa62 1039 assert(work);
9ef70f87 1040 pthread_mutex_lock(&lttng_libc_state_lock);
fa68aa62
MD
1041 for (thread = 0; thread < nr_threads; thread++) {
1042 work[thread].ht = ht;
1043 work[thread].i = i;
1044 work[thread].len = partition_len;
1045 work[thread].start = thread * partition_len;
1046 work[thread].fct = fct;
d6b18934 1047 ret = pthread_create(&(work[thread].thread_id), ht->resize_attr,
fa68aa62
MD
1048 partition_resize_thread, &work[thread]);
1049 assert(!ret);
1050 }
1051 for (thread = 0; thread < nr_threads; thread++) {
d6b18934 1052 ret = pthread_join(work[thread].thread_id, NULL);
fa68aa62
MD
1053 assert(!ret);
1054 }
9ef70f87 1055 pthread_mutex_unlock(&lttng_libc_state_lock);
fa68aa62 1056 free(work);
fa68aa62
MD
1057}
1058
1059/*
1060 * Holding RCU read lock to protect _cds_lfht_add against memory
1061 * reclaim that could be performed by other call_rcu worker threads (ABA
1062 * problem).
1063 *
1064 * When we reach a certain length, we can split this population phase over
1065 * many worker threads, based on the number of CPUs available in the system.
1066 * This should therefore take care of not having the expand lagging behind too
1067 * many concurrent insertion threads by using the scheduler's ability to
bec39940 1068 * schedule bucket node population fairly with insertions.
fa68aa62
MD
1069 */
1070static
1071void init_table_populate_partition(struct cds_lfht *ht, unsigned long i,
1072 unsigned long start, unsigned long len)
1073{
bec39940 1074 unsigned long j, size = 1UL << (i - 1);
fa68aa62 1075
bec39940
DG
1076 assert(i > MIN_TABLE_ORDER);
1077 ht->flavor->read_lock();
1078 for (j = size + start; j < size + start + len; j++) {
1079 struct cds_lfht_node *new_node = bucket_at(ht, j);
fa68aa62 1080
bec39940
DG
1081 assert(j >= size && j < (size << 1));
1082 dbg_printf("init populate: order %lu index %lu hash %lu\n",
1083 i, j, j);
1084 new_node->reverse_hash = bit_reverse_ulong(j);
1085 _cds_lfht_add(ht, j, NULL, NULL, size, new_node, NULL, 1);
fa68aa62 1086 }
bec39940 1087 ht->flavor->read_unlock();
fa68aa62
MD
1088}
1089
1090static
1091void init_table_populate(struct cds_lfht *ht, unsigned long i,
1092 unsigned long len)
1093{
1094 assert(nr_cpus_mask != -1);
1095 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
bec39940 1096 ht->flavor->thread_online();
fa68aa62 1097 init_table_populate_partition(ht, i, 0, len);
bec39940 1098 ht->flavor->thread_offline();
fa68aa62
MD
1099 return;
1100 }
1101 partition_resize_helper(ht, i, len, init_table_populate_partition);
1102}
1103
1104static
1105void init_table(struct cds_lfht *ht,
d6b18934 1106 unsigned long first_order, unsigned long last_order)
fa68aa62 1107{
d6b18934 1108 unsigned long i;
fa68aa62 1109
d6b18934
DG
1110 dbg_printf("init table: first_order %lu last_order %lu\n",
1111 first_order, last_order);
bec39940 1112 assert(first_order > MIN_TABLE_ORDER);
d6b18934 1113 for (i = first_order; i <= last_order; i++) {
fa68aa62
MD
1114 unsigned long len;
1115
d6b18934 1116 len = 1UL << (i - 1);
fa68aa62
MD
1117 dbg_printf("init order %lu len: %lu\n", i, len);
1118
1119 /* Stop expand if the resize target changes under us */
bec39940 1120 if (CMM_LOAD_SHARED(ht->resize_target) < (1UL << i))
fa68aa62
MD
1121 break;
1122
bec39940 1123 cds_lfht_alloc_bucket_table(ht, i);
fa68aa62
MD
1124
1125 /*
bec39940
DG
1126 * Set all bucket nodes reverse hash values for a level and
1127 * link all bucket nodes into the table.
fa68aa62
MD
1128 */
1129 init_table_populate(ht, i, len);
1130
1131 /*
1132 * Update table size.
1133 */
1134 cmm_smp_wmb(); /* populate data before RCU size */
bec39940 1135 CMM_STORE_SHARED(ht->size, 1UL << i);
fa68aa62 1136
d6b18934 1137 dbg_printf("init new size: %lu\n", 1UL << i);
fa68aa62
MD
1138 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1139 break;
1140 }
1141}
1142
1143/*
1144 * Holding RCU read lock to protect _cds_lfht_remove against memory
1145 * reclaim that could be performed by other call_rcu worker threads (ABA
1146 * problem).
1147 * For a single level, we logically remove and garbage collect each node.
1148 *
1149 * As a design choice, we perform logical removal and garbage collection on a
1150 * node-per-node basis to simplify this algorithm. We also assume keeping good
1151 * cache locality of the operation would overweight possible performance gain
1152 * that could be achieved by batching garbage collection for multiple levels.
1153 * However, this would have to be justified by benchmarks.
1154 *
1155 * Concurrent removal and add operations are helping us perform garbage
1156 * collection of logically removed nodes. We guarantee that all logically
1157 * removed nodes have been garbage-collected (unlinked) before call_rcu is
bec39940 1158 * invoked to free a hole level of bucket nodes (after a grace period).
fa68aa62
MD
1159 *
1160 * Logical removal and garbage collection can therefore be done in batch or on a
1161 * node-per-node basis, as long as the guarantee above holds.
1162 *
1163 * When we reach a certain length, we can split this removal over many worker
1164 * threads, based on the number of CPUs available in the system. This should
1165 * take care of not letting resize process lag behind too many concurrent
1166 * updater threads actively inserting into the hash table.
1167 */
1168static
1169void remove_table_partition(struct cds_lfht *ht, unsigned long i,
1170 unsigned long start, unsigned long len)
1171{
bec39940 1172 unsigned long j, size = 1UL << (i - 1);
fa68aa62 1173
bec39940
DG
1174 assert(i > MIN_TABLE_ORDER);
1175 ht->flavor->read_lock();
1176 for (j = size + start; j < size + start + len; j++) {
1177 struct cds_lfht_node *fini_bucket = bucket_at(ht, j);
1178 struct cds_lfht_node *parent_bucket = bucket_at(ht, j - size);
fa68aa62 1179
bec39940
DG
1180 assert(j >= size && j < (size << 1));
1181 dbg_printf("remove entry: order %lu index %lu hash %lu\n",
1182 i, j, j);
1183 /* Set the REMOVED_FLAG to freeze the ->next for gc */
1184 uatomic_or(&fini_bucket->next, REMOVED_FLAG);
1185 _cds_lfht_gc_bucket(parent_bucket, fini_bucket);
fa68aa62 1186 }
bec39940 1187 ht->flavor->read_unlock();
fa68aa62
MD
1188}
1189
1190static
1191void remove_table(struct cds_lfht *ht, unsigned long i, unsigned long len)
1192{
1193
1194 assert(nr_cpus_mask != -1);
1195 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
bec39940 1196 ht->flavor->thread_online();
fa68aa62 1197 remove_table_partition(ht, i, 0, len);
bec39940 1198 ht->flavor->thread_offline();
fa68aa62
MD
1199 return;
1200 }
1201 partition_resize_helper(ht, i, len, remove_table_partition);
1202}
1203
bec39940
DG
1204/*
1205 * fini_table() is never called for first_order == 0, which is why
1206 * free_by_rcu_order == 0 can be used as criterion to know if free must
1207 * be called.
1208 */
fa68aa62
MD
1209static
1210void fini_table(struct cds_lfht *ht,
d6b18934 1211 unsigned long first_order, unsigned long last_order)
fa68aa62 1212{
d6b18934 1213 long i;
bec39940 1214 unsigned long free_by_rcu_order = 0;
fa68aa62 1215
d6b18934
DG
1216 dbg_printf("fini table: first_order %lu last_order %lu\n",
1217 first_order, last_order);
bec39940 1218 assert(first_order > MIN_TABLE_ORDER);
d6b18934 1219 for (i = last_order; i >= first_order; i--) {
fa68aa62
MD
1220 unsigned long len;
1221
d6b18934 1222 len = 1UL << (i - 1);
fa68aa62
MD
1223 dbg_printf("fini order %lu len: %lu\n", i, len);
1224
1225 /* Stop shrink if the resize target changes under us */
bec39940 1226 if (CMM_LOAD_SHARED(ht->resize_target) > (1UL << (i - 1)))
fa68aa62
MD
1227 break;
1228
1229 cmm_smp_wmb(); /* populate data before RCU size */
bec39940 1230 CMM_STORE_SHARED(ht->size, 1UL << (i - 1));
fa68aa62
MD
1231
1232 /*
1233 * We need to wait for all add operations to reach Q.S. (and
1234 * thus use the new table for lookups) before we can start
bec39940 1235 * releasing the old bucket nodes. Otherwise their lookup will
fa68aa62
MD
1236 * return a logically removed node as insert position.
1237 */
bec39940
DG
1238 ht->flavor->update_synchronize_rcu();
1239 if (free_by_rcu_order)
1240 cds_lfht_free_bucket_table(ht, free_by_rcu_order);
fa68aa62
MD
1241
1242 /*
bec39940
DG
1243 * Set "removed" flag in bucket nodes about to be removed.
1244 * Unlink all now-logically-removed bucket node pointers.
fa68aa62
MD
1245 * Concurrent add/remove operation are helping us doing
1246 * the gc.
1247 */
1248 remove_table(ht, i, len);
1249
bec39940 1250 free_by_rcu_order = i;
fa68aa62
MD
1251
1252 dbg_printf("fini new size: %lu\n", 1UL << i);
1253 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1254 break;
1255 }
d6b18934 1256
bec39940
DG
1257 if (free_by_rcu_order) {
1258 ht->flavor->update_synchronize_rcu();
1259 cds_lfht_free_bucket_table(ht, free_by_rcu_order);
d6b18934
DG
1260 }
1261}
1262
1263static
bec39940 1264void cds_lfht_create_bucket(struct cds_lfht *ht, unsigned long size)
d6b18934 1265{
bec39940
DG
1266 struct cds_lfht_node *prev, *node;
1267 unsigned long order, len, i;
d6b18934 1268
bec39940 1269 cds_lfht_alloc_bucket_table(ht, 0);
d6b18934 1270
bec39940
DG
1271 dbg_printf("create bucket: order 0 index 0 hash 0\n");
1272 node = bucket_at(ht, 0);
1273 node->next = flag_bucket(get_end());
1274 node->reverse_hash = 0;
d6b18934 1275
bec39940 1276 for (order = 1; order < cds_lfht_get_count_order_ulong(size) + 1; order++) {
d6b18934 1277 len = 1UL << (order - 1);
bec39940 1278 cds_lfht_alloc_bucket_table(ht, order);
d6b18934 1279
bec39940
DG
1280 for (i = 0; i < len; i++) {
1281 /*
1282 * Now, we are trying to init the node with the
1283 * hash=(len+i) (which is also a bucket with the
1284 * index=(len+i)) and insert it into the hash table,
1285 * so this node has to be inserted after the bucket
1286 * with the index=(len+i)&(len-1)=i. And because there
1287 * is no other non-bucket node nor bucket node with
1288 * larger index/hash inserted, so the bucket node
1289 * being inserted should be inserted directly linked
1290 * after the bucket node with index=i.
1291 */
1292 prev = bucket_at(ht, i);
1293 node = bucket_at(ht, len + i);
1294
1295 dbg_printf("create bucket: order %lu index %lu hash %lu\n",
1296 order, len + i, len + i);
1297 node->reverse_hash = bit_reverse_ulong(len + i);
d6b18934 1298
bec39940
DG
1299 /* insert after prev */
1300 assert(is_bucket(prev->next));
d6b18934 1301 node->next = prev->next;
bec39940 1302 prev->next = flag_bucket(node);
d6b18934
DG
1303 }
1304 }
fa68aa62
MD
1305}
1306
bec39940
DG
1307struct cds_lfht *_cds_lfht_new(unsigned long init_size,
1308 unsigned long min_nr_alloc_buckets,
1309 unsigned long max_nr_buckets,
fa68aa62 1310 int flags,
bec39940
DG
1311 const struct cds_lfht_mm_type *mm,
1312 const struct rcu_flavor_struct *flavor,
fa68aa62
MD
1313 pthread_attr_t *attr)
1314{
1315 struct cds_lfht *ht;
1316 unsigned long order;
1317
bec39940
DG
1318 /* min_nr_alloc_buckets must be power of two */
1319 if (!min_nr_alloc_buckets || (min_nr_alloc_buckets & (min_nr_alloc_buckets - 1)))
d6b18934 1320 return NULL;
bec39940 1321
fa68aa62 1322 /* init_size must be power of two */
d6b18934 1323 if (!init_size || (init_size & (init_size - 1)))
fa68aa62 1324 return NULL;
bec39940
DG
1325
1326 /*
1327 * Memory management plugin default.
1328 */
1329 if (!mm) {
1330 if (CAA_BITS_PER_LONG > 32
1331 && max_nr_buckets
1332 && max_nr_buckets <= (1ULL << 32)) {
1333 /*
1334 * For 64-bit architectures, with max number of
1335 * buckets small enough not to use the entire
1336 * 64-bit memory mapping space (and allowing a
1337 * fair number of hash table instances), use the
1338 * mmap allocator, which is faster than the
1339 * order allocator.
1340 */
1341 mm = &cds_lfht_mm_mmap;
1342 } else {
1343 /*
1344 * The fallback is to use the order allocator.
1345 */
1346 mm = &cds_lfht_mm_order;
1347 }
1348 }
1349
1350 /* max_nr_buckets == 0 for order based mm means infinite */
1351 if (mm == &cds_lfht_mm_order && !max_nr_buckets)
1352 max_nr_buckets = 1UL << (MAX_TABLE_ORDER - 1);
1353
1354 /* max_nr_buckets must be power of two */
1355 if (!max_nr_buckets || (max_nr_buckets & (max_nr_buckets - 1)))
1356 return NULL;
1357
1358 min_nr_alloc_buckets = max(min_nr_alloc_buckets, MIN_TABLE_SIZE);
1359 init_size = max(init_size, MIN_TABLE_SIZE);
1360 max_nr_buckets = max(max_nr_buckets, min_nr_alloc_buckets);
1361 init_size = min(init_size, max_nr_buckets);
1362
1363 ht = mm->alloc_cds_lfht(min_nr_alloc_buckets, max_nr_buckets);
fa68aa62 1364 assert(ht);
bec39940
DG
1365 assert(ht->mm == mm);
1366 assert(ht->bucket_at == mm->bucket_at);
1367
d6b18934 1368 ht->flags = flags;
bec39940 1369 ht->flavor = flavor;
fa68aa62 1370 ht->resize_attr = attr;
d6b18934 1371 alloc_split_items_count(ht);
fa68aa62
MD
1372 /* this mutex should not nest in read-side C.S. */
1373 pthread_mutex_init(&ht->resize_mutex, NULL);
bec39940
DG
1374 order = cds_lfht_get_count_order_ulong(init_size);
1375 ht->resize_target = 1UL << order;
1376 cds_lfht_create_bucket(ht, 1UL << order);
1377 ht->size = 1UL << order;
fa68aa62
MD
1378 return ht;
1379}
1380
bec39940
DG
1381void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
1382 cds_lfht_match_fct match, const void *key,
fa68aa62
MD
1383 struct cds_lfht_iter *iter)
1384{
bec39940
DG
1385 struct cds_lfht_node *node, *next, *bucket;
1386 unsigned long reverse_hash, size;
fa68aa62 1387
fa68aa62
MD
1388 reverse_hash = bit_reverse_ulong(hash);
1389
bec39940
DG
1390 size = rcu_dereference(ht->size);
1391 bucket = lookup_bucket(ht, size, hash);
1392 /* We can always skip the bucket node initially */
1393 node = rcu_dereference(bucket->next);
fa68aa62
MD
1394 node = clear_flag(node);
1395 for (;;) {
6e59ae26 1396 if (caa_unlikely(is_end(node))) {
fa68aa62
MD
1397 node = next = NULL;
1398 break;
1399 }
bec39940 1400 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
fa68aa62
MD
1401 node = next = NULL;
1402 break;
1403 }
bec39940 1404 next = rcu_dereference(node->next);
d6b18934 1405 assert(node == clear_flag(node));
6e59ae26 1406 if (caa_likely(!is_removed(next))
bec39940
DG
1407 && !is_bucket(next)
1408 && node->reverse_hash == reverse_hash
1409 && caa_likely(match(node, key))) {
fa68aa62
MD
1410 break;
1411 }
1412 node = clear_flag(next);
1413 }
bec39940 1414 assert(!node || !is_bucket(rcu_dereference(node->next)));
fa68aa62
MD
1415 iter->node = node;
1416 iter->next = next;
1417}
1418
bec39940
DG
1419void cds_lfht_next_duplicate(struct cds_lfht *ht, cds_lfht_match_fct match,
1420 const void *key, struct cds_lfht_iter *iter)
fa68aa62
MD
1421{
1422 struct cds_lfht_node *node, *next;
1423 unsigned long reverse_hash;
fa68aa62
MD
1424
1425 node = iter->node;
bec39940 1426 reverse_hash = node->reverse_hash;
fa68aa62
MD
1427 next = iter->next;
1428 node = clear_flag(next);
1429
1430 for (;;) {
6e59ae26 1431 if (caa_unlikely(is_end(node))) {
fa68aa62
MD
1432 node = next = NULL;
1433 break;
1434 }
bec39940 1435 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
fa68aa62
MD
1436 node = next = NULL;
1437 break;
1438 }
bec39940 1439 next = rcu_dereference(node->next);
6e59ae26 1440 if (caa_likely(!is_removed(next))
bec39940
DG
1441 && !is_bucket(next)
1442 && caa_likely(match(node, key))) {
fa68aa62
MD
1443 break;
1444 }
1445 node = clear_flag(next);
1446 }
bec39940 1447 assert(!node || !is_bucket(rcu_dereference(node->next)));
fa68aa62
MD
1448 iter->node = node;
1449 iter->next = next;
1450}
1451
f6a9efaa
DG
1452void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1453{
1454 struct cds_lfht_node *node, *next;
1455
1456 node = clear_flag(iter->next);
1457 for (;;) {
6e59ae26 1458 if (caa_unlikely(is_end(node))) {
f6a9efaa
DG
1459 node = next = NULL;
1460 break;
1461 }
bec39940 1462 next = rcu_dereference(node->next);
6e59ae26 1463 if (caa_likely(!is_removed(next))
bec39940 1464 && !is_bucket(next)) {
f6a9efaa
DG
1465 break;
1466 }
1467 node = clear_flag(next);
1468 }
bec39940 1469 assert(!node || !is_bucket(rcu_dereference(node->next)));
f6a9efaa
DG
1470 iter->node = node;
1471 iter->next = next;
1472}
1473
1474void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1475{
f6a9efaa 1476 /*
bec39940 1477 * Get next after first bucket node. The first bucket node is the
f6a9efaa
DG
1478 * first node of the linked list.
1479 */
bec39940 1480 iter->next = bucket_at(ht, 0)->next;
f6a9efaa
DG
1481 cds_lfht_next(ht, iter);
1482}
1483
bec39940
DG
1484void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
1485 struct cds_lfht_node *node)
fa68aa62 1486{
bec39940 1487 unsigned long size;
fa68aa62 1488
bec39940
DG
1489 node->reverse_hash = bit_reverse_ulong(hash);
1490 size = rcu_dereference(ht->size);
1491 _cds_lfht_add(ht, hash, NULL, NULL, size, node, NULL, 0);
d6b18934 1492 ht_count_add(ht, size, hash);
fa68aa62
MD
1493}
1494
1495struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
bec39940
DG
1496 unsigned long hash,
1497 cds_lfht_match_fct match,
1498 const void *key,
fa68aa62
MD
1499 struct cds_lfht_node *node)
1500{
bec39940 1501 unsigned long size;
d6b18934 1502 struct cds_lfht_iter iter;
fa68aa62 1503
bec39940
DG
1504 node->reverse_hash = bit_reverse_ulong(hash);
1505 size = rcu_dereference(ht->size);
1506 _cds_lfht_add(ht, hash, match, key, size, node, &iter, 0);
d6b18934
DG
1507 if (iter.node == node)
1508 ht_count_add(ht, size, hash);
1509 return iter.node;
fa68aa62
MD
1510}
1511
1512struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
bec39940
DG
1513 unsigned long hash,
1514 cds_lfht_match_fct match,
1515 const void *key,
fa68aa62
MD
1516 struct cds_lfht_node *node)
1517{
bec39940 1518 unsigned long size;
d6b18934 1519 struct cds_lfht_iter iter;
fa68aa62 1520
bec39940
DG
1521 node->reverse_hash = bit_reverse_ulong(hash);
1522 size = rcu_dereference(ht->size);
d6b18934 1523 for (;;) {
bec39940 1524 _cds_lfht_add(ht, hash, match, key, size, node, &iter, 0);
d6b18934
DG
1525 if (iter.node == node) {
1526 ht_count_add(ht, size, hash);
1527 return NULL;
1528 }
1529
1530 if (!_cds_lfht_replace(ht, size, iter.node, iter.next, node))
1531 return iter.node;
1532 }
fa68aa62
MD
1533}
1534
bec39940
DG
1535int cds_lfht_replace(struct cds_lfht *ht,
1536 struct cds_lfht_iter *old_iter,
1537 unsigned long hash,
1538 cds_lfht_match_fct match,
1539 const void *key,
fa68aa62
MD
1540 struct cds_lfht_node *new_node)
1541{
1542 unsigned long size;
1543
bec39940
DG
1544 new_node->reverse_hash = bit_reverse_ulong(hash);
1545 if (!old_iter->node)
1546 return -ENOENT;
1547 if (caa_unlikely(old_iter->node->reverse_hash != new_node->reverse_hash))
1548 return -EINVAL;
1549 if (caa_unlikely(!match(old_iter->node, key)))
1550 return -EINVAL;
1551 size = rcu_dereference(ht->size);
fa68aa62
MD
1552 return _cds_lfht_replace(ht, size, old_iter->node, old_iter->next,
1553 new_node);
1554}
1555
bec39940 1556int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node)
fa68aa62 1557{
d6b18934 1558 unsigned long size, hash;
fa68aa62
MD
1559 int ret;
1560
bec39940
DG
1561 size = rcu_dereference(ht->size);
1562 ret = _cds_lfht_del(ht, size, node);
d6b18934 1563 if (!ret) {
bec39940 1564 hash = bit_reverse_ulong(node->reverse_hash);
d6b18934
DG
1565 ht_count_del(ht, size, hash);
1566 }
fa68aa62
MD
1567 return ret;
1568}
1569
1570static
bec39940 1571int cds_lfht_delete_bucket(struct cds_lfht *ht)
fa68aa62
MD
1572{
1573 struct cds_lfht_node *node;
fa68aa62
MD
1574 unsigned long order, i, size;
1575
1576 /* Check that the table is empty */
bec39940 1577 node = bucket_at(ht, 0);
fa68aa62 1578 do {
bec39940
DG
1579 node = clear_flag(node)->next;
1580 if (!is_bucket(node))
fa68aa62
MD
1581 return -EPERM;
1582 assert(!is_removed(node));
1583 } while (!is_end(node));
1584 /*
1585 * size accessed without rcu_dereference because hash table is
1586 * being destroyed.
1587 */
bec39940
DG
1588 size = ht->size;
1589 /* Internal sanity check: all nodes left should be bucket */
1590 for (i = 0; i < size; i++) {
1591 node = bucket_at(ht, i);
1592 dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n",
1593 i, i, bit_reverse_ulong(node->reverse_hash));
1594 assert(is_bucket(node->next));
1595 }
fa68aa62 1596
bec39940
DG
1597 for (order = cds_lfht_get_count_order_ulong(size); (long)order >= 0; order--)
1598 cds_lfht_free_bucket_table(ht, order);
d6b18934 1599
fa68aa62
MD
1600 return 0;
1601}
1602
1603/*
1604 * Should only be called when no more concurrent readers nor writers can
1605 * possibly access the table.
1606 */
1607int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr)
1608{
1609 int ret;
1610
1611 /* Wait for in-flight resize operations to complete */
d6b18934
DG
1612 _CMM_STORE_SHARED(ht->in_progress_destroy, 1);
1613 cmm_smp_mb(); /* Store destroy before load resize */
fa68aa62
MD
1614 while (uatomic_read(&ht->in_progress_resize))
1615 poll(NULL, 0, 100); /* wait for 100ms */
bec39940 1616 ret = cds_lfht_delete_bucket(ht);
fa68aa62
MD
1617 if (ret)
1618 return ret;
d6b18934 1619 free_split_items_count(ht);
fa68aa62
MD
1620 if (attr)
1621 *attr = ht->resize_attr;
1622 poison_free(ht);
1623 return ret;
1624}
1625
1626void cds_lfht_count_nodes(struct cds_lfht *ht,
1627 long *approx_before,
1628 unsigned long *count,
fa68aa62
MD
1629 long *approx_after)
1630{
1631 struct cds_lfht_node *node, *next;
bec39940 1632 unsigned long nr_bucket = 0, nr_removed = 0;
fa68aa62
MD
1633
1634 *approx_before = 0;
d6b18934 1635 if (ht->split_count) {
fa68aa62
MD
1636 int i;
1637
d6b18934
DG
1638 for (i = 0; i < split_count_mask + 1; i++) {
1639 *approx_before += uatomic_read(&ht->split_count[i].add);
1640 *approx_before -= uatomic_read(&ht->split_count[i].del);
fa68aa62
MD
1641 }
1642 }
1643
1644 *count = 0;
fa68aa62 1645
bec39940
DG
1646 /* Count non-bucket nodes in the table */
1647 node = bucket_at(ht, 0);
fa68aa62 1648 do {
bec39940 1649 next = rcu_dereference(node->next);
fa68aa62 1650 if (is_removed(next)) {
bec39940
DG
1651 if (!is_bucket(next))
1652 (nr_removed)++;
fa68aa62 1653 else
bec39940
DG
1654 (nr_bucket)++;
1655 } else if (!is_bucket(next))
fa68aa62
MD
1656 (*count)++;
1657 else
bec39940 1658 (nr_bucket)++;
fa68aa62
MD
1659 node = clear_flag(next);
1660 } while (!is_end(node));
bec39940
DG
1661 dbg_printf("number of logically removed nodes: %lu\n", nr_removed);
1662 dbg_printf("number of bucket nodes: %lu\n", nr_bucket);
fa68aa62 1663 *approx_after = 0;
d6b18934 1664 if (ht->split_count) {
fa68aa62
MD
1665 int i;
1666
d6b18934
DG
1667 for (i = 0; i < split_count_mask + 1; i++) {
1668 *approx_after += uatomic_read(&ht->split_count[i].add);
1669 *approx_after -= uatomic_read(&ht->split_count[i].del);
fa68aa62
MD
1670 }
1671 }
1672}
1673
1674/* called with resize mutex held */
1675static
1676void _do_cds_lfht_grow(struct cds_lfht *ht,
1677 unsigned long old_size, unsigned long new_size)
1678{
1679 unsigned long old_order, new_order;
1680
bec39940
DG
1681 old_order = cds_lfht_get_count_order_ulong(old_size);
1682 new_order = cds_lfht_get_count_order_ulong(new_size);
d6b18934
DG
1683 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1684 old_size, old_order, new_size, new_order);
fa68aa62 1685 assert(new_size > old_size);
d6b18934 1686 init_table(ht, old_order + 1, new_order);
fa68aa62
MD
1687}
1688
1689/* called with resize mutex held */
1690static
1691void _do_cds_lfht_shrink(struct cds_lfht *ht,
1692 unsigned long old_size, unsigned long new_size)
1693{
1694 unsigned long old_order, new_order;
1695
bec39940
DG
1696 new_size = max(new_size, MIN_TABLE_SIZE);
1697 old_order = cds_lfht_get_count_order_ulong(old_size);
1698 new_order = cds_lfht_get_count_order_ulong(new_size);
d6b18934
DG
1699 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1700 old_size, old_order, new_size, new_order);
fa68aa62
MD
1701 assert(new_size < old_size);
1702
bec39940 1703 /* Remove and unlink all bucket nodes to remove. */
d6b18934 1704 fini_table(ht, new_order + 1, old_order);
fa68aa62
MD
1705}
1706
1707
1708/* called with resize mutex held */
1709static
1710void _do_cds_lfht_resize(struct cds_lfht *ht)
1711{
1712 unsigned long new_size, old_size;
1713
1714 /*
1715 * Resize table, re-do if the target size has changed under us.
1716 */
1717 do {
d6b18934
DG
1718 assert(uatomic_read(&ht->in_progress_resize));
1719 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1720 break;
bec39940
DG
1721 ht->resize_initiated = 1;
1722 old_size = ht->size;
1723 new_size = CMM_LOAD_SHARED(ht->resize_target);
fa68aa62
MD
1724 if (old_size < new_size)
1725 _do_cds_lfht_grow(ht, old_size, new_size);
1726 else if (old_size > new_size)
1727 _do_cds_lfht_shrink(ht, old_size, new_size);
bec39940 1728 ht->resize_initiated = 0;
fa68aa62
MD
1729 /* write resize_initiated before read resize_target */
1730 cmm_smp_mb();
bec39940 1731 } while (ht->size != CMM_LOAD_SHARED(ht->resize_target));
fa68aa62
MD
1732}
1733
1734static
bec39940 1735unsigned long resize_target_grow(struct cds_lfht *ht, unsigned long new_size)
fa68aa62 1736{
bec39940 1737 return _uatomic_xchg_monotonic_increase(&ht->resize_target, new_size);
fa68aa62
MD
1738}
1739
1740static
1741void resize_target_update_count(struct cds_lfht *ht,
1742 unsigned long count)
1743{
bec39940
DG
1744 count = max(count, MIN_TABLE_SIZE);
1745 count = min(count, ht->max_nr_buckets);
1746 uatomic_set(&ht->resize_target, count);
fa68aa62
MD
1747}
1748
1749void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size)
1750{
1751 resize_target_update_count(ht, new_size);
bec39940
DG
1752 CMM_STORE_SHARED(ht->resize_initiated, 1);
1753 ht->flavor->thread_offline();
fa68aa62
MD
1754 pthread_mutex_lock(&ht->resize_mutex);
1755 _do_cds_lfht_resize(ht);
1756 pthread_mutex_unlock(&ht->resize_mutex);
bec39940 1757 ht->flavor->thread_online();
fa68aa62
MD
1758}
1759
1760static
1761void do_resize_cb(struct rcu_head *head)
1762{
1763 struct rcu_resize_work *work =
1764 caa_container_of(head, struct rcu_resize_work, head);
1765 struct cds_lfht *ht = work->ht;
1766
bec39940 1767 ht->flavor->thread_offline();
fa68aa62
MD
1768 pthread_mutex_lock(&ht->resize_mutex);
1769 _do_cds_lfht_resize(ht);
1770 pthread_mutex_unlock(&ht->resize_mutex);
bec39940 1771 ht->flavor->thread_online();
fa68aa62
MD
1772 poison_free(work);
1773 cmm_smp_mb(); /* finish resize before decrement */
1774 uatomic_dec(&ht->in_progress_resize);
1775}
1776
1777static
bec39940 1778void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht)
fa68aa62
MD
1779{
1780 struct rcu_resize_work *work;
fa68aa62 1781
fa68aa62
MD
1782 /* Store resize_target before read resize_initiated */
1783 cmm_smp_mb();
bec39940 1784 if (!CMM_LOAD_SHARED(ht->resize_initiated)) {
fa68aa62 1785 uatomic_inc(&ht->in_progress_resize);
d6b18934
DG
1786 cmm_smp_mb(); /* increment resize count before load destroy */
1787 if (CMM_LOAD_SHARED(ht->in_progress_destroy)) {
1788 uatomic_dec(&ht->in_progress_resize);
1789 return;
1790 }
fa68aa62
MD
1791 work = malloc(sizeof(*work));
1792 work->ht = ht;
bec39940
DG
1793 ht->flavor->update_call_rcu(&work->head, do_resize_cb);
1794 CMM_STORE_SHARED(ht->resize_initiated, 1);
fa68aa62
MD
1795 }
1796}
1797
bec39940
DG
1798static
1799void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth)
1800{
1801 unsigned long target_size = size << growth;
1802
1803 target_size = min(target_size, ht->max_nr_buckets);
1804 if (resize_target_grow(ht, target_size) >= target_size)
1805 return;
1806
1807 __cds_lfht_resize_lazy_launch(ht);
1808}
1809
1810/*
1811 * We favor grow operations over shrink. A shrink operation never occurs
1812 * if a grow operation is queued for lazy execution. A grow operation
1813 * cancels any pending shrink lazy execution.
1814 */
fa68aa62
MD
1815static
1816void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
1817 unsigned long count)
1818{
fa68aa62
MD
1819 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
1820 return;
bec39940
DG
1821 count = max(count, MIN_TABLE_SIZE);
1822 count = min(count, ht->max_nr_buckets);
1823 if (count == size)
1824 return; /* Already the right size, no resize needed */
1825 if (count > size) { /* lazy grow */
1826 if (resize_target_grow(ht, count) >= count)
d6b18934 1827 return;
bec39940
DG
1828 } else { /* lazy shrink */
1829 for (;;) {
1830 unsigned long s;
1831
1832 s = uatomic_cmpxchg(&ht->resize_target, size, count);
1833 if (s == size)
1834 break; /* no resize needed */
1835 if (s > size)
1836 return; /* growing is/(was just) in progress */
1837 if (s <= count)
1838 return; /* some other thread do shrink */
1839 size = s;
d6b18934 1840 }
fa68aa62 1841 }
bec39940 1842 __cds_lfht_resize_lazy_launch(ht);
fa68aa62 1843}
This page took 0.106932 seconds and 4 git commands to generate.