SDL_spinlock.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 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(__WIN32__) || defined(__WINRT__)
  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. /* This function is where all the magic happens... */
  29. SDL_bool
  30. SDL_AtomicTryLock(SDL_SpinLock *lock)
  31. {
  32. #if SDL_ATOMIC_DISABLED
  33. /* Terrible terrible damage */
  34. static SDL_mutex *_spinlock_mutex;
  35. if (!_spinlock_mutex) {
  36. /* Race condition on first lock... */
  37. _spinlock_mutex = SDL_CreateMutex();
  38. }
  39. SDL_LockMutex(_spinlock_mutex);
  40. if (*lock == 0) {
  41. *lock = 1;
  42. SDL_UnlockMutex(_spinlock_mutex);
  43. return SDL_TRUE;
  44. } else {
  45. SDL_UnlockMutex(_spinlock_mutex);
  46. return SDL_FALSE;
  47. }
  48. #elif defined(_MSC_VER)
  49. SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
  50. return (InterlockedExchange((long*)lock, 1) == 0);
  51. #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
  52. return (__sync_lock_test_and_set(lock, 1) == 0);
  53. #elif defined(__GNUC__) && defined(__arm__) && \
  54. (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
  55. defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
  56. defined(__ARM_ARCH_5TEJ__))
  57. int result;
  58. __asm__ __volatile__ (
  59. "swp %0, %1, [%2]\n"
  60. : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
  61. return (result == 0);
  62. #elif defined(__GNUC__) && defined(__arm__)
  63. int result;
  64. __asm__ __volatile__ (
  65. "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
  66. : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
  67. return (result == 0);
  68. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  69. int result;
  70. __asm__ __volatile__(
  71. "lock ; xchgl %0, (%1)\n"
  72. : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
  73. return (result == 0);
  74. #elif defined(__MACOSX__) || defined(__IPHONEOS__)
  75. /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
  76. return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
  77. #elif defined(__SOLARIS__) && defined(_LP64)
  78. /* Used for Solaris with non-gcc compilers. */
  79. return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
  80. #elif defined(__SOLARIS__) && !defined(_LP64)
  81. /* Used for Solaris with non-gcc compilers. */
  82. return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0);
  83. #else
  84. #error Please implement for your platform.
  85. return SDL_FALSE;
  86. #endif
  87. }
  88. void
  89. SDL_AtomicLock(SDL_SpinLock *lock)
  90. {
  91. /* FIXME: Should we have an eventual timeout? */
  92. while (!SDL_AtomicTryLock(lock)) {
  93. SDL_Delay(0);
  94. }
  95. }
  96. void
  97. SDL_AtomicUnlock(SDL_SpinLock *lock)
  98. {
  99. #if defined(_MSC_VER)
  100. _ReadWriteBarrier();
  101. *lock = 0;
  102. #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
  103. __sync_lock_release(lock);
  104. #elif defined(__SOLARIS__)
  105. /* Used for Solaris when not using gcc. */
  106. *lock = 0;
  107. membar_producer();
  108. #else
  109. *lock = 0;
  110. #endif
  111. }
  112. /* vi: set ts=4 sw=4 expandtab: */