Skip to content

Commit

Permalink
spinlock: Favor gcc-style atomics over MSVC interfaces.
Browse files Browse the repository at this point in the history
This resolves a problem when using Clang on Windows.

Fixes #4346.
  • Loading branch information
icculus committed Jul 27, 2021
1 parent 9bcb5e7 commit f4eb7f3
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/atomic/SDL_spinlock.c
Expand Up @@ -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);

Expand All @@ -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__) || \
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit f4eb7f3

Please sign in to comment.