2001-03-09 03:17:18 +00:00
|
|
|
.\" Copyright (c) 2000-2001 John H. Baldwin <jhb@FreeBSD.org>
|
2000-11-13 20:08:17 +00:00
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2005-11-18 10:56:28 +00:00
|
|
|
.Dd September 27, 2005
|
2000-11-13 20:08:17 +00:00
|
|
|
.Dt ATOMIC 9
|
2010-04-14 19:08:06 +00:00
|
|
|
.Os
|
2000-11-13 20:08:17 +00:00
|
|
|
.Sh NAME
|
|
|
|
.Nm atomic_add ,
|
|
|
|
.Nm atomic_clear ,
|
|
|
|
.Nm atomic_cmpset ,
|
2005-09-27 17:59:29 +00:00
|
|
|
.Nm atomic_fetchadd ,
|
2000-11-13 20:08:17 +00:00
|
|
|
.Nm atomic_load ,
|
|
|
|
.Nm atomic_readandclear ,
|
|
|
|
.Nm atomic_set ,
|
|
|
|
.Nm atomic_subtract ,
|
|
|
|
.Nm atomic_store
|
|
|
|
.Nd atomic operations
|
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In sys/types.h
|
|
|
|
.In machine/atomic.h
|
2000-11-13 20:08:17 +00:00
|
|
|
.Ft void
|
2002-01-17 15:57:43 +00:00
|
|
|
.Fn atomic_add_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
|
2000-11-13 20:08:17 +00:00
|
|
|
.Ft void
|
2002-01-17 15:57:43 +00:00
|
|
|
.Fn atomic_clear_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
|
2000-11-13 20:08:17 +00:00
|
|
|
.Ft int
|
2002-01-17 15:57:43 +00:00
|
|
|
.Fo atomic_cmpset_[acq_|rel_]<type>
|
|
|
|
.Fa "volatile <type> *dst"
|
|
|
|
.Fa "<type> old"
|
|
|
|
.Fa "<type> new"
|
2000-11-13 20:08:17 +00:00
|
|
|
.Fc
|
2002-01-17 15:57:43 +00:00
|
|
|
.Ft <type>
|
2005-09-27 17:59:29 +00:00
|
|
|
.Fn atomic_fetchadd_<type> "volatile <type> *p" "<type> v"
|
|
|
|
.Ft <type>
|
2002-01-17 15:57:43 +00:00
|
|
|
.Fn atomic_load_acq_<type> "volatile <type> *p"
|
|
|
|
.Ft <type>
|
|
|
|
.Fn atomic_readandclear_<type> "volatile <type> *p"
|
2000-11-13 20:08:17 +00:00
|
|
|
.Ft void
|
2002-01-17 15:57:43 +00:00
|
|
|
.Fn atomic_set_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
|
2000-11-13 20:08:17 +00:00
|
|
|
.Ft void
|
2002-01-17 15:57:43 +00:00
|
|
|
.Fn atomic_subtract_[acq_|rel_]<type> "volatile <type> *p" "<type> v"
|
2000-11-13 20:08:17 +00:00
|
|
|
.Ft void
|
2002-01-17 15:57:43 +00:00
|
|
|
.Fn atomic_store_rel_<type> "volatile <type> *p" "<type> v"
|
2000-11-13 20:08:17 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
Each of the atomic operations is guaranteed to be atomic in the presence of
|
|
|
|
interrupts.
|
|
|
|
They can be used to implement reference counts or as building blocks for more
|
|
|
|
advanced synchronization primitives such as mutexes.
|
|
|
|
.Ss Types
|
2002-01-10 10:36:04 +00:00
|
|
|
Each atomic operation operates on a specific
|
2003-05-31 14:07:25 +00:00
|
|
|
.Fa type .
|
2000-11-13 20:08:17 +00:00
|
|
|
The type to use is indicated in the function name.
|
|
|
|
The available types that can be used are:
|
2002-01-10 10:36:04 +00:00
|
|
|
.Pp
|
|
|
|
.Bl -tag -offset indent -width short -compact
|
|
|
|
.It Li int
|
2000-11-13 20:08:17 +00:00
|
|
|
unsigned integer
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Li long
|
2000-11-13 20:08:17 +00:00
|
|
|
unsigned long integer
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Li ptr
|
2000-11-13 20:08:17 +00:00
|
|
|
unsigned integer the size of a pointer
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Li 32
|
2000-11-13 20:08:17 +00:00
|
|
|
unsigned 32-bit integer
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Li 64
|
2000-11-13 20:08:17 +00:00
|
|
|
unsigned 64-bit integer
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
For example, the function to atomically add two integers is called
|
|
|
|
.Fn atomic_add_int .
|
2001-12-15 21:28:02 +00:00
|
|
|
.Pp
|
2002-01-10 10:36:04 +00:00
|
|
|
Certain architectures also provide operations for types smaller than
|
|
|
|
.Dq Li int .
|
|
|
|
.Pp
|
|
|
|
.Bl -tag -offset indent -width short -compact
|
|
|
|
.It Li char
|
2001-12-15 21:28:02 +00:00
|
|
|
unsigned character
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Li short
|
2001-12-15 21:28:02 +00:00
|
|
|
unsigned short integer
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Li 8
|
2001-12-15 21:28:02 +00:00
|
|
|
unsigned 8-bit integer
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Li 16
|
2001-12-15 21:28:02 +00:00
|
|
|
unsigned 16-bit integer
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
These must not be used in MI code because the instructions to implement them
|
|
|
|
efficiently may not be available.
|
2000-11-13 20:08:17 +00:00
|
|
|
.Ss Memory Barriers
|
2002-01-10 10:36:04 +00:00
|
|
|
Memory barriers are used to guarantee the order of data accesses in
|
2000-11-13 20:08:17 +00:00
|
|
|
two ways.
|
|
|
|
First, they specify hints to the compiler to not re-order or optimize the
|
|
|
|
operations.
|
2002-01-10 10:36:04 +00:00
|
|
|
Second, on architectures that do not guarantee ordered data accesses,
|
2000-11-13 20:08:17 +00:00
|
|
|
special instructions or special variants of instructions are used to indicate
|
|
|
|
to the processor that data accesses need to occur in a certain order.
|
|
|
|
As a result, most of the atomic operations have three variants in order to
|
|
|
|
include optional memory barriers.
|
|
|
|
The first form just performs the operation without any explicit barriers.
|
2002-01-10 10:36:04 +00:00
|
|
|
The second form uses a read memory barrier, and the third variant uses a write
|
2000-11-13 20:08:17 +00:00
|
|
|
memory barrier.
|
|
|
|
.Pp
|
|
|
|
The second variant of each operation includes a read memory barrier.
|
|
|
|
This barrier ensures that the effects of this operation are completed before the
|
|
|
|
effects of any later data accesses.
|
|
|
|
As a result, the operation is said to have acquire semantics as it acquires a
|
|
|
|
pseudo-lock requiring further operations to wait until it has completed.
|
|
|
|
To denote this, the suffix
|
2002-01-10 10:36:04 +00:00
|
|
|
.Dq Li _acq
|
2000-11-13 20:08:17 +00:00
|
|
|
is inserted into the function name immediately prior to the
|
2003-05-31 14:07:25 +00:00
|
|
|
.Dq Li _ Ns Aq Fa type
|
2000-11-13 20:08:17 +00:00
|
|
|
suffix.
|
|
|
|
For example, to subtract two integers ensuring that any later writes will
|
|
|
|
happen after the subtraction is performed, use
|
|
|
|
.Fn atomic_subtract_acq_int .
|
|
|
|
.Pp
|
|
|
|
The third variant of each operation includes a write memory barrier.
|
|
|
|
This ensures that all effects of all previous data accesses are completed
|
|
|
|
before this operation takes place.
|
|
|
|
As a result, the operation is said to have release semantics as it releases
|
|
|
|
any pending data accesses to be completed before its operation is performed.
|
|
|
|
To denote this, the suffix
|
2002-01-10 10:36:04 +00:00
|
|
|
.Dq Li _rel
|
2000-11-13 20:08:17 +00:00
|
|
|
is inserted into the function name immediately prior to the
|
2003-05-31 14:07:25 +00:00
|
|
|
.Dq Li _ Ns Aq Fa type
|
2000-11-13 20:08:17 +00:00
|
|
|
suffix.
|
|
|
|
For example, to add two long integers ensuring that all previous
|
|
|
|
writes will happen first, use
|
|
|
|
.Fn atomic_add_rel_long .
|
|
|
|
.Pp
|
|
|
|
A practical example of using memory barriers is to ensure that data accesses
|
|
|
|
that are protected by a lock are all performed while the lock is held.
|
|
|
|
To achieve this, one would use a read barrier when acquiring the lock to
|
|
|
|
guarantee that the lock is held before any protected operations are performed.
|
|
|
|
Finally, one would use a write barrier when releasing the lock to ensure that
|
|
|
|
all of the protected operations are completed before the lock is released.
|
|
|
|
.Ss Multiple Processors
|
|
|
|
The current set of atomic operations do not necessarily guarantee atomicity
|
|
|
|
across multiple processors.
|
|
|
|
To guarantee atomicity across processors, not only does the individual
|
2001-07-13 20:11:43 +00:00
|
|
|
operation need to be atomic on the processor performing the operation, but
|
2000-11-13 20:08:17 +00:00
|
|
|
the result of the operation needs to be pushed out to stable storage and the
|
|
|
|
caches of all other processors on the system need to invalidate any cache
|
|
|
|
lines that include the affected memory region.
|
|
|
|
On the
|
|
|
|
.Tn i386
|
|
|
|
architecture, the cache coherency model requires that the hardware perform
|
|
|
|
this task, thus the atomic operations are atomic across multiple processors.
|
|
|
|
On the
|
|
|
|
.Tn ia64
|
|
|
|
architecture, coherency is only guaranteed for pages that are configured to
|
|
|
|
using a caching policy of either uncached or write back.
|
|
|
|
.Ss Semantics
|
|
|
|
This section describes the semantics of each operation using a C like notation.
|
|
|
|
.Bl -hang
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Fn atomic_add p v
|
|
|
|
.Bd -literal -compact
|
2000-11-13 20:08:17 +00:00
|
|
|
*p += v;
|
|
|
|
.Ed
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Fn atomic_clear p v
|
|
|
|
.Bd -literal -compact
|
2000-11-13 20:08:17 +00:00
|
|
|
*p &= ~v;
|
|
|
|
.Ed
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Fn atomic_cmpset dst old new
|
|
|
|
.Bd -literal -compact
|
2000-11-13 20:08:17 +00:00
|
|
|
if (*dst == old) {
|
|
|
|
*dst = new;
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
.Ed
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn atomic_cmpset
|
2002-01-10 10:36:04 +00:00
|
|
|
functions are not implemented for the types
|
|
|
|
.Dq Li char ,
|
|
|
|
.Dq Li short ,
|
|
|
|
.Dq Li 8 ,
|
|
|
|
and
|
|
|
|
.Dq Li 16 .
|
2000-11-13 20:08:17 +00:00
|
|
|
.Bl -hang
|
2005-09-27 17:59:29 +00:00
|
|
|
.It Fn atomic_fetchadd p v
|
|
|
|
.Bd -literal -compact
|
|
|
|
tmp = *p;
|
|
|
|
*p += v;
|
|
|
|
return tmp;
|
|
|
|
.Ed
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn atomic_fetchadd
|
|
|
|
functions are only implemented for the types
|
2008-03-16 21:20:50 +00:00
|
|
|
.Dq Li int ,
|
|
|
|
.Dq Li long
|
2005-09-27 17:59:29 +00:00
|
|
|
and
|
|
|
|
.Dq Li 32
|
|
|
|
and do not have any variants with memory barriers at this time.
|
|
|
|
.Bl -hang
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Fn atomic_load addr
|
|
|
|
.Bd -literal -compact
|
2000-11-13 20:08:17 +00:00
|
|
|
return (*addr)
|
|
|
|
.Ed
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn atomic_load
|
2009-05-21 13:39:46 +00:00
|
|
|
functions are only provided with acquire memory barriers.
|
2000-11-13 20:08:17 +00:00
|
|
|
.Bl -hang
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Fn atomic_readandclear addr
|
|
|
|
.Bd -literal -compact
|
2000-11-13 20:08:17 +00:00
|
|
|
temp = *addr;
|
|
|
|
*addr = 0;
|
|
|
|
return (temp);
|
|
|
|
.Ed
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn atomic_readandclear
|
2002-01-10 10:36:04 +00:00
|
|
|
functions are not implemented for the types
|
|
|
|
.Dq Li char ,
|
|
|
|
.Dq Li short ,
|
|
|
|
.Dq Li ptr ,
|
|
|
|
.Dq Li 8 ,
|
|
|
|
and
|
|
|
|
.Dq Li 16
|
|
|
|
and do
|
2000-11-13 20:08:17 +00:00
|
|
|
not have any variants with memory barriers at this time.
|
|
|
|
.Bl -hang
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Fn atomic_set p v
|
|
|
|
.Bd -literal -compact
|
2000-11-13 20:08:17 +00:00
|
|
|
*p |= v;
|
|
|
|
.Ed
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Fn atomic_subtract p v
|
|
|
|
.Bd -literal -compact
|
2000-11-13 20:08:17 +00:00
|
|
|
*p -= v;
|
|
|
|
.Ed
|
2002-01-10 10:36:04 +00:00
|
|
|
.It Fn atomic_store p v
|
|
|
|
.Bd -literal -compact
|
2000-11-13 20:08:17 +00:00
|
|
|
*p = v;
|
|
|
|
.Ed
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn atomic_store
|
2009-05-21 13:39:46 +00:00
|
|
|
functions are only provided with release memory barriers.
|
2000-11-13 20:08:17 +00:00
|
|
|
.Pp
|
|
|
|
The type
|
2002-01-10 10:36:04 +00:00
|
|
|
.Dq Li 64
|
2000-11-13 20:08:17 +00:00
|
|
|
is currently not implemented for any of the atomic operations on the
|
2005-09-27 17:59:29 +00:00
|
|
|
.Tn arm ,
|
|
|
|
.Tn i386 ,
|
|
|
|
and
|
|
|
|
.Tn powerpc
|
|
|
|
architectures.
|
2000-11-13 20:08:17 +00:00
|
|
|
.Sh RETURN VALUES
|
2002-01-10 10:36:04 +00:00
|
|
|
The
|
2000-11-13 20:08:17 +00:00
|
|
|
.Fn atomic_cmpset
|
2002-01-10 10:36:04 +00:00
|
|
|
function
|
2000-11-13 20:08:17 +00:00
|
|
|
returns the result of the compare operation.
|
2002-01-10 10:36:04 +00:00
|
|
|
The
|
2005-09-27 17:59:29 +00:00
|
|
|
.Fn atomic_fetchadd ,
|
|
|
|
.Fn atomic_load ,
|
2000-11-13 20:08:17 +00:00
|
|
|
and
|
|
|
|
.Fn atomic_readandclear
|
2002-01-10 10:36:04 +00:00
|
|
|
functions
|
2000-11-13 20:08:17 +00:00
|
|
|
return the value at the specified address.
|
|
|
|
.Sh EXAMPLES
|
|
|
|
This example uses the
|
|
|
|
.Fn atomic_cmpset_acq_ptr
|
|
|
|
and
|
|
|
|
.Fn atomic_set_ptr
|
|
|
|
functions to obtain a sleep mutex and handle recursion.
|
|
|
|
Since the
|
|
|
|
.Va mtx_lock
|
|
|
|
member of a
|
2002-01-10 10:36:04 +00:00
|
|
|
.Vt "struct mtx"
|
2000-11-13 20:08:17 +00:00
|
|
|
is a pointer, the
|
2002-01-10 10:36:04 +00:00
|
|
|
.Dq Li ptr
|
2000-11-13 20:08:17 +00:00
|
|
|
type is used.
|
|
|
|
.Bd -literal
|
2005-09-27 17:59:29 +00:00
|
|
|
/* Try to obtain mtx_lock once. */
|
2000-11-13 20:08:17 +00:00
|
|
|
#define _obtain_lock(mp, tid) \\
|
2005-09-27 17:59:29 +00:00
|
|
|
atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
|
2000-11-13 20:08:17 +00:00
|
|
|
|
|
|
|
/* Get a sleep lock, deal with recursion inline. */
|
2005-09-27 17:59:29 +00:00
|
|
|
#define _get_sleep_lock(mp, tid, opts, file, line) do { \\
|
|
|
|
uintptr_t _tid = (uintptr_t)(tid); \\
|
|
|
|
\\
|
2000-11-13 20:08:17 +00:00
|
|
|
if (!_obtain_lock(mp, tid)) { \\
|
2005-09-27 17:59:29 +00:00
|
|
|
if (((mp)->mtx_lock & MTX_FLAGMASK) != _tid) \\
|
|
|
|
_mtx_lock_sleep((mp), _tid, (opts), (file), (line));\\
|
2000-11-13 20:08:17 +00:00
|
|
|
else { \\
|
|
|
|
atomic_set_ptr(&(mp)->mtx_lock, MTX_RECURSE); \\
|
|
|
|
(mp)->mtx_recurse++; \\
|
|
|
|
} \\
|
|
|
|
} \\
|
|
|
|
} while (0)
|
|
|
|
.Ed
|
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Fn atomic_add ,
|
|
|
|
.Fn atomic_clear ,
|
|
|
|
.Fn atomic_set ,
|
|
|
|
and
|
|
|
|
.Fn atomic_subtract
|
|
|
|
operations were first introduced in
|
|
|
|
.Fx 3.0 .
|
2002-01-10 10:36:04 +00:00
|
|
|
This first set only supported the types
|
|
|
|
.Dq Li char ,
|
|
|
|
.Dq Li short ,
|
|
|
|
.Dq Li int ,
|
|
|
|
and
|
|
|
|
.Dq Li long .
|
2000-11-13 20:08:17 +00:00
|
|
|
The
|
|
|
|
.Fn atomic_cmpset ,
|
|
|
|
.Fn atomic_load ,
|
|
|
|
.Fn atomic_readandclear ,
|
|
|
|
and
|
|
|
|
.Fn atomic_store
|
|
|
|
operations were added in
|
|
|
|
.Fx 5.0 .
|
2002-01-10 10:36:04 +00:00
|
|
|
The types
|
|
|
|
.Dq Li 8 ,
|
|
|
|
.Dq Li 16 ,
|
|
|
|
.Dq Li 32 ,
|
|
|
|
.Dq Li 64 ,
|
|
|
|
and
|
2005-09-27 17:59:29 +00:00
|
|
|
.Dq Li ptr
|
2002-01-10 10:36:04 +00:00
|
|
|
and all of the acquire and release variants
|
2000-11-13 20:08:17 +00:00
|
|
|
were added in
|
|
|
|
.Fx 5.0
|
|
|
|
as well.
|
2005-09-27 17:59:29 +00:00
|
|
|
The
|
|
|
|
.Fn atomic_fetchadd
|
|
|
|
operations were added in
|
|
|
|
.Fx 6.0 .
|