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