SDL_spinlock.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include "SDL_atomic.h"
  23. #include "SDL_mutex.h"
  24. #include "SDL_timer.h"
  25. #if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
  26. #include <atomic.h>
  27. #endif
  28. #if !defined(HAVE_GCC_ATOMICS) && defined(__RISCOS__)
  29. #include <unixlib/local.h>
  30. #endif
  31. #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
  32. #include <xmmintrin.h>
  33. #endif
  34. #if defined(PS2)
  35. #include <kernel.h>
  36. #endif
  37. #if !defined(HAVE_GCC_ATOMICS) && defined(__MACOSX__)
  38. #include <libkern/OSAtomic.h>
  39. #endif
  40. #if defined(__WATCOMC__) && defined(__386__)
  41. SDL_COMPILE_TIME_ASSERT(locksize, 4==sizeof(SDL_SpinLock));
  42. extern __inline int _SDL_xchg_watcom(volatile int *a, int v);
  43. #pragma aux _SDL_xchg_watcom = \
  44. "lock xchg [ecx], eax" \
  45. parm [ecx] [eax] \
  46. value [eax] \
  47. modify exact [eax];
  48. #endif /* __WATCOMC__ && __386__ */
  49. /* This function is where all the magic happens... */
  50. SDL_bool
  51. SDL_AtomicTryLock(SDL_SpinLock *lock)
  52. {
  53. #if SDL_ATOMIC_DISABLED
  54. /* Terrible terrible damage */
  55. static SDL_mutex *_spinlock_mutex;
  56. if (!_spinlock_mutex) {
  57. /* Race condition on first lock... */
  58. _spinlock_mutex = SDL_CreateMutex();
  59. }
  60. SDL_LockMutex(_spinlock_mutex);
  61. if (*lock == 0) {
  62. *lock = 1;
  63. SDL_UnlockMutex(_spinlock_mutex);
  64. return SDL_TRUE;
  65. } else {
  66. SDL_UnlockMutex(_spinlock_mutex);
  67. return SDL_FALSE;
  68. }
  69. #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
  70. return (__sync_lock_test_and_set(lock, 1) == 0);
  71. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  72. return (_InterlockedExchange_acq(lock, 1) == 0);
  73. #elif defined(_MSC_VER)
  74. SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
  75. return (InterlockedExchange((long*)lock, 1) == 0);
  76. #elif defined(__WATCOMC__) && defined(__386__)
  77. return _SDL_xchg_watcom(lock, 1) == 0;
  78. #elif defined(__GNUC__) && defined(__arm__) && \
  79. (defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__) || \
  80. defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
  81. defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
  82. defined(__ARM_ARCH_5TEJ__))
  83. int result;
  84. #if defined(__RISCOS__)
  85. if (__cpucap_have_rex()) {
  86. __asm__ __volatile__ (
  87. "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
  88. : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
  89. return (result == 0);
  90. }
  91. #endif
  92. __asm__ __volatile__ (
  93. "swp %0, %1, [%2]\n"
  94. : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
  95. return (result == 0);
  96. #elif defined(__GNUC__) && defined(__arm__)
  97. int result;
  98. __asm__ __volatile__ (
  99. "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
  100. : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
  101. return (result == 0);
  102. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  103. int result;
  104. __asm__ __volatile__(
  105. "lock ; xchgl %0, (%1)\n"
  106. : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
  107. return (result == 0);
  108. #elif defined(__MACOSX__) || defined(__IPHONEOS__)
  109. /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
  110. return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
  111. #elif defined(__SOLARIS__) && defined(_LP64)
  112. /* Used for Solaris with non-gcc compilers. */
  113. return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
  114. #elif defined(__SOLARIS__) && !defined(_LP64)
  115. /* Used for Solaris with non-gcc compilers. */
  116. return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0);
  117. #elif defined(PS2)
  118. uint32_t oldintr;
  119. SDL_bool res = SDL_FALSE;
  120. // disable interuption
  121. oldintr = DIntr();
  122. if (*lock == 0) {
  123. *lock = 1;
  124. res = SDL_TRUE;
  125. }
  126. // enable interuption
  127. if(oldintr) { EIntr(); }
  128. return res;
  129. #else
  130. #error Please implement for your platform.
  131. return SDL_FALSE;
  132. #endif
  133. }
  134. /* "REP NOP" is PAUSE, coded for tools that don't know it by that name. */
  135. #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
  136. #define PAUSE_INSTRUCTION() __asm__ __volatile__("pause\n") /* Some assemblers can't do REP NOP, so go with PAUSE. */
  137. #elif (defined(__arm__) && __ARM_ARCH__ >= 7) || defined(__aarch64__)
  138. #define PAUSE_INSTRUCTION() __asm__ __volatile__("yield" ::: "memory")
  139. #elif (defined(__powerpc__) || defined(__powerpc64__))
  140. #define PAUSE_INSTRUCTION() __asm__ __volatile__("or 27,27,27");
  141. #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
  142. #define PAUSE_INSTRUCTION() _mm_pause() /* this is actually "rep nop" and not a SIMD instruction. No inline asm in MSVC x86-64! */
  143. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  144. #define PAUSE_INSTRUCTION() __yield()
  145. #elif defined(__WATCOMC__) && defined(__386__)
  146. /* watcom assembler rejects PAUSE if CPU < i686, and it refuses REP NOP as an invalid combination. Hardcode the bytes. */
  147. extern __inline void PAUSE_INSTRUCTION(void);
  148. #pragma aux PAUSE_INSTRUCTION = "db 0f3h,90h"
  149. #else
  150. #define PAUSE_INSTRUCTION()
  151. #endif
  152. void
  153. SDL_AtomicLock(SDL_SpinLock *lock)
  154. {
  155. int iterations = 0;
  156. /* FIXME: Should we have an eventual timeout? */
  157. while (!SDL_AtomicTryLock(lock)) {
  158. if (iterations < 32) {
  159. iterations++;
  160. PAUSE_INSTRUCTION();
  161. } else {
  162. /* !!! FIXME: this doesn't definitely give up the current timeslice, it does different things on various platforms. */
  163. SDL_Delay(0);
  164. }
  165. }
  166. }
  167. void
  168. SDL_AtomicUnlock(SDL_SpinLock *lock)
  169. {
  170. #if HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
  171. __sync_lock_release(lock);
  172. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  173. _InterlockedExchange_rel(lock, 0);
  174. #elif defined(_MSC_VER)
  175. _ReadWriteBarrier();
  176. *lock = 0;
  177. #elif defined(__WATCOMC__) && defined(__386__)
  178. SDL_CompilerBarrier ();
  179. *lock = 0;
  180. #elif defined(__SOLARIS__)
  181. /* Used for Solaris when not using gcc. */
  182. *lock = 0;
  183. membar_producer();
  184. #else
  185. *lock = 0;
  186. #endif
  187. }
  188. /* vi: set ts=4 sw=4 expandtab: */