SDL  2.0
SDL_spinlock.c File Reference
#include "../SDL_internal.h"
#include "SDL_atomic.h"
#include "SDL_mutex.h"
#include "SDL_timer.h"
+ Include dependency graph for SDL_spinlock.c:

Go to the source code of this file.

Functions

SDL_bool SDL_AtomicTryLock (SDL_SpinLock *lock)
 Try to lock a spin lock by setting it to a non-zero value. More...
 
void SDL_AtomicLock (SDL_SpinLock *lock)
 Lock a spin lock by setting it to a non-zero value. More...
 
void SDL_AtomicUnlock (SDL_SpinLock *lock)
 Unlock a spin lock by setting it to 0. Always returns immediately. More...
 

Function Documentation

§ SDL_AtomicLock()

void SDL_AtomicLock ( SDL_SpinLock lock)

Lock a spin lock by setting it to a non-zero value.

Parameters
lockPoints to the lock.

Definition at line 107 of file SDL_spinlock.c.

References SDL_AtomicTryLock(), and SDL_Delay.

108 {
109  /* FIXME: Should we have an eventual timeout? */
110  while (!SDL_AtomicTryLock(lock)) {
111  SDL_Delay(0);
112  }
113 }
#define SDL_Delay
SDL_mutex * lock
Definition: SDL_events.c:75
SDL_bool SDL_AtomicTryLock(SDL_SpinLock *lock)
Try to lock a spin lock by setting it to a non-zero value.
Definition: SDL_spinlock.c:37

§ SDL_AtomicTryLock()

SDL_bool SDL_AtomicTryLock ( SDL_SpinLock lock)

Try to lock a spin lock by setting it to a non-zero value.

Parameters
lockPoints to the lock.
Returns
SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.

Definition at line 37 of file SDL_spinlock.c.

References lock, SDL_COMPILE_TIME_ASSERT(), SDL_CreateMutex, SDL_FALSE, SDL_LockMutex, SDL_TRUE, and SDL_UnlockMutex.

Referenced by SDL_AtomicLock().

38 {
39 #if SDL_ATOMIC_DISABLED
40  /* Terrible terrible damage */
41  static SDL_mutex *_spinlock_mutex;
42 
43  if (!_spinlock_mutex) {
44  /* Race condition on first lock... */
45  _spinlock_mutex = SDL_CreateMutex();
46  }
47  SDL_LockMutex(_spinlock_mutex);
48  if (*lock == 0) {
49  *lock = 1;
50  SDL_UnlockMutex(_spinlock_mutex);
51  return SDL_TRUE;
52  } else {
53  SDL_UnlockMutex(_spinlock_mutex);
54  return SDL_FALSE;
55  }
56 
57 #elif defined(_MSC_VER)
58  SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
59  return (InterlockedExchange((long*)lock, 1) == 0);
60 
61 #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
62  return (__sync_lock_test_and_set(lock, 1) == 0);
63 
64 #elif defined(__GNUC__) && defined(__arm__) && \
65  (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
66  defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
67  defined(__ARM_ARCH_5TEJ__))
68  int result;
69  __asm__ __volatile__ (
70  "swp %0, %1, [%2]\n"
71  : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
72  return (result == 0);
73 
74 #elif defined(__GNUC__) && defined(__arm__)
75  int result;
76  __asm__ __volatile__ (
77  "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
78  : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
79  return (result == 0);
80 
81 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
82  int result;
83  __asm__ __volatile__(
84  "lock ; xchgl %0, (%1)\n"
85  : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
86  return (result == 0);
87 
88 #elif defined(__MACOSX__) || defined(__IPHONEOS__)
89  /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
90  return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
91 
92 #elif defined(__SOLARIS__) && defined(_LP64)
93  /* Used for Solaris with non-gcc compilers. */
94  return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
95 
96 #elif defined(__SOLARIS__) && !defined(_LP64)
97  /* Used for Solaris with non-gcc compilers. */
98  return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0);
99 
100 #else
101 #error Please implement for your platform.
102  return SDL_FALSE;
103 #endif
104 }
#define SDL_LockMutex
GLuint64EXT * result
#define SDL_CreateMutex
unsigned long long uint64_t
SDL_bool
Definition: SDL_stdinc.h:130
unsigned int uint32_t
SDL_mutex * lock
Definition: SDL_events.c:75
SDL_COMPILE_TIME_ASSERT(size, CountTo >0)
#define SDL_UnlockMutex

§ SDL_AtomicUnlock()

void SDL_AtomicUnlock ( SDL_SpinLock lock)

Unlock a spin lock by setting it to 0. Always returns immediately.

Parameters
lockPoints to the lock.

Definition at line 116 of file SDL_spinlock.c.

117 {
118 #if defined(_MSC_VER)
119  _ReadWriteBarrier();
120  *lock = 0;
121 
122 #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
123  __sync_lock_release(lock);
124 
125 #elif defined(__SOLARIS__)
126  /* Used for Solaris when not using gcc. */
127  *lock = 0;
128  membar_producer();
129 
130 #else
131  *lock = 0;
132 #endif
133 }
SDL_mutex * lock
Definition: SDL_events.c:75