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