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