diff --git a/src/mono/System.Private.CoreLib/src/System/Threading/Interlocked.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Threading/Interlocked.Mono.cs index b5282beaa785d9..6b187ac8d967fd 100644 --- a/src/mono/System.Private.CoreLib/src/System/Threading/Interlocked.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Threading/Interlocked.Mono.cs @@ -76,11 +76,8 @@ public static partial class Interlocked [Intrinsic] public static T CompareExchange(ref T location1, T value, T comparand) where T : class? { - unsafe - { - if (Unsafe.AsPointer(ref location1) == null) - throw new NullReferenceException(); - } + if (Unsafe.IsNullRef(ref location1)) + throw new NullReferenceException(); // Besides avoiding coop handles for efficiency, // and correctness, this also appears needed to // avoid an assertion failure in the runtime, related to @@ -104,11 +101,8 @@ public static T CompareExchange(ref T location1, T value, T comparand) where [Intrinsic] public static T Exchange([NotNullIfNotNull(nameof(value))] ref T location1, T value) where T : class? { - unsafe - { - if (Unsafe.AsPointer(ref location1) == null) - throw new NullReferenceException(); - } + if (Unsafe.IsNullRef(ref location1)) + throw new NullReferenceException(); // See CompareExchange(T) for comments. // // This is not entirely convincing due to lack of volatile. diff --git a/src/mono/mono/utils/atomic.h b/src/mono/mono/utils/atomic.h index 5d276fa7612523..5c63d00aee5f0e 100644 --- a/src/mono/mono/utils/atomic.h +++ b/src/mono/mono/utils/atomic.h @@ -51,7 +51,7 @@ Apple targets have historically being problematic, xcode 4.6 would miscompile th * libclang sees the platform header, not the clang one. */ # define MONO_USE_EMULATED_ATOMIC 1 -#elif defined(_MSC_VER) || defined(HOST_WIN32) +#elif defined(HOST_WIN32) /* * we need two things to switch to C11 atomics on Windows: * @@ -61,7 +61,11 @@ Apple targets have historically being problematic, xcode 4.6 would miscompile th * stdatomic.h) * */ -# define MONO_USE_WIN32_ATOMIC 1 +# if defined(_MSC_VER) +# define MONO_USE_WIN32_ATOMIC 1 +# else +# error FIXME: Implement atomics for MinGW and/or clang +# endif #elif defined(HOST_IOS) || defined(HOST_OSX) || defined(HOST_WATCHOS) || defined(HOST_TVOS) # define MONO_USE_C11_ATOMIC 1 #elif defined(HOST_ANDROID) @@ -89,7 +93,7 @@ Apple targets have historically being problematic, xcode 4.6 would miscompile th #if defined(MONO_USE_C11_ATOMIC) -#include +#include static inline gint32 mono_atomic_cas_i32 (volatile gint32 *dest, gint32 exch, gint32 comp) @@ -305,11 +309,12 @@ mono_atomic_store_ptr (volatile gpointer *dst, gpointer val) #define WIN32_LEAN_AND_MEAN #endif #include +#include static inline gint32 mono_atomic_cas_i32 (volatile gint32 *dest, gint32 exch, gint32 comp) { - return InterlockedCompareExchange ((LONG volatile *)dest, (LONG)exch, (LONG)comp); + return _InterlockedCompareExchange ((LONG volatile *)dest, (LONG)exch, (LONG)comp); } static inline gint64 @@ -363,7 +368,7 @@ mono_atomic_dec_i64 (volatile gint64 *dest) static inline gint32 mono_atomic_xchg_i32 (volatile gint32 *dest, gint32 exch) { - return InterlockedExchange ((LONG volatile *)dest, (LONG)exch); + return _InterlockedExchange ((LONG volatile *)dest, (LONG)exch); } static inline gint64 @@ -387,7 +392,7 @@ mono_atomic_fetch_add_i32 (volatile gint32 *dest, gint32 add) static inline gint64 mono_atomic_fetch_add_i64 (volatile gint64 *dest, gint64 add) { - return InterlockedExchangeAdd64 ((LONG64 volatile *)dest, (LONG)add); + return InterlockedExchangeAdd64 ((LONG64 volatile *)dest, (LONG64)add); } static inline gint8 @@ -441,29 +446,19 @@ mono_atomic_load_ptr (volatile gpointer *src) static inline void mono_atomic_store_i8 (volatile gint8 *dst, gint8 val) { -#if (_MSC_VER >= 1600) _InterlockedExchange8 ((CHAR volatile *)dst, (CHAR)val); -#else - *dst = val; - mono_memory_barrier (); -#endif } static inline void mono_atomic_store_i16 (volatile gint16 *dst, gint16 val) { -#if (_MSC_VER >= 1600) - InterlockedExchange16 ((SHORT volatile *)dst, (SHORT)val); -#else - *dst = val; - mono_memory_barrier (); -#endif + _InterlockedExchange16 ((SHORT volatile *)dst, (SHORT)val); } static inline void mono_atomic_store_i32 (volatile gint32 *dst, gint32 val) { - InterlockedExchange ((LONG volatile *)dst, (LONG)val); + _InterlockedExchange ((LONG volatile *)dst, (LONG)val); } static inline void