xref: /freebsd/share/man/man9/atomic.9 (revision c9b6b5826c068e0a998585c976f93f6a2622bb55)
1d265f300SJohn Baldwin.\" Copyright (c) 2000-2001 John H. Baldwin <jhb@FreeBSD.org>
2eaca6183SJohn Baldwin.\" All rights reserved.
3eaca6183SJohn Baldwin.\"
4eaca6183SJohn Baldwin.\" Redistribution and use in source and binary forms, with or without
5eaca6183SJohn Baldwin.\" modification, are permitted provided that the following conditions
6eaca6183SJohn Baldwin.\" are met:
7eaca6183SJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright
8eaca6183SJohn Baldwin.\"    notice, this list of conditions and the following disclaimer.
9eaca6183SJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright
10eaca6183SJohn Baldwin.\"    notice, this list of conditions and the following disclaimer in the
11eaca6183SJohn Baldwin.\"    documentation and/or other materials provided with the distribution.
12eaca6183SJohn Baldwin.\"
13eaca6183SJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
14eaca6183SJohn Baldwin.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15eaca6183SJohn Baldwin.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16eaca6183SJohn Baldwin.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
17eaca6183SJohn Baldwin.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18eaca6183SJohn Baldwin.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19eaca6183SJohn Baldwin.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20eaca6183SJohn Baldwin.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21eaca6183SJohn Baldwin.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22eaca6183SJohn Baldwin.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23eaca6183SJohn Baldwin.\"
24eaca6183SJohn Baldwin.\" $FreeBSD$
25eaca6183SJohn Baldwin.\"
26*c9b6b582SKonstantin Belousov.Dd March 23, 2017
27eaca6183SJohn Baldwin.Dt ATOMIC 9
28aa12cea2SUlrich Spörlein.Os
29eaca6183SJohn Baldwin.Sh NAME
30eaca6183SJohn Baldwin.Nm atomic_add ,
31eaca6183SJohn Baldwin.Nm atomic_clear ,
32eaca6183SJohn Baldwin.Nm atomic_cmpset ,
33c0b995bbSMateusz Guzik.Nm atomic_fcmpset ,
344ea211a4SJohn Baldwin.Nm atomic_fetchadd ,
35eaca6183SJohn Baldwin.Nm atomic_load ,
36eaca6183SJohn Baldwin.Nm atomic_readandclear ,
37eaca6183SJohn Baldwin.Nm atomic_set ,
38eaca6183SJohn Baldwin.Nm atomic_subtract ,
39eaca6183SJohn Baldwin.Nm atomic_store
40eaca6183SJohn Baldwin.Nd atomic operations
41eaca6183SJohn Baldwin.Sh SYNOPSIS
4232eef9aeSRuslan Ermilov.In sys/types.h
4332eef9aeSRuslan Ermilov.In machine/atomic.h
44eaca6183SJohn Baldwin.Ft void
45c6a51f1cSRuslan Ermilov.Fn atomic_add_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
46eaca6183SJohn Baldwin.Ft void
47c6a51f1cSRuslan Ermilov.Fn atomic_clear_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
48eaca6183SJohn Baldwin.Ft int
49c6a51f1cSRuslan Ermilov.Fo atomic_cmpset_[acq_|rel_]<type>
50c6a51f1cSRuslan Ermilov.Fa "volatile <type> *dst"
51c6a51f1cSRuslan Ermilov.Fa "<type> old"
52c6a51f1cSRuslan Ermilov.Fa "<type> new"
53eaca6183SJohn Baldwin.Fc
54c0b995bbSMateusz Guzik.Ft int
55c0b995bbSMateusz Guzik.Fo atomic_fcmpset_[acq_|rel_]<type>
56c0b995bbSMateusz Guzik.Fa "volatile <type> *dst"
57c0b995bbSMateusz Guzik.Fa "<type> *old"
58c0b995bbSMateusz Guzik.Fa "<type> new"
59c0b995bbSMateusz Guzik.Fc
60c6a51f1cSRuslan Ermilov.Ft <type>
614ea211a4SJohn Baldwin.Fn atomic_fetchadd_<type> "volatile <type> *p" "<type> v"
624ea211a4SJohn Baldwin.Ft <type>
63c6a51f1cSRuslan Ermilov.Fn atomic_load_acq_<type> "volatile <type> *p"
64c6a51f1cSRuslan Ermilov.Ft <type>
65c6a51f1cSRuslan Ermilov.Fn atomic_readandclear_<type> "volatile <type> *p"
66eaca6183SJohn Baldwin.Ft void
67c6a51f1cSRuslan Ermilov.Fn atomic_set_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
68eaca6183SJohn Baldwin.Ft void
69c6a51f1cSRuslan Ermilov.Fn atomic_subtract_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
70eaca6183SJohn Baldwin.Ft void
71c6a51f1cSRuslan Ermilov.Fn atomic_store_rel_<type> "volatile <type> *p" "<type> v"
728a1ee2d3SJung-uk Kim.Ft <type>
738a1ee2d3SJung-uk Kim.Fn atomic_swap_<type> "volatile <type> *p" "<type> v"
748a1ee2d3SJung-uk Kim.Ft int
75dfdc9a05SSepherosa Ziehau.Fn atomic_testandclear_<type> "volatile <type> *p" "u_int v"
76dfdc9a05SSepherosa Ziehau.Ft int
778a1ee2d3SJung-uk Kim.Fn atomic_testandset_<type> "volatile <type> *p" "u_int v"
78eaca6183SJohn Baldwin.Sh DESCRIPTION
79*c9b6b582SKonstantin BelousovAll of these operations are performed atomically across multiple
80*c9b6b582SKonstantin Belousovthreads and in the presence of interrupts, meaning that they are
81*c9b6b582SKonstantin Belousovperformed in an indivisible manner from the perspective of concurrently
82*c9b6b582SKonstantin Belousovrunning threads and interrupt handlers.
83*c9b6b582SKonstantin Belousov.Pp
84*c9b6b582SKonstantin BelousovWhen atomic operations are performed on cache-coherent memory, all
85*c9b6b582SKonstantin Belousovoperations on the same location are totally ordered.
86*c9b6b582SKonstantin Belousov.Pp
87*c9b6b582SKonstantin BelousovWhen an atomic load is performed on a location in cache-coherent memory,
88*c9b6b582SKonstantin Belousovit reads the entire value that was defined by the last atomic store to
89*c9b6b582SKonstantin Belousoveach byte of the location.
90*c9b6b582SKonstantin BelousovAn atomic load will never return a value out of thin air.
91*c9b6b582SKonstantin BelousovWhen an atomic store is performed on a location, no other thread or
92*c9b6b582SKonstantin Belousovinterrupt handler will observe a
93*c9b6b582SKonstantin Belousov.Em torn write ,
94*c9b6b582SKonstantin Belousovor partial modification of the location.
95*c9b6b582SKonstantin Belousov.Pp
96*c9b6b582SKonstantin BelousovOn all architectures supported by
97*c9b6b582SKonstantin Belousov.Fx ,
98*c9b6b582SKonstantin Belousovordinary loads and stores of naturally aligned integer types
99*c9b6b582SKonstantin Belousovare atomic, as executed by the processor.
100*c9b6b582SKonstantin Belousov.Pp
101*c9b6b582SKonstantin BelousovAtomic operations can be used to implement reference counts or as
102*c9b6b582SKonstantin Belousovbuilding blocks for synchronization primitives such as mutexes.
103*c9b6b582SKonstantin Belousov.Pp
104*c9b6b582SKonstantin BelousovThe semantics of
105*c9b6b582SKonstantin Belousov.Fx Ns 's
106*c9b6b582SKonstantin Belousovatomic operations are almost identical to those of the similarly named
107*c9b6b582SKonstantin BelousovC11 operations.
108*c9b6b582SKonstantin BelousovThe one important difference is that the C11 standard does not
109*c9b6b582SKonstantin Belousovrequire ordinary loads and stores to ever be atomic.
110*c9b6b582SKonstantin BelousovThis is is why the
111*c9b6b582SKonstantin Belousov.Fn atomic_load_explicit memory_order_relaxed
112*c9b6b582SKonstantin Belousovoperation exists in the C11 standard, but is not provided by
113*c9b6b582SKonstantin Belousov.In machine/atomic.h .
114eaca6183SJohn Baldwin.Ss Types
1152be6c09fSRuslan ErmilovEach atomic operation operates on a specific
1160640e9e0SHiten Pandya.Fa type .
117eaca6183SJohn BaldwinThe type to use is indicated in the function name.
118eaca6183SJohn BaldwinThe available types that can be used are:
1192be6c09fSRuslan Ermilov.Pp
1202be6c09fSRuslan Ermilov.Bl -tag -offset indent -width short -compact
1212be6c09fSRuslan Ermilov.It Li int
122eaca6183SJohn Baldwinunsigned integer
1232be6c09fSRuslan Ermilov.It Li long
124eaca6183SJohn Baldwinunsigned long integer
1252be6c09fSRuslan Ermilov.It Li ptr
126eaca6183SJohn Baldwinunsigned integer the size of a pointer
1272be6c09fSRuslan Ermilov.It Li 32
128eaca6183SJohn Baldwinunsigned 32-bit integer
1292be6c09fSRuslan Ermilov.It Li 64
130eaca6183SJohn Baldwinunsigned 64-bit integer
131eaca6183SJohn Baldwin.El
132eaca6183SJohn Baldwin.Pp
133eaca6183SJohn BaldwinFor example, the function to atomically add two integers is called
134eaca6183SJohn Baldwin.Fn atomic_add_int .
135c645e17aSJake Burkholder.Pp
1362be6c09fSRuslan ErmilovCertain architectures also provide operations for types smaller than
1372be6c09fSRuslan Ermilov.Dq Li int .
1382be6c09fSRuslan Ermilov.Pp
1392be6c09fSRuslan Ermilov.Bl -tag -offset indent -width short -compact
1402be6c09fSRuslan Ermilov.It Li char
141c645e17aSJake Burkholderunsigned character
1422be6c09fSRuslan Ermilov.It Li short
143c645e17aSJake Burkholderunsigned short integer
1442be6c09fSRuslan Ermilov.It Li 8
145c645e17aSJake Burkholderunsigned 8-bit integer
1462be6c09fSRuslan Ermilov.It Li 16
147c645e17aSJake Burkholderunsigned 16-bit integer
148c645e17aSJake Burkholder.El
149c645e17aSJake Burkholder.Pp
150c645e17aSJake BurkholderThese must not be used in MI code because the instructions to implement them
151cff0a327SAlan Coxefficiently might not be available.
152cff0a327SAlan Cox.Ss Acquire and Release Operations
153cff0a327SAlan CoxBy default, a thread's accesses to different memory locations might not be
154cff0a327SAlan Coxperformed in
155cff0a327SAlan Cox.Em program order ,
156cff0a327SAlan Coxthat is, the order in which the accesses appear in the source code.
157cff0a327SAlan CoxTo optimize the program's execution, both the compiler and processor might
158cff0a327SAlan Coxreorder the thread's accesses.
159cff0a327SAlan CoxHowever, both ensure that their reordering of the accesses is not visible to
160cff0a327SAlan Coxthe thread.
161cff0a327SAlan CoxOtherwise, the traditional memory model that is expected by single-threaded
162cff0a327SAlan Coxprograms would be violated.
163cff0a327SAlan CoxNonetheless, other threads in a multithreaded program, such as the
164cff0a327SAlan Cox.Fx
165cff0a327SAlan Coxkernel, might observe the reordering.
166cff0a327SAlan CoxMoreover, in some cases, such as the implementation of synchronization between
167cff0a327SAlan Coxthreads, arbitrary reordering might result in the incorrect execution of the
168cff0a327SAlan Coxprogram.
169cff0a327SAlan CoxTo constrain the reordering that both the compiler and processor might perform
170cff0a327SAlan Coxon a thread's accesses, the thread should use atomic operations with
17177054356SKonstantin Belousov.Em acquire
172cff0a327SAlan Coxand
173cff0a327SAlan Cox.Em release
174cff0a327SAlan Coxsemantics.
175cff0a327SAlan Cox.Pp
176cff0a327SAlan CoxMost of the atomic operations on memory have three variants.
177cff0a327SAlan CoxThe first variant performs the operation without imposing any ordering
178cff0a327SAlan Coxconstraints on memory accesses to other locations.
179cff0a327SAlan CoxThe second variant has acquire semantics, and the third variant has release
180cff0a327SAlan Coxsemantics.
181cff0a327SAlan CoxIn effect, operations with acquire and release semantics establish one-way
182cff0a327SAlan Coxbarriers to reordering.
183cff0a327SAlan Cox.Pp
184cff0a327SAlan CoxWhen an atomic operation has acquire semantics, the effects of the operation
185cff0a327SAlan Coxmust have completed before any subsequent load or store (by program order) is
186cff0a327SAlan Coxperformed.
187cff0a327SAlan CoxConversely, acquire semantics do not require that prior loads or stores have
188cff0a327SAlan Coxcompleted before the atomic operation is performed.
189cff0a327SAlan CoxTo denote acquire semantics, the suffix
1902be6c09fSRuslan Ermilov.Dq Li _acq
191eaca6183SJohn Baldwinis inserted into the function name immediately prior to the
1920640e9e0SHiten Pandya.Dq Li _ Ns Aq Fa type
193eaca6183SJohn Baldwinsuffix.
194cff0a327SAlan CoxFor example, to subtract two integers ensuring that subsequent loads and
195cff0a327SAlan Coxstores happen after the subtraction is performed, use
196eaca6183SJohn Baldwin.Fn atomic_subtract_acq_int .
197eaca6183SJohn Baldwin.Pp
198cff0a327SAlan CoxWhen an atomic operation has release semantics, the effects of all prior
199cff0a327SAlan Coxloads or stores (by program order) must have completed before the operation
200cff0a327SAlan Coxis performed.
201cff0a327SAlan CoxConversely, release semantics do not require that the effects of the
202cff0a327SAlan Coxatomic operation must have completed before any subsequent load or store is
203cff0a327SAlan Coxperformed.
204cff0a327SAlan CoxTo denote release semantics, the suffix
2052be6c09fSRuslan Ermilov.Dq Li _rel
206eaca6183SJohn Baldwinis inserted into the function name immediately prior to the
2070640e9e0SHiten Pandya.Dq Li _ Ns Aq Fa type
208eaca6183SJohn Baldwinsuffix.
209cff0a327SAlan CoxFor example, to add two long integers ensuring that all prior loads and
210cff0a327SAlan Coxstores happen before the addition, use
211eaca6183SJohn Baldwin.Fn atomic_add_rel_long .
212eaca6183SJohn Baldwin.Pp
213cff0a327SAlan CoxThe one-way barriers provided by acquire and release operations allow the
214cff0a327SAlan Coximplementations of common synchronization primitives to express their
215cff0a327SAlan Coxordering requirements without also imposing unnecessary ordering.
216cff0a327SAlan CoxFor example, for a critical section guarded by a mutex, an acquire operation
217cff0a327SAlan Coxwhen the mutex is locked and a release operation when the mutex is unlocked
218cff0a327SAlan Coxwill prevent any loads or stores from moving outside of the critical
219cff0a327SAlan Coxsection.
220cff0a327SAlan CoxHowever, they will not prevent the compiler or processor from moving loads
221cff0a327SAlan Coxor stores into the critical section, which does not violate the semantics of
222cff0a327SAlan Coxa mutex.
223eaca6183SJohn Baldwin.Ss Multiple Processors
224a61bd957SAlan CoxIn multiprocessor systems, the atomicity of the atomic operations on memory
225a61bd957SAlan Coxdepends on support for cache coherence in the underlying architecture.
226a61bd957SAlan CoxIn general, cache coherence on the default memory type,
227a61bd957SAlan Cox.Dv VM_MEMATTR_DEFAULT ,
228a61bd957SAlan Coxis guaranteed by all architectures that are supported by
229a61bd957SAlan Cox.Fx .
230a61bd957SAlan CoxFor example, cache coherence is guaranteed on write-back memory by the
231a61bd957SAlan Cox.Tn amd64
232a61bd957SAlan Coxand
233eaca6183SJohn Baldwin.Tn i386
234a61bd957SAlan Coxarchitectures.
235cff0a327SAlan CoxHowever, on some architectures, cache coherence might not be enabled on all
236a61bd957SAlan Coxmemory types.
237a61bd957SAlan CoxTo determine if cache coherence is enabled for a non-default memory type,
238a61bd957SAlan Coxconsult the architecture's documentation.
239eaca6183SJohn Baldwin.Ss Semantics
240eaca6183SJohn BaldwinThis section describes the semantics of each operation using a C like notation.
241eaca6183SJohn Baldwin.Bl -hang
2422be6c09fSRuslan Ermilov.It Fn atomic_add p v
2432be6c09fSRuslan Ermilov.Bd -literal -compact
244eaca6183SJohn Baldwin*p += v;
245eaca6183SJohn Baldwin.Ed
2462be6c09fSRuslan Ermilov.It Fn atomic_clear p v
2472be6c09fSRuslan Ermilov.Bd -literal -compact
248eaca6183SJohn Baldwin*p &= ~v;
249eaca6183SJohn Baldwin.Ed
2502be6c09fSRuslan Ermilov.It Fn atomic_cmpset dst old new
2512be6c09fSRuslan Ermilov.Bd -literal -compact
252eaca6183SJohn Baldwinif (*dst == old) {
253eaca6183SJohn Baldwin	*dst = new;
2548a1ee2d3SJung-uk Kim	return (1);
255eaca6183SJohn Baldwin} else
2568a1ee2d3SJung-uk Kim	return (0);
257eaca6183SJohn Baldwin.Ed
258eaca6183SJohn Baldwin.El
259eaca6183SJohn Baldwin.Pp
2603d673254SMark JohnstonSome architectures do not implement the
261eaca6183SJohn Baldwin.Fn atomic_cmpset
2623d673254SMark Johnstonfunctions for the types
2632be6c09fSRuslan Ermilov.Dq Li char ,
2642be6c09fSRuslan Ermilov.Dq Li short ,
2652be6c09fSRuslan Ermilov.Dq Li 8 ,
2662be6c09fSRuslan Ermilovand
2672be6c09fSRuslan Ermilov.Dq Li 16 .
268eaca6183SJohn Baldwin.Bl -hang
269c0b995bbSMateusz Guzik.It Fn atomic_fcmpset dst *old new
270c0b995bbSMateusz Guzik.El
271c0b995bbSMateusz Guzik.Pp
272c0b995bbSMateusz GuzikOn architectures implementing
273c0b995bbSMateusz Guzik.Em Compare And Swap
274c0b995bbSMateusz Guzikoperation in hardware, the functionality can be described as
275c0b995bbSMateusz Guzik.Bd -literal -offset indent -compact
276c0b995bbSMateusz Guzikif (*dst == *old) {
277c0b995bbSMateusz Guzik	*dst = new;
278c0b995bbSMateusz Guzik	return (1);
279c0b995bbSMateusz Guzik} else {
280c0b995bbSMateusz Guzik	*old = *dst;
281c0b995bbSMateusz Guzik	return (0);
282c0b995bbSMateusz Guzik}
283c0b995bbSMateusz Guzik.Ed
284c0b995bbSMateusz GuzikOn architectures which provide
285c0b995bbSMateusz Guzik.Em Load Linked/Store Conditional
286c0b995bbSMateusz Guzikprimitive, the write to
287c0b995bbSMateusz Guzik.Dv *dst
288c0b995bbSMateusz Guzikmight also fail for several reasons, most important of which
289c0b995bbSMateusz Guzikis a parallel write to
290c0b995bbSMateusz Guzik.Dv *dst
291c0b995bbSMateusz Guzikcache line by other CPU.
292c0b995bbSMateusz GuzikIn this case
293c0b995bbSMateusz Guzik.Fn atomic_fcmpset
294c0b995bbSMateusz Guzikfunction also returns
295c0b995bbSMateusz Guzik.Dv false ,
296c0b995bbSMateusz Guzikdespite
297c0b995bbSMateusz Guzik.Dl *old == *dst .
298c0b995bbSMateusz Guzik.Pp
2993d673254SMark JohnstonSome architectures do not implement the
300c0b995bbSMateusz Guzik.Fn atomic_fcmpset
3013d673254SMark Johnstonfunctions for the types
302c0b995bbSMateusz Guzik.Dq Li char ,
303c0b995bbSMateusz Guzik.Dq Li short ,
304c0b995bbSMateusz Guzik.Dq Li 8 ,
305c0b995bbSMateusz Guzikand
306c0b995bbSMateusz Guzik.Dq Li 16 .
307c0b995bbSMateusz Guzik.Bl -hang
3084ea211a4SJohn Baldwin.It Fn atomic_fetchadd p v
3094ea211a4SJohn Baldwin.Bd -literal -compact
3104ea211a4SJohn Baldwintmp = *p;
3114ea211a4SJohn Baldwin*p += v;
3128a1ee2d3SJung-uk Kimreturn (tmp);
3134ea211a4SJohn Baldwin.Ed
3144ea211a4SJohn Baldwin.El
3154ea211a4SJohn Baldwin.Pp
3164ea211a4SJohn BaldwinThe
3174ea211a4SJohn Baldwin.Fn atomic_fetchadd
3184ea211a4SJohn Baldwinfunctions are only implemented for the types
3196eb4157fSPawel Jakub Dawidek.Dq Li int ,
3206eb4157fSPawel Jakub Dawidek.Dq Li long
3214ea211a4SJohn Baldwinand
3224ea211a4SJohn Baldwin.Dq Li 32
3234ea211a4SJohn Baldwinand do not have any variants with memory barriers at this time.
3244ea211a4SJohn Baldwin.Bl -hang
3258a1ee2d3SJung-uk Kim.It Fn atomic_load p
3262be6c09fSRuslan Ermilov.Bd -literal -compact
3278a1ee2d3SJung-uk Kimreturn (*p);
328eaca6183SJohn Baldwin.Ed
329eaca6183SJohn Baldwin.El
330eaca6183SJohn Baldwin.Pp
331eaca6183SJohn BaldwinThe
332eaca6183SJohn Baldwin.Fn atomic_load
333e6b08944SJohn Baldwinfunctions are only provided with acquire memory barriers.
334eaca6183SJohn Baldwin.Bl -hang
3358a1ee2d3SJung-uk Kim.It Fn atomic_readandclear p
3362be6c09fSRuslan Ermilov.Bd -literal -compact
3378a1ee2d3SJung-uk Kimtmp = *p;
3388a1ee2d3SJung-uk Kim*p = 0;
3398a1ee2d3SJung-uk Kimreturn (tmp);
340eaca6183SJohn Baldwin.Ed
341eaca6183SJohn Baldwin.El
342eaca6183SJohn Baldwin.Pp
343eaca6183SJohn BaldwinThe
344eaca6183SJohn Baldwin.Fn atomic_readandclear
3452be6c09fSRuslan Ermilovfunctions are not implemented for the types
3462be6c09fSRuslan Ermilov.Dq Li char ,
3472be6c09fSRuslan Ermilov.Dq Li short ,
3482be6c09fSRuslan Ermilov.Dq Li ptr ,
3492be6c09fSRuslan Ermilov.Dq Li 8 ,
3502be6c09fSRuslan Ermilovand
3512be6c09fSRuslan Ermilov.Dq Li 16
3528a1ee2d3SJung-uk Kimand do not have any variants with memory barriers at this time.
353eaca6183SJohn Baldwin.Bl -hang
3542be6c09fSRuslan Ermilov.It Fn atomic_set p v
3552be6c09fSRuslan Ermilov.Bd -literal -compact
356eaca6183SJohn Baldwin*p |= v;
357eaca6183SJohn Baldwin.Ed
3582be6c09fSRuslan Ermilov.It Fn atomic_subtract p v
3592be6c09fSRuslan Ermilov.Bd -literal -compact
360eaca6183SJohn Baldwin*p -= v;
361eaca6183SJohn Baldwin.Ed
3622be6c09fSRuslan Ermilov.It Fn atomic_store p v
3632be6c09fSRuslan Ermilov.Bd -literal -compact
364eaca6183SJohn Baldwin*p = v;
365eaca6183SJohn Baldwin.Ed
366eaca6183SJohn Baldwin.El
367eaca6183SJohn Baldwin.Pp
368eaca6183SJohn BaldwinThe
369eaca6183SJohn Baldwin.Fn atomic_store
370e6b08944SJohn Baldwinfunctions are only provided with release memory barriers.
3718a1ee2d3SJung-uk Kim.Bl -hang
3728a1ee2d3SJung-uk Kim.It Fn atomic_swap p v
3738a1ee2d3SJung-uk Kim.Bd -literal -compact
3748a1ee2d3SJung-uk Kimtmp = *p;
3758a1ee2d3SJung-uk Kim*p = v;
3768a1ee2d3SJung-uk Kimreturn (tmp);
3778a1ee2d3SJung-uk Kim.Ed
3788a1ee2d3SJung-uk Kim.El
3798a1ee2d3SJung-uk Kim.Pp
3808a1ee2d3SJung-uk KimThe
3818a1ee2d3SJung-uk Kim.Fn atomic_swap
3828a1ee2d3SJung-uk Kimfunctions are not implemented for the types
3838a1ee2d3SJung-uk Kim.Dq Li char ,
3848a1ee2d3SJung-uk Kim.Dq Li short ,
3858a1ee2d3SJung-uk Kim.Dq Li ptr ,
3868a1ee2d3SJung-uk Kim.Dq Li 8 ,
3878a1ee2d3SJung-uk Kimand
3888a1ee2d3SJung-uk Kim.Dq Li 16
3898a1ee2d3SJung-uk Kimand do not have any variants with memory barriers at this time.
3908a1ee2d3SJung-uk Kim.Bl -hang
391dfdc9a05SSepherosa Ziehau.It Fn atomic_testandclear p v
392dfdc9a05SSepherosa Ziehau.Bd -literal -compact
393dfdc9a05SSepherosa Ziehaubit = 1 << (v % (sizeof(*p) * NBBY));
394dfdc9a05SSepherosa Ziehautmp = (*p & bit) != 0;
395dfdc9a05SSepherosa Ziehau*p &= ~bit;
396dfdc9a05SSepherosa Ziehaureturn (tmp);
397dfdc9a05SSepherosa Ziehau.Ed
398dfdc9a05SSepherosa Ziehau.El
399dfdc9a05SSepherosa Ziehau.Bl -hang
4008a1ee2d3SJung-uk Kim.It Fn atomic_testandset p v
4018a1ee2d3SJung-uk Kim.Bd -literal -compact
4028a1ee2d3SJung-uk Kimbit = 1 << (v % (sizeof(*p) * NBBY));
4038a1ee2d3SJung-uk Kimtmp = (*p & bit) != 0;
4048a1ee2d3SJung-uk Kim*p |= bit;
4058a1ee2d3SJung-uk Kimreturn (tmp);
4068a1ee2d3SJung-uk Kim.Ed
4078a1ee2d3SJung-uk Kim.El
4088a1ee2d3SJung-uk Kim.Pp
4098a1ee2d3SJung-uk KimThe
4108a1ee2d3SJung-uk Kim.Fn atomic_testandset
411dfdc9a05SSepherosa Ziehauand
412dfdc9a05SSepherosa Ziehau.Fn atomic_testandclear
4138a1ee2d3SJung-uk Kimfunctions are only implemented for the types
4148a1ee2d3SJung-uk Kim.Dq Li int ,
4158a1ee2d3SJung-uk Kim.Dq Li long
4168a1ee2d3SJung-uk Kimand
4178a1ee2d3SJung-uk Kim.Dq Li 32
4188a1ee2d3SJung-uk Kimand do not have any variants with memory barriers at this time.
4198a1ee2d3SJung-uk Kim.Pp
420eaca6183SJohn BaldwinThe type
4212be6c09fSRuslan Ermilov.Dq Li 64
422eaca6183SJohn Baldwinis currently not implemented for any of the atomic operations on the
4234ea211a4SJohn Baldwin.Tn arm ,
4244ea211a4SJohn Baldwin.Tn i386 ,
4254ea211a4SJohn Baldwinand
4264ea211a4SJohn Baldwin.Tn powerpc
4274ea211a4SJohn Baldwinarchitectures.
428eaca6183SJohn Baldwin.Sh RETURN VALUES
4292be6c09fSRuslan ErmilovThe
430eaca6183SJohn Baldwin.Fn atomic_cmpset
4318a1ee2d3SJung-uk Kimfunction returns the result of the compare operation.
4322be6c09fSRuslan ErmilovThe
433c0b995bbSMateusz Guzik.Fn atomic_fcmpset
434c0b995bbSMateusz Guzikfunction returns
435c0b995bbSMateusz Guzik.Dv true
43668278ec6SBenjamin Kadukif the operation succeeded.
437c0b995bbSMateusz GuzikOtherwise it returns
438c0b995bbSMateusz Guzik.Dv false
439c0b995bbSMateusz Guzikand sets
440c0b995bbSMateusz Guzik.Va *old
441c0b995bbSMateusz Guzikto the found value.
442c0b995bbSMateusz GuzikThe
4434ea211a4SJohn Baldwin.Fn atomic_fetchadd ,
4444ea211a4SJohn Baldwin.Fn atomic_load ,
4458a1ee2d3SJung-uk Kim.Fn atomic_readandclear ,
446eaca6183SJohn Baldwinand
4478a1ee2d3SJung-uk Kim.Fn atomic_swap
4488a1ee2d3SJung-uk Kimfunctions return the value at the specified address.
4498a1ee2d3SJung-uk KimThe
4508a1ee2d3SJung-uk Kim.Fn atomic_testandset
451dfdc9a05SSepherosa Ziehauand
452dfdc9a05SSepherosa Ziehau.Fn atomic_testandclear
4538a1ee2d3SJung-uk Kimfunction returns the result of the test operation.
454eaca6183SJohn Baldwin.Sh EXAMPLES
455eaca6183SJohn BaldwinThis example uses the
456eaca6183SJohn Baldwin.Fn atomic_cmpset_acq_ptr
457eaca6183SJohn Baldwinand
458eaca6183SJohn Baldwin.Fn atomic_set_ptr
459eaca6183SJohn Baldwinfunctions to obtain a sleep mutex and handle recursion.
460eaca6183SJohn BaldwinSince the
461eaca6183SJohn Baldwin.Va mtx_lock
462eaca6183SJohn Baldwinmember of a
4632be6c09fSRuslan Ermilov.Vt "struct mtx"
464eaca6183SJohn Baldwinis a pointer, the
4652be6c09fSRuslan Ermilov.Dq Li ptr
466eaca6183SJohn Baldwintype is used.
467eaca6183SJohn Baldwin.Bd -literal
4684ea211a4SJohn Baldwin/* Try to obtain mtx_lock once. */
469eaca6183SJohn Baldwin#define _obtain_lock(mp, tid)						\\
4704ea211a4SJohn Baldwin	atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
471eaca6183SJohn Baldwin
472eaca6183SJohn Baldwin/* Get a sleep lock, deal with recursion inline. */
4734ea211a4SJohn Baldwin#define _get_sleep_lock(mp, tid, opts, file, line) do {			\\
4744ea211a4SJohn Baldwin	uintptr_t _tid = (uintptr_t)(tid);				\\
4754ea211a4SJohn Baldwin									\\
476eaca6183SJohn Baldwin	if (!_obtain_lock(mp, tid)) {					\\
4774ea211a4SJohn Baldwin		if (((mp)->mtx_lock & MTX_FLAGMASK) != _tid)		\\
4784ea211a4SJohn Baldwin			_mtx_lock_sleep((mp), _tid, (opts), (file), (line));\\
479eaca6183SJohn Baldwin		else {							\\
480eaca6183SJohn Baldwin			atomic_set_ptr(&(mp)->mtx_lock, MTX_RECURSE);	\\
481eaca6183SJohn Baldwin			(mp)->mtx_recurse++;				\\
482eaca6183SJohn Baldwin		}							\\
483eaca6183SJohn Baldwin	}								\\
484eaca6183SJohn Baldwin} while (0)
485eaca6183SJohn Baldwin.Ed
486eaca6183SJohn Baldwin.Sh HISTORY
487eaca6183SJohn BaldwinThe
488eaca6183SJohn Baldwin.Fn atomic_add ,
489eaca6183SJohn Baldwin.Fn atomic_clear ,
490eaca6183SJohn Baldwin.Fn atomic_set ,
491eaca6183SJohn Baldwinand
492eaca6183SJohn Baldwin.Fn atomic_subtract
493eaca6183SJohn Baldwinoperations were first introduced in
494eaca6183SJohn Baldwin.Fx 3.0 .
4952be6c09fSRuslan ErmilovThis first set only supported the types
4962be6c09fSRuslan Ermilov.Dq Li char ,
4972be6c09fSRuslan Ermilov.Dq Li short ,
4982be6c09fSRuslan Ermilov.Dq Li int ,
4992be6c09fSRuslan Ermilovand
5002be6c09fSRuslan Ermilov.Dq Li long .
501eaca6183SJohn BaldwinThe
502eaca6183SJohn Baldwin.Fn atomic_cmpset ,
503eaca6183SJohn Baldwin.Fn atomic_load ,
504eaca6183SJohn Baldwin.Fn atomic_readandclear ,
505eaca6183SJohn Baldwinand
506eaca6183SJohn Baldwin.Fn atomic_store
507eaca6183SJohn Baldwinoperations were added in
508eaca6183SJohn Baldwin.Fx 5.0 .
5092be6c09fSRuslan ErmilovThe types
5102be6c09fSRuslan Ermilov.Dq Li 8 ,
5112be6c09fSRuslan Ermilov.Dq Li 16 ,
5122be6c09fSRuslan Ermilov.Dq Li 32 ,
5132be6c09fSRuslan Ermilov.Dq Li 64 ,
5142be6c09fSRuslan Ermilovand
5154ea211a4SJohn Baldwin.Dq Li ptr
5162be6c09fSRuslan Ermilovand all of the acquire and release variants
517eaca6183SJohn Baldwinwere added in
518eaca6183SJohn Baldwin.Fx 5.0
519eaca6183SJohn Baldwinas well.
5204ea211a4SJohn BaldwinThe
5214ea211a4SJohn Baldwin.Fn atomic_fetchadd
5224ea211a4SJohn Baldwinoperations were added in
5234ea211a4SJohn Baldwin.Fx 6.0 .
5248a1ee2d3SJung-uk KimThe
5258a1ee2d3SJung-uk Kim.Fn atomic_swap
5268a1ee2d3SJung-uk Kimand
5278a1ee2d3SJung-uk Kim.Fn atomic_testandset
5288a1ee2d3SJung-uk Kimoperations were added in
5298a1ee2d3SJung-uk Kim.Fx 10.0 .
530dfdc9a05SSepherosa Ziehau.Fn atomic_testandclear
531dfdc9a05SSepherosa Ziehauoperation was added in
532dfdc9a05SSepherosa Ziehau.Fx 11.0 .
533