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.
17 /* This header implements atomic operation primitives on GCC 4.7 and later. */
18 #ifndef IN_OVS_ATOMIC_H
19 #error "This header should only be included indirectly via ovs-atomic.h."
22 /* C11 standardized atomic type. */
23 typedef bool atomic_bool;
25 typedef char atomic_char;
26 typedef signed char atomic_schar;
27 typedef unsigned char atomic_uchar;
29 typedef short atomic_short;
30 typedef unsigned short atomic_ushort;
32 typedef int atomic_int;
33 typedef unsigned int atomic_uint;
35 typedef long atomic_long;
36 typedef unsigned long atomic_ulong;
38 typedef long long atomic_llong;
39 typedef unsigned long long atomic_ullong;
41 typedef size_t atomic_size_t;
42 typedef ptrdiff_t atomic_ptrdiff_t;
44 typedef intmax_t atomic_intmax_t;
45 typedef uintmax_t atomic_uintmax_t;
47 typedef intptr_t atomic_intptr_t;
48 typedef uintptr_t atomic_uintptr_t;
50 /* Nonstandard atomic types. */
51 typedef int8_t atomic_int8_t;
52 typedef uint8_t atomic_uint8_t;
54 typedef int16_t atomic_int16_t;
55 typedef uint16_t atomic_uint16_t;
57 typedef int32_t atomic_int32_t;
58 typedef uint32_t atomic_uint32_t;
60 typedef int64_t atomic_int64_t;
61 typedef uint64_t atomic_uint64_t;
64 memory_order_relaxed = __ATOMIC_RELAXED,
65 memory_order_consume = __ATOMIC_CONSUME,
66 memory_order_acquire = __ATOMIC_ACQUIRE,
67 memory_order_release = __ATOMIC_RELEASE,
68 memory_order_acq_rel = __ATOMIC_ACQ_REL,
69 memory_order_seq_cst = __ATOMIC_SEQ_CST
72 #define ATOMIC_VAR_INIT(VALUE) (VALUE)
73 #define atomic_init(OBJECT, VALUE) (*(OBJECT) = (VALUE), (void) 0)
75 #define atomic_thread_fence __atomic_thread_fence
76 #define atomic_signal_fence __atomic_signal_fence
77 #define atomic_is_lock_free __atomic_is_lock_free
79 #define atomic_store(DST, SRC) \
80 atomic_store_explicit(DST, SRC, memory_order_seq_cst)
81 #define atomic_store_explicit __atomic_store_n
83 #define atomic_read(SRC, DST) \
84 atomic_read_explicit(SRC, DST, memory_order_seq_cst)
85 #define atomic_read_explicit(SRC, DST, ORDER) \
86 (*(DST) = __atomic_load_n(SRC, ORDER), \
89 #define atomic_add(RMW, OPERAND, ORIG) \
90 atomic_add_explicit(RMW, OPERAND, ORIG, memory_order_seq_cst)
91 #define atomic_sub(RMW, OPERAND, ORIG) \
92 atomic_sub_explicit(RMW, OPERAND, ORIG, memory_order_seq_cst)
93 #define atomic_or(RMW, OPERAND, ORIG) \
94 atomic_or_explicit(RMW, OPERAND, ORIG, memory_order_seq_cst)
95 #define atomic_xor(RMW, OPERAND, ORIG) \
96 atomic_xor_explicit(RMW, OPERAND, ORIG, memory_order_seq_cst)
97 #define atomic_and(RMW, OPERAND, ORIG) \
98 atomic_and_explicit(RMW, OPERAND, ORIG, memory_order_seq_cst)
100 #define atomic_add_explicit(RMW, OPERAND, ORIG, ORDER) \
101 (*(ORIG) = __atomic_fetch_add(RMW, OPERAND, ORDER), (void) 0)
102 #define atomic_sub_explicit(RMW, OPERAND, ORIG, ORDER) \
103 (*(ORIG) = __atomic_fetch_sub(RMW, OPERAND, ORDER), (void) 0)
104 #define atomic_or_explicit(RMW, OPERAND, ORIG, ORDER) \
105 (*(ORIG) = __atomic_fetch_or(RMW, OPERAND, ORDER), (void) 0)
106 #define atomic_xor_explicit(RMW, OPERAND, ORIG, ORDER) \
107 (*(ORIG) = __atomic_fetch_xor(RMW, OPERAND, ORDER), (void) 0)
108 #define atomic_and_explicit(RMW, OPERAND, ORIG, ORDER) \
109 (*(ORIG) = __atomic_fetch_and(RMW, OPERAND, ORDER), (void) 0)
111 #include "ovs-atomic-flag-gcc4.7+.h"