Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Added native atomic operations for Windows, Mac OS X, and gcc compile…
…r intrinsics.

Changed the CAS return value to bool, so it's efficient with OSAtomicCompareAndSwap32Barrier()
Added an atomic test adapted from code by Michael Davidsaver
  • Loading branch information
slouken committed Jan 16, 2011
1 parent c4a7a39 commit 4e72e7d79730ea4e137d60e0307b5ba109c3d4a0
@@ -3985,7 +3985,14 @@
isa = PBXProject;
buildConfigurationList = 001B5A0C08BDB826006539E9 /* Build configuration list for PBXProject "SDLTest" */;
compatibilityVersion = "Xcode 3.0";
developmentRegion = English;
hasScannedForEncodings = 1;
knownRegions = (
English,
Japanese,
French,
German,
);
mainGroup = 08FB7794FE84155DC02AAC07 /* SDLTest */;
projectDirPath = "";
projectReferences = (
@@ -181,7 +181,7 @@ if test x$enable_dependency_tracking = xyes; then
DEPENDENCY_TRACKING_OPTIONS="-MMD -MT \$@"
fi
fi

dnl See whether we are allowed to use the system C library
AC_ARG_ENABLE(libc,
AC_HELP_STRING([--enable-libc], [Use the system C library [[default=yes]]]),
@@ -285,6 +285,32 @@ if test x$have_inttypes != xyes; then
AC_DEFINE(uintptr_t, unsigned long)
fi

dnl See whether we can use gcc atomic operations on this architecture
AC_ARG_ENABLE(gcc-atomics,
AC_HELP_STRING([--enable-gcc-atomics],
[Use gcc builtin atomics [[default=yes]]]),
, enable_gcc_atomics=yes)
if test x$enable_gcc_atomics = xyes; then
have_gcc_atomics=no
AC_MSG_CHECKING(for GCC builtin atomic operations)
AC_TRY_LINK([
],[
int a;
void *x, *y, *z;
__sync_lock_test_and_set(&a, 4);
__sync_fetch_and_add(&a, 1);
__sync_bool_compare_and_swap(&a, 5, 10);
__sync_bool_compare_and_swap(&x, y, z);
],[
have_gcc_atomics=yes
])
AC_MSG_RESULT($have_gcc_atomics)

if test x$have_gcc_mmd_mt = xyes; then
AC_DEFINE(HAVE_GCC_ATOMICS)
fi
fi

# Standard C sources
SOURCES="$SOURCES $srcdir/src/*.c"
SOURCES="$SOURCES $srcdir/src/audio/*.c"
@@ -108,7 +108,62 @@ extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
/*@}*//*SDL AtomicLock*/

/* Platform specific optimized versions of the atomic functions */
/* None yet... */
#if defined(__WIN32__)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define SDL_AtomicSet(a, v) InterlockedExchange(&(a)->value, v)
#define SDL_AtomicGet(a) ((a)->value)
#define SDL_AtomicAdd(a, v) InterlockedAdd(&(a)->value, v)
#define SDL_AtomicCAS(a, oldval, newval) (InterlockedCompareExchange(&(a)->value, newval, oldval) == (oldval))
#define SDL_AtomicSetPtr(a, v) InterlockedExchangePointer(a, v)
#define SDL_AtomicGetPtr(a) (*(a))
#define SDL_AtomicCASPtr(a, oldval, newval) (InterlockedCompareExchangePointer(a, newval, oldval) == (oldval))

#elif defined(__MACOSX__)
#include <libkern/OSAtomic.h>

#define SDL_AtomicSet(a, v) \
({ \
int oldvalue; \
\
do { \
oldvalue = (a)->value; \
} while (!OSAtomicCompareAndSwap32Barrier(oldvalue, v, &(a)->value)); \
\
oldvalue; \
})
#define SDL_AtomicGet(a) ((a)->value)
#define SDL_AtomicAdd(a, v) \
({ \
int oldvalue; \
\
do { \
oldvalue = (a)->value; \
} while (!OSAtomicCompareAndSwap32Barrier(oldvalue, oldvalue+v, &(a)->value)); \
\
oldvalue; \
})
#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier(oldval, newval, &(a)->value)
#define SDL_AtomicSetPtr(a, v) (*(a) = v, OSMemoryBarrier())
#define SDL_AtomicGetPtr(a) (*(a))
#if SIZEOF_VOIDP == 4
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
#elif SIZEOF_VOIDP == 8
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
#endif

#elif defined(HAVE_GCC_ATOMICS)

#define SDL_AtomicSet(a, v) __sync_lock_test_and_set(&(a)->value, v)
#define SDL_AtomicGet(a) ((a)->value)
#define SDL_AtomicAdd(a, v) __sync_fetch_and_add(&(a)->value, v)
#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
#define SDL_AtomicSetPtr(a, v) (*(a) = v, __sync_synchronize())
#define SDL_AtomicGetPtr(a) (*(a))
#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)

#endif

/**
* \brief A type representing an atomic integer value. It is a struct
@@ -163,12 +218,12 @@ extern DECLSPEC SDL_bool SDLCALL SDL_AtomicDecRef(SDL_atomic_t *a);
/**
* \brief Set an atomic variable to a new value if it is currently an old value.
*
* \return The previous value of the atomic variable
* \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
*
* \note If you don't know what this function is for, you shouldn't use it!
*/
#ifndef SDL_AtomicCAS
extern DECLSPEC int SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
#endif

/**
@@ -188,12 +243,12 @@ extern DECLSPEC void* SDLCALL SDL_AtomicGetPtr(void** a);
/**
* \brief Set a pointer to a new value if it is currently an old value.
*
* \return The previous value of the pointer
* \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
*
* \note If you don't know what this function is for, you shouldn't use it!
*/
#ifndef SDL_AtomicCASPtr
extern DECLSPEC void* SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
#endif

/* Ends C function definitions when using C++ */
@@ -58,6 +58,7 @@

#undef SIZEOF_VOIDP
#undef SDL_HAS_64BIT_TYPE
#undef HAVE_GCC_ATOMICS

/* Comment this if you want to build without any C library requirements */
#undef HAVE_LIBC
@@ -43,6 +43,8 @@ typedef unsigned long uintptr_t;

#define SDL_HAS_64BIT_TYPE 1

#define HAVE_GCC_ATOMICS 1

#define HAVE_ALLOCA_H 1
#define HAVE_SYS_TYPES_H 1
#define HAVE_STDIO_H 1
@@ -23,6 +23,12 @@

#include "SDL_atomic.h"

/* Note that we undefine the atomic operations here, in case they are
defined as compiler intrinsics while building SDL but the library user
doesn't have that compiler. That way we always have a working set of
atomic operations built into the library.
*/

/*
If any of the operations are not provided then we must emulate some
of them. That means we need a nice implementation of spin locks
@@ -51,20 +57,20 @@ static SDL_SpinLock locks[32];
static __inline__ void
enterLock(void *a)
{
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);

SDL_AtomicLock(&locks[index]);
SDL_AtomicLock(&locks[index]);
}

static __inline__ void
leaveLock(void *a)
{
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);

SDL_AtomicUnlock(&locks[index]);
SDL_AtomicUnlock(&locks[index]);
}

#ifndef SDL_AtomicSet
#undef SDL_AtomicSet
int
SDL_AtomicSet(SDL_atomic_t *a, int value)
{
@@ -77,9 +83,8 @@ SDL_AtomicSet(SDL_atomic_t *a, int value)

return oldvalue;
}
#endif

#ifndef SDL_AtomicGet
#undef SDL_AtomicGet
int
SDL_AtomicGet(SDL_atomic_t *a)
{
@@ -88,9 +93,8 @@ SDL_AtomicGet(SDL_atomic_t *a)
*/
return a->value;
}
#endif

#ifndef SDL_AtomicAdd
#undef SDL_AtomicAdd
int
SDL_AtomicAdd(SDL_atomic_t *a, int value)
{
@@ -103,53 +107,48 @@ SDL_AtomicAdd(SDL_atomic_t *a, int value)

return oldvalue;
}
#endif

#ifndef SDL_AtomicIncRef
#undef SDL_AtomicIncRef
void
SDL_AtomicIncRef(SDL_atomic_t *a)
{
SDL_AtomicAdd(a, 1);
}
#endif

#ifndef SDL_AtomicDecRef
#undef SDL_AtomicDecRef
SDL_bool
SDL_AtomicDecRef(SDL_atomic_t *a)
{
return SDL_AtomicAdd(a, -1) == 1;
}
#endif

#ifndef SDL_AtomicCAS
int
#undef SDL_AtomicCAS
SDL_bool
SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
{
int prevval;
SDL_bool retval = SDL_FALSE;

enterLock(a);
prevval = a->value;
if (prevval == oldval) {
if (a->value == oldval) {
a->value = newval;
retval = SDL_TRUE;
}
leaveLock(a);

return prevval;
return retval;
}
#endif

#ifndef SDL_AtomicSetPtr
#undef SDL_AtomicSetPtr
void
SDL_AtomicSetPtr(void** a, void* value)
{
void *prevval;
do {
prevval = *a;
} while (SDL_AtomicCASPtr(a, prevval, value) != prevval);
} while (!SDL_AtomicCASPtr(a, prevval, value));
}
#endif

#ifndef SDL_AtomicGetPtr
#undef SDL_AtomicGetPtr
void*
SDL_AtomicGetPtr(void** a)
{
@@ -158,22 +157,20 @@ SDL_AtomicGetPtr(void** a)
*/
return *a;
}
#endif

#ifndef SDL_AtomicCASPtr
void* SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
#undef SDL_AtomicCASPtr
SDL_bool SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
{
void *prevval;
SDL_bool retval = SDL_FALSE;

enterLock(a);
prevval = *a;
if (*a == oldval) {
*a = newval;
retval = SDL_TRUE;
}
leaveLock(a);

return prevval;
return retval;
}
#endif

/* vi: set ts=4 sw=4 expandtab: */
@@ -44,24 +44,22 @@ SDL_AtomicTryLock(SDL_SpinLock *lock)
#elif defined(__MACOSX__)
return OSAtomicCompareAndSwap32Barrier(0, 1, lock);

#elif defined(__GNUC__)
#if defined(__arm__)
#ifdef __ARM_ARCH_5__
#elif defined(HAVE_GCC_ATOMICS)
return (__sync_lock_test_and_set(lock, 1) == 0);

#elif defined(__GNUC__) && defined(__arm__) && defined(__ARM_ARCH_5__)
int result;
__asm__ __volatile__ (
"swp %0, %1, [%2]\n"
: "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
return (result == 0);
#else

#elif defined(__GNUC__) && defined(__arm__)
int result;
__asm__ __volatile__ (
"ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
: "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
return (result == 0);
#endif
#else
return (__sync_lock_test_and_set(lock, 1) == 0);
#endif

#else
/* Need CPU instructions for spinlock here! */
@@ -81,19 +79,8 @@ SDL_AtomicLock(SDL_SpinLock *lock)
void
SDL_AtomicUnlock(SDL_SpinLock *lock)
{
#if defined(__WIN32__)
/* Assuming atomic assignment operation and full memory barrier in lock */
*lock = 0;

#elif defined(__MACOSX__)
*lock = 0;

#elif defined(__GNUC__) && !defined(__arm__)
__sync_lock_release(lock);

#else
/* Assuming memory barrier in lock and integral assignment operation */
*lock = 0;
#endif
}

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 4e72e7d

Please sign in to comment.