2011-01-15 12:41:59 -08:00
|
|
|
/*
|
|
|
|
SDL - Simple DirectMedia Layer
|
2011-02-11 22:37:15 -08:00
|
|
|
Copyright (C) 1997-2011 Sam Lantinga
|
2011-01-15 12:41:59 -08:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
Sam Lantinga
|
|
|
|
slouken@libsdl.org
|
|
|
|
*/
|
|
|
|
#include "SDL_stdinc.h"
|
|
|
|
|
|
|
|
#include "SDL_atomic.h"
|
2011-02-07 22:57:33 -08:00
|
|
|
#include "SDL_mutex.h"
|
2011-01-15 12:41:59 -08:00
|
|
|
#include "SDL_timer.h"
|
|
|
|
|
2011-01-21 23:46:51 -08:00
|
|
|
/* Don't do the check for Visual Studio 2005, it's safe here */
|
2011-01-24 21:22:00 -08:00
|
|
|
#ifdef __WIN32__
|
2011-01-24 21:20:30 -08:00
|
|
|
#include "../core/windows/SDL_windows.h"
|
2011-01-24 21:22:00 -08:00
|
|
|
#endif
|
2011-01-15 12:41:59 -08:00
|
|
|
|
|
|
|
/* This function is where all the magic happens... */
|
|
|
|
SDL_bool
|
|
|
|
SDL_AtomicTryLock(SDL_SpinLock *lock)
|
|
|
|
{
|
2011-02-07 22:57:33 -08:00
|
|
|
#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)
|
2011-01-17 14:05:43 -08:00
|
|
|
SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
|
2011-01-24 21:20:30 -08:00
|
|
|
return (InterlockedExchange((long*)lock, 1) == 0);
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-01-25 18:02:41 -08:00
|
|
|
#elif defined(__MACOSX__)
|
2011-01-15 12:41:59 -08:00
|
|
|
return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
|
|
|
|
|
2011-01-25 17:40:06 -08:00
|
|
|
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
|
2011-01-16 15:16:39 -08:00
|
|
|
return (__sync_lock_test_and_set(lock, 1) == 0);
|
|
|
|
|
2011-02-16 17:17:21 -08:00
|
|
|
#elif defined(__GNUC__) && defined(__arm__) && \
|
|
|
|
(defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
|
|
|
|
defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__))
|
2011-01-15 12:41:59 -08:00
|
|
|
int result;
|
|
|
|
__asm__ __volatile__ (
|
|
|
|
"swp %0, %1, [%2]\n"
|
|
|
|
: "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
|
|
|
|
return (result == 0);
|
2011-01-16 15:16:39 -08:00
|
|
|
|
2011-01-25 18:02:41 -08:00
|
|
|
#elif defined(__GNUC__) && defined(__arm__)
|
2011-01-15 12:41:59 -08:00
|
|
|
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);
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Need CPU instructions for spinlock here! */
|
|
|
|
__need_spinlock_implementation__
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SDL_AtomicLock(SDL_SpinLock *lock)
|
|
|
|
{
|
|
|
|
/* FIXME: Should we have an eventual timeout? */
|
|
|
|
while (!SDL_AtomicTryLock(lock)) {
|
|
|
|
SDL_Delay(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SDL_AtomicUnlock(SDL_SpinLock *lock)
|
|
|
|
{
|
2011-01-25 17:40:06 -08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
_ReadWriteBarrier();
|
|
|
|
*lock = 0;
|
|
|
|
|
|
|
|
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
|
|
|
|
__sync_lock_release(lock);
|
|
|
|
|
|
|
|
#else
|
2011-01-15 12:41:59 -08:00
|
|
|
*lock = 0;
|
2011-01-25 17:40:06 -08:00
|
|
|
#endif
|
2011-01-15 12:41:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|