2011-01-15 12:41:59 -08:00
|
|
|
/*
|
2011-04-08 13:03:26 -07:00
|
|
|
Simple DirectMedia Layer
|
2011-12-31 09:28:07 -05:00
|
|
|
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
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.
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
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:
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-04-08 13:03:26 -07:00
|
|
|
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.
|
2011-01-15 12:41:59 -08:00
|
|
|
*/
|
2012-01-14 13:21:19 -05:00
|
|
|
#include "SDL_config.h"
|
2011-01-15 12:41:59 -08:00
|
|
|
|
|
|
|
#include "SDL_atomic.h"
|
|
|
|
|
2011-01-16 15:16:39 -08:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 12:41:59 -08:00
|
|
|
/*
|
|
|
|
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)
|
|
|
|
{
|
2011-01-16 15:16:39 -08:00
|
|
|
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-01-16 15:16:39 -08:00
|
|
|
SDL_AtomicLock(&locks[index]);
|
2011-01-15 12:41:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void
|
|
|
|
leaveLock(void *a)
|
|
|
|
{
|
2011-01-16 15:16:39 -08:00
|
|
|
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
|
2011-01-15 12:41:59 -08:00
|
|
|
|
2011-01-16 15:16:39 -08:00
|
|
|
SDL_AtomicUnlock(&locks[index]);
|
2011-01-15 12:41:59 -08:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:16:39 -08:00
|
|
|
SDL_bool
|
2011-01-25 17:40:06 -08:00
|
|
|
SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval)
|
2011-01-15 12:41:59 -08:00
|
|
|
{
|
2011-01-16 15:16:39 -08:00
|
|
|
SDL_bool retval = SDL_FALSE;
|
2011-01-15 12:41:59 -08:00
|
|
|
|
|
|
|
enterLock(a);
|
2011-01-16 15:16:39 -08:00
|
|
|
if (a->value == oldval) {
|
2011-01-15 12:41:59 -08:00
|
|
|
a->value = newval;
|
2011-01-16 15:16:39 -08:00
|
|
|
retval = SDL_TRUE;
|
2011-01-15 12:41:59 -08:00
|
|
|
}
|
|
|
|
leaveLock(a);
|
|
|
|
|
2011-01-16 15:16:39 -08:00
|
|
|
return retval;
|
2011-01-15 12:41:59 -08:00
|
|
|
}
|
|
|
|
|
2011-01-25 17:40:06 -08:00
|
|
|
SDL_bool
|
|
|
|
SDL_AtomicCASPtr_(void **a, void *oldval, void *newval)
|
2011-01-15 12:41:59 -08:00
|
|
|
{
|
2011-01-16 15:16:39 -08:00
|
|
|
SDL_bool retval = SDL_FALSE;
|
2011-01-15 12:41:59 -08:00
|
|
|
|
|
|
|
enterLock(a);
|
|
|
|
if (*a == oldval) {
|
|
|
|
*a = newval;
|
2011-01-16 15:16:39 -08:00
|
|
|
retval = SDL_TRUE;
|
2011-01-15 12:41:59 -08:00
|
|
|
}
|
|
|
|
leaveLock(a);
|
|
|
|
|
2011-01-16 15:16:39 -08:00
|
|
|
return retval;
|
2011-01-15 12:41:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|