SDL_spinlock.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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(__WIN32__) || defined(__WINRT__) || defined(__GDK__)
  20. #include "../core/windows/SDL_windows.h"
  21. #endif
  22. #if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
  23. #include <atomic.h>
  24. #endif
  25. #if !defined(HAVE_GCC_ATOMICS) && defined(__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. #if defined(PS2)
  32. #include <kernel.h>
  33. #endif
  34. #if !defined(HAVE_GCC_ATOMICS) && defined(__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. SDL_bool
  50. SDL_AtomicTryLock(SDL_SpinLock *lock)
  51. {
  52. #if SDL_ATOMIC_DISABLED
  53. /* Terrible terrible damage */
  54. static SDL_mutex *_spinlock_mutex;
  55. if (_spinlock_mutex == NULL) {
  56. /* Race condition on first lock... */
  57. _spinlock_mutex = SDL_CreateMutex();
  58. }
  59. SDL_LockMutex(_spinlock_mutex);
  60. if (*lock == 0) {
  61. *lock = 1;
  62. SDL_UnlockMutex(_spinlock_mutex);
  63. return SDL_TRUE;
  64. } else {
  65. SDL_UnlockMutex(_spinlock_mutex);
  66. return SDL_FALSE;
  67. }
  68. #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
  69. return __sync_lock_test_and_set(lock, 1) == 0;
  70. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  71. return _InterlockedExchange_acq(lock, 1) == 0;
  72. #elif defined(_MSC_VER)
  73. SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
  74. return InterlockedExchange((long *)lock, 1) == 0;
  75. #elif defined(__WATCOMC__) && defined(__386__)
  76. return _SDL_xchg_watcom(lock, 1) == 0;
  77. #elif defined(__GNUC__) && defined(__arm__) && \
  78. (defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__) || \
  79. defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
  80. defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
  81. defined(__ARM_ARCH_5TEJ__))
  82. int result;
  83. #if defined(__RISCOS__)
  84. if (__cpucap_have_rex()) {
  85. __asm__ __volatile__(
  86. "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
  87. : "=&r"(result)
  88. : "r"(1), "r"(lock)
  89. : "cc", "memory");
  90. return result == 0;
  91. }
  92. #endif
  93. __asm__ __volatile__(
  94. "swp %0, %1, [%2]\n"
  95. : "=&r,&r"(result)
  96. : "r,0"(1), "r,r"(lock)
  97. : "memory");
  98. return result == 0;
  99. #elif defined(__GNUC__) && defined(__arm__)
  100. int 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");
  106. return result == 0;
  107. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  108. int result;
  109. __asm__ __volatile__(
  110. "lock ; xchgl %0, (%1)\n"
  111. : "=r"(result)
  112. : "r"(lock), "0"(1)
  113. : "cc", "memory");
  114. return result == 0;
  115. #elif defined(__MACOS__) || defined(__IOS__) || defined(__TVOS__)
  116. /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
  117. return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
  118. #elif defined(__SOLARIS__) && defined(_LP64)
  119. /* Used for Solaris with non-gcc compilers. */
  120. return (SDL_bool)((int)atomic_cas_64((volatile uint64_t *)lock, 0, 1) == 0);
  121. #elif defined(__SOLARIS__) && !defined(_LP64)
  122. /* Used for Solaris with non-gcc compilers. */
  123. return (SDL_bool)((int)atomic_cas_32((volatile uint32_t *)lock, 0, 1) == 0);
  124. #elif defined(PS2)
  125. uint32_t oldintr;
  126. SDL_bool res = SDL_FALSE;
  127. // disable interuption
  128. oldintr = DIntr();
  129. if (*lock == 0) {
  130. *lock = 1;
  131. res = SDL_TRUE;
  132. }
  133. // enable interuption
  134. if (oldintr) {
  135. EIntr();
  136. }
  137. return res;
  138. #else
  139. #error Please implement for your platform.
  140. return SDL_FALSE;
  141. #endif
  142. }
  143. void SDL_AtomicLock(SDL_SpinLock *lock)
  144. {
  145. int iterations = 0;
  146. /* FIXME: Should we have an eventual timeout? */
  147. while (!SDL_AtomicTryLock(lock)) {
  148. if (iterations < 32) {
  149. iterations++;
  150. SDL_CPUPauseInstruction();
  151. } else {
  152. /* !!! FIXME: this doesn't definitely give up the current timeslice, it does different things on various platforms. */
  153. SDL_Delay(0);
  154. }
  155. }
  156. }
  157. void SDL_AtomicUnlock(SDL_SpinLock *lock)
  158. {
  159. #if HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
  160. __sync_lock_release(lock);
  161. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  162. _InterlockedExchange_rel(lock, 0);
  163. #elif defined(_MSC_VER)
  164. _ReadWriteBarrier();
  165. *lock = 0;
  166. #elif defined(__WATCOMC__) && defined(__386__)
  167. SDL_CompilerBarrier();
  168. *lock = 0;
  169. #elif defined(__SOLARIS__)
  170. /* Used for Solaris when not using gcc. */
  171. *lock = 0;
  172. membar_producer();
  173. #else
  174. *lock = 0;
  175. #endif
  176. }