2012-11-12 16:53:47 +01:00
|
|
|
//===-- sanitizer_atomic_msvc.h ---------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-08-14 10:47:11 +02:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-11-12 16:53:47 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
|
|
|
|
// Not intended for direct inclusion. Include sanitizer_atomic.h.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef SANITIZER_ATOMIC_MSVC_H
|
|
|
|
#define SANITIZER_ATOMIC_MSVC_H
|
|
|
|
|
|
|
|
extern "C" void _ReadWriteBarrier();
|
|
|
|
#pragma intrinsic(_ReadWriteBarrier)
|
|
|
|
extern "C" void _mm_mfence();
|
|
|
|
#pragma intrinsic(_mm_mfence)
|
|
|
|
extern "C" void _mm_pause();
|
|
|
|
#pragma intrinsic(_mm_pause)
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" char _InterlockedExchange8(char volatile *Addend, char Value);
|
2015-10-21 09:32:45 +02:00
|
|
|
#pragma intrinsic(_InterlockedExchange8)
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" short _InterlockedExchange16(short volatile *Addend, short Value);
|
2015-10-21 09:32:45 +02:00
|
|
|
#pragma intrinsic(_InterlockedExchange16)
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" long _InterlockedExchange(long volatile *Addend, long Value);
|
2015-10-21 09:32:45 +02:00
|
|
|
#pragma intrinsic(_InterlockedExchange)
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" long _InterlockedExchangeAdd(long volatile *Addend, long Value);
|
2012-11-12 16:53:47 +01:00
|
|
|
#pragma intrinsic(_InterlockedExchangeAdd)
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" char _InterlockedCompareExchange8(char volatile *Destination,
|
|
|
|
char Exchange, char Comparand);
|
2016-11-08 23:04:09 +01:00
|
|
|
#pragma intrinsic(_InterlockedCompareExchange8)
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" short _InterlockedCompareExchange16(short volatile *Destination,
|
|
|
|
short Exchange, short Comparand);
|
2014-05-22 09:09:21 +02:00
|
|
|
#pragma intrinsic(_InterlockedCompareExchange16)
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" long long _InterlockedCompareExchange64(
|
|
|
|
long long volatile *Destination, long long Exchange, long long Comparand);
|
2014-05-22 09:09:21 +02:00
|
|
|
#pragma intrinsic(_InterlockedCompareExchange64)
|
2013-01-10 13:44:08 +01:00
|
|
|
extern "C" void *_InterlockedCompareExchangePointer(
|
2012-11-12 16:53:47 +01:00
|
|
|
void *volatile *Destination,
|
|
|
|
void *Exchange, void *Comparand);
|
2013-01-10 13:44:08 +01:00
|
|
|
#pragma intrinsic(_InterlockedCompareExchangePointer)
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" long __cdecl _InterlockedCompareExchange(long volatile *Destination,
|
|
|
|
long Exchange,
|
|
|
|
long Comparand);
|
2013-01-10 13:44:08 +01:00
|
|
|
#pragma intrinsic(_InterlockedCompareExchange)
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
#ifdef _WIN64
|
2019-11-05 14:54:57 +01:00
|
|
|
extern "C" long long _InterlockedExchangeAdd64(long long volatile *Addend,
|
|
|
|
long long Value);
|
2014-09-23 19:59:53 +02:00
|
|
|
#pragma intrinsic(_InterlockedExchangeAdd64)
|
2013-01-10 13:44:08 +01:00
|
|
|
#endif
|
2012-11-12 16:53:47 +01:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline void atomic_signal_fence(memory_order) {
|
2012-11-12 16:53:47 +01:00
|
|
|
_ReadWriteBarrier();
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline void atomic_thread_fence(memory_order) {
|
2012-11-12 16:53:47 +01:00
|
|
|
_mm_mfence();
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline void proc_yield(int cnt) {
|
2012-11-12 16:53:47 +01:00
|
|
|
for (int i = 0; i < cnt; i++)
|
|
|
|
_mm_pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-16 10:03:04 +02:00
|
|
|
inline typename T::Type atomic_load(
|
2012-11-12 16:53:47 +01:00
|
|
|
const volatile T *a, memory_order mo) {
|
|
|
|
DCHECK(mo & (memory_order_relaxed | memory_order_consume
|
|
|
|
| memory_order_acquire | memory_order_seq_cst));
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
|
|
|
typename T::Type v;
|
2013-01-23 12:41:33 +01:00
|
|
|
// FIXME(dvyukov): 64-bit load is not atomic on 32-bits.
|
2012-11-12 16:53:47 +01:00
|
|
|
if (mo == memory_order_relaxed) {
|
|
|
|
v = a->val_dont_use;
|
|
|
|
} else {
|
|
|
|
atomic_signal_fence(memory_order_seq_cst);
|
|
|
|
v = a->val_dont_use;
|
|
|
|
atomic_signal_fence(memory_order_seq_cst);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-10-16 10:03:04 +02:00
|
|
|
inline void atomic_store(volatile T *a, typename T::Type v, memory_order mo) {
|
2012-11-12 16:53:47 +01:00
|
|
|
DCHECK(mo & (memory_order_relaxed | memory_order_release
|
|
|
|
| memory_order_seq_cst));
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
2013-01-23 12:41:33 +01:00
|
|
|
// FIXME(dvyukov): 64-bit store is not atomic on 32-bits.
|
2012-11-12 16:53:47 +01:00
|
|
|
if (mo == memory_order_relaxed) {
|
|
|
|
a->val_dont_use = v;
|
|
|
|
} else {
|
|
|
|
atomic_signal_fence(memory_order_seq_cst);
|
|
|
|
a->val_dont_use = v;
|
|
|
|
atomic_signal_fence(memory_order_seq_cst);
|
|
|
|
}
|
|
|
|
if (mo == memory_order_seq_cst)
|
|
|
|
atomic_thread_fence(memory_order_seq_cst);
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline u32 atomic_fetch_add(volatile atomic_uint32_t *a,
|
2012-11-12 16:53:47 +01:00
|
|
|
u32 v, memory_order mo) {
|
|
|
|
(void)mo;
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
2019-11-05 14:54:57 +01:00
|
|
|
return (u32)_InterlockedExchangeAdd((volatile long *)&a->val_dont_use,
|
|
|
|
(long)v);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline uptr atomic_fetch_add(volatile atomic_uintptr_t *a,
|
2014-05-22 09:09:21 +02:00
|
|
|
uptr v, memory_order mo) {
|
|
|
|
(void)mo;
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
|
|
|
#ifdef _WIN64
|
2019-11-05 14:54:57 +01:00
|
|
|
return (uptr)_InterlockedExchangeAdd64((volatile long long *)&a->val_dont_use,
|
|
|
|
(long long)v);
|
2014-05-22 09:09:21 +02:00
|
|
|
#else
|
2019-11-05 14:54:57 +01:00
|
|
|
return (uptr)_InterlockedExchangeAdd((volatile long *)&a->val_dont_use,
|
|
|
|
(long)v);
|
2014-05-22 09:09:21 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline u32 atomic_fetch_sub(volatile atomic_uint32_t *a,
|
2014-05-22 09:09:21 +02:00
|
|
|
u32 v, memory_order mo) {
|
|
|
|
(void)mo;
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
2019-11-05 14:54:57 +01:00
|
|
|
return (u32)_InterlockedExchangeAdd((volatile long *)&a->val_dont_use,
|
|
|
|
-(long)v);
|
2014-05-22 09:09:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline uptr atomic_fetch_sub(volatile atomic_uintptr_t *a,
|
2014-05-22 09:09:21 +02:00
|
|
|
uptr v, memory_order mo) {
|
|
|
|
(void)mo;
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
|
|
|
#ifdef _WIN64
|
2019-11-05 14:54:57 +01:00
|
|
|
return (uptr)_InterlockedExchangeAdd64((volatile long long *)&a->val_dont_use,
|
|
|
|
-(long long)v);
|
2014-05-22 09:09:21 +02:00
|
|
|
#else
|
2019-11-05 14:54:57 +01:00
|
|
|
return (uptr)_InterlockedExchangeAdd((volatile long *)&a->val_dont_use,
|
|
|
|
-(long)v);
|
2014-05-22 09:09:21 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline u8 atomic_exchange(volatile atomic_uint8_t *a,
|
2012-11-12 16:53:47 +01:00
|
|
|
u8 v, memory_order mo) {
|
|
|
|
(void)mo;
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
2015-10-21 09:32:45 +02:00
|
|
|
return (u8)_InterlockedExchange8((volatile char*)&a->val_dont_use, v);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline u16 atomic_exchange(volatile atomic_uint16_t *a,
|
2012-11-12 16:53:47 +01:00
|
|
|
u16 v, memory_order mo) {
|
|
|
|
(void)mo;
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
2015-10-21 09:32:45 +02:00
|
|
|
return (u16)_InterlockedExchange16((volatile short*)&a->val_dont_use, v);
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline u32 atomic_exchange(volatile atomic_uint32_t *a,
|
2015-10-21 09:32:45 +02:00
|
|
|
u32 v, memory_order mo) {
|
|
|
|
(void)mo;
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
|
|
|
return (u32)_InterlockedExchange((volatile long*)&a->val_dont_use, v);
|
2012-11-12 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline bool atomic_compare_exchange_strong(volatile atomic_uint8_t *a,
|
2013-11-04 22:33:31 +01:00
|
|
|
u8 *cmp,
|
|
|
|
u8 xchgv,
|
|
|
|
memory_order mo) {
|
|
|
|
(void)mo;
|
|
|
|
DCHECK(!((uptr)a % sizeof(*a)));
|
|
|
|
u8 cmpv = *cmp;
|
2016-11-08 23:04:09 +01:00
|
|
|
#ifdef _WIN64
|
|
|
|
u8 prev = (u8)_InterlockedCompareExchange8(
|
|
|
|
(volatile char*)&a->val_dont_use, (char)xchgv, (char)cmpv);
|
|
|
|
#else
|
2013-11-04 22:33:31 +01:00
|
|
|
u8 prev;
|
|
|
|
__asm {
|
|
|
|
mov al, cmpv
|
|
|
|
mov ecx, a
|
|
|
|
mov dl, xchgv
|
|
|
|
lock cmpxchg [ecx], dl
|
|
|
|
mov prev, al
|
|
|
|
}
|
2016-11-08 23:04:09 +01:00
|
|
|
#endif
|
2013-11-04 22:33:31 +01:00
|
|
|
if (prev == cmpv)
|
|
|
|
return true;
|
|
|
|
*cmp = prev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline bool atomic_compare_exchange_strong(volatile atomic_uintptr_t *a,
|
2012-11-12 16:53:47 +01:00
|
|
|
uptr *cmp,
|
|
|
|
uptr xchg,
|
|
|
|
memory_order mo) {
|
|
|
|
uptr cmpv = *cmp;
|
2013-01-10 13:44:08 +01:00
|
|
|
uptr prev = (uptr)_InterlockedCompareExchangePointer(
|
2012-11-12 16:53:47 +01:00
|
|
|
(void*volatile*)&a->val_dont_use, (void*)xchg, (void*)cmpv);
|
|
|
|
if (prev == cmpv)
|
|
|
|
return true;
|
|
|
|
*cmp = prev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline bool atomic_compare_exchange_strong(volatile atomic_uint16_t *a,
|
2014-05-22 09:09:21 +02:00
|
|
|
u16 *cmp,
|
|
|
|
u16 xchg,
|
|
|
|
memory_order mo) {
|
|
|
|
u16 cmpv = *cmp;
|
|
|
|
u16 prev = (u16)_InterlockedCompareExchange16(
|
|
|
|
(volatile short*)&a->val_dont_use, (short)xchg, (short)cmpv);
|
|
|
|
if (prev == cmpv)
|
|
|
|
return true;
|
|
|
|
*cmp = prev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline bool atomic_compare_exchange_strong(volatile atomic_uint32_t *a,
|
2014-05-22 09:09:21 +02:00
|
|
|
u32 *cmp,
|
|
|
|
u32 xchg,
|
|
|
|
memory_order mo) {
|
|
|
|
u32 cmpv = *cmp;
|
|
|
|
u32 prev = (u32)_InterlockedCompareExchange(
|
|
|
|
(volatile long*)&a->val_dont_use, (long)xchg, (long)cmpv);
|
|
|
|
if (prev == cmpv)
|
|
|
|
return true;
|
|
|
|
*cmp = prev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 10:03:04 +02:00
|
|
|
inline bool atomic_compare_exchange_strong(volatile atomic_uint64_t *a,
|
2014-05-22 09:09:21 +02:00
|
|
|
u64 *cmp,
|
|
|
|
u64 xchg,
|
|
|
|
memory_order mo) {
|
|
|
|
u64 cmpv = *cmp;
|
|
|
|
u64 prev = (u64)_InterlockedCompareExchange64(
|
|
|
|
(volatile long long*)&a->val_dont_use, (long long)xchg, (long long)cmpv);
|
|
|
|
if (prev == cmpv)
|
|
|
|
return true;
|
|
|
|
*cmp = prev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-12 16:53:47 +01:00
|
|
|
template<typename T>
|
2020-10-16 10:03:04 +02:00
|
|
|
inline bool atomic_compare_exchange_weak(volatile T *a,
|
2013-11-04 22:33:31 +01:00
|
|
|
typename T::Type *cmp,
|
|
|
|
typename T::Type xchg,
|
|
|
|
memory_order mo) {
|
2012-11-12 16:53:47 +01:00
|
|
|
return atomic_compare_exchange_strong(a, cmp, xchg, mo);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_ATOMIC_CLANG_H
|