zenilib: zenilib/jni/external/sdl/src/atomic/SDL_atomic.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_atomic.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
25 /* Note that we undefine the atomic operations here, in case they are
26 defined as compiler intrinsics while building SDL but the library user
27 doesn't have that compiler. That way we always have a working set of
28 atomic operations built into the library.
29 */
30 #undef SDL_AtomicCAS
31 #undef SDL_AtomicCASPtr
32
33 /*
34 If any of the operations are not provided then we must emulate some
35 of them. That means we need a nice implementation of spin locks
36 that avoids the "one big lock" problem. We use a vector of spin
37 locks and pick which one to use based on the address of the operand
38 of the function.
39
40 To generate the index of the lock we first shift by 3 bits to get
41 rid on the zero bits that result from 32 and 64 bit allignment of
42 data. We then mask off all but 5 bits and use those 5 bits as an
43 index into the table.
44
45 Picking the lock this way insures that accesses to the same data at
46 the same time will go to the same lock. OTOH, accesses to different
47 data have only a 1/32 chance of hitting the same lock. That should
48 pretty much eliminate the chances of several atomic operations on
49 different data from waiting on the same "big lock". If it isn't
50 then the table of locks can be expanded to a new size so long as
51 the new size is a power of two.
52
53 Contributed by Bob Pendleton, bob@pendleton.com
54 */
55
56 static SDL_SpinLock locks[32];
57
58 static __inline__ void
59 enterLock(void *a)
60 {
61 uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
62
63 SDL_AtomicLock(&locks[index]);
64 }
65
66 static __inline__ void
67 leaveLock(void *a)
68 {
69 uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
70
71 SDL_AtomicUnlock(&locks[index]);
72 }
73
74 DECLSPEC SDL_bool SDLCALL
75 SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
76 {
77 SDL_bool retval = SDL_FALSE;
78
79 enterLock(a);
80 if (a->value == oldval) {
81 a->value = newval;
82 retval = SDL_TRUE;
83 }
84 leaveLock(a);
85
86 return retval;
87 }
88
89 DECLSPEC SDL_bool SDLCALL
90 SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
91 {
92 SDL_bool retval = SDL_FALSE;
93
94 enterLock(a);
95 if (*a == oldval) {
96 *a = newval;
97 retval = SDL_TRUE;
98 }
99 leaveLock(a);
100
101 return retval;
102 }
103
104 #if defined(__GNUC__) && defined(__arm__) && \
105 (defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__))
106 __asm__(
107 " .align 2\n"
108 " .globl _SDL_MemoryBarrierRelease\n"
109 " .globl _SDL_MemoryBarrierAcquire\n"
110 "_SDL_MemoryBarrierRelease:\n"
111 "_SDL_MemoryBarrierAcquire:\n"
112 " mov r0, #0\n"
113 " mcr p15, 0, r0, c7, c10, 5\n"
114 " bx lr\n"
115 );
116 #endif /* __GNUC__ && __arm__ && ARMV6 */
117
118 /* vi: set ts=4 sw=4 expandtab: */
enterLock
static __inline__ void enterLock(void *a)
Definition: SDL_atomic.c:59
__inline__
#define __inline__
Definition: begin_code.h:119
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_atomic_t
A type representing an atomic integer value. It is a struct so people don&#39;t accidentally use numeric ...
Definition: SDL_atomic.h:233
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:116
SDL_TRUE
Definition: SDL_stdinc.h:119
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8736
DECLSPEC
#define DECLSPEC
Definition: begin_code.h:62
SDLCALL
#define SDLCALL
Definition: begin_code.h:72
SDL_FALSE
Definition: SDL_stdinc.h:118
SDL_config.h
SDL_atomic.h
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
leaveLock
static __inline__ void leaveLock(void *a)
Definition: SDL_atomic.c:67
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:233
index
GLuint index
Definition: glew.h:1800
uintptr_t
unsigned int uintptr_t
Definition: SDL_config_windows.h:69
SDL_AtomicCASPtr
DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
Set a pointer to a new value if it is currently an old value.
Definition: SDL_atomic.c:90
SDL_AtomicCAS
DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
Set an atomic variable to a new value if it is currently an old value.
Definition: SDL_atomic.c:75
locks
static SDL_SpinLock locks[32]
Definition: SDL_atomic.c:56
SDL_SpinLock
int SDL_SpinLock
Definition: SDL_atomic.h:96