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

Latest commit

 

History

History
102 lines (81 loc) · 3.04 KB

SDL_atomic.c

File metadata and controls

102 lines (81 loc) · 3.04 KB
 
Apr 8, 2011
Apr 8, 2011
2
3
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
Apr 8, 2011
Apr 8, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Apr 8, 2011
Apr 8, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
Apr 8, 2011
Apr 8, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
22
23
24
*/
#include "SDL_stdinc.h"
#include "SDL_atomic.h"
Jan 16, 2011
Jan 16, 2011
25
26
27
28
29
30
/* Note that we undefine the atomic operations here, in case they are
defined as compiler intrinsics while building SDL but the library user
doesn't have that compiler. That way we always have a working set of
atomic operations built into the library.
*/
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
If any of the operations are not provided then we must emulate some
of them. That means we need a nice implementation of spin locks
that avoids the "one big lock" problem. We use a vector of spin
locks and pick which one to use based on the address of the operand
of the function.
To generate the index of the lock we first shift by 3 bits to get
rid on the zero bits that result from 32 and 64 bit allignment of
data. We then mask off all but 5 bits and use those 5 bits as an
index into the table.
Picking the lock this way insures that accesses to the same data at
the same time will go to the same lock. OTOH, accesses to different
data have only a 1/32 chance of hitting the same lock. That should
pretty much eliminate the chances of several atomic operations on
different data from waiting on the same "big lock". If it isn't
then the table of locks can be expanded to a new size so long as
the new size is a power of two.
Contributed by Bob Pendleton, bob@pendleton.com
*/
static SDL_SpinLock locks[32];
static __inline__ void
enterLock(void *a)
{
Jan 16, 2011
Jan 16, 2011
59
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
Jan 16, 2011
Jan 16, 2011
61
SDL_AtomicLock(&locks[index]);
62
63
64
65
66
}
static __inline__ void
leaveLock(void *a)
{
Jan 16, 2011
Jan 16, 2011
67
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
Jan 16, 2011
Jan 16, 2011
69
SDL_AtomicUnlock(&locks[index]);
70
71
}
Jan 16, 2011
Jan 16, 2011
72
SDL_bool
Jan 26, 2011
Jan 26, 2011
73
SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval)
Jan 16, 2011
Jan 16, 2011
75
SDL_bool retval = SDL_FALSE;
76
77
enterLock(a);
Jan 16, 2011
Jan 16, 2011
78
if (a->value == oldval) {
79
a->value = newval;
Jan 16, 2011
Jan 16, 2011
80
retval = SDL_TRUE;
81
82
83
}
leaveLock(a);
Jan 16, 2011
Jan 16, 2011
84
return retval;
85
86
}
Jan 26, 2011
Jan 26, 2011
87
88
SDL_bool
SDL_AtomicCASPtr_(void **a, void *oldval, void *newval)
Jan 16, 2011
Jan 16, 2011
90
SDL_bool retval = SDL_FALSE;
91
92
93
94
enterLock(a);
if (*a == oldval) {
*a = newval;
Jan 16, 2011
Jan 16, 2011
95
retval = SDL_TRUE;
96
97
98
}
leaveLock(a);
Jan 16, 2011
Jan 16, 2011
99
return retval;
100
101
102
}
/* vi: set ts=4 sw=4 expandtab: */