Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
109 lines (90 loc) · 2.9 KB

SDL_spinlock.c

File metadata and controls

109 lines (90 loc) · 2.9 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_stdinc.h"
#include "SDL_atomic.h"
Feb 8, 2011
Feb 8, 2011
25
#include "SDL_mutex.h"
26
27
#include "SDL_timer.h"
Jan 22, 2011
Jan 22, 2011
28
/* Don't do the check for Visual Studio 2005, it's safe here */
Jan 25, 2011
Jan 25, 2011
29
#ifdef __WIN32__
Jan 25, 2011
Jan 25, 2011
30
#include "../core/windows/SDL_windows.h"
Jan 25, 2011
Jan 25, 2011
31
#endif
32
33
34
35
36
/* This function is where all the magic happens... */
SDL_bool
SDL_AtomicTryLock(SDL_SpinLock *lock)
{
Feb 8, 2011
Feb 8, 2011
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#if SDL_ATOMIC_DISABLED
/* Terrible terrible damage */
static SDL_mutex *_spinlock_mutex;
if (!_spinlock_mutex) {
/* Race condition on first lock... */
_spinlock_mutex = SDL_CreateMutex();
}
SDL_mutexP(_spinlock_mutex);
if (*lock == 0) {
*lock = 1;
SDL_mutexV(_spinlock_mutex);
return SDL_TRUE;
} else {
SDL_mutexV(_spinlock_mutex);
return SDL_FALSE;
}
#elif defined(_MSC_VER)
Jan 17, 2011
Jan 17, 2011
56
SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
Jan 25, 2011
Jan 25, 2011
57
return (InterlockedExchange((long*)lock, 1) == 0);
Jan 26, 2011
Jan 26, 2011
59
#elif defined(__MACOSX__)
60
61
return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
Jan 26, 2011
Jan 26, 2011
62
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
Jan 16, 2011
Jan 16, 2011
63
64
return (__sync_lock_test_and_set(lock, 1) == 0);
Jan 26, 2011
Jan 26, 2011
65
#elif defined(__GNUC__) && defined(__arm__) && defined(__ARM_ARCH_5__)
66
67
68
69
70
int result;
__asm__ __volatile__ (
"swp %0, %1, [%2]\n"
: "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
return (result == 0);
Jan 16, 2011
Jan 16, 2011
71
Jan 26, 2011
Jan 26, 2011
72
#elif defined(__GNUC__) && defined(__arm__)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
int result;
__asm__ __volatile__ (
"ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
: "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
return (result == 0);
#else
/* Need CPU instructions for spinlock here! */
__need_spinlock_implementation__
#endif
}
void
SDL_AtomicLock(SDL_SpinLock *lock)
{
/* FIXME: Should we have an eventual timeout? */
while (!SDL_AtomicTryLock(lock)) {
SDL_Delay(0);
}
}
void
SDL_AtomicUnlock(SDL_SpinLock *lock)
{
Jan 26, 2011
Jan 26, 2011
97
98
99
100
101
102
103
104
#if defined(_MSC_VER)
_ReadWriteBarrier();
*lock = 0;
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
__sync_lock_release(lock);
#else
105
*lock = 0;
Jan 26, 2011
Jan 26, 2011
106
#endif
107
108
109
}
/* vi: set ts=4 sw=4 expandtab: */