2 Simple DirectMedia Layer
3 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../SDL_internal.h"
23 #if defined(__WIN32__) || defined(__WINRT__)
24 #include "../core/windows/SDL_windows.h"
27 #include "SDL_atomic.h"
28 #include "SDL_mutex.h"
29 #include "SDL_timer.h"
31 #if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
35 #if defined(__WATCOMC__) && defined(__386__)
36 SDL_COMPILE_TIME_ASSERT(locksize, 4==sizeof(SDL_SpinLock));
37 extern _inline int _SDL_xchg_watcom(volatile int *a, int v);
38 #pragma aux _SDL_xchg_watcom = \
43 #endif /* __WATCOMC__ && __386__ */
45 /* This function is where all the magic happens... */
47 SDL_AtomicTryLock(SDL_SpinLock *lock)
49 #if SDL_ATOMIC_DISABLED
50 /* Terrible terrible damage */
51 static SDL_mutex *_spinlock_mutex;
53 if (!_spinlock_mutex) {
54 /* Race condition on first lock... */
55 _spinlock_mutex = SDL_CreateMutex();
57 SDL_LockMutex(_spinlock_mutex);
60 SDL_UnlockMutex(_spinlock_mutex);
63 SDL_UnlockMutex(_spinlock_mutex);
67 #elif defined(_MSC_VER)
68 SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
69 return (InterlockedExchange((long*)lock, 1) == 0);
71 #elif defined(__WATCOMC__) && defined(__386__)
72 return _SDL_xchg_watcom(lock, 1) == 0;
74 #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
75 return (__sync_lock_test_and_set(lock, 1) == 0);
77 #elif defined(__GNUC__) && defined(__arm__) && \
78 (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
79 defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
80 defined(__ARM_ARCH_5TEJ__))
82 __asm__ __volatile__ (
84 : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
87 #elif defined(__GNUC__) && defined(__arm__)
89 __asm__ __volatile__ (
90 "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
91 : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
94 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
97 "lock ; xchgl %0, (%1)\n"
98 : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
101 #elif defined(__MACOSX__) || defined(__IPHONEOS__)
102 /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
103 return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
105 #elif defined(__SOLARIS__) && defined(_LP64)
106 /* Used for Solaris with non-gcc compilers. */
107 return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
109 #elif defined(__SOLARIS__) && !defined(_LP64)
110 /* Used for Solaris with non-gcc compilers. */
111 return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0);
114 #error Please implement for your platform.
120 SDL_AtomicLock(SDL_SpinLock *lock)
122 /* FIXME: Should we have an eventual timeout? */
123 while (!SDL_AtomicTryLock(lock)) {
129 SDL_AtomicUnlock(SDL_SpinLock *lock)
131 #if defined(_MSC_VER)
135 #elif defined(__WATCOMC__) && defined(__386__)
136 SDL_CompilerBarrier ();
139 #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
140 __sync_lock_release(lock);
142 #elif defined(__SOLARIS__)
143 /* Used for Solaris when not using gcc. */
152 /* vi: set ts=4 sw=4 expandtab: */