0d27bb740018f534ea732ba53fa1bf760ca40703
[dragonfly.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: src/share/man/man9/atomic.9,v 1.17 2010/05/27 13:56:27 uqs Exp $
25 .\"
26 .Dd June 13, 2012
27 .Dt ATOMIC 9
28 .Os
29 .Sh NAME
30 .Nm atomic_add ,
31 .Nm atomic_clear ,
32 .Nm atomic_cmpset ,
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 .Nd atomic operations
40 .Sh SYNOPSIS
41 .In sys/types.h
42 .In machine/atomic.h
43 .Ft void
44 .Fn atomic_add_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
45 .Ft void
46 .Fn atomic_clear_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
47 .Ft int
48 .Fo atomic_cmpset_[acq_|rel_]<type>
49 .Fa "volatile <type> *dst"
50 .Fa "<type> old"
51 .Fa "<type> new"
52 .Fc
53 .Ft <type>
54 .Fn atomic_fetchadd_<type> "volatile <type> *p" "<type> v"
55 .Ft <type>
56 .Fn atomic_load_acq_<type> "volatile <type> *p"
57 .Ft <type>
58 .Fn atomic_readandclear_<type> "volatile <type> *p"
59 .Ft void
60 .Fn atomic_set_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
61 .Ft void
62 .Fn atomic_subtract_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
63 .Ft void
64 .Fn atomic_store_rel_<type> "volatile <type> *p" "<type> v"
65 .Sh DESCRIPTION
66 Each of the atomic operations is guaranteed to be atomic in the presence of
67 interrupts.
68 They can be used to implement reference counts or as building blocks for more
69 advanced synchronization primitives such as mutexes.
70 .Ss Types
71 Each atomic operation operates on a specific
72 .Fa type .
73 The type to use is indicated in the function name.
74 The available types that can be used are:
75 .Pp
76 .Bl -tag -offset indent -width short -compact
77 .It Li int
78 unsigned integer
79 .It Li long
80 unsigned long integer
81 .It Li ptr
82 unsigned integer the size of a pointer
83 .It Li 32
84 unsigned 32-bit integer
85 .\".It Li 64
86 .\"unsigned 64-bit integer
87 .El
88 .Pp
89 For example, the function to atomically add two integers is called
90 .Fn atomic_add_int .
91 .Pp
92 Certain architectures also provide operations for types smaller than
93 .Dq Li int .
94 .Pp
95 .Bl -tag -offset indent -width short -compact
96 .It Li char
97 unsigned character
98 .It Li short
99 unsigned short integer
100 .It Li 8
101 unsigned 8-bit integer
102 .It Li 16
103 unsigned 16-bit integer
104 .El
105 .Pp
106 These must not be used in MI code because the instructions to implement them
107 efficiently may not be available.
108 .Ss Memory Barriers
109 Memory barriers are used to guarantee the order of data accesses in
110 two ways.
111 First, they specify hints to the compiler to not re-order or optimize the
112 operations.
113 Second, on architectures that do not guarantee ordered data accesses,
114 special instructions or special variants of instructions are used to indicate
115 to the processor that data accesses need to occur in a certain order.
116 As a result, most of the atomic operations have three variants in order to
117 include optional memory barriers.
118 The first form just performs the operation without any explicit barriers.
119 The second form uses a read memory barrier, and the third variant uses a write
120 memory barrier.
121 .Pp
122 The second variant of each operation includes a read memory barrier.
123 This barrier ensures that the effects of this operation are completed before the
124 effects of any later data accesses.
125 As a result, the operation is said to have acquire semantics as it acquires a
126 pseudo-lock requiring further operations to wait until it has completed.
127 To denote this, the suffix
128 .Dq Li _acq
129 is inserted into the function name immediately prior to the
130 .Dq Li _ Ns Aq Fa type
131 suffix.
132 For example, to subtract two integers ensuring that any later writes will
133 happen after the subtraction is performed, use
134 .Fn atomic_subtract_acq_int .
135 .Pp
136 The third variant of each operation includes a write memory barrier.
137 This ensures that all effects of all previous data accesses are completed
138 before this operation takes place.
139 As a result, the operation is said to have release semantics as it releases
140 any pending data accesses to be completed before its operation is performed.
141 To denote this, the suffix
142 .Dq Li _rel
143 is inserted into the function name immediately prior to the
144 .Dq Li _ Ns Aq Fa type
145 suffix.
146 For example, to add two long integers ensuring that all previous
147 writes will happen first, use
148 .Fn atomic_add_rel_long .
149 .Pp
150 A practical example of using memory barriers is to ensure that data accesses
151 that are protected by a lock are all performed while the lock is held.
152 To achieve this, one would use a read barrier when acquiring the lock to
153 guarantee that the lock is held before any protected operations are performed.
154 Finally, one would use a write barrier when releasing the lock to ensure that
155 all of the protected operations are completed before the lock is released.
156 .Ss Multiple Processors
157 The current set of atomic operations do not necessarily guarantee atomicity
158 across multiple processors.
159 To guarantee atomicity across processors, not only does the individual
160 operation need to be atomic on the processor performing the operation, but
161 the result of the operation needs to be pushed out to stable storage and the
162 caches of all other processors on the system need to invalidate any cache
163 lines that include the affected memory region.
164 On the
165 .Tn i386
166 architecture, the cache coherency model requires that the hardware perform
167 this task, thus the atomic operations are atomic across multiple processors.
168 .\"On the
169 .\".Tn ia64
170 .\"architecture, coherency is only guaranteed for pages that are configured to
171 .\"using a caching policy of either uncached or write back.
172 .Ss Semantics
173 This section describes the semantics of each operation using a C like notation.
174 .Bl -hang
175 .It Fn atomic_add p v
176 .Bd -literal -compact
177 *p += v;
178 .Ed
179 .It Fn atomic_clear p v
180 .Bd -literal -compact
181 *p &= ~v;
182 .Ed
183 .It Fn atomic_cmpset dst old new
184 .Bd -literal -compact
185 if (*dst == old) {
186         *dst = new;
187         return 1;
188 } else
189         return 0;
190 .Ed
191 .El
192 .Pp
193 The
194 .Fn atomic_cmpset
195 functions are not implemented for the types
196 .Dq Li char ,
197 .Dq Li short ,
198 .Dq Li 8 ,
199 and
200 .Dq Li 16 .
201 .Bl -hang
202 .It Fn atomic_fetchadd p v
203 .Bd -literal -compact
204 tmp = *p;
205 *p += v;
206 return tmp;
207 .Ed
208 .El
209 .Pp
210 The
211 .Fn atomic_fetchadd
212 functions are only implemented for the types
213 .Dq Li int ,
214 .Dq Li long
215 and
216 .Dq Li 32
217 and do not have any variants with memory barriers at this time.
218 .Bl -hang
219 .It Fn atomic_load addr
220 .Bd -literal -compact
221 return (*addr)
222 .Ed
223 .El
224 .Pp
225 The
226 .Fn atomic_load
227 functions are only provided with acquire memory barriers.
228 .Bl -hang
229 .It Fn atomic_readandclear addr
230 .Bd -literal -compact
231 temp = *addr;
232 *addr = 0;
233 return (temp);
234 .Ed
235 .El
236 .Pp
237 The
238 .Fn atomic_readandclear
239 functions are not implemented for the types
240 .Dq Li char ,
241 .Dq Li short ,
242 .Dq Li ptr ,
243 .Dq Li 8 ,
244 and
245 .Dq Li 16
246 and do
247 not have any variants with memory barriers at this time.
248 .Bl -hang
249 .It Fn atomic_set p v
250 .Bd -literal -compact
251 *p |= v;
252 .Ed
253 .It Fn atomic_subtract p v
254 .Bd -literal -compact
255 *p -= v;
256 .Ed
257 .It Fn atomic_store p v
258 .Bd -literal -compact
259 *p = v;
260 .Ed
261 .El
262 .Pp
263 The
264 .Fn atomic_store
265 functions are only provided with release memory barriers.
266 .\".Pp
267 .\"The type
268 .\".Dq Li 64
269 .\"is currently not implemented for any of the atomic operations on the
270 .\".Tn arm ,
271 .\".Tn i386 ,
272 .\"and
273 .\".Tn powerpc
274 .\"architectures.
275 .Sh RETURN VALUES
276 The
277 .Fn atomic_cmpset
278 function
279 returns the result of the compare operation.
280 The
281 .Fn atomic_fetchadd ,
282 .Fn atomic_load ,
283 and
284 .Fn atomic_readandclear
285 functions
286 return the value at the specified address.
287 .\".Sh EXAMPLES
288 .\"This example uses the
289 .\".Fn atomic_cmpset_acq_ptr
290 .\"and
291 .\".Fn atomic_set_ptr
292 .\"functions to obtain a sleep mutex and handle recursion.
293 .\"Since the
294 .\".Va mtx_lock
295 .\"member of a
296 .\".Vt "struct mtx"
297 .\"is a pointer, the
298 .\".Dq Li ptr
299 .\"type is used.
300 .\".Bd -literal
301 .\"/* Try to obtain mtx_lock once. */
302 .\"#define _obtain_lock(mp, tid)                                                \\
303 .\"     atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
304 .\"
305 .\"/* Get a sleep lock, deal with recursion inline. */
306 .\"#define _get_sleep_lock(mp, tid, opts, file, line) do {                      \\
307 .\"     uintptr_t _tid = (uintptr_t)(tid);                              \\
308 .\"                                                                     \\
309 .\"     if (!_obtain_lock(mp, tid)) {                                   \\
310 .\"             if (((mp)->mtx_lock & MTX_FLAGMASK) != _tid)            \\
311 .\"                     _mtx_lock_sleep((mp), _tid, (opts), (file), (line));\\
312 .\"             else {                                                  \\
313 .\"                     atomic_set_ptr(&(mp)->mtx_lock, MTX_RECURSE);   \\
314 .\"                     (mp)->mtx_recurse++;                            \\
315 .\"             }                                                       \\
316 .\"     }                                                               \\
317 .\"} while (0)
318 .\".Ed
319 .Sh HISTORY
320 The
321 .Fn atomic_add ,
322 .Fn atomic_clear ,
323 .Fn atomic_set ,
324 and
325 .Fn atomic_subtract
326 operations were first introduced in
327 .Fx 3.0 .
328 This first set only supported the types
329 .Dq Li char ,
330 .Dq Li short ,
331 .Dq Li int ,
332 and
333 .Dq Li long .
334 The
335 .Fn atomic_cmpset ,
336 .Fn atomic_load ,
337 .Fn atomic_readandclear ,
338 and
339 .Fn atomic_store
340 operations were added in
341 .Fx 5.0 .
342 The types
343 .Dq Li 8 ,
344 .Dq Li 16 ,
345 .Dq Li 32 ,
346 .\".Dq Li 64 ,
347 and
348 .Dq Li ptr
349 and all of the acquire and release variants
350 were added in
351 .Fx 5.0
352 as well.
353 The
354 .Fn atomic_fetchadd
355 operations were added in
356 .Fx 6.0 .