]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/atomic.9
Update mandoc to cvs snaphot from 20150302
[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 August 20, 2013
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 .Ft <type>
66 .Fn atomic_swap_<type> "volatile <type> *p" "<type> v"
67 .Ft int
68 .Fn atomic_testandset_<type> "volatile <type> *p" "u_int v"
69 .Sh DESCRIPTION
70 Each of the atomic operations is guaranteed to be atomic in the presence of
71 interrupts.
72 They can be used to implement reference counts or as building blocks for more
73 advanced synchronization primitives such as mutexes.
74 .Ss Types
75 Each atomic operation operates on a specific
76 .Fa type .
77 The type to use is indicated in the function name.
78 The available types that can be used are:
79 .Pp
80 .Bl -tag -offset indent -width short -compact
81 .It Li int
82 unsigned integer
83 .It Li long
84 unsigned long integer
85 .It Li ptr
86 unsigned integer the size of a pointer
87 .It Li 32
88 unsigned 32-bit integer
89 .It Li 64
90 unsigned 64-bit integer
91 .El
92 .Pp
93 For example, the function to atomically add two integers is called
94 .Fn atomic_add_int .
95 .Pp
96 Certain architectures also provide operations for types smaller than
97 .Dq Li int .
98 .Pp
99 .Bl -tag -offset indent -width short -compact
100 .It Li char
101 unsigned character
102 .It Li short
103 unsigned short integer
104 .It Li 8
105 unsigned 8-bit integer
106 .It Li 16
107 unsigned 16-bit integer
108 .El
109 .Pp
110 These must not be used in MI code because the instructions to implement them
111 efficiently may not be available.
112 .Ss Memory Barriers
113 Memory barriers are used to guarantee the order of data accesses in
114 two ways.
115 First, they specify hints to the compiler to not re-order or optimize the
116 operations.
117 Second, on architectures that do not guarantee ordered data accesses,
118 special instructions or special variants of instructions are used to indicate
119 to the processor that data accesses need to occur in a certain order.
120 As a result, most of the atomic operations have three variants in order to
121 include optional memory barriers.
122 The first form just performs the operation without any explicit barriers.
123 The second form uses a read memory barrier, and the third variant uses a write
124 memory barrier.
125 .Pp
126 The second variant of each operation includes a read memory barrier.
127 This barrier ensures that the effects of this operation are completed before the
128 effects of any later data accesses.
129 As a result, the operation is said to have acquire semantics as it acquires a
130 pseudo-lock requiring further operations to wait until it has completed.
131 To denote this, the suffix
132 .Dq Li _acq
133 is inserted into the function name immediately prior to the
134 .Dq Li _ Ns Aq Fa type
135 suffix.
136 For example, to subtract two integers ensuring that any later writes will
137 happen after the subtraction is performed, use
138 .Fn atomic_subtract_acq_int .
139 .Pp
140 The third variant of each operation includes a write memory barrier.
141 This ensures that all effects of all previous data accesses are completed
142 before this operation takes place.
143 As a result, the operation is said to have release semantics as it releases
144 any pending data accesses to be completed before its operation is performed.
145 To denote this, the suffix
146 .Dq Li _rel
147 is inserted into the function name immediately prior to the
148 .Dq Li _ Ns Aq Fa type
149 suffix.
150 For example, to add two long integers ensuring that all previous
151 writes will happen first, use
152 .Fn atomic_add_rel_long .
153 .Pp
154 A practical example of using memory barriers is to ensure that data accesses
155 that are protected by a lock are all performed while the lock is held.
156 To achieve this, one would use a read barrier when acquiring the lock to
157 guarantee that the lock is held before any protected operations are performed.
158 Finally, one would use a write barrier when releasing the lock to ensure that
159 all of the protected operations are completed before the lock is released.
160 .Ss Multiple Processors
161 The current set of atomic operations do not necessarily guarantee atomicity
162 across multiple processors.
163 To guarantee atomicity across processors, not only does the individual
164 operation need to be atomic on the processor performing the operation, but
165 the result of the operation needs to be pushed out to stable storage and the
166 caches of all other processors on the system need to invalidate any cache
167 lines that include the affected memory region.
168 On the
169 .Tn i386
170 architecture, the cache coherency model requires that the hardware perform
171 this task, thus the atomic operations are atomic across multiple processors.
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 p
220 .Bd -literal -compact
221 return (*p);
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 p
230 .Bd -literal -compact
231 tmp = *p;
232 *p = 0;
233 return (tmp);
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 not have any variants with memory barriers at this time.
247 .Bl -hang
248 .It Fn atomic_set p v
249 .Bd -literal -compact
250 *p |= v;
251 .Ed
252 .It Fn atomic_subtract p v
253 .Bd -literal -compact
254 *p -= v;
255 .Ed
256 .It Fn atomic_store p v
257 .Bd -literal -compact
258 *p = v;
259 .Ed
260 .El
261 .Pp
262 The
263 .Fn atomic_store
264 functions are only provided with release memory barriers.
265 .Bl -hang
266 .It Fn atomic_swap p v
267 .Bd -literal -compact
268 tmp = *p;
269 *p = v;
270 return (tmp);
271 .Ed
272 .El
273 .Pp
274 The
275 .Fn atomic_swap
276 functions are not implemented for the types
277 .Dq Li char ,
278 .Dq Li short ,
279 .Dq Li ptr ,
280 .Dq Li 8 ,
281 and
282 .Dq Li 16
283 and do not have any variants with memory barriers at this time.
284 .Bl -hang
285 .It Fn atomic_testandset p v
286 .Bd -literal -compact
287 bit = 1 << (v % (sizeof(*p) * NBBY));
288 tmp = (*p & bit) != 0;
289 *p |= bit;
290 return (tmp);
291 .Ed
292 .El
293 .Pp
294 The
295 .Fn atomic_testandset
296 functions are only implemented for the types
297 .Dq Li int ,
298 .Dq Li long
299 and
300 .Dq Li 32
301 and do not have any variants with memory barriers at this time.
302 .Pp
303 The type
304 .Dq Li 64
305 is currently not implemented for any of the atomic operations on the
306 .Tn arm ,
307 .Tn i386 ,
308 and
309 .Tn powerpc
310 architectures.
311 .Sh RETURN VALUES
312 The
313 .Fn atomic_cmpset
314 function returns the result of the compare operation.
315 The
316 .Fn atomic_fetchadd ,
317 .Fn atomic_load ,
318 .Fn atomic_readandclear ,
319 and
320 .Fn atomic_swap
321 functions return the value at the specified address.
322 The
323 .Fn atomic_testandset
324 function returns the result of the test operation.
325 .Sh EXAMPLES
326 This example uses the
327 .Fn atomic_cmpset_acq_ptr
328 and
329 .Fn atomic_set_ptr
330 functions to obtain a sleep mutex and handle recursion.
331 Since the
332 .Va mtx_lock
333 member of a
334 .Vt "struct mtx"
335 is a pointer, the
336 .Dq Li ptr
337 type is used.
338 .Bd -literal
339 /* Try to obtain mtx_lock once. */
340 #define _obtain_lock(mp, tid)                                           \\
341         atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
342
343 /* Get a sleep lock, deal with recursion inline. */
344 #define _get_sleep_lock(mp, tid, opts, file, line) do {                 \\
345         uintptr_t _tid = (uintptr_t)(tid);                              \\
346                                                                         \\
347         if (!_obtain_lock(mp, tid)) {                                   \\
348                 if (((mp)->mtx_lock & MTX_FLAGMASK) != _tid)            \\
349                         _mtx_lock_sleep((mp), _tid, (opts), (file), (line));\\
350                 else {                                                  \\
351                         atomic_set_ptr(&(mp)->mtx_lock, MTX_RECURSE);   \\
352                         (mp)->mtx_recurse++;                            \\
353                 }                                                       \\
354         }                                                               \\
355 } while (0)
356 .Ed
357 .Sh HISTORY
358 The
359 .Fn atomic_add ,
360 .Fn atomic_clear ,
361 .Fn atomic_set ,
362 and
363 .Fn atomic_subtract
364 operations were first introduced in
365 .Fx 3.0 .
366 This first set only supported the types
367 .Dq Li char ,
368 .Dq Li short ,
369 .Dq Li int ,
370 and
371 .Dq Li long .
372 The
373 .Fn atomic_cmpset ,
374 .Fn atomic_load ,
375 .Fn atomic_readandclear ,
376 and
377 .Fn atomic_store
378 operations were added in
379 .Fx 5.0 .
380 The types
381 .Dq Li 8 ,
382 .Dq Li 16 ,
383 .Dq Li 32 ,
384 .Dq Li 64 ,
385 and
386 .Dq Li ptr
387 and all of the acquire and release variants
388 were added in
389 .Fx 5.0
390 as well.
391 The
392 .Fn atomic_fetchadd
393 operations were added in
394 .Fx 6.0 .
395 The
396 .Fn atomic_swap
397 and
398 .Fn atomic_testandset
399 operations were added in
400 .Fx 10.0 .