Update SDL_spinlock.c

This commit is contained in:
Patrick Babb
2025-03-18 21:55:59 -05:00
committed by GitHub
parent 5cdf952623
commit 0b3b33f727

View File

@@ -14,10 +14,11 @@
claim that you wrote the original software. If you use this software claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be in a product, an acknowledgment in the product documentation would be
appreciated but is not required. appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be 2. Altered source versions must be plainly marked as such, and must not
misrepresented as being the original software. be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
#include "SDL_stdinc.h" #include "SDL_stdinc.h"
#include "SDL_atomic.h" #include "SDL_atomic.h"
@@ -30,8 +31,7 @@
#endif #endif
/* This function is where all the magic happens... */ /* This function is where all the magic happens... */
SDL_bool SDL_bool SDL_AtomicTryLock(SDL_SpinLock *lock)
SDL_AtomicTryLock(SDL_SpinLock *lock)
{ {
#if SDL_ATOMIC_DISABLED #if SDL_ATOMIC_DISABLED
/* Terrible terrible damage */ /* Terrible terrible damage */
@@ -85,13 +85,27 @@ SDL_AtomicTryLock(SDL_SpinLock *lock)
return (result == 0); return (result == 0);
#else #else
/* Need CPU instructions for spinlock here! */ /* Fallback implementation using a standard mutex */
__need_spinlock_implementation__ /* You can use SDL_mutex as a fallback if no atomic instructions are available */
static SDL_mutex *mutex;
if (!mutex) {
mutex = SDL_CreateMutex();
}
SDL_mutexP(mutex);
if (*lock == 0) {
*lock = 1;
SDL_mutexV(mutex);
return SDL_TRUE;
} else {
SDL_mutexV(mutex);
return SDL_FALSE;
}
#endif #endif
} }
void void SDL_AtomicLock(SDL_SpinLock *lock)
SDL_AtomicLock(SDL_SpinLock *lock)
{ {
/* FIXME: Should we have an eventual timeout? */ /* FIXME: Should we have an eventual timeout? */
while (!SDL_AtomicTryLock(lock)) { while (!SDL_AtomicTryLock(lock)) {
@@ -99,8 +113,7 @@ SDL_AtomicLock(SDL_SpinLock *lock)
} }
} }
void void SDL_AtomicUnlock(SDL_SpinLock *lock)
SDL_AtomicUnlock(SDL_SpinLock *lock)
{ {
#if defined(_MSC_VER) #if defined(_MSC_VER)
_ReadWriteBarrier(); _ReadWriteBarrier();