Fix: change order of _cds_lfht_new_with_alloc parameters
[urcu.git] / README.md
... / ...
CommitLineData
1<!--
2SPDX-FileCopyrightText: 2023 EfficiOS Inc.
3
4SPDX-License-Identifier: CC-BY-4.0
5-->
6
7Userspace RCU Implementation
8============================
9
10by Mathieu Desnoyers and Paul E. McKenney
11
12
13Building
14--------
15
16 ./bootstrap # skip if using tarball
17 ./configure
18 make
19 make install
20 ldconfig
21
22Hints:
23
24 - Forcing 32-bit build:
25
26 CFLAGS="-m32 -g -O2" ./configure
27
28 - Forcing 64-bit build:
29
30 CFLAGS="-m64 -g -O2" ./configure
31
32 - Forcing a 32-bit build with 386 backward compatibility:
33
34 CFLAGS="-m32 -g -O2" ./configure --host=i386-pc-linux-gnu
35
36 - Forcing a 32-bit build for Sparcv9 (typical for Sparc v9)
37
38 CFLAGS="-m32 -Wa,-Av9a -g -O2" ./configure
39
40
41Architectures supported
42-----------------------
43
44Currently, the following architectures are supported:
45
46 - x86 (i386, i486, i586, i686)
47 - amd64 / x86\_64
48 - PowerPC 32/64
49 - S390, S390x
50 - ARM 32/64
51 - MIPS
52 - NIOS2
53 - Alpha
54 - ia64
55 - Sparcv9 32/64
56 - Tilera
57 - hppa/PA-RISC
58 - m68k
59 - RISC-V
60 - LoongArch
61
62Tested on:
63
64 - Linux all architectures
65 - FreeBSD 13 i386/amd64
66 - Cygwin i386/amd64
67 - MacOS amd64/arm64
68
69Should also work on:
70
71 - Android
72 - NetBSD 5
73 - OpenBSD
74 - Solaris
75
76(more testing needed before claiming support for these OS).
77
78
79Toolchain support
80-----------------
81
82The C compiler used needs to support at least C99. The C++ compiler used needs
83to support at least C++11. The oldest GCC version officialy supported and
84tested is 4.8.
85
86Older GCC versions might still work with the following exceptions:
87
88 - GCC 3.3 and 3.4 have a bug that prevents them from generating volatile
89 accesses to offsets in a TLS structure on 32-bit x86. These versions are
90 therefore not compatible with `liburcu` on x86 32-bit
91 (i386, i486, i586, i686).
92 The problem has been reported to the GCC community:
93 <http://www.mail-archive.com/gcc-bugs@gcc.gnu.org/msg281255.html>
94 - GCC 3.3 cannot match the "xchg" instruction on 32-bit x86 build.
95 See <http://kerneltrap.org/node/7507>
96 - Alpha, ia64 and ARM architectures depend on GCC 4.x with atomic builtins
97 support. For ARM this was introduced with GCC 4.4:
98 <http://gcc.gnu.org/gcc-4.4/changes.html>.
99 - Linux aarch64 depends on GCC 5.1 or better because prior versions
100 perform unsafe access to deallocated stack.
101
102Clang version 3.0 (based on LLVM 3.0) is supported.
103
104Glibc >= 2.4 should work but the older version we test against is
105currently 2.17.
106
107
108Build system
109------------
110
111For developers using the Git tree:
112
113This source tree is based on the autotools suite from GNU to simplify
114portability. Here are some things you should have on your system in order to
115compile the git repository tree :
116
117 - GNU autotools (automake >=1.12, autoconf >=2.69)
118 (make sure your system wide `automake` points to a recent version!)
119 - GNU Libtool >=2.2
120 (for more information, go to <http://www.gnu.org/software/autoconf/>)
121
122If you get the tree from the repository, you will need to use the `bootstrap`
123script in the root of the tree. It calls all the GNU tools needed to prepare
124the tree configuration.
125
126Test scripts provided in the `tests/` directory of the source tree depend
127on `bash` and the `seq` program.
128
129
130API
131---
132
133See the relevant API documentation files in `doc/`. The APIs provided by
134Userspace RCU are, by prefix:
135
136 - `rcu_`: Read-Copy Update (see [`doc/rcu-api.md`](doc/rcu-api.md))
137 - `cmm_`: Concurrent Memory Model
138 - `caa_`: Concurrent Architecture Abstraction
139 - `cds_`: Concurrent Data Structures
140 (see [`doc/cds-api.md`](doc/cds-api.md))
141 - `uatomic_`: Userspace Atomic
142 (see [`doc/uatomic-api.md`](doc/uatomic-api.md))
143
144
145Quick start guide
146-----------------
147
148### Usage of all urcu libraries:
149
150 - Define `_LGPL_SOURCE` (only) if your code is LGPL or GPL compatible
151 before including the `urcu.h` or `urcu-qsbr.h` header. If your application
152 is distributed under another license, function calls will be generated
153 instead of inlines, so your application can link with the library.
154 - Linking with one of the libraries below is always necessary even for
155 LGPL and GPL applications.
156 - Define `URCU_INLINE_SMALL_FUNCTIONS` before including Userspace RCU
157 headers if you want Userspace RCU to inline small functions (10
158 lines or less) into the application. It can be used by applications
159 distributed under any kind of license, and does *not* make the
160 application a derived work of Userspace RCU.
161
162Those small inlined functions are guaranteed to match the library
163content as long as the library major version is unchanged.
164Therefore, the application *must* be compiled with headers matching
165the library major version number. Applications using
166`URCU_INLINE_SMALL_FUNCTIONS` may be unable to use debugging
167features of Userspace RCU without being recompiled.
168
169There are multiple flavors of liburcu available:
170
171 - `memb`,
172 - `qsbr`,
173 - `mb`,
174 - `bp`.
175
176The API members start with the prefix `urcu_<flavor>_`, where
177`<flavor>` is the chosen flavor name.
178
179
180### Usage of `liburcu-memb`
181
182 1. `#include <urcu/urcu-memb.h>`
183 2. Link the application with `-lurcu-memb`
184
185This is the preferred version of the library, in terms of
186grace-period detection speed, read-side speed and flexibility.
187Dynamically detects kernel support for `sys_membarrier()`. Falls back
188on `urcu-mb` scheme if support is not present, which has slower
189read-side. Use the `--disable-sys-membarrier-fallback` configure option
190to disable the fall back, thus requiring `sys_membarrier()` to be
191available. This gives a small speedup when `sys_membarrier()` is
192supported by the kernel, and aborts in the library constructor if not
193supported.
194
195
196### Usage of `liburcu-qsbr`
197
198 1. `#include <urcu/urcu-qsbr.h>`
199 2. Link with `-lurcu-qsbr`
200
201The QSBR flavor of RCU needs to have each reader thread executing
202`rcu_quiescent_state()` periodically to progress. `rcu_thread_online()`
203and `rcu_thread_offline()` can be used to mark long periods for which
204the threads are not active. It provides the fastest read-side at the
205expense of more intrusiveness in the application code.
206
207
208### Usage of `liburcu-mb`
209
210 1. `#include <urcu/urcu-mb.h>`
211 2. Link with `-lurcu-mb`
212
213This version of the urcu library uses memory barriers on the writer
214and reader sides. This results in faster grace-period detection, but
215results in slower reads.
216
217
218### Usage of `liburcu-bp`
219
220 1. `#include <urcu/urcu-bp.h>`
221 2. Link with `-lurcu-bp`
222
223The BP library flavor stands for "bulletproof". It is specifically
224designed to help tracing library to hook on applications without
225requiring to modify these applications. `urcu_bp_init()`, and
226`urcu_bp_unregister_thread()` all become nops, whereas calling
227`urcu_bp_register_thread()` becomes optional. The state is dealt with by
228the library internally at the expense of read-side and write-side
229performance.
230
231
232### Initialization
233
234Each thread that has reader critical sections (that uses
235`urcu_<flavor>_read_lock()`/`urcu_<flavor>_read_unlock()` must first
236register to the URCU library. This is done by calling
237`urcu_<flavor>_register_thread()`. Unregistration must be performed
238before exiting the thread by using `urcu_<flavor>_unregister_thread()`.
239
240
241### Reading
242
243Reader critical sections must be protected by locating them between
244calls to `urcu_<flavor>_read_lock()` and `urcu_<flavor>_read_unlock()`.
245Inside that lock, `rcu_dereference()` may be called to read an RCU
246protected pointer.
247
248
249### Writing
250
251`rcu_assign_pointer()` and `rcu_xchg_pointer()` may be called anywhere.
252After, `urcu_<flavor>_synchronize_rcu()` must be called. When it
253returns, the old values are not in usage anymore.
254
255As an alternative to `urcu_<flavor>_synchronize_rcu()`,
256it is also possible to use the urcu polling mechanism to wait for a
257grace period to elapse. This can be done by using
258`urcu_<flavor>_start_poll_synchronize_rcu()`
259to start the grace period polling, and then invoke
260`urcu_<flavor>_poll_state_synchronize_rcu()`, which returns true if
261the grace period has completed, false otherwise.
262
263
264### Usage of `liburcu-defer`
265
266 - Follow instructions for either `liburcu-memb`, `liburcu-qsbr`,
267 `liburcu-mb`, or `liburcu-bp` above.
268 The `liburcu-defer` functionality is pulled into each of
269 those library modules.
270 - Provides `urcu_<flavor>_defer_rcu()` primitive to enqueue delayed
271 callbacks. Queued callbacks are executed in batch periodically after
272 a grace period. Do _not_ use `urcu_<flavor>_defer_rcu()` within a
273 read-side critical section, because it may call
274 `urcu_<flavor>_synchronize_rcu()` if the thread queue is full. This
275 can lead to deadlock or worse.
276 - Requires that `urcu_<flavor>_defer_barrier()` must be called in
277 library destructor if a library queues callbacks and is expected to
278 be unloaded with `dlclose()`.
279
280Its API is currently experimental. It may change in future library releases.
281
282
283### Usage of `urcu-call-rcu`
284
285 - Follow instructions for either `liburcu-memb`, `liburcu-qsbr`,
286 `liburcu-mb`, or `liburcu-bp` above.
287 The `urcu-call-rcu` functionality is pulled into each of
288 those library modules.
289 - Provides the `urcu_<flavor>_call_rcu()` primitive to enqueue delayed
290 callbacks in a manner similar to `urcu_<flavor>_defer_rcu()`, but
291 without ever delaying for a grace period. On the other hand,
292 `urcu_<flavor>_call_rcu()`'s best-case overhead is not quite as good
293 as that of `urcu_<flavor>_defer_rcu()`.
294 - Provides `urcu_<flavor>_call_rcu()` to allow asynchronous handling
295 of RCU grace periods. A number of additional functions are provided
296 to manage the helper threads used by `urcu_<flavor>_call_rcu()`, but
297 reasonable defaults are used if these additional functions are not
298 invoked. See [`doc/rcu-api.md`](doc/rcu-api.md) in userspace-rcu
299 documentation for more details.
300
301
302### Being careful with signals
303
304Read-side critical sections are allowed in a signal handler,
305except those setup with `sigaltstack(2)`, with `liburcu-memb` and
306`liburcu-mb`. Be careful, however, to disable these signals
307between thread creation and calls to `urcu_<flavor>_register_thread()`,
308because a signal handler nesting on an unregistered thread would not be
309allowed to call `urcu_<flavor>_read_lock()`.
310
311Read-side critical sections are _not_ allowed in a signal handler with
312`liburcu-qsbr`, unless signals are disabled explicitly around each
313`urcu_qsbr_quiescent_state()` calls, when threads are put offline and around
314calls to `urcu_qsbr_synchronize_rcu()`. Even then, we do not recommend it.
315
316
317### Interaction with mutexes
318
319One must be careful to do not cause deadlocks due to interaction of
320`urcu_<flavor>_synchronize_rcu()` and RCU read-side with mutexes. If
321`urcu_<flavor>_synchronize_rcu()` is called with a mutex held, this
322mutex (or any mutex which has this mutex in its dependency chain) should
323not be acquired from within a RCU read-side critical section.
324
325This is especially important to understand in the context of the
326QSBR flavor: a registered reader thread being "online" by
327default should be considered as within a RCU read-side critical
328section unless explicitly put "offline". Therefore, if
329`urcu_qsbr_synchronize_rcu()` is called with a mutex held, this mutex,
330as well as any mutex which has this mutex in its dependency chain should
331only be taken when the RCU reader thread is "offline" (this can be
332performed by calling `urcu_qsbr_thread_offline()`).
333
334
335### Interaction with `fork()`
336
337Special care must be taken for applications performing `fork()` without
338any following `exec()`. This is caused by the fact that Linux only clones
339the thread calling `fork()`, and thus never replicates any of the other
340parent thread into the child process. Most `liburcu` implementations
341require that all registrations (as reader, `defer_rcu` and `call_rcu`
342threads) should be released before a `fork()` is performed, except for the
343rather common scenario where `fork()` is immediately followed by `exec()` in
344the child process. The only implementation not subject to that rule is
345`liburcu-bp`, which is designed to handle `fork()` by calling
346`urcu_bp_before_fork`, `urcu_bp_after_fork_parent` and
347`urcu_bp_after_fork_child`.
348
349Applications that use `urcu_<flavor>_call_rcu()` and that `fork()`
350without doing an immediate `exec()` must take special action. The
351parent must invoke `urcu_<flavor>_call_rcu_before_fork()` before the
352`fork()` and `urcu_<flavor>_call_rcu_after_fork_parent()` after the
353`fork()`. The child process must invoke
354`urcu_<flavor>_call_rcu_after_fork_child()`. Even though these three
355APIs are suitable for passing to `pthread_atfork()`, use of
356`pthread_atfork()` is **STRONGLY DISCOURAGED** for programs calling the
357glibc memory allocator (`malloc()`, `calloc()`, `free()`, ...) within
358`urcu_<flavor>_call_rcu` callbacks. This is due to limitations in the
359way glibc memory allocator handles calls to the memory allocator from
360concurrent threads while the `pthread_atfork()` handlers are executing.
361
362Combining e.g.:
363
364 - call to `free()` from callbacks executed within
365 `urcu_<flavor>_call_rcu` worker threads,
366 - executing `urcu_<flavor>_call_rcu` atfork handlers within the glibc
367 pthread atfork mechanism,
368
369will sometimes trigger interesting process hangs. This usually
370hangs on a memory allocator lock within glibc.
371
372
373### Thread Local Storage (TLS)
374
375Userspace RCU can fall back on `pthread_getspecific()` to emulate
376TLS variables on systems where it is not available. This behavior
377can be forced by specifying `--disable-compiler-tls` as configure
378argument.
379
380
381### Usage of `DEBUG_RCU` & `--enable-rcu-debug`
382
383By default the library is configured with internal debugging
384self-checks disabled.
385
386For always-on debugging self-checks:
387
388 ./configure --enable-rcu-debug
389
390For fine grained enabling of debugging self-checks, build
391userspace-rcu with `DEBUG_RCU` defined and compile dependent
392applications with `DEBUG_RCU` defined when necessary.
393
394Warning: Enabling this feature result in a performance penalty.
395
396
397### Usage of `DEBUG_YIELD`
398
399`DEBUG_YIELD` is used to add random delays in the code for testing
400purposes.
401
402
403### SMP support
404
405By default the library is configured to use synchronization primitives
406adequate for SMP systems. On uniprocessor systems, support for SMP
407systems can be disabled with:
408
409 ./configure --disable-smp-support
410
411theoretically yielding slightly better performance.
412
413
414### Usage of `--enable-cds-lfht-iter-debug`
415
416By default the library is configured with extra debugging checks for
417lock-free hash table iterator traversal disabled.
418
419Building liburcu with `--enable-cds-lfht-iter-debug` and rebuilding
420application to match the ABI change allows finding cases where the hash
421table iterator is re-purposed to be used on a different hash table while
422still being used to iterate on a hash table.
423
424This option alters the rculfhash ABI. Make sure to compile both library
425and application with matching configuration.
426
427### Usage of `--enable-compiler-atomic-builtins`
428
429Building liburcu with `--enable-compiler-atomic-builtins` implements the uatomic
430API with the compiler atomic builtins if supported.
431
432Make targets
433------------
434
435In addition to the usual `make check` target, Userspace RCU features
436`make regtest`, `make short_bench` and `make long_bench` targets:
437
438 - `make check`: short tests, meant to be run when rebuilding or
439 porting Userspace RCU.
440 - `make regtest`: long (many hours) test, meant to be run when
441 modifying Userspace RCU or porting it to a new architecture or
442 operating system.
443 - `make short_bench`: short benchmarks, 3 seconds per test.
444 - `make long_bench`: long (many hours) benchmarks, 30 seconds per test.
445
446
447Known issues
448------------
449
450There is an application vs library compatibility issue between
451applications built using Userspace RCU 0.10 headers linked against
452Userspace RCU 0.11 or 0.12 shared objects. The problem occurs as
453follows:
454
455 - An application executable is built with `_LGPL_SOURCE` defined, includes
456 any of the Userspace RCU 0.10 urcu flavor headers, and is built
457 without the `-fpic` compiler option.
458
459 - The Userspace RCU 0.10 library shared objects are updated to 0.11
460 or 0.12 without rebuilding the application.
461
462 - The application will hang, typically when RCU grace period
463 (synchronize_rcu) is invoked.
464
465Some possible work-arounds for this are:
466
467 - Rebuild the application against Userspace RCU 0.11+.
468
469 - Rebuild the application with `-fpic`.
470
471 - Upgrade Userspace RCU to 0.13+ without installing 0.11 nor 0.12.
472
473
474Contacts
475--------
476
477You can contact the maintainers on the following mailing list:
478`lttng-dev@lists.lttng.org`.
479
480IRC channel: [#lttng](irc://irc.oftc.net/lttng) on the OFTC network
481
482Bug tracker: [Userspace RCU bug tracker](https://bugs.lttng.org/projects/urcu)
483
484Code review: [_userspace-rcu_ project](https://review.lttng.org/q/project:userspace-rcu) on LTTng Review
485
486Continuous integration: [Userspace RCU](https://ci.lttng.org/view/Liburcu/) on LTTng's CI
487
488GitHub mirror: [urcu/userspace-rcu](https://github.com/urcu/userspace-rcu)
489
490Patches are principally submitted and reviewed on [LTTng Review](https://review.lttng.org),
491but may also be submitted to the [mailing list](mailto:lttng-dev@lists.lttng.org)
492with the subject prefix `PATCH urcu` or by pull request on the
493[GitHub mirror](https://github.com/urcu/userspace-rcu).
This page took 0.024085 seconds and 4 git commands to generate.