SDL_spinlock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(SDL_PLATFORM_WINDOWS)
  20. #include "../core/windows/SDL_windows.h"
  21. #endif
  22. #if !defined(HAVE_GCC_ATOMICS) && defined(SDL_PLATFORM_SOLARIS)
  23. #include <atomic.h>
  24. #endif
  25. #if !defined(HAVE_GCC_ATOMICS) && defined(SDL_PLATFORM_RISCOS)
  26. #include <unixlib/local.h>
  27. #endif
  28. #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
  29. #include <xmmintrin.h>
  30. #endif
  31. #ifdef PS2
  32. #include <kernel.h>
  33. #endif
  34. #if !defined(HAVE_GCC_ATOMICS) && defined(SDL_PLATFORM_MACOS)
  35. #include <libkern/OSAtomic.h>
  36. #endif
  37. /* *INDENT-OFF* */ // clang-format off
  38. #if defined(__WATCOMC__) && defined(__386__)
  39. SDL_COMPILE_TIME_ASSERT(locksize, 4==sizeof(SDL_SpinLock));
  40. extern __inline int _SDL_xchg_watcom(volatile int *a, int v);
  41. #pragma aux _SDL_xchg_watcom = \
  42. "lock xchg [ecx], eax" \
  43. parm [ecx] [eax] \
  44. value [eax] \
  45. modify exact [eax];
  46. #endif // __WATCOMC__ && __386__
  47. /* *INDENT-ON* */ // clang-format on
  48. // This function is where all the magic happens...
  49. bool SDL_TryLockSpinlock(SDL_SpinLock *lock)
  50. {
  51. #if defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET)
  52. return __sync_lock_test_and_set(lock, 1) == 0;
  53. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  54. return _InterlockedExchange_acq(lock, 1) == 0;
  55. #elif defined(_MSC_VER)
  56. SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
  57. return InterlockedExchange((long *)lock, 1) == 0;
  58. #elif defined(__WATCOMC__) && defined(__386__)
  59. return _SDL_xchg_watcom(lock, 1) == 0;
  60. #elif defined(__GNUC__) && defined(__arm__) && \
  61. (defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__) || \
  62. defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
  63. defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
  64. defined(__ARM_ARCH_5TEJ__))
  65. int result;
  66. #ifdef SDL_PLATFORM_RISCOS
  67. if (__cpucap_have_rex()) {
  68. __asm__ __volatile__(
  69. "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
  70. : "=&r"(result)
  71. : "r"(1), "r"(lock)
  72. : "cc", "memory");
  73. return result == 0;
  74. }
  75. #endif
  76. __asm__ __volatile__(
  77. "swp %0, %1, [%2]\n"
  78. : "=&r,&r"(result)
  79. : "r,0"(1), "r,r"(lock)
  80. : "memory");
  81. return result == 0;
  82. #elif defined(__GNUC__) && defined(__arm__)
  83. int result;
  84. __asm__ __volatile__(
  85. "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
  86. : "=&r"(result)
  87. : "r"(1), "r"(lock)
  88. : "cc", "memory");
  89. return result == 0;
  90. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  91. int result;
  92. __asm__ __volatile__(
  93. "lock ; xchgl %0, (%1)\n"
  94. : "=r"(result)
  95. : "r"(lock), "0"(1)
  96. : "cc", "memory");
  97. return result == 0;
  98. #elif defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)
  99. // Maybe used for PowerPC, but the Intel asm or gcc atomics are favored.
  100. return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
  101. #elif defined(SDL_PLATFORM_SOLARIS) && defined(_LP64)
  102. // Used for Solaris with non-gcc compilers.
  103. return ((int)atomic_cas_64((volatile uint64_t *)lock, 0, 1) == 0);
  104. #elif defined(SDL_PLATFORM_SOLARIS) && !defined(_LP64)
  105. // Used for Solaris with non-gcc compilers.
  106. return ((int)atomic_cas_32((volatile uint32_t *)lock, 0, 1) == 0);
  107. #elif defined(PS2)
  108. uint32_t oldintr;
  109. bool res = false;
  110. // disable interruption
  111. oldintr = DIntr();
  112. if (*lock == 0) {
  113. *lock = 1;
  114. res = true;
  115. }
  116. // enable interruption
  117. if (oldintr) {
  118. EIntr();
  119. }
  120. return res;
  121. #else
  122. // Terrible terrible damage
  123. static SDL_Mutex *_spinlock_mutex;
  124. if (!_spinlock_mutex) {
  125. // Race condition on first lock...
  126. _spinlock_mutex = SDL_CreateMutex();
  127. }
  128. SDL_LockMutex(_spinlock_mutex);
  129. if (*lock == 0) {
  130. *lock = 1;
  131. SDL_UnlockMutex(_spinlock_mutex);
  132. return true;
  133. } else {
  134. SDL_UnlockMutex(_spinlock_mutex);
  135. return false;
  136. }
  137. #endif
  138. }
  139. void SDL_LockSpinlock(SDL_SpinLock *lock)
  140. {
  141. int iterations = 0;
  142. // FIXME: Should we have an eventual timeout?
  143. while (!SDL_TryLockSpinlock(lock)) {
  144. if (iterations < 32) {
  145. iterations++;
  146. SDL_CPUPauseInstruction();
  147. } else {
  148. // !!! FIXME: this doesn't definitely give up the current timeslice, it does different things on various platforms.
  149. SDL_Delay(0);
  150. }
  151. }
  152. }
  153. void SDL_UnlockSpinlock(SDL_SpinLock *lock)
  154. {
  155. #if defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET)
  156. __sync_lock_release(lock);
  157. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  158. _InterlockedExchange_rel(lock, 0);
  159. #elif defined(_MSC_VER)
  160. _ReadWriteBarrier();
  161. *lock = 0;
  162. #elif defined(__WATCOMC__) && defined(__386__)
  163. SDL_CompilerBarrier();
  164. *lock = 0;
  165. #elif defined(SDL_PLATFORM_SOLARIS)
  166. // Used for Solaris when not using gcc.
  167. *lock = 0;
  168. membar_producer();
  169. #else
  170. *lock = 0;
  171. #endif
  172. }