Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / lib / ovs-atomic.h
1 /*
2  * Copyright (c) 2013, 2014 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef OVS_ATOMIC_H
18 #define OVS_ATOMIC_H 1
19
20 /* Atomic operations.
21  *
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
26  * for atomics.
27  *
28  * This comment describes the common features of all the implementations.
29  *
30  *
31  * Types
32  * =====
33  *
34  * The following atomic types are supported as typedefs for atomic versions of
35  * the listed ordinary types:
36  *
37  *     ordinary type            atomic version
38  *     -------------------      ----------------------
39  *     bool                     atomic_bool
40  *
41  *     char                     atomic_char
42  *     signed char              atomic_schar
43  *     unsigned char            atomic_uchar
44  *
45  *     short                    atomic_short
46  *     unsigned short           atomic_ushort
47  *
48  *     int                      atomic_int
49  *     unsigned int             atomic_uint
50  *
51  *     long                     atomic_long
52  *     unsigned long            atomic_ulong
53  *
54  *     long long                atomic_llong
55  *     unsigned long long       atomic_ullong
56  *
57  *     size_t                   atomic_size_t
58  *     ptrdiff_t                atomic_ptrdiff_t
59  *
60  *     intmax_t                 atomic_intmax_t
61  *     uintmax_t                atomic_uintmax_t
62  *
63  *     intptr_t                 atomic_intptr_t
64  *     uintptr_t                atomic_uintptr_t
65  *
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     (*)
74  *
75  *     (*) Not specified by C11.
76  *
77  * Atomic types may also be obtained via ATOMIC(TYPE), e.g. ATOMIC(void *).
78  * Only basic integer types and pointer types can be made atomic this way,
79  * e.g. atomic structs are not supported.
80  *
81  * The atomic version of a type doesn't necessarily have the same size or
82  * representation as the ordinary version; for example, atomic_int might be a
83  * typedef for a struct.  The range of an atomic type does match the range of
84  * the corresponding ordinary type.
85  *
86  * C11 says that one may use the _Atomic keyword in place of the typedef name,
87  * e.g. "_Atomic int" instead of "atomic_int".  This library doesn't support
88  * that.
89  *
90  *
91  * Life Cycle
92  * ==========
93  *
94  * To initialize an atomic variable at its point of definition, use
95  * ATOMIC_VAR_INIT:
96  *
97  *     static atomic_int ai = ATOMIC_VAR_INIT(123);
98  *
99  * To initialize an atomic variable in code, use atomic_init():
100  *
101  *     static atomic_int ai;
102  * ...
103  *     atomic_init(&ai, 123);
104  *
105  *
106  * Barriers
107  * ========
108  *
109  * enum memory_order specifies the strictness of a memory barrier.  It has the
110  * following values:
111  *
112  *    memory_order_relaxed:
113  *
114  *        Compiler barrier only.  Does not imply any CPU memory ordering.
115  *
116  *    memory_order_acquire:
117  *
118  *        Memory accesses after an acquire barrier cannot be moved before the
119  *        barrier.  Memory accesses before an acquire barrier *can* be moved
120  *        after it.
121  *
122  *    memory_order_release:
123  *
124  *        Memory accesses before a release barrier cannot be moved after the
125  *        barrier.  Memory accesses after a release barrier *can* be moved
126  *        before it.
127  *
128  *    memory_order_acq_rel:
129  *
130  *        Memory accesses cannot be moved across an acquire-release barrier in
131  *        either direction.
132  *
133  *    memory_order_seq_cst:
134  *
135  *        Prevents movement of memory accesses like an acquire-release barrier,
136  *        but whereas acquire-release synchronizes cooperating threads,
137  *        sequential-consistency synchronizes the whole system.
138  *
139  *    memory_order_consume:
140  *
141  *        A slight relaxation of memory_order_acquire.
142  *
143  * The following functions insert explicit barriers.  Most of the other atomic
144  * functions also include barriers.
145  *
146  *     void atomic_thread_fence(memory_order order);
147  *
148  *         Inserts a barrier of the specified type.
149  *
150  *         For memory_order_relaxed, this is a no-op.
151  *
152  *     void atomic_signal_fence(memory_order order);
153  *
154  *         Inserts a barrier of the specified type, but only with respect to
155  *         signal handlers in the same thread as the barrier.  This is
156  *         basically a compiler optimization barrier, except for
157  *         memory_order_relaxed, which is a no-op.
158  *
159  *
160  * Atomic Operations
161  * =================
162  *
163  * In this section, A is an atomic type and C is the corresponding non-atomic
164  * type.
165  *
166  * The "store" primitives match C11:
167  *
168  *     void atomic_store(A *object, C value);
169  *     void atomic_store_explicit(A *object, C value, memory_order);
170  *
171  *         Atomically stores 'value' into '*object', respecting the given
172  *         memory order (or memory_order_seq_cst for atomic_store()).
173  *
174  * The following primitives differ from the C11 ones (and have different names)
175  * because there does not appear to be a way to implement the standard
176  * primitives in standard C:
177  *
178  *     void atomic_read(A *src, C *dst);
179  *     void atomic_read_explicit(A *src, C *dst, memory_order);
180  *
181  *         Atomically loads a value from 'src', writing the value read into
182  *         '*dst', respecting the given memory order (or memory_order_seq_cst
183  *         for atomic_read()).
184  *
185  *     void atomic_add(A *rmw, C arg, C *orig);
186  *     void atomic_sub(A *rmw, C arg, C *orig);
187  *     void atomic_or(A *rmw, C arg, C *orig);
188  *     void atomic_xor(A *rmw, C arg, C *orig);
189  *     void atomic_and(A *rmw, C arg, C *orig);
190  *     void atomic_add_explicit(A *rmw, C arg, C *orig, memory_order);
191  *     void atomic_sub_explicit(A *rmw, C arg, C *orig, memory_order);
192  *     void atomic_or_explicit(A *rmw, C arg, C *orig, memory_order);
193  *     void atomic_xor_explicit(A *rmw, C arg, C *orig, memory_order);
194  *     void atomic_and_explicit(A *rmw, C arg, C *orig, memory_order);
195  *
196  *         Atomically applies the given operation, with 'arg' as the second
197  *         operand, to '*rmw', and stores the original value of '*rmw' into
198  *         '*orig', respecting the given memory order (or memory_order_seq_cst
199  *         if none is specified).
200  *
201  *         The results are similar to those that would be obtained with +=, -=,
202  *         |=, ^=, or |= on non-atomic types.
203  *
204  *
205  * atomic_flag
206  * ===========
207  *
208  * atomic_flag is a typedef for a type with two states, set and clear, that
209  * provides atomic test-and-set functionality.
210  *
211  *
212  * Life Cycle
213  * ----------
214  *
215  * ATOMIC_FLAG_INIT is an initializer for atomic_flag.  The initial state is
216  * "clear".
217  *
218  * An atomic_flag may also be initialized at runtime with atomic_flag_clear().
219  *
220  *
221  * Operations
222  * ----------
223  *
224  * The following functions are available.
225  *
226  *     bool atomic_flag_test_and_set(atomic_flag *object)
227  *     bool atomic_flag_test_and_set_explicit(atomic_flag *object,
228  *                                            memory_order);
229  *
230  *         Atomically sets '*object', respsecting the given memory order (or
231  *         memory_order_seq_cst for atomic_flag_test_and_set()).  Returns the
232  *         previous value of the flag (false for clear, true for set).
233  *
234  *     void atomic_flag_clear(atomic_flag *object);
235  *     void atomic_flag_clear_explicit(atomic_flag *object, memory_order);
236  *
237  *         Atomically clears '*object', respecting the given memory order (or
238  *         memory_order_seq_cst for atomic_flag_clear()).
239  */
240
241 #include <limits.h>
242 #include <pthread.h>
243 #include <stdbool.h>
244 #include <stddef.h>
245 #include <stdint.h>
246 #include "compiler.h"
247 #include "util.h"
248
249 #define IN_OVS_ATOMIC_H
250     #if __CHECKER__
251         /* sparse doesn't understand some GCC extensions we use. */
252         #include "ovs-atomic-pthreads.h"
253     #elif HAVE_STDATOMIC_H
254         #include "ovs-atomic-c11.h"
255     #elif __has_extension(c_atomic)
256         #include "ovs-atomic-clang.h"
257     #elif __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
258         #include "ovs-atomic-gcc4.7+.h"
259     #elif HAVE_GCC4_ATOMICS
260         #include "ovs-atomic-gcc4+.h"
261     #else
262         #include "ovs-atomic-pthreads.h"
263     #endif
264 #undef IN_OVS_ATOMIC_H
265
266 #ifndef OMIT_STANDARD_ATOMIC_TYPES
267 typedef ATOMIC(bool)               atomic_bool;
268
269 typedef ATOMIC(char)               atomic_char;
270 typedef ATOMIC(signed char)        atomic_schar;
271 typedef ATOMIC(unsigned char)      atomic_uchar;
272
273 typedef ATOMIC(short)              atomic_short;
274 typedef ATOMIC(unsigned short)     atomic_ushort;
275
276 typedef ATOMIC(int)                atomic_int;
277 typedef ATOMIC(unsigned int)       atomic_uint;
278
279 typedef ATOMIC(long)               atomic_long;
280 typedef ATOMIC(unsigned long)      atomic_ulong;
281
282 typedef ATOMIC(long long)          atomic_llong;
283 typedef ATOMIC(unsigned long long) atomic_ullong;
284
285 typedef ATOMIC(size_t)             atomic_size_t;
286 typedef ATOMIC(ptrdiff_t)          atomic_ptrdiff_t;
287
288 typedef ATOMIC(intmax_t)           atomic_intmax_t;
289 typedef ATOMIC(uintmax_t)          atomic_uintmax_t;
290
291 typedef ATOMIC(intptr_t)           atomic_intptr_t;
292 typedef ATOMIC(uintptr_t)          atomic_uintptr_t;
293 #endif  /* !OMIT_STANDARD_ATOMIC_TYPES */
294
295 /* Nonstandard atomic types. */
296 typedef ATOMIC(uint8_t)   atomic_uint8_t;
297 typedef ATOMIC(uint16_t)  atomic_uint16_t;
298 typedef ATOMIC(uint32_t)  atomic_uint32_t;
299 typedef ATOMIC(uint64_t)  atomic_uint64_t;
300
301 typedef ATOMIC(int8_t)    atomic_int8_t;
302 typedef ATOMIC(int16_t)   atomic_int16_t;
303 typedef ATOMIC(int32_t)   atomic_int32_t;
304 typedef ATOMIC(int64_t)   atomic_int64_t;
305
306 /* Reference count. */
307 struct ovs_refcount {
308     atomic_uint count;
309 };
310
311 /* Initializes 'refcount'.  The reference count is initially 1. */
312 static inline void
313 ovs_refcount_init(struct ovs_refcount *refcount)
314 {
315     atomic_init(&refcount->count, 1);
316 }
317
318 /* Increments 'refcount'. */
319 static inline void
320 ovs_refcount_ref(struct ovs_refcount *refcount)
321 {
322     unsigned int old_refcount;
323
324     atomic_add(&refcount->count, 1, &old_refcount);
325     ovs_assert(old_refcount > 0);
326 }
327
328 /* Decrements 'refcount' and returns the previous reference count.  Often used
329  * in this form:
330  *
331  * if (ovs_refcount_unref(&object->ref_cnt) == 1) {
332  *     // ...uninitialize object...
333  *     free(object);
334  * }
335  */
336 static inline unsigned int
337 ovs_refcount_unref(struct ovs_refcount *refcount)
338 {
339     unsigned int old_refcount;
340
341     atomic_sub(&refcount->count, 1, &old_refcount);
342     ovs_assert(old_refcount > 0);
343     return old_refcount;
344 }
345
346 /* Reads and returns 'ref_count_''s current reference count.
347  *
348  * Rarely useful. */
349 static inline unsigned int
350 ovs_refcount_read(const struct ovs_refcount *refcount_)
351 {
352     struct ovs_refcount *refcount
353         = CONST_CAST(struct ovs_refcount *, refcount_);
354     unsigned int count;
355
356     atomic_read(&refcount->count, &count);
357     return count;
358 }
359
360 #endif /* ovs-atomic.h */