2011-01-15 12:41:59 -08:00
|
|
|
/*
|
2011-04-08 13:03:26 -07:00
|
|
|
Simple DirectMedia Layer
|
2018-01-03 10:03:25 -08:00
|
|
|
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
2011-01-15 12:41:59 -08:00
|
|
|
*/
|
2013-11-24 23:56:17 -05:00
|
|
|
#include "../SDL_internal.h"
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2014-03-09 11:06:11 -07:00
|
|
|
#if defined(__WIN32__) || defined(__WINRT__)
|
2013-10-17 23:02:29 -07:00
|
|
|
#include "../core/windows/SDL_windows.h"
|
|
|
|
#endif
|
|
|
|
|
2011-01-15 12:41:59 -08:00
|
|
|
#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"
|
|
|
|
|
2014-07-05 16:11:23 -07:00
|
|
|
#if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
|
|
|
|
#include <atomic.h>
|
|
|
|
#endif
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2018-06-25 15:58:35 -04:00
|
|
|
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
|
|
|
|
#include <xmmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2017-08-18 16:35:55 -04:00
|
|
|
#if defined(__WATCOMC__) && defined(__386__)
|
|
|
|
SDL_COMPILE_TIME_ASSERT(locksize, 4==sizeof(SDL_SpinLock));
|
|
|
|
extern _inline int _SDL_xchg_watcom(volatile int *a, int v);
|
|
|
|
#pragma aux _SDL_xchg_watcom = \
|
|
|
|
"xchg [ecx], eax" \
|
|
|
|
parm [ecx] [eax] \
|
|
|
|
value [eax] \
|
|
|
|
modify exact [eax];
|
|
|
|
#endif /* __WATCOMC__ && __386__ */
|
|
|
|
|
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();
|
|
|
|
}
|
2013-03-07 20:12:40 -08:00
|
|
|
SDL_LockMutex(_spinlock_mutex);
|
2011-02-07 22:57:33 -08:00
|
|
|
if (*lock == 0) {
|
|
|
|
*lock = 1;
|
2013-03-07 20:12:40 -08:00
|
|
|
SDL_UnlockMutex(_spinlock_mutex);
|
2011-02-07 22:57:33 -08:00
|
|
|
return SDL_TRUE;
|
|
|
|
} else {
|
2013-03-07 20:12:40 -08:00
|
|
|
SDL_UnlockMutex(_spinlock_mutex);
|
2011-02-07 22:57:33 -08:00
|
|
|
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
|
|
|
|
2017-08-18 16:35:55 -04:00
|
|
|
#elif defined(__WATCOMC__) && defined(__386__)
|
|
|
|
return _SDL_xchg_watcom(lock, 1) == 0;
|
|
|
|
|
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__) || \
|
2011-10-16 03:27:48 -04:00
|
|
|
defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
|
|
|
|
defined(__ARM_ARCH_5TEJ__))
|
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);
|
|
|
|
|
2011-09-18 03:19:41 -04:00
|
|
|
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
2013-05-18 14:17:52 -07:00
|
|
|
int result;
|
|
|
|
__asm__ __volatile__(
|
2011-09-18 03:19:41 -04:00
|
|
|
"lock ; xchgl %0, (%1)\n"
|
|
|
|
: "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
|
2013-05-18 14:17:52 -07:00
|
|
|
return (result == 0);
|
2011-09-18 03:19:41 -04:00
|
|
|
|
2011-10-16 03:02:46 -04:00
|
|
|
#elif defined(__MACOSX__) || defined(__IPHONEOS__)
|
2011-09-18 03:19:41 -04:00
|
|
|
/* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
|
|
|
|
return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
|
|
|
|
|
2014-07-05 16:11:23 -07:00
|
|
|
#elif defined(__SOLARIS__) && defined(_LP64)
|
|
|
|
/* Used for Solaris with non-gcc compilers. */
|
|
|
|
return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
|
|
|
|
|
|
|
|
#elif defined(__SOLARIS__) && !defined(_LP64)
|
|
|
|
/* Used for Solaris with non-gcc compilers. */
|
|
|
|
return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0);
|
|
|
|
|
2011-09-18 03:19:41 -04:00
|
|
|
#else
|
2011-09-18 02:55:45 -04:00
|
|
|
#error Please implement for your platform.
|
|
|
|
return SDL_FALSE;
|
2011-01-15 12:41:59 -08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:58:35 -04:00
|
|
|
/* "REP NOP" is PAUSE, coded for tools that don't know it by that name. */
|
|
|
|
#if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
|
2018-06-29 16:55:55 -04:00
|
|
|
#define PAUSE_INSTRUCTION() __asm__ __volatile__("pause\n") /* Some assemblers can't do REP NOP, so go with PAUSE. */
|
2018-06-25 15:58:35 -04:00
|
|
|
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
|
2018-06-25 23:00:38 -04:00
|
|
|
#define PAUSE_INSTRUCTION() _mm_pause() /* this is actually "rep nop" and not a SIMD instruction. No inline asm in MSVC x86-64! */
|
2018-06-25 15:58:35 -04:00
|
|
|
#elif defined(__WATCOMC__) && defined(__386__)
|
2018-06-30 20:04:01 +03:00
|
|
|
/* watcom assembler rejects PAUSE if cpu <= i686, and it refuses REP NOP as an invalid combination. Hardcode the bytes. */
|
2018-06-25 15:58:35 -04:00
|
|
|
extern _inline void PAUSE_INSTRUCTION(void);
|
2018-06-25 16:34:16 -04:00
|
|
|
#pragma aux PAUSE_INSTRUCTION = "db 0f3h,90h"
|
2018-06-25 15:58:35 -04:00
|
|
|
#else
|
|
|
|
#define PAUSE_INSTRUCTION()
|
|
|
|
#endif
|
|
|
|
|
2011-01-15 12:41:59 -08:00
|
|
|
void
|
|
|
|
SDL_AtomicLock(SDL_SpinLock *lock)
|
|
|
|
{
|
2018-06-25 15:58:35 -04:00
|
|
|
int iterations = 0;
|
2011-01-15 12:41:59 -08:00
|
|
|
/* FIXME: Should we have an eventual timeout? */
|
|
|
|
while (!SDL_AtomicTryLock(lock)) {
|
2018-06-25 15:58:35 -04:00
|
|
|
if (iterations < 32) {
|
|
|
|
iterations++;
|
|
|
|
PAUSE_INSTRUCTION();
|
|
|
|
} else {
|
|
|
|
/* !!! FIXME: this doesn't definitely give up the current timeslice, it does different things on various platforms. */
|
|
|
|
SDL_Delay(0);
|
|
|
|
}
|
2011-01-15 12:41:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SDL_AtomicUnlock(SDL_SpinLock *lock)
|
|
|
|
{
|
2011-01-25 17:40:06 -08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
_ReadWriteBarrier();
|
|
|
|
*lock = 0;
|
|
|
|
|
2017-08-18 16:35:55 -04:00
|
|
|
#elif defined(__WATCOMC__) && defined(__386__)
|
|
|
|
SDL_CompilerBarrier ();
|
|
|
|
*lock = 0;
|
|
|
|
|
2011-01-25 17:40:06 -08:00
|
|
|
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
|
|
|
|
__sync_lock_release(lock);
|
2013-05-18 14:17:52 -07:00
|
|
|
|
2014-07-05 16:11:23 -07:00
|
|
|
#elif defined(__SOLARIS__)
|
|
|
|
/* Used for Solaris when not using gcc. */
|
|
|
|
*lock = 0;
|
|
|
|
membar_producer();
|
|
|
|
|
2011-01-25 17:40:06 -08:00
|
|
|
#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: */
|