Merge branch 'master' of ssh://git.onelab.eu/git/sliver-openvswitch
[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 #define atomic_destroy(OBJECT) ((void) (OBJECT))
67
68 /* Clang hard-codes these exact values internally but does not appear to
69  * export any names for them. */
70 typedef enum {
71     memory_order_relaxed = 0,
72     memory_order_consume = 1,
73     memory_order_acquire = 2,
74     memory_order_release = 3,
75     memory_order_acq_rel = 4,
76     memory_order_seq_cst = 5
77 } memory_order;
78
79 #define atomic_thread_fence(ORDER) __c11_atomic_thread_fence(ORDER)
80 #define atomic_signal_fence(ORDER) __c11_atomic_signal_fence(ORDER)
81
82 #define atomic_store(DST, SRC) \
83     atomic_store_explicit(DST, SRC, memory_order_seq_cst)
84 #define atomic_store_explicit(DST, SRC, ORDER) \
85     __c11_atomic_store(DST, SRC, ORDER)
86
87
88 #define atomic_read(SRC, DST) \
89     atomic_read_explicit(SRC, DST, memory_order_seq_cst)
90 #define atomic_read_explicit(SRC, DST, ORDER)   \
91     (*(DST) = __c11_atomic_load(SRC, ORDER), \
92      (void) 0)
93
94 #define atomic_add(RMW, ARG, ORIG) \
95     atomic_add_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
96 #define atomic_sub(RMW, ARG, ORIG) \
97     atomic_sub_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
98 #define atomic_or(RMW, ARG, ORIG) \
99     atomic_or_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
100 #define atomic_xor(RMW, ARG, ORIG) \
101     atomic_xor_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
102 #define atomic_and(RMW, ARG, ORIG) \
103     atomic_and_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
104
105 #define atomic_add_explicit(RMW, ARG, ORIG, ORDER) \
106     (*(ORIG) = __c11_atomic_fetch_add(RMW, ARG, ORDER), (void) 0)
107 #define atomic_sub_explicit(RMW, ARG, ORIG, ORDER) \
108     (*(ORIG) = __c11_atomic_fetch_sub(RMW, ARG, ORDER), (void) 0)
109 #define atomic_or_explicit(RMW, ARG, ORIG, ORDER) \
110     (*(ORIG) = __c11_atomic_fetch_or(RMW, ARG, ORDER), (void) 0)
111 #define atomic_xor_explicit(RMW, ARG, ORIG, ORDER) \
112     (*(ORIG) = __c11_atomic_fetch_xor(RMW, ARG, ORDER), (void) 0)
113 #define atomic_and_explicit(RMW, ARG, ORIG, ORDER) \
114     (*(ORIG) = __c11_atomic_fetch_and(RMW, ARG, ORDER), (void) 0)
115
116 #include "ovs-atomic-flag-gcc4.7+.h"