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