ovs-atomic: New library for atomic operations.
[sliver-openvswitch.git] / lib / ovs-atomic-c11.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 compilers that
18  * have built-in support for C11 <stdatomic.h>  */
19 #ifndef IN_OVS_ATOMIC_H
20 #error "This header should only be included indirectly via ovs-atomic.h."
21 #endif
22
23 #include <stdatomic.h>
24
25 /* Nonstandard atomic types. */
26 typedef _Atomic(uint8_t)   atomic_uint8_t;
27 typedef _Atomic(uint16_t)  atomic_uint16_t;
28 typedef _Atomic(uint32_t)  atomic_uint32_t;
29 typedef _Atomic(uint64_t)  atomic_uint64_t;
30
31 typedef _Atomic(int8_t)    atomic_int8_t;
32 typedef _Atomic(int16_t)   atomic_int16_t;
33 typedef _Atomic(int32_t)   atomic_int32_t;
34 typedef _Atomic(int64_t)   atomic_int64_t;
35
36 #define atomic_read(SRC, DST) \
37     atomic_read_explicit(SRC, DST, memory_order_seq_cst)
38 #define atomic_read_explicit(SRC, DST, ORDER)   \
39     (*(DST) = atomic_load_explicit(SRC, ORDER), \
40      (void) 0)
41
42 #define atomic_add(RMW, ARG, ORIG) \
43     atomic_add_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
44 #define atomic_sub(RMW, ARG, ORIG) \
45     atomic_sub_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
46 #define atomic_or(RMW, ARG, ORIG) \
47     atomic_or_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
48 #define atomic_xor(RMW, ARG, ORIG) \
49     atomic_xor_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
50 #define atomic_and(RMW, ARG, ORIG) \
51     atomic_and_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
52
53 #define atomic_add_explicit(RMW, ARG, ORIG, ORDER) \
54     (*(ORIG) = atomic_fetch_add_explicit(RMW, ARG, ORDER), (void) 0)
55 #define atomic_sub_explicit(RMW, ARG, ORIG, ORDER) \
56     (*(ORIG) = atomic_fetch_sub_explicit(RMW, ARG, ORDER), (void) 0)
57 #define atomic_or_explicit(RMW, ARG, ORIG, ORDER) \
58     (*(ORIG) = atomic_fetch_or_explicit(RMW, ARG, ORDER), (void) 0)
59 #define atomic_xor_explicit(RMW, ARG, ORIG, ORDER) \
60     (*(ORIG) = atomic_fetch_xor_explicit(RMW, ARG, ORDER), (void) 0)
61 #define atomic_and_explicit(RMW, ARG, ORIG, ORDER) \
62     (*(ORIG) = atomic_fetch_and_explicit(RMW, ARG, ORDER), (void) 0)