Commit f4eb7f329e4adfe7307c983ebde4c8568fba79fe

Ryan C. Gordon 2021-07-27T13:45:33

spinlock: Favor gcc-style atomics over MSVC interfaces. This resolves a problem when using Clang on Windows. Fixes #4346.

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;