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