1 .\" Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .Nm atomic_compare_exchange_strong ,
37 .Nm atomic_compare_exchange_weak ,
38 .Nm atomic_fetch_add ,
39 .Nm atomic_fetch_and ,
41 .Nm atomic_fetch_sub ,
42 .Nm atomic_fetch_xor ,
43 .Nm atomic_is_lock_free
44 .Nd type-generic atomic operations
48 .Vt _Atomic(T) Va v No = \*[Fn-font]ATOMIC_VAR_INIT\*[No-font] Ns Pq Fa c ;
49 .Vt _Atomic T Va v No = \*[Fn-font]ATOMIC_VAR_INIT\*[No-font] Ns Pq Fa c ;
51 .Fn atomic_init "_Atomic(T) *object" "T value"
53 .Fn atomic_load "_Atomic(T) *object"
55 .Fn atomic_load_explicit "_Atomic(T) *object" "memory_order order"
57 .Fn atomic_store "_Atomic(T) *object" "T desired"
59 .Fn atomic_store_explicit "_Atomic(T) *object" "T desired" "memory_order order"
61 .Fn atomic_exchange "_Atomic(T) *object" "T desired"
63 .Fn atomic_exchange_explicit "_Atomic(T) *object" "T desired" "memory_order order"
65 .Fn atomic_compare_exchange_strong "_Atomic(T) *object" "T *expected" "T desired"
67 .Fn atomic_compare_exchange_strong_explicit "_Atomic(T) *object" "T *expected" "T desired" "memory_order success" "memory_order failure"
69 .Fn atomic_compare_exchange_weak "_Atomic(T) *object" "T *expected" "T desired"
71 .Fn atomic_compare_exchange_weak_explicit "_Atomic(T) *object" "T *expected" "T desired" "memory_order success" "memory_order failure"
73 .Fn atomic_fetch_add "_Atomic(T) *object" "T operand"
75 .Fn atomic_fetch_add_explicit "_Atomic(T) *object" "T operand" "memory_order order"
77 .Fn atomic_fetch_and "_Atomic(T) *object" "T operand"
79 .Fn atomic_fetch_and_explicit "_Atomic(T) *object" "T operand" "memory_order order"
81 .Fn atomic_fetch_or "_Atomic(T) *object" "T operand"
83 .Fn atomic_fetch_or_explicit "_Atomic(T) *object" "T operand" "memory_order order"
85 .Fn atomic_fetch_sub "_Atomic(T) *object" "T operand"
87 .Fn atomic_fetch_sub_explicit "_Atomic(T) *object" "T operand" "memory_order order"
89 .Fn atomic_fetch_xor "_Atomic(T) *object" "T operand"
91 .Fn atomic_fetch_xor_explicit "_Atomic(T) *object" "T operand" "memory_order order"
93 .Fn atomic_is_lock_free "const _Atomic(T) *object"
97 provides type-generic operations on atomic operations.
99 Atomic variables are declared using the
101 type specifier or the
104 Such variables are not type-compatible with their non-atomic
105 counterparts and may have different alignment.
107 Operations on atomic variables that do not use the
109 interfaces, including compound assignment operations, will behave as if the
110 .Pf non- Fn _explicit
111 versions of those interfaces had been used.
115 operation initializes the atomic variable
119 Atomic variables can be initialized while being declared using
120 .Fn ATOMIC_VAR_INIT .
124 operation returns the value of atomic variable
128 operation sets the atomic variable
136 operation combines the behaviour of
140 It sets the atomic variable
144 and returns the original contents of the atomic variable.
147 .Fn atomic_compare_exchange_strong
150 value into atomic variable
152 but only if the atomic variable is equal to the
155 Upon success, the operation returns
159 value is overwritten with the contents of the atomic variable and
164 .Fn atomic_compare_exchange_weak
165 operation is identical to
166 .Fn atomic_compare_exchange_strong ,
167 but is allowed to fail even if atomic variable
172 .Fn atomic_compare_exchange
173 operation is in a loop, the weak version will yield better performance on
175 .Fn atomic_compare_exchange_weak
176 would require a loop and
177 .Fn atomic_compare_exchange_strong
178 would not, the strong version is preferable.
182 operation adds the value
186 and returns the original contents of the atomic variable.
190 operation applies the
192 operator to atomic variable
196 and stores the result into
198 while returning the original contents of the atomic variable.
202 operation applies the
204 operator to atomic variable
208 and stores the result into
210 while returning the original contents of the atomic variable.
214 operation subtracts the value
218 and returns the original contents of the atomic variable.
222 operation applies the
224 operator to atomic variable
228 and stores the result into
230 while returning the original contents of the atomic variable.
233 .Fn atomic_is_lock_free
234 operation returns whether atomic variable
236 uses locks to implement atomic operations.
238 C11 defines a memory model that may allow for the reordering of operations in the
239 absence of fences or explicit memory ordering operations.
241 .Pf non- Fn _explicit
242 interfaces use the strictest available memory order: sequential
245 interfaces allow for configuration of the memory order operation which is
246 present. The types of available memory order operations are explained in
254 interfaces can have one of the following values:
255 .Bl -tag -width memory_order_relaxed
256 .It Dv memory_order_relaxed
257 Operation does not order memory.
258 .It Dv memory_order_consume
259 Performs a consume operation.
260 .It Dv memory_order_acquire
261 Performs an acquire operation.
262 .It Dv memory_order_release
263 Performs a release operation.
264 .It Dv memory_order_acq_rel
265 Performs both an acquire and a release operation.
266 .It Dv memory_order_seq_cst
267 Provides sequential consistency.
273 These interfaces conform to