mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-09-28 22:18:28 +00:00

binarycrusader Since changeset 358696c354a8, SDL 2.0 has been broken on Solaris when compiling with the Solaris Studio compiler (which uses the pthread implementation of SDL_AtomicLock). Notably, it gets stuck at the MemoryBarrierRelease in SDL_GetErrBuf: 6585 # 218 6586 if (!tls_errbuf && !tls_being_created) { 6587 SDL_AtomicLock_REAL ( & tls_lock ); 6588 if (!tls_errbuf) { 6589 SDL_TLSID slot; 6590 tls_being_created = SDL_TRUE; 6591 slot = SDL_TLSCreate_REAL ( ); 6592 tls_being_created = SDL_FALSE; 6593 { SDL_SpinLock _tmp = 0 ; SDL_AtomicLock_REAL ( & _tmp ) ; SDL_AtomicUnlock_REAL ( & _tmp ) ; }; ^^^ loops forever above 6594 tls_errbuf = slot; 6595 } 6596 SDL_AtomicUnlock_REAL ( & tls_lock ); 6597 } Running: testthread (process id 28926) ^Cdbx: warning: Interrupt ignored but forwarded to child. signal INT (Interrupt) in __nanosleep at 0xfe52a875 0xfe52a875: __nanosleep+0x0015: jae __nanosleep+0x23 [ 0xfe52a883, .+0xe ] Current function is SDL_Delay_REAL 204 was_error = nanosleep(&tv, &elapsed); (dbx) where [1] __nanosleep(0xfeffe848, 0xfeffe850, 0xfe75a5ac, 0xfe5169d8), at 0xfe52a875 [2] nanosleep(0xfeffe848, 0xfeffe850), at 0xfe516a3b =>[3] SDL_Delay_REAL(ms = 0), line 204 in "SDL_systimer.c" [4] SDL_AtomicLock_REAL(lock = 0xfeffe88c), line 104 in "SDL_spinlock.c" [5] SDL_GetErrBuf(), line 225 in "SDL_thread.c" [6] SDL_ClearError_REAL(), line 216 in "SDL_error.c" [7] SDL_InitSubSystem_REAL(flags = 0), line 116 in "SDL.c" [8] SDL_Init_REAL(flags = 0), line 244 in "SDL.c" [9] SDL_Init(a = 0), line 89 in "SDL_dynapi_procs.h" [10] main(argc = 1, argv = 0xfeffe948), line 65 in "testthread.c" As far as I can tell, this is because pthread_spin_trylock() always returns EBUSY for this particular lock; since it works in other places, I'm suspicious. Different Solaris Studio compiler versions seem to make no difference. I've verified this is broken on Linux as well if SDL_spinlock.c is modified to use the pthread implementation. This appears to be because pthread_spin_init() and pthread_spin_destroy() are not used with the locks as required.
136 lines
4.0 KiB
C
136 lines
4.0 KiB
C
/*
|
|
Simple DirectMedia Layer
|
|
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
|
|
|
|
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.
|
|
|
|
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:
|
|
|
|
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.
|
|
*/
|
|
#include "../SDL_internal.h"
|
|
|
|
#if defined(__WIN32__) || defined(__WINRT__)
|
|
#include "../core/windows/SDL_windows.h"
|
|
#endif
|
|
|
|
#include "SDL_atomic.h"
|
|
#include "SDL_mutex.h"
|
|
#include "SDL_timer.h"
|
|
|
|
#if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
|
|
#include <atomic.h>
|
|
#endif
|
|
|
|
/* This function is where all the magic happens... */
|
|
SDL_bool
|
|
SDL_AtomicTryLock(SDL_SpinLock *lock)
|
|
{
|
|
#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_LockMutex(_spinlock_mutex);
|
|
if (*lock == 0) {
|
|
*lock = 1;
|
|
SDL_UnlockMutex(_spinlock_mutex);
|
|
return SDL_TRUE;
|
|
} else {
|
|
SDL_UnlockMutex(_spinlock_mutex);
|
|
return SDL_FALSE;
|
|
}
|
|
|
|
#elif defined(_MSC_VER)
|
|
SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
|
|
return (InterlockedExchange((long*)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_4__) || defined(__ARM_ARCH_4T__) || \
|
|
defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
|
|
defined(__ARM_ARCH_5TEJ__))
|
|
int result;
|
|
__asm__ __volatile__ (
|
|
"swp %0, %1, [%2]\n"
|
|
: "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
|
|
return (result == 0);
|
|
|
|
#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);
|
|
|
|
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
|
int result;
|
|
__asm__ __volatile__(
|
|
"lock ; xchgl %0, (%1)\n"
|
|
: "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
|
|
return (result == 0);
|
|
|
|
#elif defined(__MACOSX__) || defined(__IPHONEOS__)
|
|
/* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
|
|
return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
|
|
|
|
#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);
|
|
|
|
#else
|
|
#error Please implement for your platform.
|
|
return SDL_FALSE;
|
|
#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)
|
|
{
|
|
#if defined(_MSC_VER)
|
|
_ReadWriteBarrier();
|
|
*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;
|
|
membar_producer();
|
|
|
|
#else
|
|
*lock = 0;
|
|
#endif
|
|
}
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|