]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovs-atomic-gcc4.7+.h
netdev-linux, netdev-bsd: Make access to AF_INET socket thread-safe.
[mirror_ovs.git] / lib / ovs-atomic-gcc4.7+.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 GCC 4.7 and later. */
18 #ifndef IN_OVS_ATOMIC_H
19 #error "This header should only be included indirectly via ovs-atomic.h."
20 #endif
21
22 /* C11 standardized atomic type. */
23 typedef bool atomic_bool;
24
25 typedef char atomic_char;
26 typedef signed char atomic_schar;
27 typedef unsigned char atomic_uchar;
28
29 typedef short atomic_short;
30 typedef unsigned short atomic_ushort;
31
32 typedef int atomic_int;
33 typedef unsigned int atomic_uint;
34
35 typedef long atomic_long;
36 typedef unsigned long atomic_ulong;
37
38 typedef long long atomic_llong;
39 typedef unsigned long long atomic_ullong;
40
41 typedef size_t atomic_size_t;
42 typedef ptrdiff_t atomic_ptrdiff_t;
43
44 typedef intmax_t atomic_intmax_t;
45 typedef uintmax_t atomic_uintmax_t;
46
47 typedef intptr_t atomic_intptr_t;
48 typedef uintptr_t atomic_uintptr_t;
49
50 /* Nonstandard atomic types. */
51 typedef int8_t atomic_int8_t;
52 typedef uint8_t atomic_uint8_t;
53
54 typedef int16_t atomic_int16_t;
55 typedef uint16_t atomic_uint16_t;
56
57 typedef int32_t atomic_int32_t;
58 typedef uint32_t atomic_uint32_t;
59
60 typedef int64_t atomic_int64_t;
61 typedef uint64_t atomic_uint64_t;
62
63 typedef enum {
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
70 } memory_order;
71
72 #define ATOMIC_VAR_INIT(VALUE) (VALUE)
73 #define atomic_init(OBJECT, VALUE) (*(OBJECT) = (VALUE), (void) 0)
74
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
78
79 #define atomic_store(DST, SRC) \
80 atomic_store_explicit(DST, SRC, memory_order_seq_cst)
81 #define atomic_store_explicit __atomic_store_n
82
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), \
87 (void) 0)
88
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)
99
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)
110 \f
111 /* atomic_flag */
112
113 typedef struct {
114 unsigned char b;
115 } atomic_flag;
116 #define ATOMIC_FLAG_INIT { .b = false }
117
118 static inline bool
119 atomic_flag_test_and_set_explicit(volatile atomic_flag *object,
120 memory_order order)
121 {
122 return __atomic_test_and_set(&object->b, order);
123 }
124
125 static inline bool
126 atomic_flag_test_and_set(volatile atomic_flag *object)
127 {
128 return atomic_flag_test_and_set_explicit(object, memory_order_seq_cst);
129 }
130
131 static inline void
132 atomic_flag_clear_explicit(volatile atomic_flag *object, memory_order order)
133 {
134 __atomic_clear(object, order);
135 }
136
137 static inline void
138 atomic_flag_clear(volatile atomic_flag *object)
139 {
140 atomic_flag_clear_explicit(object, memory_order_seq_cst);
141 }