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