]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - share/man/man9/atomic.9
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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 June 20, 2015
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 an
127 .Em acquire
128 memory barrier.
129 This barrier ensures that the effects of this operation are completed before the
130 effects of any later data accesses.
131 As a result, the operation is said to have acquire semantics as it acquires a
132 pseudo-lock requiring further operations to wait until it has completed.
133 To denote this, the suffix
134 .Dq Li _acq
135 is inserted into the function name immediately prior to the
136 .Dq Li _ Ns Aq Fa type
137 suffix.
138 For example, to subtract two integers ensuring that any later writes will
139 happen after the subtraction is performed, use
140 .Fn atomic_subtract_acq_int .
141 .Pp
142 The third variant of each operation includes a
143 .Em release
144 memory barrier.
145 This ensures that all effects of all previous data accesses are completed
146 before this operation takes place.
147 As a result, the operation is said to have release semantics as it releases
148 any pending data accesses to be completed before its operation is performed.
149 To denote this, the suffix
150 .Dq Li _rel
151 is inserted into the function name immediately prior to the
152 .Dq Li _ Ns Aq Fa type
153 suffix.
154 For example, to add two long integers ensuring that all previous
155 writes will happen first, use
156 .Fn atomic_add_rel_long .
157 .Pp
158 A practical example of using memory barriers is to ensure that data accesses
159 that are protected by a lock are all performed while the lock is held.
160 To achieve this, one would use a read barrier when acquiring the lock to
161 guarantee that the lock is held before any protected operations are performed.
162 Finally, one would use a write barrier when releasing the lock to ensure that
163 all of the protected operations are completed before the lock is released.
164 .Ss Multiple Processors
165 The current set of atomic operations do not necessarily guarantee atomicity
166 across multiple processors.
167 To guarantee atomicity across processors, not only does the individual
168 operation need to be atomic on the processor performing the operation, but
169 the result of the operation needs to be pushed out to stable storage and the
170 caches of all other processors on the system need to invalidate any cache
171 lines that include the affected memory region.
172 On the
173 .Tn i386
174 architecture, the cache coherency model requires that the hardware perform
175 this task, thus the atomic operations are atomic across multiple processors.
176 On the
177 .Tn ia64
178 architecture, coherency is only guaranteed for pages that are configured to
179 using a caching policy of either uncached or write back.
180 .Ss Semantics
181 This section describes the semantics of each operation using a C like notation.
182 .Bl -hang
183 .It Fn atomic_add p v
184 .Bd -literal -compact
185 *p += v;
186 .Ed
187 .It Fn atomic_clear p v
188 .Bd -literal -compact
189 *p &= ~v;
190 .Ed
191 .It Fn atomic_cmpset dst old new
192 .Bd -literal -compact
193 if (*dst == old) {
194         *dst = new;
195         return (1);
196 } else
197         return (0);
198 .Ed
199 .El
200 .Pp
201 The
202 .Fn atomic_cmpset
203 functions are not implemented for the types
204 .Dq Li char ,
205 .Dq Li short ,
206 .Dq Li 8 ,
207 and
208 .Dq Li 16 .
209 .Bl -hang
210 .It Fn atomic_fetchadd p v
211 .Bd -literal -compact
212 tmp = *p;
213 *p += v;
214 return (tmp);
215 .Ed
216 .El
217 .Pp
218 The
219 .Fn atomic_fetchadd
220 functions are only implemented for the types
221 .Dq Li int ,
222 .Dq Li long
223 and
224 .Dq Li 32
225 and do not have any variants with memory barriers at this time.
226 .Bl -hang
227 .It Fn atomic_load p
228 .Bd -literal -compact
229 return (*p);
230 .Ed
231 .El
232 .Pp
233 The
234 .Fn atomic_load
235 functions are only provided with acquire memory barriers.
236 .Bl -hang
237 .It Fn atomic_readandclear p
238 .Bd -literal -compact
239 tmp = *p;
240 *p = 0;
241 return (tmp);
242 .Ed
243 .El
244 .Pp
245 The
246 .Fn atomic_readandclear
247 functions are not implemented for the types
248 .Dq Li char ,
249 .Dq Li short ,
250 .Dq Li ptr ,
251 .Dq Li 8 ,
252 and
253 .Dq Li 16
254 and do not have any variants with memory barriers at this time.
255 .Bl -hang
256 .It Fn atomic_set p v
257 .Bd -literal -compact
258 *p |= v;
259 .Ed
260 .It Fn atomic_subtract p v
261 .Bd -literal -compact
262 *p -= v;
263 .Ed
264 .It Fn atomic_store p v
265 .Bd -literal -compact
266 *p = v;
267 .Ed
268 .El
269 .Pp
270 The
271 .Fn atomic_store
272 functions are only provided with release memory barriers.
273 .Bl -hang
274 .It Fn atomic_swap p v
275 .Bd -literal -compact
276 tmp = *p;
277 *p = v;
278 return (tmp);
279 .Ed
280 .El
281 .Pp
282 The
283 .Fn atomic_swap
284 functions are not implemented for the types
285 .Dq Li char ,
286 .Dq Li short ,
287 .Dq Li ptr ,
288 .Dq Li 8 ,
289 and
290 .Dq Li 16
291 and do not have any variants with memory barriers at this time.
292 .Bl -hang
293 .It Fn atomic_testandset p v
294 .Bd -literal -compact
295 bit = 1 << (v % (sizeof(*p) * NBBY));
296 tmp = (*p & bit) != 0;
297 *p |= bit;
298 return (tmp);
299 .Ed
300 .El
301 .Pp
302 The
303 .Fn atomic_testandset
304 functions are only implemented for the types
305 .Dq Li int ,
306 .Dq Li long
307 and
308 .Dq Li 32
309 and do not have any variants with memory barriers at this time.
310 .Pp
311 The type
312 .Dq Li 64
313 is currently not implemented for any of the atomic operations on the
314 .Tn arm ,
315 .Tn i386 ,
316 and
317 .Tn powerpc
318 architectures.
319 .Sh RETURN VALUES
320 The
321 .Fn atomic_cmpset
322 function returns the result of the compare operation.
323 The
324 .Fn atomic_fetchadd ,
325 .Fn atomic_load ,
326 .Fn atomic_readandclear ,
327 and
328 .Fn atomic_swap
329 functions return the value at the specified address.
330 The
331 .Fn atomic_testandset
332 function returns the result of the test operation.
333 .Sh EXAMPLES
334 This example uses the
335 .Fn atomic_cmpset_acq_ptr
336 and
337 .Fn atomic_set_ptr
338 functions to obtain a sleep mutex and handle recursion.
339 Since the
340 .Va mtx_lock
341 member of a
342 .Vt "struct mtx"
343 is a pointer, the
344 .Dq Li ptr
345 type is used.
346 .Bd -literal
347 /* Try to obtain mtx_lock once. */
348 #define _obtain_lock(mp, tid)                                           \\
349         atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
350
351 /* Get a sleep lock, deal with recursion inline. */
352 #define _get_sleep_lock(mp, tid, opts, file, line) do {                 \\
353         uintptr_t _tid = (uintptr_t)(tid);                              \\
354                                                                         \\
355         if (!_obtain_lock(mp, tid)) {                                   \\
356                 if (((mp)->mtx_lock & MTX_FLAGMASK) != _tid)            \\
357                         _mtx_lock_sleep((mp), _tid, (opts), (file), (line));\\
358                 else {                                                  \\
359                         atomic_set_ptr(&(mp)->mtx_lock, MTX_RECURSE);   \\
360                         (mp)->mtx_recurse++;                            \\
361                 }                                                       \\
362         }                                                               \\
363 } while (0)
364 .Ed
365 .Sh HISTORY
366 The
367 .Fn atomic_add ,
368 .Fn atomic_clear ,
369 .Fn atomic_set ,
370 and
371 .Fn atomic_subtract
372 operations were first introduced in
373 .Fx 3.0 .
374 This first set only supported the types
375 .Dq Li char ,
376 .Dq Li short ,
377 .Dq Li int ,
378 and
379 .Dq Li long .
380 The
381 .Fn atomic_cmpset ,
382 .Fn atomic_load ,
383 .Fn atomic_readandclear ,
384 and
385 .Fn atomic_store
386 operations were added in
387 .Fx 5.0 .
388 The types
389 .Dq Li 8 ,
390 .Dq Li 16 ,
391 .Dq Li 32 ,
392 .Dq Li 64 ,
393 and
394 .Dq Li ptr
395 and all of the acquire and release variants
396 were added in
397 .Fx 5.0
398 as well.
399 The
400 .Fn atomic_fetchadd
401 operations were added in
402 .Fx 6.0 .
403 The
404 .Fn atomic_swap
405 and
406 .Fn atomic_testandset
407 operations were added in
408 .Fx 10.0 .