2 * Copyright (c) 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* This header implements atomic operation primitives on GCC 4.x. */
18 #ifndef IN_OVS_ATOMIC_H
19 #error "This header should only be included indirectly via ovs-atomic.h."
22 #include "ovs-atomic-locked.h"
23 #define OVS_ATOMIC_GCC4P_IMPL 1
25 #define ATOMIC(TYPE) TYPE
27 #define ATOMIC_BOOL_LOCK_FREE 2
28 #define ATOMIC_CHAR_LOCK_FREE 2
29 #define ATOMIC_SHORT_LOCK_FREE 2
30 #define ATOMIC_INT_LOCK_FREE 2
31 #define ATOMIC_LONG_LOCK_FREE (ULONG_MAX <= UINTPTR_MAX ? 2 : 0)
32 #define ATOMIC_LLONG_LOCK_FREE (ULLONG_MAX <= UINTPTR_MAX ? 2 : 0)
33 #define ATOMIC_POINTER_LOCK_FREE 2
44 #define IS_LOCKLESS_ATOMIC(OBJECT) (sizeof(OBJECT) <= sizeof(void *))
46 #define ATOMIC_VAR_INIT(VALUE) VALUE
47 #define atomic_init(OBJECT, VALUE) (*(OBJECT) = (VALUE), (void) 0)
50 atomic_thread_fence(memory_order order)
52 if (order != memory_order_relaxed) {
58 atomic_thread_fence_if_seq_cst(memory_order order)
60 if (order == memory_order_seq_cst) {
66 atomic_signal_fence(memory_order order OVS_UNUSED)
68 if (order != memory_order_relaxed) {
69 asm volatile("" : : : "memory");
73 #define atomic_is_lock_free(OBJ) \
75 IF_LOCKLESS_ATOMIC(OBJ, true, false))
77 #define atomic_store(DST, SRC) \
78 atomic_store_explicit(DST, SRC, memory_order_seq_cst)
79 #define atomic_store_explicit(DST, SRC, ORDER) \
81 typeof(DST) dst__ = (DST); \
82 typeof(SRC) src__ = (SRC); \
83 memory_order order__ = (ORDER); \
85 if (IS_LOCKLESS_ATOMIC(*dst__)) { \
86 atomic_thread_fence(order__); \
88 atomic_thread_fence_if_seq_cst(order__); \
90 atomic_store_locked(DST, SRC); \
94 #define atomic_read(SRC, DST) \
95 atomic_read_explicit(SRC, DST, memory_order_seq_cst)
96 #define atomic_read_explicit(SRC, DST, ORDER) \
98 typeof(DST) dst__ = (DST); \
99 typeof(SRC) src__ = (SRC); \
100 memory_order order__ = (ORDER); \
102 if (IS_LOCKLESS_ATOMIC(*src__)) { \
103 atomic_thread_fence_if_seq_cst(order__); \
106 atomic_read_locked(SRC, DST); \
111 #define atomic_op__(RMW, OP, ARG, ORIG) \
113 typeof(RMW) rmw__ = (RMW); \
114 typeof(ARG) arg__ = (ARG); \
115 typeof(ORIG) orig__ = (ORIG); \
117 if (IS_LOCKLESS_ATOMIC(*rmw__)) { \
118 *orig__ = __sync_fetch_and_##OP(rmw__, arg__); \
120 atomic_op_locked(RMW, OP, ARG, ORIG); \
124 #define atomic_add(RMW, ARG, ORIG) atomic_op__(RMW, add, ARG, ORIG)
125 #define atomic_sub(RMW, ARG, ORIG) atomic_op__(RMW, sub, ARG, ORIG)
126 #define atomic_or( RMW, ARG, ORIG) atomic_op__(RMW, or, ARG, ORIG)
127 #define atomic_xor(RMW, ARG, ORIG) atomic_op__(RMW, xor, ARG, ORIG)
128 #define atomic_and(RMW, ARG, ORIG) atomic_op__(RMW, and, ARG, ORIG)
130 #define atomic_add_explicit(RMW, OPERAND, ORIG, ORDER) \
131 ((void) (ORDER), atomic_add(RMW, OPERAND, ORIG))
132 #define atomic_sub_explicit(RMW, OPERAND, ORIG, ORDER) \
133 ((void) (ORDER), atomic_sub(RMW, OPERAND, ORIG))
134 #define atomic_or_explicit(RMW, OPERAND, ORIG, ORDER) \
135 ((void) (ORDER), atomic_or(RMW, OPERAND, ORIG))
136 #define atomic_xor_explicit(RMW, OPERAND, ORIG, ORDER) \
137 ((void) (ORDER), atomic_xor(RMW, OPERAND, ORIG))
138 #define atomic_and_explicit(RMW, OPERAND, ORIG, ORDER) \
139 ((void) (ORDER), atomic_and(RMW, OPERAND, ORIG))
146 #define ATOMIC_FLAG_INIT { false }
149 atomic_flag_test_and_set(volatile atomic_flag *object)
151 return __sync_lock_test_and_set(&object->b, 1);
155 atomic_flag_test_and_set_explicit(volatile atomic_flag *object,
156 memory_order order OVS_UNUSED)
158 return atomic_flag_test_and_set(object);
162 atomic_flag_clear(volatile atomic_flag *object)
164 __sync_lock_release(&object->b);
168 atomic_flag_clear_explicit(volatile atomic_flag *object,
169 memory_order order OVS_UNUSED)
171 atomic_flag_clear(object);