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