ovs-atomic-types: Move into ovs-atomic.h.
[sliver-openvswitch.git] / lib / ovs-atomic-clang.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 /* 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 #define ATOMIC(TYPE) _Atomic(TYPE)
25
26 #define ATOMIC_VAR_INIT(VALUE) (VALUE)
27
28 #define atomic_init(OBJECT, VALUE) __c11_atomic_init(OBJECT, VALUE)
29 #define atomic_destroy(OBJECT) ((void) (OBJECT))
30
31 /* Clang hard-codes these exact values internally but does not appear to
32  * export any names for them. */
33 typedef enum {
34     memory_order_relaxed = 0,
35     memory_order_consume = 1,
36     memory_order_acquire = 2,
37     memory_order_release = 3,
38     memory_order_acq_rel = 4,
39     memory_order_seq_cst = 5
40 } memory_order;
41
42 #define atomic_thread_fence(ORDER) __c11_atomic_thread_fence(ORDER)
43 #define atomic_signal_fence(ORDER) __c11_atomic_signal_fence(ORDER)
44
45 #define atomic_store(DST, SRC) \
46     atomic_store_explicit(DST, SRC, memory_order_seq_cst)
47 #define atomic_store_explicit(DST, SRC, ORDER) \
48     __c11_atomic_store(DST, SRC, ORDER)
49
50
51 #define atomic_read(SRC, DST) \
52     atomic_read_explicit(SRC, DST, memory_order_seq_cst)
53 #define atomic_read_explicit(SRC, DST, ORDER)   \
54     (*(DST) = __c11_atomic_load(SRC, ORDER), \
55      (void) 0)
56
57 #define atomic_add(RMW, ARG, ORIG) \
58     atomic_add_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
59 #define atomic_sub(RMW, ARG, ORIG) \
60     atomic_sub_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
61 #define atomic_or(RMW, ARG, ORIG) \
62     atomic_or_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
63 #define atomic_xor(RMW, ARG, ORIG) \
64     atomic_xor_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
65 #define atomic_and(RMW, ARG, ORIG) \
66     atomic_and_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
67
68 #define atomic_add_explicit(RMW, ARG, ORIG, ORDER) \
69     (*(ORIG) = __c11_atomic_fetch_add(RMW, ARG, ORDER), (void) 0)
70 #define atomic_sub_explicit(RMW, ARG, ORIG, ORDER) \
71     (*(ORIG) = __c11_atomic_fetch_sub(RMW, ARG, ORDER), (void) 0)
72 #define atomic_or_explicit(RMW, ARG, ORIG, ORDER) \
73     (*(ORIG) = __c11_atomic_fetch_or(RMW, ARG, ORDER), (void) 0)
74 #define atomic_xor_explicit(RMW, ARG, ORIG, ORDER) \
75     (*(ORIG) = __c11_atomic_fetch_xor(RMW, ARG, ORDER), (void) 0)
76 #define atomic_and_explicit(RMW, ARG, ORIG, ORDER) \
77     (*(ORIG) = __c11_atomic_fetch_and(RMW, ARG, ORDER), (void) 0)
78
79 #include "ovs-atomic-flag-gcc4.7+.h"