Path: blob/master/thirdparty/sdl/atomic/SDL_spinlock.c
9904 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122#if defined(SDL_PLATFORM_WINDOWS)23#include "../core/windows/SDL_windows.h"24#endif2526#if !defined(HAVE_GCC_ATOMICS) && defined(SDL_PLATFORM_SOLARIS)27#include <atomic.h>28#endif2930#if !defined(HAVE_GCC_ATOMICS) && defined(SDL_PLATFORM_RISCOS)31#include <unixlib/local.h>32#endif3334#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))35#include <xmmintrin.h>36#endif3738#ifdef PS239#include <kernel.h>40#endif4142#if !defined(HAVE_GCC_ATOMICS) && defined(SDL_PLATFORM_MACOS)43#include <libkern/OSAtomic.h>44#endif4546/* *INDENT-OFF* */ // clang-format off47#if defined(__WATCOMC__) && defined(__386__)48SDL_COMPILE_TIME_ASSERT(locksize, 4==sizeof(SDL_SpinLock));49extern __inline int _SDL_xchg_watcom(volatile int *a, int v);50#pragma aux _SDL_xchg_watcom = \51"lock xchg [ecx], eax" \52parm [ecx] [eax] \53value [eax] \54modify exact [eax];55#endif // __WATCOMC__ && __386__56/* *INDENT-ON* */ // clang-format on5758// This function is where all the magic happens...59bool SDL_TryLockSpinlock(SDL_SpinLock *lock)60{61#if defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET)62return __sync_lock_test_and_set(lock, 1) == 0;6364#elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))65return _InterlockedExchange_acq(lock, 1) == 0;6667#elif defined(_MSC_VER)68SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));69return InterlockedExchange((long *)lock, 1) == 0;7071#elif defined(__WATCOMC__) && defined(__386__)72return _SDL_xchg_watcom(lock, 1) == 0;7374#elif defined(__GNUC__) && defined(__arm__) && \75(defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__) || \76defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \77defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \78defined(__ARM_ARCH_5TEJ__))79int result;8081#ifdef SDL_PLATFORM_RISCOS82if (__cpucap_have_rex()) {83__asm__ __volatile__(84"ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"85: "=&r"(result)86: "r"(1), "r"(lock)87: "cc", "memory");88return result == 0;89}90#endif9192__asm__ __volatile__(93"swp %0, %1, [%2]\n"94: "=&r,&r"(result)95: "r,0"(1), "r,r"(lock)96: "memory");97return result == 0;9899#elif defined(__GNUC__) && defined(__arm__)100int result;101__asm__ __volatile__(102"ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"103: "=&r"(result)104: "r"(1), "r"(lock)105: "cc", "memory");106return result == 0;107108#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))109int result;110__asm__ __volatile__(111"lock ; xchgl %0, (%1)\n"112: "=r"(result)113: "r"(lock), "0"(1)114: "cc", "memory");115return result == 0;116117#elif defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)118// Maybe used for PowerPC, but the Intel asm or gcc atomics are favored.119return OSAtomicCompareAndSwap32Barrier(0, 1, lock);120121#elif defined(SDL_PLATFORM_SOLARIS) && defined(_LP64)122// Used for Solaris with non-gcc compilers.123return ((int)atomic_cas_64((volatile uint64_t *)lock, 0, 1) == 0);124125#elif defined(SDL_PLATFORM_SOLARIS) && !defined(_LP64)126// Used for Solaris with non-gcc compilers.127return ((int)atomic_cas_32((volatile uint32_t *)lock, 0, 1) == 0);128#elif defined(PS2)129uint32_t oldintr;130bool res = false;131// disable interruption132oldintr = DIntr();133134if (*lock == 0) {135*lock = 1;136res = true;137}138// enable interruption139if (oldintr) {140EIntr();141}142return res;143#else144// Terrible terrible damage145static SDL_Mutex *_spinlock_mutex;146147if (!_spinlock_mutex) {148// Race condition on first lock...149_spinlock_mutex = SDL_CreateMutex();150}151SDL_LockMutex(_spinlock_mutex);152if (*lock == 0) {153*lock = 1;154SDL_UnlockMutex(_spinlock_mutex);155return true;156} else {157SDL_UnlockMutex(_spinlock_mutex);158return false;159}160#endif161}162163void SDL_LockSpinlock(SDL_SpinLock *lock)164{165int iterations = 0;166// FIXME: Should we have an eventual timeout?167while (!SDL_TryLockSpinlock(lock)) {168if (iterations < 32) {169iterations++;170SDL_CPUPauseInstruction();171} else {172// !!! FIXME: this doesn't definitely give up the current timeslice, it does different things on various platforms.173SDL_Delay(0);174}175}176}177178void SDL_UnlockSpinlock(SDL_SpinLock *lock)179{180#if defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET)181__sync_lock_release(lock);182183#elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))184_InterlockedExchange_rel(lock, 0);185186#elif defined(_MSC_VER)187_ReadWriteBarrier();188*lock = 0;189190#elif defined(__WATCOMC__) && defined(__386__)191SDL_CompilerBarrier();192*lock = 0;193194#elif defined(SDL_PLATFORM_SOLARIS)195// Used for Solaris when not using gcc.196*lock = 0;197membar_producer();198199#else200*lock = 0;201#endif202}203204205