2 SPDX-FileCopyrightText: 2023 EfficiOS Inc.
4 SPDX-License-Identifier: CC-BY-4.0
7 Userspace RCU Implementation
8 ============================
10 by Mathieu Desnoyers and Paul E. McKenney
16 ./bootstrap # skip if using tarball
24 - Forcing 32-bit build:
26 CFLAGS="-m32 -g -O2" ./configure
28 - Forcing 64-bit build:
30 CFLAGS="-m64 -g -O2" ./configure
32 - Forcing a 32-bit build with 386 backward compatibility:
34 CFLAGS="-m32 -g -O2" ./configure --host=i386-pc-linux-gnu
36 - Forcing a 32-bit build for Sparcv9 (typical for Sparc v9)
38 CFLAGS="-m32 -Wa,-Av9a -g -O2" ./configure
41 Architectures supported
42 -----------------------
44 Currently, the following architectures are supported:
46 - x86 (i386, i486, i586, i686)
63 - Linux all architectures
64 - FreeBSD 13 i386/amd64
75 (more testing needed before claiming support for these OS).
81 The C compiler used needs to support at least C99. The C++ compiler used needs
82 to support at least C++11. The oldest GCC version officialy supported and
85 Older GCC versions might still work with the following exceptions:
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:
92 <http://www.mail-archive.com/gcc-bugs@gcc.gnu.org/msg281255.html>
93 - GCC 3.3 cannot match the "xchg" instruction on 32-bit x86 build.
94 See <http://kerneltrap.org/node/7507>
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:
97 <http://gcc.gnu.org/gcc-4.4/changes.html>.
98 - Linux aarch64 depends on GCC 5.1 or better because prior versions
99 perform unsafe access to deallocated stack.
101 Clang version 3.0 (based on LLVM 3.0) is supported.
103 Glibc >= 2.4 should work but the older version we test against is
110 For developers using the Git tree:
112 This source tree is based on the autotools suite from GNU to simplify
113 portability. Here are some things you should have on your system in order to
114 compile the git repository tree :
116 - GNU autotools (automake >=1.12, autoconf >=2.69)
117 (make sure your system wide `automake` points to a recent version!)
119 (for more information, go to <http://www.gnu.org/software/autoconf/>)
121 If you get the tree from the repository, you will need to use the `bootstrap`
122 script in the root of the tree. It calls all the GNU tools needed to prepare
123 the tree configuration.
125 Test scripts provided in the `tests/` directory of the source tree depend
126 on `bash` and the `seq` program.
132 See the relevant API documentation files in `doc/`. The APIs provided by
133 Userspace RCU are, by prefix:
135 - `rcu_`: Read-Copy Update (see [`doc/rcu-api.md`](doc/rcu-api.md))
136 - `cmm_`: Concurrent Memory Model
137 - `caa_`: Concurrent Architecture Abstraction
138 - `cds_`: Concurrent Data Structures
139 (see [`doc/cds-api.md`](doc/cds-api.md))
140 - `uatomic_`: Userspace Atomic
141 (see [`doc/uatomic-api.md`](doc/uatomic-api.md))
147 ### Usage of all urcu libraries:
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.
161 Those small inlined functions are guaranteed to match the library
162 content as long as the library major version is unchanged.
163 Therefore, the application *must* be compiled with headers matching
164 the library major version number. Applications using
165 `URCU_INLINE_SMALL_FUNCTIONS` may be unable to use debugging
166 features of Userspace RCU without being recompiled.
168 There are multiple flavors of liburcu available:
176 The API members start with the prefix `urcu_<flavor>_`, where
177 `<flavor>` is the chosen flavor name.
180 ### Usage of `liburcu-memb`
182 1. `#include <urcu/urcu-memb.h>`
183 2. Link the application with `-lurcu-memb`
185 This is the preferred version of the library, in terms of
186 grace-period detection speed, read-side speed and flexibility.
187 Dynamically detects kernel support for `sys_membarrier()`. Falls back
188 on `urcu-mb` scheme if support is not present, which has slower
189 read-side. Use the `--disable-sys-membarrier-fallback` configure option
190 to disable the fall back, thus requiring `sys_membarrier()` to be
191 available. This gives a small speedup when `sys_membarrier()` is
192 supported by the kernel, and aborts in the library constructor if not
196 ### Usage of `liburcu-qsbr`
198 1. `#include <urcu/urcu-qsbr.h>`
199 2. Link with `-lurcu-qsbr`
201 The QSBR flavor of RCU needs to have each reader thread executing
202 `rcu_quiescent_state()` periodically to progress. `rcu_thread_online()`
203 and `rcu_thread_offline()` can be used to mark long periods for which
204 the threads are not active. It provides the fastest read-side at the
205 expense of more intrusiveness in the application code.
208 ### Usage of `liburcu-mb`
210 1. `#include <urcu/urcu-mb.h>`
211 2. Link with `-lurcu-mb`
213 This version of the urcu library uses memory barriers on the writer
214 and reader sides. This results in faster grace-period detection, but
215 results in slower reads.
218 ### Usage of `liburcu-signal`
220 1. `#include <urcu/urcu-signal.h>`
221 2. Link the application with `-lurcu-signal`
223 NOTE: The `liburcu-signal` flavor is *deprecated* and will be removed in the
224 future. It is now identical to `liburcu-mb` at the exception of the symbols and
225 public header files. It is therefore slower than previous versions. Users are
226 encouraged to migrate to the `liburcu-memb` flavor.
228 ### Usage of `liburcu-bp`
230 1. `#include <urcu/urcu-bp.h>`
231 2. Link with `-lurcu-bp`
233 The BP library flavor stands for "bulletproof". It is specifically
234 designed to help tracing library to hook on applications without
235 requiring to modify these applications. `urcu_bp_init()`, and
236 `urcu_bp_unregister_thread()` all become nops, whereas calling
237 `urcu_bp_register_thread()` becomes optional. The state is dealt with by
238 the library internally at the expense of read-side and write-side
244 Each thread that has reader critical sections (that uses
245 `urcu_<flavor>_read_lock()`/`urcu_<flavor>_read_unlock()` must first
246 register to the URCU library. This is done by calling
247 `urcu_<flavor>_register_thread()`. Unregistration must be performed
248 before exiting the thread by using `urcu_<flavor>_unregister_thread()`.
253 Reader critical sections must be protected by locating them between
254 calls to `urcu_<flavor>_read_lock()` and `urcu_<flavor>_read_unlock()`.
255 Inside that lock, `rcu_dereference()` may be called to read an RCU
261 `rcu_assign_pointer()` and `rcu_xchg_pointer()` may be called anywhere.
262 After, `urcu_<flavor>_synchronize_rcu()` must be called. When it
263 returns, the old values are not in usage anymore.
265 As an alternative to `urcu_<flavor>_synchronize_rcu()`,
266 it is also possible to use the urcu polling mechanism to wait for a
267 grace period to elapse. This can be done by using
268 `urcu_<flavor>_start_poll_synchronize_rcu()`
269 to start the grace period polling, and then invoke
270 `urcu_<flavor>_poll_state_synchronize_rcu()`, which returns true if
271 the grace period has completed, false otherwise.
274 ### Usage of `liburcu-defer`
276 - Follow instructions for either `liburcu-memb`, `liburcu-qsbr`,
277 `liburcu-mb`, `liburcu-signal`, or `liburcu-bp` above.
278 The `liburcu-defer` functionality is pulled into each of
279 those library modules.
280 - Provides `urcu_<flavor>_defer_rcu()` primitive to enqueue delayed
281 callbacks. Queued callbacks are executed in batch periodically after
282 a grace period. Do _not_ use `urcu_<flavor>_defer_rcu()` within a
283 read-side critical section, because it may call
284 `urcu_<flavor>_synchronize_rcu()` if the thread queue is full. This
285 can lead to deadlock or worse.
286 - Requires that `urcu_<flavor>_defer_barrier()` must be called in
287 library destructor if a library queues callbacks and is expected to
288 be unloaded with `dlclose()`.
290 Its API is currently experimental. It may change in future library releases.
293 ### Usage of `urcu-call-rcu`
295 - Follow instructions for either `liburcu-memb`, `liburcu-qsbr`,
296 `liburcu-mb`, `liburcu-signal`, or `liburcu-bp` above.
297 The `urcu-call-rcu` functionality is pulled into each of
298 those library modules.
299 - Provides the `urcu_<flavor>_call_rcu()` primitive to enqueue delayed
300 callbacks in a manner similar to `urcu_<flavor>_defer_rcu()`, but
301 without ever delaying for a grace period. On the other hand,
302 `urcu_<flavor>_call_rcu()`'s best-case overhead is not quite as good
303 as that of `urcu_<flavor>_defer_rcu()`.
304 - Provides `urcu_<flavor>_call_rcu()` to allow asynchronous handling
305 of RCU grace periods. A number of additional functions are provided
306 to manage the helper threads used by `urcu_<flavor>_call_rcu()`, but
307 reasonable defaults are used if these additional functions are not
308 invoked. See [`doc/rcu-api.md`](doc/rcu-api.md) in userspace-rcu
309 documentation for more details.
312 ### Being careful with signals
314 The `liburcu-signal` library uses signals internally. The signal handler is
315 registered with the `SA_RESTART` flag. However, these signals may cause
316 some non-restartable system calls to fail with `errno = EINTR`. Care
317 should be taken to restart system calls manually if they fail with this
318 error. A list of non-restartable system calls may be found in
321 Read-side critical sections are allowed in a signal handler,
322 except those setup with `sigaltstack(2)`, with `liburcu-memb` and
323 `liburcu-mb`. Be careful, however, to disable these signals
324 between thread creation and calls to `urcu_<flavor>_register_thread()`,
325 because a signal handler nesting on an unregistered thread would not be
326 allowed to call `urcu_<flavor>_read_lock()`.
328 Read-side critical sections are _not_ allowed in a signal handler with
329 `liburcu-qsbr`, unless signals are disabled explicitly around each
330 `urcu_qsbr_quiescent_state()` calls, when threads are put offline and around
331 calls to `urcu_qsbr_synchronize_rcu()`. Even then, we do not recommend it.
334 ### Interaction with mutexes
336 One must be careful to do not cause deadlocks due to interaction of
337 `urcu_<flavor>_synchronize_rcu()` and RCU read-side with mutexes. If
338 `urcu_<flavor>_synchronize_rcu()` is called with a mutex held, this
339 mutex (or any mutex which has this mutex in its dependency chain) should
340 not be acquired from within a RCU read-side critical section.
342 This is especially important to understand in the context of the
343 QSBR flavor: a registered reader thread being "online" by
344 default should be considered as within a RCU read-side critical
345 section unless explicitly put "offline". Therefore, if
346 `urcu_qsbr_synchronize_rcu()` is called with a mutex held, this mutex,
347 as well as any mutex which has this mutex in its dependency chain should
348 only be taken when the RCU reader thread is "offline" (this can be
349 performed by calling `urcu_qsbr_thread_offline()`).
352 ### Interaction with `fork()`
354 Special care must be taken for applications performing `fork()` without
355 any following `exec()`. This is caused by the fact that Linux only clones
356 the thread calling `fork()`, and thus never replicates any of the other
357 parent thread into the child process. Most `liburcu` implementations
358 require that all registrations (as reader, `defer_rcu` and `call_rcu`
359 threads) should be released before a `fork()` is performed, except for the
360 rather common scenario where `fork()` is immediately followed by `exec()` in
361 the child process. The only implementation not subject to that rule is
362 `liburcu-bp`, which is designed to handle `fork()` by calling
363 `urcu_bp_before_fork`, `urcu_bp_after_fork_parent` and
364 `urcu_bp_after_fork_child`.
366 Applications that use `urcu_<flavor>_call_rcu()` and that `fork()`
367 without doing an immediate `exec()` must take special action. The
368 parent must invoke `urcu_<flavor>_call_rcu_before_fork()` before the
369 `fork()` and `urcu_<flavor>_call_rcu_after_fork_parent()` after the
370 `fork()`. The child process must invoke
371 `urcu_<flavor>_call_rcu_after_fork_child()`. Even though these three
372 APIs are suitable for passing to `pthread_atfork()`, use of
373 `pthread_atfork()` is **STRONGLY DISCOURAGED** for programs calling the
374 glibc memory allocator (`malloc()`, `calloc()`, `free()`, ...) within
375 `urcu_<flavor>_call_rcu` callbacks. This is due to limitations in the
376 way glibc memory allocator handles calls to the memory allocator from
377 concurrent threads while the `pthread_atfork()` handlers are executing.
381 - call to `free()` from callbacks executed within
382 `urcu_<flavor>_call_rcu` worker threads,
383 - executing `urcu_<flavor>_call_rcu` atfork handlers within the glibc
384 pthread atfork mechanism,
386 will sometimes trigger interesting process hangs. This usually
387 hangs on a memory allocator lock within glibc.
390 ### Thread Local Storage (TLS)
392 Userspace RCU can fall back on `pthread_getspecific()` to emulate
393 TLS variables on systems where it is not available. This behavior
394 can be forced by specifying `--disable-compiler-tls` as configure
398 ### Usage of `DEBUG_RCU` & `--enable-rcu-debug`
400 By default the library is configured with internal debugging
401 self-checks disabled.
403 For always-on debugging self-checks:
405 ./configure --enable-rcu-debug
407 For fine grained enabling of debugging self-checks, build
408 userspace-rcu with `DEBUG_RCU` defined and compile dependent
409 applications with `DEBUG_RCU` defined when necessary.
411 Warning: Enabling this feature result in a performance penalty.
414 ### Usage of `DEBUG_YIELD`
416 `DEBUG_YIELD` is used to add random delays in the code for testing
422 By default the library is configured to use synchronization primitives
423 adequate for SMP systems. On uniprocessor systems, support for SMP
424 systems can be disabled with:
426 ./configure --disable-smp-support
428 theoretically yielding slightly better performance.
431 ### Usage of `--enable-cds-lfht-iter-debug`
433 By default the library is configured with extra debugging checks for
434 lock-free hash table iterator traversal disabled.
436 Building liburcu with `--enable-cds-lfht-iter-debug` and rebuilding
437 application to match the ABI change allows finding cases where the hash
438 table iterator is re-purposed to be used on a different hash table while
439 still being used to iterate on a hash table.
441 This option alters the rculfhash ABI. Make sure to compile both library
442 and application with matching configuration.
444 ### Usage of `--enable-compiler-atomic-builtins`
446 Building liburcu with `--enable-compiler-atomic-builtins` implements the uatomic
447 API with the compiler atomic builtins if supported.
452 In addition to the usual `make check` target, Userspace RCU features
453 `make regtest`, `make short_bench` and `make long_bench` targets:
455 - `make check`: short tests, meant to be run when rebuilding or
456 porting Userspace RCU.
457 - `make regtest`: long (many hours) test, meant to be run when
458 modifying Userspace RCU or porting it to a new architecture or
460 - `make short_bench`: short benchmarks, 3 seconds per test.
461 - `make long_bench`: long (many hours) benchmarks, 30 seconds per test.
467 There is an application vs library compatibility issue between
468 applications built using Userspace RCU 0.10 headers linked against
469 Userspace RCU 0.11 or 0.12 shared objects. The problem occurs as
472 - An application executable is built with `_LGPL_SOURCE` defined, includes
473 any of the Userspace RCU 0.10 urcu flavor headers, and is built
474 without the `-fpic` compiler option.
476 - The Userspace RCU 0.10 library shared objects are updated to 0.11
477 or 0.12 without rebuilding the application.
479 - The application will hang, typically when RCU grace period
480 (synchronize_rcu) is invoked.
482 Some possible work-arounds for this are:
484 - Rebuild the application against Userspace RCU 0.11+.
486 - Rebuild the application with `-fpic`.
488 - Upgrade Userspace RCU to 0.13+ without installing 0.11 nor 0.12.
494 You can contact the maintainers on the following mailing list:
495 `lttng-dev@lists.lttng.org`.