ovs-atomic: Add native Clang implementation.
[sliver-openvswitch.git] / lib / ovs-atomic-clang.h
1 /*
2  * Copyright (c) 2013 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 /* This header implements atomic operation primitives on Clang. */
18 #ifndef IN_OVS_ATOMIC_H
19 #error "This header should only be included indirectly via ovs-atomic.h."
20 #endif
21
22 #define OVS_ATOMIC_CLANG_IMPL 1
23
24 /* Standard atomic types. */
25 typedef _Atomic(_Bool) atomic_bool;
26
27 typedef _Atomic(char) atomic_char;
28 typedef _Atomic(signed char) atomic_schar;
29 typedef _Atomic(unsigned char) atomic_uchar;
30
31 typedef _Atomic(short) atomic_short;
32 typedef _Atomic(unsigned short) atomic_ushort;
33
34 typedef _Atomic(int) atomic_int;
35 typedef _Atomic(unsigned int) atomic_uint;
36
37 typedef _Atomic(long) atomic_long;
38 typedef _Atomic(unsigned long) atomic_ulong;
39
40 typedef _Atomic(long long) atomic_llong;
41 typedef _Atomic(unsigned long long) atomic_ullong;
42
43 typedef _Atomic(size_t) atomic_size_t;
44 typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
45
46 typedef _Atomic(intmax_t) atomic_intmax_t;
47 typedef _Atomic(uintmax_t) atomic_uintmax_t;
48
49 typedef _Atomic(intptr_t) atomic_intptr_t;
50 typedef _Atomic(uintptr_t) atomic_uintptr_t;
51
52 /* Nonstandard atomic types. */
53 typedef _Atomic(uint8_t)   atomic_uint8_t;
54 typedef _Atomic(uint16_t)  atomic_uint16_t;
55 typedef _Atomic(uint32_t)  atomic_uint32_t;
56 typedef _Atomic(uint64_t)  atomic_uint64_t;
57
58 typedef _Atomic(int8_t)    atomic_int8_t;
59 typedef _Atomic(int16_t)   atomic_int16_t;
60 typedef _Atomic(int32_t)   atomic_int32_t;
61 typedef _Atomic(int64_t)   atomic_int64_t;
62
63 #define ATOMIC_VAR_INIT(VALUE) (VALUE)
64
65 #define atomic_init(OBJECT, VALUE) __c11_atomic_init(OBJECT, VALUE)
66
67 /* Clang hard-codes these exact values internally but does not appear to
68  * export any names for them. */
69 typedef enum {
70     memory_order_relaxed = 0,
71     memory_order_consume = 1,
72     memory_order_acquire = 2,
73     memory_order_release = 3,
74     memory_order_acq_rel = 4,
75     memory_order_seq_cst = 5
76 } memory_order;
77
78 #define atomic_thread_fence(ORDER) __c11_atomic_thread_fence(ORDER)
79 #define atomic_signal_fence(ORDER) __c11_atomic_signal_fence(ORDER)
80
81 #define atomic_store(DST, SRC) \
82     atomic_store_explicit(DST, SRC, memory_order_seq_cst)
83 #define atomic_store_explicit(DST, SRC, ORDER) \
84     __c11_atomic_store(DST, SRC, ORDER)
85
86
87 #define atomic_read(SRC, DST) \
88     atomic_read_explicit(SRC, DST, memory_order_seq_cst)
89 #define atomic_read_explicit(SRC, DST, ORDER)   \
90     (*(DST) = __c11_atomic_load(SRC, ORDER), \
91      (void) 0)
92
93 #define atomic_add(RMW, ARG, ORIG) \
94     atomic_add_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
95 #define atomic_sub(RMW, ARG, ORIG) \
96     atomic_sub_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
97 #define atomic_or(RMW, ARG, ORIG) \
98     atomic_or_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
99 #define atomic_xor(RMW, ARG, ORIG) \
100     atomic_xor_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
101 #define atomic_and(RMW, ARG, ORIG) \
102     atomic_and_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
103
104 #define atomic_add_explicit(RMW, ARG, ORIG, ORDER) \
105     (*(ORIG) = __c11_atomic_fetch_add(RMW, ARG, ORDER), (void) 0)
106 #define atomic_sub_explicit(RMW, ARG, ORIG, ORDER) \
107     (*(ORIG) = __c11_atomic_fetch_sub(RMW, ARG, ORDER), (void) 0)
108 #define atomic_or_explicit(RMW, ARG, ORIG, ORDER) \
109     (*(ORIG) = __c11_atomic_fetch_or(RMW, ARG, ORDER), (void) 0)
110 #define atomic_xor_explicit(RMW, ARG, ORIG, ORDER) \
111     (*(ORIG) = __c11_atomic_fetch_xor(RMW, ARG, ORDER), (void) 0)
112 #define atomic_and_explicit(RMW, ARG, ORIG, ORDER) \
113     (*(ORIG) = __c11_atomic_fetch_and(RMW, ARG, ORDER), (void) 0)
114
115 #include "ovs-atomic-flag-gcc4.7+.h"