1 //===-- sanitizer_atomic_msvc.h ---------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 // Not intended for direct inclusion. Include sanitizer_atomic.h.
12 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_ATOMIC_MSVC_H
15 #define SANITIZER_ATOMIC_MSVC_H
17 extern "C" void _ReadWriteBarrier();
18 #pragma intrinsic(_ReadWriteBarrier)
19 extern "C" void _mm_mfence();
20 #pragma intrinsic(_mm_mfence)
21 extern "C" void _mm_pause();
22 #pragma intrinsic(_mm_pause)
23 extern "C" char _InterlockedExchange8(char volatile *Addend
, char Value
);
24 #pragma intrinsic(_InterlockedExchange8)
25 extern "C" short _InterlockedExchange16(short volatile *Addend
, short Value
);
26 #pragma intrinsic(_InterlockedExchange16)
27 extern "C" long _InterlockedExchange(long volatile *Addend
, long Value
);
28 #pragma intrinsic(_InterlockedExchange)
29 extern "C" long _InterlockedExchangeAdd(long volatile *Addend
, long Value
);
30 #pragma intrinsic(_InterlockedExchangeAdd)
31 extern "C" char _InterlockedCompareExchange8(char volatile *Destination
,
32 char Exchange
, char Comparand
);
33 #pragma intrinsic(_InterlockedCompareExchange8)
34 extern "C" short _InterlockedCompareExchange16(short volatile *Destination
,
35 short Exchange
, short Comparand
);
36 #pragma intrinsic(_InterlockedCompareExchange16)
37 extern "C" long long _InterlockedCompareExchange64(
38 long long volatile *Destination
, long long Exchange
, long long Comparand
);
39 #pragma intrinsic(_InterlockedCompareExchange64)
40 extern "C" void *_InterlockedCompareExchangePointer(
41 void *volatile *Destination
,
42 void *Exchange
, void *Comparand
);
43 #pragma intrinsic(_InterlockedCompareExchangePointer)
44 extern "C" long __cdecl
_InterlockedCompareExchange(long volatile *Destination
,
47 #pragma intrinsic(_InterlockedCompareExchange)
50 extern "C" long long _InterlockedExchangeAdd64(long long volatile *Addend
,
52 #pragma intrinsic(_InterlockedExchangeAdd64)
55 namespace __sanitizer
{
57 inline void atomic_signal_fence(memory_order
) {
61 inline void atomic_thread_fence(memory_order
) {
65 inline void proc_yield(int cnt
) {
66 for (int i
= 0; i
< cnt
; i
++)
71 inline typename
T::Type
atomic_load(
72 const volatile T
*a
, memory_order mo
) {
73 DCHECK(mo
== memory_order_relaxed
|| mo
== memory_order_consume
||
74 mo
== memory_order_acquire
|| mo
== memory_order_seq_cst
);
75 DCHECK(!((uptr
)a
% sizeof(*a
)));
77 // FIXME(dvyukov): 64-bit load is not atomic on 32-bits.
78 if (mo
== memory_order_relaxed
) {
81 atomic_signal_fence(memory_order_seq_cst
);
83 atomic_signal_fence(memory_order_seq_cst
);
89 inline void atomic_store(volatile T
*a
, typename
T::Type v
, memory_order mo
) {
90 DCHECK(mo
== memory_order_relaxed
|| mo
== memory_order_release
||
91 mo
== memory_order_seq_cst
);
92 DCHECK(!((uptr
)a
% sizeof(*a
)));
93 // FIXME(dvyukov): 64-bit store is not atomic on 32-bits.
94 if (mo
== memory_order_relaxed
) {
97 atomic_signal_fence(memory_order_seq_cst
);
99 atomic_signal_fence(memory_order_seq_cst
);
101 if (mo
== memory_order_seq_cst
)
102 atomic_thread_fence(memory_order_seq_cst
);
105 inline u32
atomic_fetch_add(volatile atomic_uint32_t
*a
,
106 u32 v
, memory_order mo
) {
108 DCHECK(!((uptr
)a
% sizeof(*a
)));
109 return (u32
)_InterlockedExchangeAdd((volatile long *)&a
->val_dont_use
,
113 inline uptr
atomic_fetch_add(volatile atomic_uintptr_t
*a
,
114 uptr v
, memory_order mo
) {
116 DCHECK(!((uptr
)a
% sizeof(*a
)));
118 return (uptr
)_InterlockedExchangeAdd64((volatile long long *)&a
->val_dont_use
,
121 return (uptr
)_InterlockedExchangeAdd((volatile long *)&a
->val_dont_use
,
126 inline u32
atomic_fetch_sub(volatile atomic_uint32_t
*a
,
127 u32 v
, memory_order mo
) {
129 DCHECK(!((uptr
)a
% sizeof(*a
)));
130 return (u32
)_InterlockedExchangeAdd((volatile long *)&a
->val_dont_use
,
134 inline uptr
atomic_fetch_sub(volatile atomic_uintptr_t
*a
,
135 uptr v
, memory_order mo
) {
137 DCHECK(!((uptr
)a
% sizeof(*a
)));
139 return (uptr
)_InterlockedExchangeAdd64((volatile long long *)&a
->val_dont_use
,
142 return (uptr
)_InterlockedExchangeAdd((volatile long *)&a
->val_dont_use
,
147 inline u8
atomic_exchange(volatile atomic_uint8_t
*a
,
148 u8 v
, memory_order mo
) {
150 DCHECK(!((uptr
)a
% sizeof(*a
)));
151 return (u8
)_InterlockedExchange8((volatile char*)&a
->val_dont_use
, v
);
154 inline u16
atomic_exchange(volatile atomic_uint16_t
*a
,
155 u16 v
, memory_order mo
) {
157 DCHECK(!((uptr
)a
% sizeof(*a
)));
158 return (u16
)_InterlockedExchange16((volatile short*)&a
->val_dont_use
, v
);
161 inline u32
atomic_exchange(volatile atomic_uint32_t
*a
,
162 u32 v
, memory_order mo
) {
164 DCHECK(!((uptr
)a
% sizeof(*a
)));
165 return (u32
)_InterlockedExchange((volatile long*)&a
->val_dont_use
, v
);
168 inline bool atomic_compare_exchange_strong(volatile atomic_uint8_t
*a
,
173 DCHECK(!((uptr
)a
% sizeof(*a
)));
176 u8 prev
= (u8
)_InterlockedCompareExchange8(
177 (volatile char*)&a
->val_dont_use
, (char)xchgv
, (char)cmpv
);
184 lock cmpxchg
[ecx
], dl
194 inline bool atomic_compare_exchange_strong(volatile atomic_uintptr_t
*a
,
199 uptr prev
= (uptr
)_InterlockedCompareExchangePointer(
200 (void*volatile*)&a
->val_dont_use
, (void*)xchg
, (void*)cmpv
);
207 inline bool atomic_compare_exchange_strong(volatile atomic_uint16_t
*a
,
212 u16 prev
= (u16
)_InterlockedCompareExchange16(
213 (volatile short*)&a
->val_dont_use
, (short)xchg
, (short)cmpv
);
220 inline bool atomic_compare_exchange_strong(volatile atomic_uint32_t
*a
,
225 u32 prev
= (u32
)_InterlockedCompareExchange(
226 (volatile long*)&a
->val_dont_use
, (long)xchg
, (long)cmpv
);
233 inline bool atomic_compare_exchange_strong(volatile atomic_uint64_t
*a
,
238 u64 prev
= (u64
)_InterlockedCompareExchange64(
239 (volatile long long*)&a
->val_dont_use
, (long long)xchg
, (long long)cmpv
);
247 inline bool atomic_compare_exchange_weak(volatile T
*a
,
248 typename
T::Type
*cmp
,
249 typename
T::Type xchg
,
251 return atomic_compare_exchange_strong(a
, cmp
, xchg
, mo
);
254 } // namespace __sanitizer
256 #endif // SANITIZER_ATOMIC_CLANG_H