SDL_spinlock.c 3.6 KB

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