xref: /minix3/lib/libc/atomic/atomic_ops.3 (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras.\"	$NetBSD: atomic_ops.3,v 1.5 2010/04/14 08:49:49 jruoho Exp $
2*2fe8fb19SBen Gras.\"
3*2fe8fb19SBen Gras.\" Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
4*2fe8fb19SBen Gras.\" All rights reserved.
5*2fe8fb19SBen Gras.\"
6*2fe8fb19SBen Gras.\" This code is derived from software contributed to The NetBSD Foundation
7*2fe8fb19SBen Gras.\" by Jason R. Thorpe.
8*2fe8fb19SBen Gras.\"
9*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
10*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
11*2fe8fb19SBen Gras.\" are met:
12*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
13*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer.
14*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
15*2fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the
16*2fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution.
17*2fe8fb19SBen Gras.\"
18*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*2fe8fb19SBen Gras.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*2fe8fb19SBen Gras.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*2fe8fb19SBen Gras.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*2fe8fb19SBen Gras.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*2fe8fb19SBen Gras.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*2fe8fb19SBen Gras.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*2fe8fb19SBen Gras.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*2fe8fb19SBen Gras.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*2fe8fb19SBen Gras.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*2fe8fb19SBen Gras.\" POSSIBILITY OF SUCH DAMAGE.
29*2fe8fb19SBen Gras.\"
30*2fe8fb19SBen Gras.Dd April 14, 2010
31*2fe8fb19SBen Gras.Dt ATOMIC_OPS 3
32*2fe8fb19SBen Gras.Os
33*2fe8fb19SBen Gras.Sh NAME
34*2fe8fb19SBen Gras.Nm atomic_ops
35*2fe8fb19SBen Gras.Nd atomic memory operations
36*2fe8fb19SBen Gras.\" .Sh LIBRARY
37*2fe8fb19SBen Gras.\" .Lb libc
38*2fe8fb19SBen Gras.Sh SYNOPSIS
39*2fe8fb19SBen Gras.In sys/atomic.h
40*2fe8fb19SBen Gras.Sh DESCRIPTION
41*2fe8fb19SBen GrasThe
42*2fe8fb19SBen Gras.Nm atomic_ops
43*2fe8fb19SBen Grasfamily of functions provide atomic memory operations.
44*2fe8fb19SBen GrasThere are 7 classes of atomic memory operations available:
45*2fe8fb19SBen Gras.Pp
46*2fe8fb19SBen Gras.Bl -tag -width "atomic_swap(3)" -offset indent
47*2fe8fb19SBen Gras.It Xr atomic_add 3
48*2fe8fb19SBen GrasThese functions perform atomic addition.
49*2fe8fb19SBen Gras.It Xr atomic_and 3
50*2fe8fb19SBen GrasThese functions perform atomic logical
51*2fe8fb19SBen Gras.Dq and .
52*2fe8fb19SBen Gras.It Xr atomic_cas 3
53*2fe8fb19SBen GrasThese functions perform atomic compare-and-swap.
54*2fe8fb19SBen Gras.It Xr atomic_dec 3
55*2fe8fb19SBen GrasThese functions perform atomic decrement.
56*2fe8fb19SBen Gras.It Xr atomic_inc 3
57*2fe8fb19SBen GrasThese functions perform atomic increment.
58*2fe8fb19SBen Gras.It Xr atomic_or 3
59*2fe8fb19SBen GrasThese functions perform atomic logical
60*2fe8fb19SBen Gras.Dq or .
61*2fe8fb19SBen Gras.It Xr atomic_swap 3
62*2fe8fb19SBen GrasThese functions perform atomic swap.
63*2fe8fb19SBen Gras.El
64*2fe8fb19SBen Gras.Ss Synchronization Mechanisms
65*2fe8fb19SBen GrasWhere the architecture does not provide hardware support for atomic compare
66*2fe8fb19SBen Grasand swap (CAS), atomicity is provided by a restartable sequence or by a
67*2fe8fb19SBen Grasspinlock.
68*2fe8fb19SBen GrasThe chosen method is not ordinarily distinguishable by or visible to users
69*2fe8fb19SBen Grasof the interface.
70*2fe8fb19SBen GrasThe following architectures can be assumed to provide CAS in hardware:
71*2fe8fb19SBen Grasalpha, amd64, i386, powerpc, powerpc64, sparc64.
72*2fe8fb19SBen Gras.Ss Scope and Restrictions
73*2fe8fb19SBen GrasIf hardware CAS is available, the atomic operations are globally atomic:
74*2fe8fb19SBen Grasoperations within a memory region shared between processes are
75*2fe8fb19SBen Grasguaranteed to be performed atomically.
76*2fe8fb19SBen GrasIf hardware CAS is not available, it may only be assumed that the operations
77*2fe8fb19SBen Grasare atomic with respect to threads in the same process.
78*2fe8fb19SBen GrasAdditionally, if hardware CAS is not available, the atomic operations must
79*2fe8fb19SBen Grasnot be used within a signal handler.
80*2fe8fb19SBen Gras.Pp
81*2fe8fb19SBen GrasUsers of atomic memory operations should not make assumptions about how
82*2fe8fb19SBen Grasthe memory access is performed
83*2fe8fb19SBen Gras.Pq specifically, the width of the memory access .
84*2fe8fb19SBen GrasFor this reason, applications making use of atomic memory operations should
85*2fe8fb19SBen Graslimit their use to regular memory.
86*2fe8fb19SBen GrasThe results of using atomic memory operations on anything other than
87*2fe8fb19SBen Grasregular memory are undefined.
88*2fe8fb19SBen Gras.Pp
89*2fe8fb19SBen GrasUsers of atomic memory operations should take care to modify any given
90*2fe8fb19SBen Grasmemory location either entirely with atomic operations or entirely with
91*2fe8fb19SBen Grassome other synchronization mechanism.
92*2fe8fb19SBen GrasIntermixing of atomic operations with other synchronization mechanisms
93*2fe8fb19SBen Grasfor the same memory location results in undefined behavior.
94*2fe8fb19SBen Gras.Ss Visibility and Ordering of Memory Accesses
95*2fe8fb19SBen GrasIf hardware CAS is available, stores to the target memory location by an
96*2fe8fb19SBen Grasatomic operation will reach global visibility before the operation
97*2fe8fb19SBen Grascompletes.
98*2fe8fb19SBen GrasIf hardware CAS is not available, the store may not reach global visibility
99*2fe8fb19SBen Grasuntil some time after the atomic operation has completed.
100*2fe8fb19SBen GrasHowever, in all cases a subsequent atomic operation on the same memory cell
101*2fe8fb19SBen Graswill be delayed until the result of any preceeding operation has reached
102*2fe8fb19SBen Grasglobal visibility.
103*2fe8fb19SBen Gras.Pp
104*2fe8fb19SBen GrasAtomic operations are strongly ordered with respect to each other.
105*2fe8fb19SBen GrasThe global visibility of other loads and stores before and after an atomic
106*2fe8fb19SBen Grasoperation is undefined.
107*2fe8fb19SBen GrasApplications that require synchronization of loads and stores with respect
108*2fe8fb19SBen Grasto an atomic operation must use memory barriers.
109*2fe8fb19SBen GrasSee
110*2fe8fb19SBen Gras.Xr membar_ops 3 .
111*2fe8fb19SBen Gras.Ss Performance
112*2fe8fb19SBen GrasBecause atomic memory operations require expensive synchronization at the
113*2fe8fb19SBen Grashardware level, applications should take care to minimize their use.
114*2fe8fb19SBen GrasIn certain cases, it may be more appropriate to use a mutex, especially
115*2fe8fb19SBen Grasif more than one memory location will be modified.
116*2fe8fb19SBen Gras.Sh SEE ALSO
117*2fe8fb19SBen Gras.Xr atomic_add 3 ,
118*2fe8fb19SBen Gras.Xr atomic_and 3 ,
119*2fe8fb19SBen Gras.Xr atomic_cas 3 ,
120*2fe8fb19SBen Gras.Xr atomic_dec 3 ,
121*2fe8fb19SBen Gras.Xr atomic_inc 3 ,
122*2fe8fb19SBen Gras.Xr atomic_or 3 ,
123*2fe8fb19SBen Gras.Xr atomic_swap 3 ,
124*2fe8fb19SBen Gras.Xr membar_ops 3
125*2fe8fb19SBen Gras.Sh HISTORY
126*2fe8fb19SBen GrasThe
127*2fe8fb19SBen Gras.Nm atomic_ops
128*2fe8fb19SBen Grasfunctions first appeared in
129*2fe8fb19SBen Gras.Nx 5.0 .
130