]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/ovs-atomic-msvc.h
2 * Copyright (c) 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 for MSVC
18 * on i586 or greater platforms (32 bit). */
19 #ifndef IN_OVS_ATOMIC_H
20 #error "This header should only be included indirectly via ovs-atomic.h."
23 /* From msdn documentation: With Visual Studio 2003, volatile to volatile
24 * references are ordered; the compiler will not re-order volatile variable
25 * access. With Visual Studio 2005, the compiler also uses acquire semantics
26 * for read operations on volatile variables and release semantics for write
27 * operations on volatile variables (when supported by the CPU).
29 * Though there is no clear documentation that states that anything greater
30 * than VS 2005 has the same behavior as described above, looking through MSVCs
31 * C++ atomics library in VS2013 shows that the compiler still takes
32 * acquire/release semantics on volatile variables. */
33 #define ATOMIC(TYPE) TYPE volatile
44 #if _MSC_VER > 1800 && defined(_M_IX86)
45 /* From WDK 10 _InlineInterlocked* functions are renamed to
46 * _InlineInterlocked* although the documentation does not specify it */
47 #define _InterlockedExchangeAdd64 _InlineInterlockedExchangeAdd64
48 #define _InterlockedExchange64 _InlineInterlockedExchange64
51 #define ATOMIC_BOOL_LOCK_FREE 2
52 #define ATOMIC_CHAR_LOCK_FREE 2
53 #define ATOMIC_SHORT_LOCK_FREE 2
54 #define ATOMIC_INT_LOCK_FREE 2
55 #define ATOMIC_LONG_LOCK_FREE 2
56 #define ATOMIC_LLONG_LOCK_FREE 2
57 #define ATOMIC_POINTER_LOCK_FREE 2
59 #define IS_LOCKLESS_ATOMIC(OBJECT) \
60 (sizeof(OBJECT) <= 8 && IS_POW2(sizeof(OBJECT)))
62 #define ATOMIC_VAR_INIT(VALUE) (VALUE)
63 #define atomic_init(OBJECT, VALUE) (*(OBJECT) = (VALUE), (void) 0)
66 atomic_compiler_barrier(memory_order order
)
68 /* In case of 'memory_order_consume', it is implicitly assumed that
69 * the compiler will not move instructions that have data-dependency
70 * on the variable in question before the barrier. */
71 if (order
> memory_order_consume
) {
77 atomic_thread_fence(memory_order order
)
79 /* x86 is strongly ordered and acquire/release semantics come
81 atomic_compiler_barrier(order
);
82 if (order
== memory_order_seq_cst
) {
84 atomic_compiler_barrier(order
);
89 atomic_signal_fence(memory_order order
)
91 atomic_compiler_barrier(order
);
94 /* 1, 2 and 4 bytes loads and stores are atomic on aligned memory. In addition,
95 * since the compiler automatically takes acquire and release semantics on
96 * volatile variables, for any order lesser than 'memory_order_seq_cst', we
97 * can directly assign or read values. */
99 #define atomic_store32(DST, SRC, ORDER) \
100 if (ORDER == memory_order_seq_cst) { \
101 InterlockedExchange((long volatile *) (DST), \
107 /* MSVC converts 64 bit writes into two instructions. So there is
108 * a possibility that an interrupt can make a 64 bit write non-atomic even
109 * when 8 byte aligned. So use InterlockedExchange64().
111 * For atomic stores, 'consume' and 'acquire' semantics are not valid. But we
112 * are using 'Exchange' to get atomic stores here and we only have
113 * InterlockedExchange64(), InterlockedExchangeNoFence64() and
114 * InterlockedExchange64Acquire() available. So we are forced to use
115 * InterlockedExchange64() which uses full memory barrier for everything
116 * greater than 'memory_order_relaxed'. */
118 #define atomic_store64(DST, SRC, ORDER) \
119 if (ORDER == memory_order_relaxed) { \
120 InterlockedExchangeNoFence64((int64_t volatile *) (DST), \
123 InterlockedExchange64((int64_t volatile *) (DST), (int64_t) (SRC));\
126 /* 64 bit writes are atomic on amd64 if 64 bit aligned. */
127 #define atomic_store64(DST, SRC, ORDER) \
128 atomic_storeX(64, DST, SRC, ORDER)
131 #define atomic_store8(DST, SRC, ORDER) \
132 if (ORDER == memory_order_seq_cst) { \
133 InterlockedExchange8((char volatile *) (DST), (char) (SRC)); \
138 #define atomic_store16(DST, SRC, ORDER) \
139 if (ORDER == memory_order_seq_cst) { \
140 InterlockedExchange16((short volatile *) (DST), (short) (SRC)); \
145 #define atomic_store(DST, SRC) \
146 atomic_store_explicit(DST, SRC, memory_order_seq_cst)
148 #define atomic_store_explicit(DST, SRC, ORDER) \
149 if (sizeof *(DST) == 1) { \
150 atomic_store8(DST, SRC, ORDER) \
151 } else if (sizeof *(DST) == 2) { \
152 atomic_store16( DST, SRC, ORDER) \
153 } else if (sizeof *(DST) == 4) { \
154 atomic_store32(DST, SRC, ORDER) \
155 } else if (sizeof *(DST) == 8) { \
156 atomic_store64(DST, SRC, ORDER) \
161 /* On x86, for 'memory_order_seq_cst', if stores are locked, the corresponding
162 * reads don't need to be locked (based on the following in Intel Developers
164 * “Locked operations are atomic with respect to all other memory operations
165 * and all externally visible events. Only instruction fetch and page table
166 * accesses can pass locked instructions. Locked instructions can be used to
167 * synchronize data written by one processor and read by another processor.
168 * For the P6 family processors, locked operations serialize all outstanding
169 * load and store operations (that is, wait for them to complete). This rule
170 * is also true for the Pentium 4 and Intel Xeon processors, with one
171 * exception. Load operations that reference weakly ordered memory types
172 * (such as the WC memory type) may not be serialized."). */
174 /* For 8, 16 and 32 bit variations. */
175 #define atomic_readX(SRC, DST, ORDER) \
178 /* MSVC converts 64 bit reads into two instructions. So there is
179 * a possibility that an interrupt can make a 64 bit read non-atomic even
180 * when 8 byte aligned. So use fully memory barrier InterlockedOr64(). */
182 #define atomic_read64(SRC, DST, ORDER) \
183 __pragma (warning(push)) \
184 __pragma (warning(disable:4047)) \
185 *(DST) = InterlockedOr64((int64_t volatile *) (SRC), 0); \
186 __pragma (warning(pop))
188 /* 64 bit reads are atomic on amd64 if 64 bit aligned. */
189 #define atomic_read64(SRC, DST, ORDER) \
193 #define atomic_read(SRC, DST) \
194 atomic_read_explicit(SRC, DST, memory_order_seq_cst)
196 #define atomic_read_explicit(SRC, DST, ORDER) \
197 if (sizeof *(DST) == 1 || sizeof *(DST) == 2 || sizeof *(DST) == 4) { \
198 atomic_readX(SRC, DST, ORDER) \
199 } else if (sizeof *(DST) == 8) { \
200 atomic_read64(SRC, DST, ORDER) \
205 /* For add, sub, and logical operations, for 8, 16 and 64 bit data types,
206 * functions for all the different memory orders does not exist
207 * (though documentation exists for some of them). The MSVC C++ library which
208 * implements the c11 atomics simply calls the full memory barrier function
209 * for everything in x86(see xatomic.h). So do the same here. */
211 /* For 8, 16 and 64 bit variations. */
212 #define atomic_op(OP, X, RMW, ARG, ORIG, ORDER) \
213 atomic_##OP##_generic(X, RMW, ARG, ORIG, ORDER)
215 /* Arithmetic addition calls. */
217 #define atomic_add8(RMW, ARG, ORIG, ORDER) \
218 *(ORIG) = _InterlockedExchangeAdd8((char volatile *) (RMW), \
221 #define atomic_add16(RMW, ARG, ORIG, ORDER) \
222 *(ORIG) = _InterlockedExchangeAdd16((short volatile *) (RMW), \
225 #define atomic_add32(RMW, ARG, ORIG, ORDER) \
226 *(ORIG) = InterlockedExchangeAdd((long volatile *) (RMW), \
228 #define atomic_add64(RMW, ARG, ORIG, ORDER) \
229 *(ORIG) = _InterlockedExchangeAdd64((int64_t volatile *) (RMW), \
232 #define atomic_add(RMW, ARG, ORIG) \
233 atomic_add_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
235 #define atomic_add_explicit(RMW, ARG, ORIG, ORDER) \
236 if (sizeof *(RMW) == 1) { \
237 atomic_add8(RMW, ARG, ORIG, ORDER) \
238 } else if (sizeof *(RMW) == 2) { \
239 atomic_add16(RMW, ARG, ORIG, ORDER) \
240 } else if (sizeof *(RMW) == 4) { \
241 atomic_add32(RMW, ARG, ORIG, ORDER) \
242 } else if (sizeof *(RMW) == 8) { \
243 atomic_add64(RMW, ARG, ORIG, ORDER) \
248 /* Arithmetic subtraction calls. */
250 #define atomic_sub(RMW, ARG, ORIG) \
251 atomic_add_explicit(RMW, (0 - (ARG)), ORIG, memory_order_seq_cst)
253 #define atomic_sub_explicit(RMW, ARG, ORIG, ORDER) \
254 atomic_add_explicit(RMW, (0 - (ARG)), ORIG, ORDER)
256 /* Logical 'and' calls. */
258 #define atomic_and32(RMW, ARG, ORIG, ORDER) \
259 *(ORIG) = InterlockedAnd((int32_t volatile *) (RMW), (int32_t) (ARG));
261 /* For 8, 16 and 64 bit variations. */
262 #define atomic_and_generic(X, RMW, ARG, ORIG, ORDER) \
263 *(ORIG) = InterlockedAnd##X((int##X##_t volatile *) (RMW), \
266 #define atomic_and(RMW, ARG, ORIG) \
267 atomic_and_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
269 #define atomic_and_explicit(RMW, ARG, ORIG, ORDER) \
270 if (sizeof *(RMW) == 1) { \
271 atomic_op(and, 8, RMW, ARG, ORIG, ORDER) \
272 } else if (sizeof *(RMW) == 2) { \
273 atomic_op(and, 16, RMW, ARG, ORIG, ORDER) \
274 } else if (sizeof *(RMW) == 4) { \
275 atomic_and32(RMW, ARG, ORIG, ORDER) \
276 } else if (sizeof *(RMW) == 8) { \
277 atomic_op(and, 64, RMW, ARG, ORIG, ORDER) \
282 /* Logical 'Or' calls. */
284 #define atomic_or32(RMW, ARG, ORIG, ORDER) \
285 *(ORIG) = InterlockedOr((int32_t volatile *) (RMW), (int32_t) (ARG));
287 /* For 8, 16 and 64 bit variations. */
288 #define atomic_or_generic(X, RMW, ARG, ORIG, ORDER) \
289 *(ORIG) = InterlockedOr##X((int##X##_t volatile *) (RMW), \
292 #define atomic_or(RMW, ARG, ORIG) \
293 atomic_or_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
295 #define atomic_or_explicit(RMW, ARG, ORIG, ORDER) \
296 if (sizeof *(RMW) == 1) { \
297 atomic_op(or, 8, RMW, ARG, ORIG, ORDER) \
298 } else if (sizeof *(RMW) == 2) { \
299 atomic_op(or, 16, RMW, ARG, ORIG, ORDER) \
300 } else if (sizeof *(RMW) == 4) { \
301 atomic_or32(RMW, ARG, ORIG, ORDER) \
302 } else if (sizeof *(RMW) == 8) { \
303 atomic_op(or, 64, RMW, ARG, ORIG, ORDER) \
308 /* Logical Xor calls. */
310 #define atomic_xor32(RMW, ARG, ORIG, ORDER) \
311 *(ORIG) = InterlockedXor((int32_t volatile *) (RMW), (int32_t) (ARG));
313 /* For 8, 16 and 64 bit variations. */
314 #define atomic_xor_generic(X, RMW, ARG, ORIG, ORDER) \
315 *(ORIG) = InterlockedXor##X((int##X##_t volatile *) (RMW), \
318 #define atomic_xor(RMW, ARG, ORIG) \
319 atomic_xor_explicit(RMW, ARG, ORIG, memory_order_seq_cst)
321 #define atomic_xor_explicit(RMW, ARG, ORIG, ORDER) \
322 if (sizeof *(RMW) == 1) { \
323 atomic_op(xor, 8, RMW, ARG, ORIG, ORDER) \
324 } else if (sizeof *(RMW) == 2) { \
325 atomic_op(xor, 16, RMW, ARG, ORIG, ORDER) \
326 } else if (sizeof *(RMW) == 4) { \
327 atomic_xor32(RMW, ARG, ORIG, ORDER); \
328 } else if (sizeof *(RMW) == 8) { \
329 atomic_op(xor, 64, RMW, ARG, ORIG, ORDER) \
334 #define atomic_compare_exchange_strong(DST, EXP, SRC) \
335 atomic_compare_exchange_strong_explicit(DST, EXP, SRC, \
336 memory_order_seq_cst, \
337 memory_order_seq_cst)
339 #define atomic_compare_exchange_weak atomic_compare_exchange_strong
340 #define atomic_compare_exchange_weak_explicit \
341 atomic_compare_exchange_strong_explicit
343 /* MSVCs c++ compiler implements c11 atomics and looking through its
344 * implementation (in xatomic.h), orders are ignored for x86 platform.
345 * Do the same here. */
347 atomic_compare_exchange8(int8_t volatile *dst
, int8_t *expected
, int8_t src
)
349 int8_t previous
= _InterlockedCompareExchange8((char volatile *)dst
,
351 if (previous
== *expected
) {
354 *expected
= previous
;
360 atomic_compare_exchange16(int16_t volatile *dst
, int16_t *expected
,
363 int16_t previous
= InterlockedCompareExchange16(dst
, src
, *expected
);
364 if (previous
== *expected
) {
367 *expected
= previous
;
373 atomic_compare_exchange32(int32_t volatile *dst
, int32_t *expected
,
376 int32_t previous
= InterlockedCompareExchange((long volatile *)dst
,
378 if (previous
== *expected
) {
381 *expected
= previous
;
387 atomic_compare_exchange64(int64_t volatile *dst
, int64_t *expected
,
390 int64_t previous
= InterlockedCompareExchange64(dst
, src
, *expected
);
391 if (previous
== *expected
) {
394 *expected
= previous
;
400 atomic_compare_unreachable()
405 #define atomic_compare_exchange_strong_explicit(DST, EXP, SRC, ORD1, ORD2) \
406 (sizeof *(DST) == 1 \
407 ? atomic_compare_exchange8((int8_t volatile *) (DST), (int8_t *) (EXP), \
409 : (sizeof *(DST) == 2 \
410 ? atomic_compare_exchange16((int16_t volatile *) (DST), \
411 (int16_t *) (EXP), (int16_t) (SRC)) \
412 : (sizeof *(DST) == 4 \
413 ? atomic_compare_exchange32((int32_t volatile *) (DST), \
414 (int32_t *) (EXP), (int32_t) (SRC)) \
415 : (sizeof *(DST) == 8 \
416 ? atomic_compare_exchange64((int64_t volatile *) (DST), \
417 (int64_t *) (EXP), (int64_t) (SRC)) \
418 : ovs_fatal(0, "atomic operation with size greater than 8 bytes"), \
419 atomic_compare_unreachable()))))
424 typedef ATOMIC(int32_t) atomic_flag
;
425 #define ATOMIC_FLAG_INIT 0
427 #define atomic_flag_test_and_set(FLAG) \
428 (bool) InterlockedBitTestAndSet(FLAG, 0)
430 #define atomic_flag_test_and_set_explicit(FLAG, ORDER) \
431 atomic_flag_test_and_set(FLAG)
433 #define atomic_flag_clear_explicit(FLAG, ORDER) \
435 #define atomic_flag_clear(FLAG) \
436 InterlockedBitTestAndReset(FLAG, 0)