1 //===-- sanitizer_atomic_msvc.h ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 // Not intended for direct inclusion. Include sanitizer_atomic.h.
13 //===----------------------------------------------------------------------===//
15 #ifndef SANITIZER_ATOMIC_MSVC_H
16 #define SANITIZER_ATOMIC_MSVC_H
18 extern "C" void _ReadWriteBarrier();
19 #pragma intrinsic(_ReadWriteBarrier)
20 extern "C" void _mm_mfence();
21 #pragma intrinsic(_mm_mfence)
22 extern "C" void _mm_pause();
23 #pragma intrinsic(_mm_pause)
24 extern "C" long _InterlockedExchangeAdd( // NOLINT
25 long volatile * Addend
, long Value
); // NOLINT
26 #pragma intrinsic(_InterlockedExchangeAdd)
27 extern "C" short _InterlockedCompareExchange16( // NOLINT
28 short volatile *Destination
, // NOLINT
29 short Exchange
, short Comparand
); // NOLINT
30 #pragma intrinsic(_InterlockedCompareExchange16)
32 long long _InterlockedCompareExchange64( // NOLINT
33 long long volatile *Destination
, // NOLINT
34 long long Exchange
, long long Comparand
); // NOLINT
35 #pragma intrinsic(_InterlockedCompareExchange64)
36 extern "C" void *_InterlockedCompareExchangePointer(
37 void *volatile *Destination
,
38 void *Exchange
, void *Comparand
);
39 #pragma intrinsic(_InterlockedCompareExchangePointer)
41 long __cdecl
_InterlockedCompareExchange( // NOLINT
42 long volatile *Destination
, // NOLINT
43 long Exchange
, long Comparand
); // NOLINT
44 #pragma intrinsic(_InterlockedCompareExchange)
47 extern "C" long long _InterlockedExchangeAdd64( // NOLINT
48 long long volatile * Addend
, long long Value
); // NOLINT
49 #pragma intrinsic(_InterlockedExchangeAdd64)
52 namespace __sanitizer
{
54 INLINE
void atomic_signal_fence(memory_order
) {
58 INLINE
void atomic_thread_fence(memory_order
) {
62 INLINE
void proc_yield(int cnt
) {
63 for (int i
= 0; i
< cnt
; i
++)
68 INLINE typename
T::Type
atomic_load(
69 const volatile T
*a
, memory_order mo
) {
70 DCHECK(mo
& (memory_order_relaxed
| memory_order_consume
71 | memory_order_acquire
| memory_order_seq_cst
));
72 DCHECK(!((uptr
)a
% sizeof(*a
)));
74 // FIXME(dvyukov): 64-bit load is not atomic on 32-bits.
75 if (mo
== memory_order_relaxed
) {
78 atomic_signal_fence(memory_order_seq_cst
);
80 atomic_signal_fence(memory_order_seq_cst
);
86 INLINE
void atomic_store(volatile T
*a
, typename
T::Type v
, memory_order mo
) {
87 DCHECK(mo
& (memory_order_relaxed
| memory_order_release
88 | memory_order_seq_cst
));
89 DCHECK(!((uptr
)a
% sizeof(*a
)));
90 // FIXME(dvyukov): 64-bit store is not atomic on 32-bits.
91 if (mo
== memory_order_relaxed
) {
94 atomic_signal_fence(memory_order_seq_cst
);
96 atomic_signal_fence(memory_order_seq_cst
);
98 if (mo
== memory_order_seq_cst
)
99 atomic_thread_fence(memory_order_seq_cst
);
102 INLINE u32
atomic_fetch_add(volatile atomic_uint32_t
*a
,
103 u32 v
, memory_order mo
) {
105 DCHECK(!((uptr
)a
% sizeof(*a
)));
106 return (u32
)_InterlockedExchangeAdd(
107 (volatile long*)&a
->val_dont_use
, (long)v
); // NOLINT
110 INLINE uptr
atomic_fetch_add(volatile atomic_uintptr_t
*a
,
111 uptr v
, memory_order mo
) {
113 DCHECK(!((uptr
)a
% sizeof(*a
)));
115 return (uptr
)_InterlockedExchangeAdd64(
116 (volatile long long*)&a
->val_dont_use
, (long long)v
); // NOLINT
118 return (uptr
)_InterlockedExchangeAdd(
119 (volatile long*)&a
->val_dont_use
, (long)v
); // NOLINT
123 INLINE u32
atomic_fetch_sub(volatile atomic_uint32_t
*a
,
124 u32 v
, memory_order mo
) {
126 DCHECK(!((uptr
)a
% sizeof(*a
)));
127 return (u32
)_InterlockedExchangeAdd(
128 (volatile long*)&a
->val_dont_use
, -(long)v
); // NOLINT
131 INLINE uptr
atomic_fetch_sub(volatile atomic_uintptr_t
*a
,
132 uptr v
, memory_order mo
) {
134 DCHECK(!((uptr
)a
% sizeof(*a
)));
136 return (uptr
)_InterlockedExchangeAdd64(
137 (volatile long long*)&a
->val_dont_use
, -(long long)v
); // NOLINT
139 return (uptr
)_InterlockedExchangeAdd(
140 (volatile long*)&a
->val_dont_use
, -(long)v
); // NOLINT
144 INLINE u8
atomic_exchange(volatile atomic_uint8_t
*a
,
145 u8 v
, memory_order mo
) {
147 DCHECK(!((uptr
)a
% sizeof(*a
)));
151 xchg
[eax
], cl
// NOLINT
157 INLINE u16
atomic_exchange(volatile atomic_uint16_t
*a
,
158 u16 v
, memory_order mo
) {
160 DCHECK(!((uptr
)a
% sizeof(*a
)));
164 xchg
[eax
], cx
// NOLINT
170 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uint8_t
*a
,
175 DCHECK(!((uptr
)a
% sizeof(*a
)));
182 lock cmpxchg
[ecx
], dl
191 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uintptr_t
*a
,
196 uptr prev
= (uptr
)_InterlockedCompareExchangePointer(
197 (void*volatile*)&a
->val_dont_use
, (void*)xchg
, (void*)cmpv
);
204 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uint16_t
*a
,
209 u16 prev
= (u16
)_InterlockedCompareExchange16(
210 (volatile short*)&a
->val_dont_use
, (short)xchg
, (short)cmpv
);
217 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uint32_t
*a
,
222 u32 prev
= (u32
)_InterlockedCompareExchange(
223 (volatile long*)&a
->val_dont_use
, (long)xchg
, (long)cmpv
);
230 INLINE
bool atomic_compare_exchange_strong(volatile atomic_uint64_t
*a
,
235 u64 prev
= (u64
)_InterlockedCompareExchange64(
236 (volatile long long*)&a
->val_dont_use
, (long long)xchg
, (long long)cmpv
);
244 INLINE
bool atomic_compare_exchange_weak(volatile T
*a
,
245 typename
T::Type
*cmp
,
246 typename
T::Type xchg
,
248 return atomic_compare_exchange_strong(a
, cmp
, xchg
, mo
);
251 } // namespace __sanitizer
253 #endif // SANITIZER_ATOMIC_CLANG_H