Atlas - SDL_spinlock.c
Home / ext / SDL2 / src / atomic Lines: 6 | Size: 5750 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2018 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "../SDL_internal.h" 22 23#if defined(__WIN32__) || defined(__WINRT__) 24#include "../core/windows/SDL_windows.h" 25#endif 26 27#include "SDL_atomic.h" 28#include "SDL_mutex.h" 29#include "SDL_timer.h" 30 31#if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__) 32#include <atomic.h> 33#endif 34 35#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) 36#include <xmmintrin.h> 37#endif 38 39#if defined(__WATCOMC__) && defined(__386__) 40SDL_COMPILE_TIME_ASSERT(locksize, 4==sizeof(SDL_SpinLock)); 41extern _inline int _SDL_xchg_watcom(volatile int *a, int v); 42#pragma aux _SDL_xchg_watcom = \ 43 "xchg [ecx], eax" \ 44 parm [ecx] [eax] \ 45 value [eax] \ 46 modify exact [eax]; 47#endif /* __WATCOMC__ && __386__ */ 48 49/* This function is where all the magic happens... */ 50SDL_bool 51SDL_AtomicTryLock(SDL_SpinLock *lock) 52{ 53#if SDL_ATOMIC_DISABLED 54 /* Terrible terrible damage */ 55 static SDL_mutex *_spinlock_mutex; 56 57 if (!_spinlock_mutex) { 58 /* Race condition on first lock... */ 59 _spinlock_mutex = SDL_CreateMutex(); 60 } 61 SDL_LockMutex(_spinlock_mutex); 62 if (*lock == 0) { 63 *lock = 1; 64 SDL_UnlockMutex(_spinlock_mutex); 65 return SDL_TRUE; 66 } else { 67 SDL_UnlockMutex(_spinlock_mutex); 68 return SDL_FALSE; 69 } 70 71#elif defined(_MSC_VER) 72 SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long)); 73 return (InterlockedExchange((long*)lock, 1) == 0); 74 75#elif defined(__WATCOMC__) && defined(__386__) 76 return _SDL_xchg_watcom(lock, 1) == 0; 77 78#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET 79 return (__sync_lock_test_and_set(lock, 1) == 0); 80 81#elif defined(__GNUC__) && defined(__arm__) && \ 82 (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \ 83 defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \ 84 defined(__ARM_ARCH_5TEJ__)) 85 int result; 86 __asm__ __volatile__ ( 87 "swp %0, %1, [%2]\n" 88 : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory"); 89 return (result == 0); 90 91#elif defined(__GNUC__) && defined(__arm__) 92 int result; 93 __asm__ __volatile__ ( 94 "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]" 95 : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory"); 96 return (result == 0); 97 98#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) 99 int result; 100 __asm__ __volatile__( 101 "lock ; xchgl %0, (%1)\n" 102 : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory"); 103 return (result == 0); 104 105#elif defined(__MACOSX__) || defined(__IPHONEOS__) 106 /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */ 107 return OSAtomicCompareAndSwap32Barrier(0, 1, lock); 108 109#elif defined(__SOLARIS__) && defined(_LP64) 110 /* Used for Solaris with non-gcc compilers. */ 111 return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0); 112 113#elif defined(__SOLARIS__) && !defined(_LP64) 114 /* Used for Solaris with non-gcc compilers. */ 115 return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0); 116 117#else 118#error Please implement for your platform. 119 return SDL_FALSE; 120#endif 121} 122 123/* "REP NOP" is PAUSE, coded for tools that don't know it by that name. */ 124#if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) 125 #define PAUSE_INSTRUCTION() __asm__ __volatile__("pause\n") /* Some assemblers can't do REP NOP, so go with PAUSE. */ 126#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) 127 #define PAUSE_INSTRUCTION() _mm_pause() /* this is actually "rep nop" and not a SIMD instruction. No inline asm in MSVC x86-64! */ 128#elif defined(__WATCOMC__) && defined(__386__) 129 /* watcom assembler rejects PAUSE if CPU < i686, and it refuses REP NOP as an invalid combination. Hardcode the bytes. */ 130 extern _inline void PAUSE_INSTRUCTION(void); 131 #pragma aux PAUSE_INSTRUCTION = "db 0f3h,90h" 132#else 133 #define PAUSE_INSTRUCTION() 134#endif 135 136void 137SDL_AtomicLock(SDL_SpinLock *lock) 138{ 139 int iterations = 0; 140 /* FIXME: Should we have an eventual timeout? */ 141 while (!SDL_AtomicTryLock(lock)) { 142 if (iterations < 32) { 143 iterations++; 144 PAUSE_INSTRUCTION(); 145 } else { 146 /* !!! FIXME: this doesn't definitely give up the current timeslice, it does different things on various platforms. */ 147 SDL_Delay(0); 148 } 149 } 150} 151 152void 153SDL_AtomicUnlock(SDL_SpinLock *lock) 154{ 155#if defined(_MSC_VER) 156 _ReadWriteBarrier(); 157 *lock = 0; 158 159#elif defined(__WATCOMC__) && defined(__386__) 160 SDL_CompilerBarrier (); 161 *lock = 0; 162 163#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET 164 __sync_lock_release(lock); 165 166#elif defined(__SOLARIS__) 167 /* Used for Solaris when not using gcc. */ 168 *lock = 0; 169 membar_producer(); 170 171#else 172 *lock = 0; 173#endif 174} 175 176/* vi: set ts=4 sw=4 expandtab: */ 177[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.