zenilib: zenilib/jni/external/sdl/src/atomic/SDL_spinlock.c Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_spinlock.c
Go to the documentation of this file.
1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
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.
8
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:
12
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.
20 */
21 #include "SDL_config.h"
22
23 #include "SDL_atomic.h"
24 #include "SDL_mutex.h"
25 #include "SDL_timer.h"
26
27 /* Don't do the check for Visual Studio 2005, it's safe here */
28 #ifdef __WIN32__
29 #include "../core/windows/SDL_windows.h"
30 #endif
31
32 /* This function is where all the magic happens... */
33 SDL_bool
34 SDL_AtomicTryLock(SDL_SpinLock *lock)
35 {
36 #if SDL_ATOMIC_DISABLED
37 /* Terrible terrible damage */
38 static SDL_mutex *_spinlock_mutex;
39
40 if (!_spinlock_mutex) {
41 /* Race condition on first lock... */
42 _spinlock_mutex = SDL_CreateMutex();
43 }
44 SDL_LockMutex(_spinlock_mutex);
45 if (*lock == 0) {
46 *lock = 1;
47 SDL_UnlockMutex(_spinlock_mutex);
48 return SDL_TRUE;
49 } else {
50 SDL_UnlockMutex(_spinlock_mutex);
51 return SDL_FALSE;
52 }
53
54 #elif defined(_MSC_VER)
55 SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
56 return (InterlockedExchange((long*)lock, 1) == 0);
57
58 #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
59 return (__sync_lock_test_and_set(lock, 1) == 0);
60
61 #elif defined(__GNUC__) && defined(__arm__) && \
62 (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
63 defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
64 defined(__ARM_ARCH_5TEJ__))
65 int result;
66 __asm__ __volatile__ (
67 "swp %0, %1, [%2]\n"
68 : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
69 return (result == 0);
70
71 #elif defined(__GNUC__) && defined(__arm__)
72 int result;
73 __asm__ __volatile__ (
74 "ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
75 : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
76 return (result == 0);
77
78 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
79 int result;
80 __asm__ __volatile__(
81 "lock ; xchgl %0, (%1)\n"
82 : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
83 return (result == 0);
84
85 #elif defined(__MACOSX__) || defined(__IPHONEOS__)
86 /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
87 return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
88
89 #elif HAVE_PTHREAD_SPINLOCK
90 /* pthread instructions */
91 return (pthread_spin_trylock(lock) == 0);
92
93 #else
94 #error Please implement for your platform.
95 return SDL_FALSE;
96 #endif
97 }
98
99 void
100 SDL_AtomicLock(SDL_SpinLock *lock)
101 {
102 /* FIXME: Should we have an eventual timeout? */
103 while (!SDL_AtomicTryLock(lock)) {
104 SDL_Delay(0);
105 }
106 }
107
108 void
109 SDL_AtomicUnlock(SDL_SpinLock *lock)
110 {
111 #if defined(_MSC_VER)
112 _ReadWriteBarrier();
113 *lock = 0;
114
115 #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
116 __sync_lock_release(lock);
117
118 #elif HAVE_PTHREAD_SPINLOCK
119 pthread_spin_unlock(lock);
120
121 #else
122 *lock = 0;
123 #endif
124 }
125
126 /* vi: set ts=4 sw=4 expandtab: */
SDL_AtomicUnlock
DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock)
Unlock a spin lock by setting it to 0. Always returns immediately.
Definition: SDL_spinlock.c:109
SDL_LockMutex
DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:73
SDL_AtomicTryLock
DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock)
Try to lock a spin lock by setting it to a non-zero value.
Definition: SDL_spinlock.c:34
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:116
SDL_COMPILE_TIME_ASSERT
#define SDL_COMPILE_TIME_ASSERT(name, x)
Definition: SDL_stdinc.h:159
SDL_TRUE
Definition: SDL_stdinc.h:119
SDL_CreateMutex
DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void)
Definition: SDL_sysmutex.c:38
SDL_FALSE
Definition: SDL_stdinc.h:118
SDL_config.h
SDL_UnlockMutex
DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:160
SDL_atomic.h
result
GLuint64EXT * result
Definition: glew.h:12708
SDL_AtomicLock
DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock)
Lock a spin lock by setting it to a non-zero value.
Definition: SDL_spinlock.c:100
SDL_Delay
DECLSPEC void SDLCALL SDL_Delay(Uint32 ms)
Wait a specified number of milliseconds before returning.
Definition: SDL_systimer.c:70
SDL_mutex
struct SDL_mutex SDL_mutex
Definition: SDL_mutex.h:59
lock
SDL_mutex * lock
Definition: SDL_events.c:75
SDL_SpinLock
int SDL_SpinLock
Definition: SDL_atomic.h:96
SDL_timer.h