]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/atomic.9
MFV: r329072
[FreeBSD/FreeBSD.git] / share / man / man9 / atomic.9
1 .\" Copyright (c) 2000-2001 John H. Baldwin <jhb@FreeBSD.org>
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
14 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
17 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 .\"
24 .\" $FreeBSD$
25 .\"
26 .Dd December 22, 2017
27 .Dt ATOMIC 9
28 .Os
29 .Sh NAME
30 .Nm atomic_add ,
31 .Nm atomic_clear ,
32 .Nm atomic_cmpset ,
33 .Nm atomic_fcmpset ,
34 .Nm atomic_fetchadd ,
35 .Nm atomic_load ,
36 .Nm atomic_readandclear ,
37 .Nm atomic_set ,
38 .Nm atomic_subtract ,
39 .Nm atomic_store ,
40 .Nm atomic_thread_fence
41 .Nd atomic operations
42 .Sh SYNOPSIS
43 .In sys/types.h
44 .In machine/atomic.h
45 .Ft void
46 .Fn atomic_add_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
47 .Ft void
48 .Fn atomic_clear_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
49 .Ft int
50 .Fo atomic_cmpset_[acq_|rel_]<type>
51 .Fa "volatile <type> *dst"
52 .Fa "<type> old"
53 .Fa "<type> new"
54 .Fc
55 .Ft int
56 .Fo atomic_fcmpset_[acq_|rel_]<type>
57 .Fa "volatile <type> *dst"
58 .Fa "<type> *old"
59 .Fa "<type> new"
60 .Fc
61 .Ft <type>
62 .Fn atomic_fetchadd_<type> "volatile <type> *p" "<type> v"
63 .Ft <type>
64 .Fn atomic_load_[acq_]<type> "volatile <type> *p"
65 .Ft <type>
66 .Fn atomic_readandclear_<type> "volatile <type> *p"
67 .Ft void
68 .Fn atomic_set_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
69 .Ft void
70 .Fn atomic_subtract_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
71 .Ft void
72 .Fn atomic_store_[rel_]<type> "volatile <type> *p" "<type> v"
73 .Ft <type>
74 .Fn atomic_swap_<type> "volatile <type> *p" "<type> v"
75 .Ft int
76 .Fn atomic_testandclear_<type> "volatile <type> *p" "u_int v"
77 .Ft int
78 .Fn atomic_testandset_<type> "volatile <type> *p" "u_int v"
79 .Ft void
80 .Fn atomic_thread_fence_[acq|acq_rel|rel|seq_cst] "void"
81 .Sh DESCRIPTION
82 Atomic operations are commonly used to implement reference counts and as
83 building blocks for synchronization primitives, such as mutexes.
84 .Pp
85 All of these operations are performed
86 .Em atomically
87 across multiple threads and in the presence of interrupts, meaning that they
88 are performed in an indivisible manner from the perspective of concurrently
89 running threads and interrupt handlers.
90 .Pp
91 On all architectures supported by
92 .Fx ,
93 ordinary loads and stores of integers in cache-coherent memory are
94 inherently atomic if the integer is naturally aligned and its size does not
95 exceed the processor's word size.
96 However, such loads and stores may be elided from the program by
97 the compiler, whereas atomic operations are always performed.
98 .Pp
99 When atomic operations are performed on cache-coherent memory, all
100 operations on the same location are totally ordered.
101 .Pp
102 When an atomic load is performed on a location in cache-coherent memory,
103 it reads the entire value that was defined by the last atomic store to
104 each byte of the location.
105 An atomic load will never return a value out of thin air.
106 When an atomic store is performed on a location, no other thread or
107 interrupt handler will observe a
108 .Em torn write ,
109 or partial modification of the location.
110 .Pp
111 Except as noted below, the semantics of these operations are almost
112 identical to the semantics of similarly named C11 atomic operations.
113 .Ss Types
114 Most atomic operations act upon a specific
115 .Fa type .
116 That type is indicated in the function name.
117 In contrast to C11 atomic operations,
118 .Fx Ns 's
119 atomic operations are performed on ordinary integer types.
120 The available types are:
121 .Pp
122 .Bl -tag -offset indent -width short -compact
123 .It Li int
124 unsigned integer
125 .It Li long
126 unsigned long integer
127 .It Li ptr
128 unsigned integer the size of a pointer
129 .It Li 32
130 unsigned 32-bit integer
131 .It Li 64
132 unsigned 64-bit integer
133 .El
134 .Pp
135 For example, the function to atomically add two integers is called
136 .Fn atomic_add_int .
137 .Pp
138 Certain architectures also provide operations for types smaller than
139 .Dq Li int .
140 .Pp
141 .Bl -tag -offset indent -width short -compact
142 .It Li char
143 unsigned character
144 .It Li short
145 unsigned short integer
146 .It Li 8
147 unsigned 8-bit integer
148 .It Li 16
149 unsigned 16-bit integer
150 .El
151 .Pp
152 These types must not be used in machine-independent code.
153 .Ss Acquire and Release Operations
154 By default, a thread's accesses to different memory locations might not be
155 performed in
156 .Em program order ,
157 that is, the order in which the accesses appear in the source code.
158 To optimize the program's execution, both the compiler and processor might
159 reorder the thread's accesses.
160 However, both ensure that their reordering of the accesses is not visible to
161 the thread.
162 Otherwise, the traditional memory model that is expected by single-threaded
163 programs would be violated.
164 Nonetheless, other threads in a multithreaded program, such as the
165 .Fx
166 kernel, might observe the reordering.
167 Moreover, in some cases, such as the implementation of synchronization between
168 threads, arbitrary reordering might result in the incorrect execution of the
169 program.
170 To constrain the reordering that both the compiler and processor might perform
171 on a thread's accesses, a programmer can use atomic operations with
172 .Em acquire
173 and
174 .Em release
175 semantics.
176 .Pp
177 Atomic operations on memory have up to three variants.
178 The first, or
179 .Em relaxed
180 variant, performs the operation without imposing any ordering constraints on
181 accesses to other memory locations.
182 This variant is the default.
183 The second variant has acquire semantics, and the third variant has release
184 semantics.
185 .Pp
186 When an atomic operation has acquire semantics, the operation must have
187 completed before any subsequent load or store (by program order) is
188 performed.
189 Conversely, acquire semantics do not require that prior loads or stores have
190 completed before the atomic operation is performed.
191 An atomic operation can only have acquire semantics if it performs a load
192 from memory.
193 To denote acquire semantics, the suffix
194 .Dq Li _acq
195 is inserted into the function name immediately prior to the
196 .Dq Li _ Ns Aq Fa type
197 suffix.
198 For example, to subtract two integers ensuring that the subtraction is
199 completed before any subsequent loads and stores are performed, use
200 .Fn atomic_subtract_acq_int .
201 .Pp
202 When an atomic operation has release semantics, all prior loads or stores
203 (by program order) must have completed before the operation is performed.
204 Conversely, release semantics do not require that the atomic operation must
205 have completed before any subsequent load or store is performed.
206 An atomic operation can only have release semantics if it performs a store
207 to memory.
208 To denote release semantics, the suffix
209 .Dq Li _rel
210 is inserted into the function name immediately prior to the
211 .Dq Li _ Ns Aq Fa type
212 suffix.
213 For example, to add two long integers ensuring that all prior loads and
214 stores are completed before the addition is performed, use
215 .Fn atomic_add_rel_long .
216 .Pp
217 When a release operation by one thread
218 .Em synchronizes with
219 an acquire operation by another thread, usually meaning that the acquire
220 operation reads the value written by the release operation, then the effects
221 of all prior stores by the releasing thread must become visible to
222 subsequent loads by the acquiring thread.
223 Moreover, the effects of all stores (by other threads) that were visible to
224 the releasing thread must also become visible to the acquiring thread.
225 These rules only apply to the synchronizing threads.
226 Other threads might observe these stores in a different order.
227 .Pp
228 In effect, atomic operations with acquire and release semantics establish
229 one-way barriers to reordering that enable the implementations of
230 synchronization primitives to express their ordering requirements without
231 also imposing unnecessary ordering.
232 For example, for a critical section guarded by a mutex, an acquire operation
233 when the mutex is locked and a release operation when the mutex is unlocked
234 will prevent any loads or stores from moving outside of the critical
235 section.
236 However, they will not prevent the compiler or processor from moving loads
237 or stores into the critical section, which does not violate the semantics of
238 a mutex.
239 .Ss Thread Fence Operations
240 Alternatively, a programmer can use atomic thread fence operations to
241 constrain the reordering of accesses.
242 In contrast to other atomic operations, fences do not, themselves, access
243 memory.
244 .Pp
245 When a fence has acquire semantics, all prior loads (by program order) must
246 have completed before any subsequent load or store is performed.
247 Thus, an acquire fence is a two-way barrier for load operations.
248 To denote acquire semantics, the suffix
249 .Dq Li _acq
250 is appended to the function name, for example,
251 .Fn atomic_thread_fence_acq .
252 .Pp
253 When a fence has release semantics, all prior loads or stores (by program
254 order) must have completed before any subsequent store operation is
255 performed.
256 Thus, a release fence is a two-way barrier for store operations.
257 To denote release semantics, the suffix
258 .Dq Li _rel
259 is appended to the function name, for example,
260 .Fn atomic_thread_fence_rel .
261 .Pp
262 Although
263 .Fn atomic_thread_fence_acq_rel
264 implements both acquire and release semantics, it is not a full barrier.
265 For example, a store prior to the fence (in program order) may be completed
266 after a load subsequent to the fence.
267 In contrast,
268 .Fn atomic_thread_fence_seq_cst
269 implements a full barrier.
270 Neither loads nor stores may cross this barrier in either direction.
271 .Pp
272 In C11, a release fence by one thread synchronizes with an acquire fence by
273 another thread when an atomic load that is prior to the acquire fence (by
274 program order) reads the value written by an atomic store that is subsequent
275 to the release fence.
276 In constrast, in FreeBSD, because of the atomicity of ordinary, naturally
277 aligned loads and stores, fences can also be synchronized by ordinary loads
278 and stores.
279 This simplifies the implementation and use of some synchronization
280 primitives in
281 .Fx .
282 .Pp
283 Since neither a compiler nor a processor can foresee which (atomic) load
284 will read the value written by an (atomic) store, the ordering constraints
285 imposed by fences must be more restrictive than acquire loads and release
286 stores.
287 Essentially, this is why fences are two-way barriers.
288 .Pp
289 Although fences impose more restrictive ordering than acquire loads and
290 release stores, by separating access from ordering, they can sometimes
291 facilitate more efficient implementations of synchronization primitives.
292 For example, they can be used to avoid executing a memory barrier until a
293 memory access shows that some condition is satisfied.
294 .Ss Multiple Processors
295 In multiprocessor systems, the atomicity of the atomic operations on memory
296 depends on support for cache coherence in the underlying architecture.
297 In general, cache coherence on the default memory type,
298 .Dv VM_MEMATTR_DEFAULT ,
299 is guaranteed by all architectures that are supported by
300 .Fx .
301 For example, cache coherence is guaranteed on write-back memory by the
302 .Tn amd64
303 and
304 .Tn i386
305 architectures.
306 However, on some architectures, cache coherence might not be enabled on all
307 memory types.
308 To determine if cache coherence is enabled for a non-default memory type,
309 consult the architecture's documentation.
310 .Ss Semantics
311 This section describes the semantics of each operation using a C like notation.
312 .Bl -hang
313 .It Fn atomic_add p v
314 .Bd -literal -compact
315 *p += v;
316 .Ed
317 .It Fn atomic_clear p v
318 .Bd -literal -compact
319 *p &= ~v;
320 .Ed
321 .It Fn atomic_cmpset dst old new
322 .Bd -literal -compact
323 if (*dst == old) {
324         *dst = new;
325         return (1);
326 } else
327         return (0);
328 .Ed
329 .El
330 .Pp
331 Some architectures do not implement the
332 .Fn atomic_cmpset
333 functions for the types
334 .Dq Li char ,
335 .Dq Li short ,
336 .Dq Li 8 ,
337 and
338 .Dq Li 16 .
339 .Bl -hang
340 .It Fn atomic_fcmpset dst *old new
341 .El
342 .Pp
343 On architectures implementing
344 .Em Compare And Swap
345 operation in hardware, the functionality can be described as
346 .Bd -literal -offset indent -compact
347 if (*dst == *old) {
348         *dst = new;
349         return (1);
350 } else {
351         *old = *dst;
352         return (0);
353 }
354 .Ed
355 On architectures which provide
356 .Em Load Linked/Store Conditional
357 primitive, the write to
358 .Dv *dst
359 might also fail for several reasons, most important of which
360 is a parallel write to
361 .Dv *dst
362 cache line by other CPU.
363 In this case
364 .Fn atomic_fcmpset
365 function also returns
366 .Dv false ,
367 despite
368 .Dl *old == *dst .
369 .Pp
370 Some architectures do not implement the
371 .Fn atomic_fcmpset
372 functions for the types
373 .Dq Li char ,
374 .Dq Li short ,
375 .Dq Li 8 ,
376 and
377 .Dq Li 16 .
378 .Bl -hang
379 .It Fn atomic_fetchadd p v
380 .Bd -literal -compact
381 tmp = *p;
382 *p += v;
383 return (tmp);
384 .Ed
385 .El
386 .Pp
387 The
388 .Fn atomic_fetchadd
389 functions are only implemented for the types
390 .Dq Li int ,
391 .Dq Li long
392 and
393 .Dq Li 32
394 and do not have any variants with memory barriers at this time.
395 .Bl -hang
396 .It Fn atomic_load p
397 .Bd -literal -compact
398 return (*p);
399 .Ed
400 .It Fn atomic_readandclear p
401 .Bd -literal -compact
402 tmp = *p;
403 *p = 0;
404 return (tmp);
405 .Ed
406 .El
407 .Pp
408 The
409 .Fn atomic_readandclear
410 functions are not implemented for the types
411 .Dq Li char ,
412 .Dq Li short ,
413 .Dq Li ptr ,
414 .Dq Li 8 ,
415 and
416 .Dq Li 16
417 and do not have any variants with memory barriers at this time.
418 .Bl -hang
419 .It Fn atomic_set p v
420 .Bd -literal -compact
421 *p |= v;
422 .Ed
423 .It Fn atomic_subtract p v
424 .Bd -literal -compact
425 *p -= v;
426 .Ed
427 .It Fn atomic_store p v
428 .Bd -literal -compact
429 *p = v;
430 .Ed
431 .It Fn atomic_swap p v
432 .Bd -literal -compact
433 tmp = *p;
434 *p = v;
435 return (tmp);
436 .Ed
437 .El
438 .Pp
439 The
440 .Fn atomic_swap
441 functions are not implemented for the types
442 .Dq Li char ,
443 .Dq Li short ,
444 .Dq Li ptr ,
445 .Dq Li 8 ,
446 and
447 .Dq Li 16
448 and do not have any variants with memory barriers at this time.
449 .Bl -hang
450 .It Fn atomic_testandclear p v
451 .Bd -literal -compact
452 bit = 1 << (v % (sizeof(*p) * NBBY));
453 tmp = (*p & bit) != 0;
454 *p &= ~bit;
455 return (tmp);
456 .Ed
457 .El
458 .Bl -hang
459 .It Fn atomic_testandset p v
460 .Bd -literal -compact
461 bit = 1 << (v % (sizeof(*p) * NBBY));
462 tmp = (*p & bit) != 0;
463 *p |= bit;
464 return (tmp);
465 .Ed
466 .El
467 .Pp
468 The
469 .Fn atomic_testandset
470 and
471 .Fn atomic_testandclear
472 functions are only implemented for the types
473 .Dq Li int ,
474 .Dq Li long
475 and
476 .Dq Li 32
477 and do not have any variants with memory barriers at this time.
478 .Pp
479 The type
480 .Dq Li 64
481 is currently not implemented for any of the atomic operations on the
482 .Tn arm ,
483 .Tn i386 ,
484 and
485 .Tn powerpc
486 architectures.
487 .Sh RETURN VALUES
488 The
489 .Fn atomic_cmpset
490 function returns the result of the compare operation.
491 The
492 .Fn atomic_fcmpset
493 function returns
494 .Dv true
495 if the operation succeeded.
496 Otherwise it returns
497 .Dv false
498 and sets
499 .Va *old
500 to the found value.
501 The
502 .Fn atomic_fetchadd ,
503 .Fn atomic_load ,
504 .Fn atomic_readandclear ,
505 and
506 .Fn atomic_swap
507 functions return the value at the specified address.
508 The
509 .Fn atomic_testandset
510 and
511 .Fn atomic_testandclear
512 function returns the result of the test operation.
513 .Sh EXAMPLES
514 This example uses the
515 .Fn atomic_cmpset_acq_ptr
516 and
517 .Fn atomic_set_ptr
518 functions to obtain a sleep mutex and handle recursion.
519 Since the
520 .Va mtx_lock
521 member of a
522 .Vt "struct mtx"
523 is a pointer, the
524 .Dq Li ptr
525 type is used.
526 .Bd -literal
527 /* Try to obtain mtx_lock once. */
528 #define _obtain_lock(mp, tid)                                           \\
529         atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
530
531 /* Get a sleep lock, deal with recursion inline. */
532 #define _get_sleep_lock(mp, tid, opts, file, line) do {                 \\
533         uintptr_t _tid = (uintptr_t)(tid);                              \\
534                                                                         \\
535         if (!_obtain_lock(mp, tid)) {                                   \\
536                 if (((mp)->mtx_lock & MTX_FLAGMASK) != _tid)            \\
537                         _mtx_lock_sleep((mp), _tid, (opts), (file), (line));\\
538                 else {                                                  \\
539                         atomic_set_ptr(&(mp)->mtx_lock, MTX_RECURSE);   \\
540                         (mp)->mtx_recurse++;                            \\
541                 }                                                       \\
542         }                                                               \\
543 } while (0)
544 .Ed
545 .Sh HISTORY
546 The
547 .Fn atomic_add ,
548 .Fn atomic_clear ,
549 .Fn atomic_set ,
550 and
551 .Fn atomic_subtract
552 operations were introduced in
553 .Fx 3.0 .
554 Initially, these operations were defined on the types
555 .Dq Li char ,
556 .Dq Li short ,
557 .Dq Li int ,
558 and
559 .Dq Li long .
560 .Pp
561 The
562 .Fn atomic_cmpset ,
563 .Fn atomic_load_acq ,
564 .Fn atomic_readandclear ,
565 and
566 .Fn atomic_store_rel
567 operations were added in
568 .Fx 5.0 .
569 Simultaneously, the acquire and release variants were introduced, and
570 support was added for operation on the types
571 .Dq Li 8 ,
572 .Dq Li 16 ,
573 .Dq Li 32 ,
574 .Dq Li 64 ,
575 and
576 .Dq Li ptr .
577 .Pp
578 The
579 .Fn atomic_fetchadd
580 operation was added in
581 .Fx 6.0 .
582 .Pp
583 The
584 .Fn atomic_swap
585 and
586 .Fn atomic_testandset
587 operations were added in
588 .Fx 10.0 .
589 .Pp
590 The
591 .Fn atomic_testandclear
592 and
593 .Fn atomic_thread_fence
594 operations were added in
595 .Fx 11.0 .
596 .Pp
597 The relaxed variants of
598 .Fn atomic_load
599 and
600 .Fn atomic_store
601 were added in
602 .Fx 12.0 .