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