1 //===-- sanitizer_atomic_msvc.h ---------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
9 // Not intended for direct inclusion. Include sanitizer_atomic.h.
11 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_ATOMIC_MSVC_H
14 #define SANITIZER_ATOMIC_MSVC_H
16 extern "C" void _ReadWriteBarrier();
17 #pragma intrinsic(_ReadWriteBarrier)
18 extern "C" void _mm_mfence();
19 #pragma intrinsic(_mm_mfence)
20 extern "C" void _mm_pause();
21 #pragma intrinsic(_mm_pause)
22 extern "C" long _InterlockedExchangeAdd( // NOLINT
23 long volatile * Addend
, long Value
); // NOLINT
24 #pragma intrinsic(_InterlockedExchangeAdd)
25 extern "C" short _InterlockedCompareExchange16( // NOLINT
26 short volatile *Destination
, // NOLINT
27 short Exchange
, short Comparand
); // NOLINT
28 #pragma intrinsic(_InterlockedCompareExchange16)
30 long long _InterlockedCompareExchange64( // NOLINT
31 long long volatile *Destination
, // NOLINT
32 long long Exchange
, long long Comparand
); // NOLINT
33 #pragma intrinsic(_InterlockedCompareExchange64)
34 extern "C" void *_InterlockedCompareExchangePointer(
35 void *volatile *Destination
,
36 void *Exchange
, void *Comparand
);
37 #pragma intrinsic(_InterlockedCompareExchangePointer)
39 long __cdecl
_InterlockedCompareExchange( // NOLINT
40 long volatile *Destination
, // NOLINT
41 long Exchange
, long Comparand
); // NOLINT
42 #pragma intrinsic(_InterlockedCompareExchange)
45 extern "C" long long _InterlockedExchangeAdd64( // NOLINT
46 long long volatile * Addend
, long long Value
); // NOLINT
47 #pragma intrinsic(_InterlockedExchangeAdd64)
50 namespace __sanitizer
{
52 INLINE
void atomic_signal_fence(memory_order
) {
56 INLINE
void atomic_thread_fence(memory_order
) {
60 INLINE
void proc_yield(int cnt
) {
61 for (int i
= 0; i
< cnt
; i
++)
66 INLINE typename
T::Type
atomic_load(
67 const volatile T
*a
, memory_order mo
) {
68 DCHECK(mo
& (memory_order_relaxed
| memory_order_consume
69 | memory_order_acquire
| memory_order_seq_cst
));
70 DCHECK(!((uptr
)a
% sizeof(*a
)));
72 // FIXME(dvyukov): 64-bit load is not atomic on 32-bits.
73 if (mo
== memory_order_relaxed
) {
76 atomic_signal_fence(memory_order_seq_cst
);
78 atomic_signal_fence(memory_order_seq_cst
);
84 INLINE
void atomic_store(volatile T
*a
, typename
T::Type v
, memory_order mo
) {
85 DCHECK(mo
& (memory_order_relaxed
| memory_order_release
86 | memory_order_seq_cst
));
87 DCHECK(!((uptr
)a
% sizeof(*a
)));
88 // FIXME(dvyukov): 64-bit store is not atomic on 32-bits.
89 if (mo
== memory_order_relaxed
) {
92 atomic_signal_fence(memory_order_seq_cst
);
94 atomic_signal_fence(memory_order_seq_cst
);
96 if (mo
== memory_order_seq_cst
)
97 atomic_thread_fence(memory_order_seq_cst
);
100 INLINE u32
atomic_fetch_add(volatile atomic_uint32_t
*a
,
101 u32 v
, memory_order mo
) {
103 DCHECK(!((uptr
)a
% sizeof(*a
)));
104 return (u32
)_InterlockedExchangeAdd(
105 (volatile long*)&a
->val_dont_use
, (long)v
); // NOLINT
108 INLINE uptr
atomic_fetch_add(volatile atomic_uintptr_t
*a
,
109 uptr v
, memory_order mo
) {
111 DCHECK(!((uptr
)a
% sizeof(*a
)));
113 return (uptr
)_InterlockedExchangeAdd64(
114 (volatile long long*)&a
->val_dont_use
, (long long)v
); // NOLINT
116 return (uptr
)_InterlockedExchangeAdd(
117 (volatile long*)&a
->val_dont_use
, (long)v
); // NOLINT
121 INLINE u32
atomic_fetch_sub(volatile atomic_uint32_t
*a
,
122 u32 v
, memory_order mo
) {
124 DCHECK(!((uptr
)a
% sizeof(*a
)));
125 return (u32
)_InterlockedExchangeAdd(
126 (volatile long*)&a
->val_dont_use
, -(long)v
); // NOLINT
129 INLINE uptr
atomic_fetch_sub(volatile atomic_uintptr_t
*a
,
130 uptr v
, memory_order mo
) {
132 DCHECK(!((uptr
)a
% sizeof(*a
)));
134 return (uptr
)_InterlockedExchangeAdd64(
135 (volatile long long*)&a
->val_dont_use
, -(long long)v
); // NOLINT
137 return (uptr
)_InterlockedExchangeAdd(
138 (volatile long*)&a
->val_dont_use
, -(long)v
); // NOLINT
142 INLINE u8
atomic_exchange(volatile atomic_uint8_t
*a
,
143 u8 v
, memory_order mo
) {
145 DCHECK(!((uptr
)a
% sizeof(*a
)));
149 xchg
[eax
], cl
// NOLINT
155 INLINE u16
atomic_exchange(volatile atomic_uint16_t
*a
,
156 u16 v
, memory_order mo
) {
158 DCHECK(!((uptr
)a
% sizeof(*a
)));
162 xchg
[eax
], cx
// NOLINT
168 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uint8_t
*a
,
173 DCHECK(!((uptr
)a
% sizeof(*a
)));
180 lock cmpxchg
[ecx
], dl
189 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uintptr_t
*a
,
194 uptr prev
= (uptr
)_InterlockedCompareExchangePointer(
195 (void*volatile*)&a
->val_dont_use
, (void*)xchg
, (void*)cmpv
);
202 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uint16_t
*a
,
207 u16 prev
= (u16
)_InterlockedCompareExchange16(
208 (volatile short*)&a
->val_dont_use
, (short)xchg
, (short)cmpv
);
215 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uint32_t
*a
,
220 u32 prev
= (u32
)_InterlockedCompareExchange(
221 (volatile long*)&a
->val_dont_use
, (long)xchg
, (long)cmpv
);
228 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uint64_t
*a
,
233 u64 prev
= (u64
)_InterlockedCompareExchange64(
234 (volatile long long*)&a
->val_dont_use
, (long long)xchg
, (long long)cmpv
);
242 INLINE
bool atomic_compare_exchange_weak(volatile T
*a
,
243 typename
T::Type
*cmp
,
244 typename
T::Type xchg
,
246 return atomic_compare_exchange_strong(a
, cmp
, xchg
, mo
);
249 } // namespace __sanitizer
251 #endif // SANITIZER_ATOMIC_CLANG_H