2 * Copyright (c) 2013 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 using pthreads. */
18 #ifndef IN_OVS_ATOMIC_H
19 #error "This header should only be included indirectly via ovs-atomic.h."
22 #define OVS_ATOMIC_PTHREADS_IMPL 1
24 #define DEFINE_PTHREAD_ATOMIC(TYPE, NAME) \
27 pthread_mutex_t mutex; \
30 #define ATOMIC_BOOL_LOCK_FREE 0
31 DEFINE_PTHREAD_ATOMIC(bool, atomic_bool);
33 #define ATOMIC_CHAR_LOCK_FREE 0
34 DEFINE_PTHREAD_ATOMIC(char, atomic_char);
35 DEFINE_PTHREAD_ATOMIC(signed char, atomic_schar);
36 DEFINE_PTHREAD_ATOMIC(unsigned char, atomic_uchar);
38 #define ATOMIC_SHORT_LOCK_FREE 0
39 DEFINE_PTHREAD_ATOMIC(short, atomic_short);
40 DEFINE_PTHREAD_ATOMIC(unsigned short, atomic_ushort);
42 #define ATOMIC_INT_LOCK_FREE 0
43 DEFINE_PTHREAD_ATOMIC(int, atomic_int);
44 DEFINE_PTHREAD_ATOMIC(unsigned int, atomic_uint);
46 #define ATOMIC_LONG_LOCK_FREE 0
47 DEFINE_PTHREAD_ATOMIC(long, atomic_long);
48 DEFINE_PTHREAD_ATOMIC(unsigned long, atomic_ulong);
50 #define ATOMIC_LLONG_LOCK_FREE 0
51 DEFINE_PTHREAD_ATOMIC(long long, atomic_llong);
52 DEFINE_PTHREAD_ATOMIC(unsigned long long, atomic_ullong);
54 DEFINE_PTHREAD_ATOMIC(size_t, atomic_size_t);
55 DEFINE_PTHREAD_ATOMIC(ptrdiff_t, atomic_ptrdiff_t);
57 DEFINE_PTHREAD_ATOMIC(intmax_t, atomic_intmax_t);
58 DEFINE_PTHREAD_ATOMIC(uintmax_t, atomic_uintmax_t);
60 #define ATOMIC_POINTER_LOCK_FREE 0
61 DEFINE_PTHREAD_ATOMIC(intptr_t, atomic_intptr_t);
62 DEFINE_PTHREAD_ATOMIC(uintptr_t, atomic_uintptr_t);
64 /* Nonstandard atomic types. */
65 DEFINE_PTHREAD_ATOMIC(uint8_t, atomic_uint8_t);
66 DEFINE_PTHREAD_ATOMIC(uint16_t, atomic_uint16_t);
67 DEFINE_PTHREAD_ATOMIC(uint32_t, atomic_uint32_t);
68 DEFINE_PTHREAD_ATOMIC(int8_t, atomic_int8_t);
69 DEFINE_PTHREAD_ATOMIC(int16_t, atomic_int16_t);
70 DEFINE_PTHREAD_ATOMIC(int32_t, atomic_int32_t);
71 DEFINE_PTHREAD_ATOMIC(uint64_t, atomic_uint64_t);
72 DEFINE_PTHREAD_ATOMIC(int64_t, atomic_int64_t);
83 #define ATOMIC_VAR_INIT(VALUE) { VALUE, PTHREAD_MUTEX_INITIALIZER }
84 #define atomic_init(OBJECT, VALUE) \
85 ((OBJECT)->value = (VALUE), \
86 pthread_mutex_init(&(OBJECT)->mutex, NULL), \
88 #define atomic_destroy(OBJECT) \
89 (pthread_mutex_destroy(&(OBJECT)->mutex), \
93 atomic_thread_fence(memory_order order OVS_UNUSED)
99 atomic_signal_fence(memory_order order OVS_UNUSED)
104 #define atomic_is_lock_free(OBJ) false
106 #define atomic_store(DST, SRC) \
107 (pthread_mutex_lock(&(DST)->mutex), \
108 (DST)->value = (SRC), \
109 pthread_mutex_unlock(&(DST)->mutex), \
111 #define atomic_store_explicit(DST, SRC, ORDER) \
112 ((void) (ORDER), atomic_store(DST, SRC))
114 #define atomic_read(SRC, DST) \
115 (pthread_mutex_lock(CONST_CAST(pthread_mutex_t *, &(SRC)->mutex)), \
116 *(DST) = (SRC)->value, \
117 pthread_mutex_unlock(CONST_CAST(pthread_mutex_t *, &(SRC)->mutex)), \
119 #define atomic_read_explicit(SRC, DST, ORDER) \
120 ((void) (ORDER), atomic_read(SRC, DST))
122 #define atomic_op__(RMW, OPERATOR, OPERAND, ORIG) \
123 (pthread_mutex_lock(&(RMW)->mutex), \
124 *(ORIG) = (RMW)->value, \
125 (RMW)->value OPERATOR (OPERAND), \
126 pthread_mutex_unlock(&(RMW)->mutex), \
129 #define atomic_add(RMW, OPERAND, ORIG) atomic_op__(RMW, +=, OPERAND, ORIG)
130 #define atomic_sub(RMW, OPERAND, ORIG) atomic_op__(RMW, -=, OPERAND, ORIG)
131 #define atomic_or( RMW, OPERAND, ORIG) atomic_op__(RMW, |=, OPERAND, ORIG)
132 #define atomic_xor(RMW, OPERAND, ORIG) atomic_op__(RMW, ^=, OPERAND, ORIG)
133 #define atomic_and(RMW, OPERAND, ORIG) atomic_op__(RMW, &=, OPERAND, ORIG)
135 #define atomic_add_explicit(RMW, OPERAND, ORIG, ORDER) \
136 ((void) (ORDER), atomic_add(RMW, OPERAND, ORIG))
137 #define atomic_sub_explicit(RMW, OPERAND, ORIG, ORDER) \
138 ((void) (ORDER), atomic_sub(RMW, OPERAND, ORIG))
139 #define atomic_or_explicit(RMW, OPERAND, ORIG, ORDER) \
140 ((void) (ORDER), atomic_or(RMW, OPERAND, ORIG))
141 #define atomic_xor_explicit(RMW, OPERAND, ORIG, ORDER) \
142 ((void) (ORDER), atomic_xor(RMW, OPERAND, ORIG))
143 #define atomic_and_explicit(RMW, OPERAND, ORIG, ORDER) \
144 ((void) (ORDER), atomic_and(RMW, OPERAND, ORIG))
150 pthread_mutex_t mutex;
152 #define ATOMIC_FLAG_INIT { false, PTHREAD_MUTEX_INITIALIZER }
154 void atomic_flag_init(volatile atomic_flag *);
155 void atomic_flag_destroy(volatile atomic_flag *);
157 bool atomic_flag_test_and_set(volatile atomic_flag *);
158 bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order);
160 void atomic_flag_clear(volatile atomic_flag *);
161 void atomic_flag_clear_explicit(volatile atomic_flag *, memory_order);