1
0

SDL_spinlock.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  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. // This function is where all the magic happens...
  38. bool SDL_TryLockSpinlock(SDL_SpinLock *lock)
  39. {
  40. #if defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET)
  41. return __sync_lock_test_and_set(lock, 1) == 0;
  42. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  43. return _InterlockedExchange_acq(lock, 1) == 0;
  44. #elif defined(_MSC_VER)
  45. SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
  46. return InterlockedExchange((long *)lock, 1) == 0;
  47. #elif defined(__GNUC__) && defined(__arm__) && \
  48. (defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__) || \
  49. defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
  50. defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
  51. defined(__ARM_ARCH_5TEJ__))
  52. int result;
  53. #ifdef SDL_PLATFORM_RISCOS
  54. if (__cpucap_have_rex()) {
  55. __asm__ __volatile__(
  56. "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
  57. : "=&r"(result)
  58. : "r"(1), "r"(lock)
  59. : "cc", "memory");
  60. return result == 0;
  61. }
  62. #endif
  63. __asm__ __volatile__(
  64. "swp %0, %1, [%2]\n"
  65. : "=&r,&r"(result)
  66. : "r,0"(1), "r,r"(lock)
  67. : "memory");
  68. return result == 0;
  69. #elif defined(__GNUC__) && defined(__arm__)
  70. int result;
  71. __asm__ __volatile__(
  72. "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
  73. : "=&r"(result)
  74. : "r"(1), "r"(lock)
  75. : "cc", "memory");
  76. return result == 0;
  77. #elif (defined(__GNUC__) || defined(__TINYC__)) && (defined(__i386__) || defined(__x86_64__))
  78. int result;
  79. __asm__ __volatile__(
  80. "lock ; xchgl %0, (%1)\n"
  81. : "=r"(result)
  82. : "r"(lock), "0"(1)
  83. : "cc", "memory");
  84. return result == 0;
  85. #elif defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)
  86. // Maybe used for PowerPC, but the Intel asm or gcc atomics are favored.
  87. return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
  88. #elif defined(SDL_PLATFORM_SOLARIS) && defined(_LP64)
  89. // Used for Solaris with non-gcc compilers.
  90. return ((int)atomic_cas_64((volatile uint64_t *)lock, 0, 1) == 0);
  91. #elif defined(SDL_PLATFORM_SOLARIS) && !defined(_LP64)
  92. // Used for Solaris with non-gcc compilers.
  93. return ((int)atomic_cas_32((volatile uint32_t *)lock, 0, 1) == 0);
  94. #elif defined(PS2)
  95. uint32_t oldintr;
  96. bool res = false;
  97. // disable interruption
  98. oldintr = DIntr();
  99. if (*lock == 0) {
  100. *lock = 1;
  101. res = true;
  102. }
  103. // enable interruption
  104. if (oldintr) {
  105. EIntr();
  106. }
  107. return res;
  108. #else
  109. // Terrible terrible damage
  110. static SDL_Mutex *_spinlock_mutex;
  111. if (!_spinlock_mutex) {
  112. // Race condition on first lock...
  113. _spinlock_mutex = SDL_CreateMutex();
  114. }
  115. SDL_LockMutex(_spinlock_mutex);
  116. if (*lock == 0) {
  117. *lock = 1;
  118. SDL_UnlockMutex(_spinlock_mutex);
  119. return true;
  120. } else {
  121. SDL_UnlockMutex(_spinlock_mutex);
  122. return false;
  123. }
  124. #endif
  125. }
  126. void SDL_LockSpinlock(SDL_SpinLock *lock)
  127. {
  128. int iterations = 0;
  129. // FIXME: Should we have an eventual timeout?
  130. while (!SDL_TryLockSpinlock(lock)) {
  131. if (iterations < 32) {
  132. iterations++;
  133. SDL_CPUPauseInstruction();
  134. } else {
  135. // !!! FIXME: this doesn't definitely give up the current timeslice, it does different things on various platforms.
  136. SDL_Delay(0);
  137. }
  138. }
  139. }
  140. void SDL_UnlockSpinlock(SDL_SpinLock *lock)
  141. {
  142. #if defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET)
  143. __sync_lock_release(lock);
  144. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  145. _InterlockedExchange_rel(lock, 0);
  146. #elif defined(_MSC_VER)
  147. _ReadWriteBarrier();
  148. *lock = 0;
  149. #elif defined(SDL_PLATFORM_SOLARIS)
  150. // Used for Solaris when not using gcc.
  151. *lock = 0;
  152. membar_producer();
  153. #else
  154. *lock = 0;
  155. #endif
  156. }