2 * Copyright (c) 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #define OVS_ATOMIC_H 1
22 * This library implements atomic operations with an API based on the one
23 * defined in C11. It includes multiple implementations for compilers and
24 * libraries with varying degrees of built-in support for C11, including a
25 * fallback implementation for systems that have pthreads but no other support
28 * This comment describes the common features of all the implementations.
34 * The following atomic types are supported as typedefs for atomic versions of
35 * the listed ordinary types:
37 * ordinary type atomic version
38 * ------------------- ----------------------
42 * signed char atomic_schar
43 * unsigned char atomic_uchar
46 * unsigned short atomic_ushort
49 * unsigned int atomic_uint
52 * unsigned long atomic_ulong
54 * long long atomic_llong
55 * unsigned long long atomic_ullong
57 * size_t atomic_size_t
58 * ptrdiff_t atomic_ptrdiff_t
60 * intmax_t atomic_intmax_t
61 * uintmax_t atomic_uintmax_t
63 * intptr_t atomic_intptr_t
64 * uintptr_t atomic_uintptr_t
66 * uint8_t atomic_uint8_t (*)
67 * uint16_t atomic_uint16_t (*)
68 * uint32_t atomic_uint32_t (*)
69 * int8_t atomic_int8_t (*)
70 * int16_t atomic_int16_t (*)
71 * int32_t atomic_int32_t (*)
72 * uint64_t atomic_uint64_t (*)
73 * int64_t atomic_int64_t (*)
75 * (*) Not specified by C11.
77 * The atomic version of a type doesn't necessarily have the same size or
78 * representation as the ordinary version; for example, atomic_int might be a
79 * typedef for a struct that also includes a mutex. The range of an atomic
80 * type does match the range of the corresponding ordinary type.
82 * C11 says that one may use the _Atomic keyword in place of the typedef name,
83 * e.g. "_Atomic int" instead of "atomic_int". This library doesn't support
90 * To initialize an atomic variable at its point of definition, use
93 * static atomic_int ai = ATOMIC_VAR_INIT(123);
95 * To initialize an atomic variable in code, use atomic_init():
97 * static atomic_int ai;
99 * atomic_init(&ai, 123);
105 * enum memory_order specifies the strictness of a memory barrier. It has the
108 * memory_order_relaxed:
110 * Compiler barrier only. Does not imply any CPU memory ordering.
112 * memory_order_acquire:
114 * Memory accesses after an acquire barrier cannot be moved before the
115 * barrier. Memory accesses before an acquire barrier *can* be moved
118 * memory_order_release:
120 * Memory accesses before a release barrier cannot be moved after the
121 * barrier. Memory accesses after a release barrier *can* be moved
124 * memory_order_acq_rel:
126 * Memory accesses cannot be moved across an acquire-release barrier in
129 * memory_order_seq_cst:
131 * Prevents movement of memory accesses like an acquire-release barrier,
132 * but whereas acquire-release synchronizes cooperating threads,
133 * sequential-consistency synchronizes the whole system.
135 * memory_order_consume:
137 * A slight relaxation of memory_order_acquire.
139 * The following functions insert explicit barriers. Most of the other atomic
140 * functions also include barriers.
142 * void atomic_thread_fence(memory_order order);
144 * Inserts a barrier of the specified type.
146 * For memory_order_relaxed, this is a no-op.
148 * void atomic_signal_fence(memory_order order);
150 * Inserts a barrier of the specified type, but only with respect to
151 * signal handlers in the same thread as the barrier. This is
152 * basically a compiler optimization barrier, except for
153 * memory_order_relaxed, which is a no-op.
159 * In this section, A is an atomic type and C is the corresponding non-atomic
162 * The "store" primitives match C11:
164 * void atomic_store(A *object, C value);
165 * void atomic_store_explicit(A *object, C value, memory_order);
167 * Atomically stores 'value' into '*object', respecting the given
168 * memory order (or memory_order_seq_cst for atomic_store()).
170 * The following primitives differ from the C11 ones (and have different names)
171 * because there does not appear to be a way to implement the standard
172 * primitives in standard C:
174 * void atomic_read(A *src, C *dst);
175 * void atomic_read_explicit(A *src, C *dst, memory_order);
177 * Atomically loads a value from 'src', writing the value read into
178 * '*dst', respecting the given memory order (or memory_order_seq_cst
179 * for atomic_read()).
181 * void atomic_add(A *rmw, C arg, C *orig);
182 * void atomic_sub(A *rmw, C arg, C *orig);
183 * void atomic_or(A *rmw, C arg, C *orig);
184 * void atomic_xor(A *rmw, C arg, C *orig);
185 * void atomic_and(A *rmw, C arg, C *orig);
186 * void atomic_add_explicit(A *rmw, C arg, C *orig, memory_order);
187 * void atomic_sub_explicit(A *rmw, C arg, C *orig, memory_order);
188 * void atomic_or_explicit(A *rmw, C arg, C *orig, memory_order);
189 * void atomic_xor_explicit(A *rmw, C arg, C *orig, memory_order);
190 * void atomic_and_explicit(A *rmw, C arg, C *orig, memory_order);
192 * Atomically applies the given operation, with 'arg' as the second
193 * operand, to '*rmw', and stores the original value of '*rmw' into
194 * '*orig', respecting the given memory order (or memory_order_seq_cst
195 * if none is specified).
197 * The results are similar to those that would be obtained with +=, -=,
198 * |=, ^=, or |= on non-atomic types.
204 * atomic_flag is a typedef for a type with two states, set and clear, that
205 * provides atomic test-and-set functionality.
207 * ATOMIC_FLAG_INIT is an initializer for atomic_flag. The initial state is
210 * The following functions are available.
212 * bool atomic_flag_test_and_set(atomic_flag *object)
213 * bool atomic_flag_test_and_set_explicit(atomic_flag *object,
216 * Atomically sets '*object', respsecting the given memory order (or
217 * memory_order_seq_cst for atomic_flag_test_and_set()). Returns the
218 * previous value of the flag (false for clear, true for set).
220 * void atomic_flag_clear(atomic_flag *object);
221 * void atomic_flag_clear_explicit(atomic_flag *object, memory_order);
223 * Atomically clears '*object', respecting the given memory order (or
224 * memory_order_seq_cst for atomic_flag_clear()).
232 #include "compiler.h"
235 #define IN_OVS_ATOMIC_H
237 /* sparse doesn't understand some GCC extensions we use. */
238 #include "ovs-atomic-pthreads.h"
239 #elif HAVE_STDATOMIC_H
240 #include "ovs-atomic-c11.h"
241 #elif __has_extension(c_atomic)
242 #include "ovs-atomic-clang.h"
243 #elif __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
244 #include "ovs-atomic-gcc4.7+.h"
245 #elif HAVE_GCC4_ATOMICS
246 #include "ovs-atomic-gcc4+.h"
248 #include "ovs-atomic-pthreads.h"
250 #undef IN_OVS_ATOMIC_H
252 #endif /* ovs-atomic.h */