spinlock: Favor gcc-style atomics over MSVC interfaces. This resolves a problem when using Clang on Windows. Fixes #4346.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
diff --git a/src/atomic/SDL_spinlock.c b/src/atomic/SDL_spinlock.c
index a58a073..63b8bd9 100644
--- a/src/atomic/SDL_spinlock.c
+++ b/src/atomic/SDL_spinlock.c
@@ -72,6 +72,9 @@ SDL_AtomicTryLock(SDL_SpinLock *lock)
return SDL_FALSE;
}
+#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
+ return (__sync_lock_test_and_set(lock, 1) == 0);
+
#elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
return (_InterlockedExchange_acq(lock, 1) == 0);
@@ -82,9 +85,6 @@ SDL_AtomicTryLock(SDL_SpinLock *lock)
#elif defined(__WATCOMC__) && defined(__386__)
return _SDL_xchg_watcom(lock, 1) == 0;
-#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
- return (__sync_lock_test_and_set(lock, 1) == 0);
-
#elif defined(__GNUC__) && defined(__arm__) && \
(defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__) || \
defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
@@ -176,8 +176,12 @@ SDL_AtomicLock(SDL_SpinLock *lock)
void
SDL_AtomicUnlock(SDL_SpinLock *lock)
{
-#if defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
+#if HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
+ __sync_lock_release(lock);
+
+#elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
_InterlockedExchange_rel(lock, 0);
+
#elif defined(_MSC_VER)
_ReadWriteBarrier();
*lock = 0;
@@ -186,9 +190,6 @@ SDL_AtomicUnlock(SDL_SpinLock *lock)
SDL_CompilerBarrier ();
*lock = 0;
-#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
- __sync_lock_release(lock);
-
#elif defined(__SOLARIS__)
/* Used for Solaris when not using gcc. */
*lock = 0;