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