From 7c0c1e80ceb5c555003341ec0ced80d3ef9b990a Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 7 Feb 2011 22:57:33 -0800 Subject: [PATCH] It's now possible to disable the fast atomic operations, at a huge performance penalty. --- configure.in | 6 ++++++ include/SDL_atomic.h | 3 +++ include/SDL_config.h.in | 1 + src/atomic/SDL_spinlock.c | 21 ++++++++++++++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/configure.in b/configure.in index f0d3ac58e..252f15f46 100644 --- a/configure.in +++ b/configure.in @@ -422,6 +422,12 @@ AC_HELP_STRING([--enable-cpuinfo], [Enable the cpuinfo subsystem [[default=yes]] if test x$enable_cpuinfo != xyes; then AC_DEFINE(SDL_CPUINFO_DISABLED) fi +AC_ARG_ENABLE(atomic, +AC_HELP_STRING([--enable-atomic], [Enable the atomic operations [[default=yes]]]), + , enable_atomic=yes) +if test x$enable_atomic != xyes; then + AC_DEFINE(SDL_ATOMIC_DISABLED) +fi AC_ARG_ENABLE(assembly, AC_HELP_STRING([--enable-assembly], [Enable assembly routines [[default=yes]]]), , enable_assembly=yes) diff --git a/include/SDL_atomic.h b/include/SDL_atomic.h index e7af7269a..236ee861e 100644 --- a/include/SDL_atomic.h +++ b/include/SDL_atomic.h @@ -141,6 +141,9 @@ void _ReadWriteBarrier(void); /* Platform specific optimized versions of the atomic functions, * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE */ +#if SDL_ATOMIC_DISABLED +#define SDL_DISABLE_ATOMIC_INLINE +#endif #ifndef SDL_DISABLE_ATOMIC_INLINE #ifdef HAVE_MSC_ATOMICS diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in index 70b540fc2..551ceb6ad 100644 --- a/include/SDL_config.h.in +++ b/include/SDL_config.h.in @@ -171,6 +171,7 @@ #undef SDL_DEFAULT_ASSERT_LEVEL /* Allow disabling of core subsystems */ +#undef SDL_ATOMIC_DISABLED #undef SDL_AUDIO_DISABLED #undef SDL_CPUINFO_DISABLED #undef SDL_EVENTS_DISABLED diff --git a/src/atomic/SDL_spinlock.c b/src/atomic/SDL_spinlock.c index 83ba93727..9eb03b319 100644 --- a/src/atomic/SDL_spinlock.c +++ b/src/atomic/SDL_spinlock.c @@ -22,6 +22,7 @@ #include "SDL_stdinc.h" #include "SDL_atomic.h" +#include "SDL_mutex.h" #include "SDL_timer.h" /* Don't do the check for Visual Studio 2005, it's safe here */ @@ -33,7 +34,25 @@ SDL_bool SDL_AtomicTryLock(SDL_SpinLock *lock) { -#if defined(_MSC_VER) +#if SDL_ATOMIC_DISABLED + /* Terrible terrible damage */ + static SDL_mutex *_spinlock_mutex; + + if (!_spinlock_mutex) { + /* Race condition on first lock... */ + _spinlock_mutex = SDL_CreateMutex(); + } + SDL_mutexP(_spinlock_mutex); + if (*lock == 0) { + *lock = 1; + SDL_mutexV(_spinlock_mutex); + return SDL_TRUE; + } else { + SDL_mutexV(_spinlock_mutex); + return SDL_FALSE; + } + +#elif defined(_MSC_VER) SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long)); return (InterlockedExchange((long*)lock, 1) == 0);