Use a generic int type to handle enum swaps

This commit is contained in:
Chris Robinson
2011-08-29 21:30:12 -07:00
parent da081b81c4
commit 7408396fd4
5 changed files with 48 additions and 62 deletions
+3 -3
View File
@@ -982,7 +982,7 @@ ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
ALenum UpdateSources = AL_FALSE;
if(!DeferUpdates)
UpdateSources = Exchange_ALenum(&ctx->UpdateSources, AL_FALSE);
UpdateSources = ExchangeInt(&ctx->UpdateSources, AL_FALSE);
src = ctx->ActiveSources;
src_end = src + ctx->ActiveSourceCount;
@@ -995,7 +995,7 @@ ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
continue;
}
if(!DeferUpdates && (Exchange_ALenum(&(*src)->NeedsUpdate, AL_FALSE) ||
if(!DeferUpdates && (ExchangeInt(&(*src)->NeedsUpdate, AL_FALSE) ||
UpdateSources))
ALsource_Update(*src, ctx);
@@ -1019,7 +1019,7 @@ ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size)
ALEffectSlot->PendingClicks[i] = 0.0f;
}
if(!DeferUpdates && Exchange_ALenum(&ALEffectSlot->NeedsUpdate, AL_FALSE))
if(!DeferUpdates && ExchangeInt(&ALEffectSlot->NeedsUpdate, AL_FALSE))
ALEffect_Update(ALEffectSlot->EffectState, ctx, ALEffectSlot);
ALEffect_Process(ALEffectSlot->EffectState, ALEffectSlot,
+2 -2
View File
@@ -268,13 +268,13 @@ void SetRTPriority(void)
static void Lock(volatile ALenum *l)
{
while(Exchange_ALenum(l, AL_TRUE) == AL_TRUE)
while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
Sleep(0);
}
static void Unlock(volatile ALenum *l)
{
Exchange_ALenum(l, AL_FALSE);
ExchangeInt(l, AL_FALSE);
}
void ReadLock(RWLock *lock)
+36 -50
View File
@@ -231,14 +231,13 @@ static __inline RefCount IncrementRef(volatile RefCount *ptr)
static __inline RefCount DecrementRef(volatile RefCount *ptr)
{ return __sync_sub_and_fetch(ptr, 1); }
#define DECL_TEMPLATE(T) \
static __inline T Exchange_##T(volatile T *ptr, T newval) \
{ \
return __sync_lock_test_and_set(ptr, newval); \
} \
static __inline ALboolean CompExchange_##T(volatile T *ptr, T oldval, T newval)\
{ \
return __sync_bool_compare_and_swap(ptr, oldval, newval); \
static __inline int ExchangeInt(volatile int *ptr, int newval)
{
return __sync_lock_test_and_set(ptr, newval);
}
static __inline ALboolean CompExchangeInt(volatile int *ptr, int oldval, int newval)
{
return __sync_bool_compare_and_swap(ptr, oldval, newval);
}
#elif defined(_WIN32)
@@ -249,22 +248,23 @@ static __inline RefCount IncrementRef(volatile RefCount *ptr)
static __inline RefCount DecrementRef(volatile RefCount *ptr)
{ return InterlockedDecrement(ptr); }
#define DECL_TEMPLATE(T) \
static __inline T Exchange_##T(volatile T *ptr, T newval) \
{ \
union { \
volatile T *t; \
volatile LONG *l; \
} u = { ptr }; \
return InterlockedExchange(u.l, newval); \
} \
static __inline ALboolean CompExchange_##T(volatile T *ptr, T oldval, T newval)\
{ \
union { \
volatile T *t; \
volatile LONG *l; \
} u = { ptr }; \
return InterlockedCompareExchange(u.l, newval, oldval) == oldval; \
static LONG_size_does_not_match_int[(sizeof(LONG)==sizeof(int))?0:-1];
static __inline int ExchangeInt(volatile int *ptr, int newval)
{
union {
volatile int *i;
volatile LONG *l;
} u = { ptr };
return InterlockedExchange(u.l, newval);
}
static __inline ALboolean CompExchangeInt(volatile int *ptr, int oldval, int newval)
{
union {
volatile int *i;
volatile LONG *l;
} u = { ptr };
return InterlockedCompareExchange(u.l, newval, oldval) == oldval;
}
#elif defined(__APPLE__)
@@ -277,39 +277,25 @@ static __inline RefCount IncrementRef(volatile RefCount *ptr)
static __inline RefCount DecrementRef(volatile RefCount *ptr)
{ return OSAtomicDecrement32Barrier(ptr); }
#define DECL_TEMPLATE(T) \
static __inline T Exchange_##T(volatile T *ptr, T newval) \
{ \
union { \
volatile T *t; \
volatile int32_t *l; \
} u = { ptr }; \
/* Really? No regular old atomic swap? */ \
T oldval; \
do { \
oldval = *u.i; \
} while(!OSAtomicCompareAndSwap32Barrier(oldval, newval, u.i)); \
return oldval; \
} \
static __inline ALboolean CompExchange_##T(volatile T *ptr, T oldval, T newval)\
{ \
union { \
volatile T *t; \
volatile int32_t *i; \
} u = { ptr }; \
return OSAtomicCompareAndSwap32Barrier(oldval, newval, u.i); \
static __inline int ExchangeInt(volatile int *ptr, int newval)
{
/* Really? No regular old atomic swap? */
int oldval;
do {
oldval = *ptr;
} while(!OSAtomicCompareAndSwap32Barrier(oldval, newval, ptr));
return oldval;
}
static __inline ALboolean CompExchangeInt(volatile int *ptr, int oldval, int newval)
{
return OSAtomicCompareAndSwap32Barrier(oldval, newval, ptr);
}
#else
#error "No atomic functions available on this platform!"
typedef ALuint RefCount;
#define DECL_TEMPLATE(T)
#endif
DECL_TEMPLATE(ALenum)
#undef DECL_TEMPLATE
typedef struct {
volatile RefCount read_count;
+2 -2
View File
@@ -32,7 +32,7 @@ AL_API ALenum AL_APIENTRY alGetError(ALvoid)
Context = GetReffedContext();
if(!Context) return AL_INVALID_OPERATION;
errorCode = Exchange_ALenum(&Context->LastError, AL_NO_ERROR);
errorCode = ExchangeInt(&Context->LastError, AL_NO_ERROR);
ALCcontext_DecRef(Context);
@@ -41,5 +41,5 @@ AL_API ALenum AL_APIENTRY alGetError(ALvoid)
ALvoid alSetError(ALCcontext *Context, ALenum errorCode)
{
CompExchange_ALenum(&Context->LastError, AL_NO_ERROR, errorCode);
CompExchangeInt(&Context->LastError, AL_NO_ERROR, errorCode);
}
+5 -5
View File
@@ -591,7 +591,7 @@ AL_API ALvoid AL_APIENTRY alDeferUpdatesSOFT(void)
Context->DeferUpdates = AL_TRUE;
/* Make sure all pending updates are performed */
UpdateSources = Exchange_ALenum(&Context->UpdateSources, AL_FALSE);
UpdateSources = ExchangeInt(&Context->UpdateSources, AL_FALSE);
src = Context->ActiveSources;
src_end = src + Context->ActiveSourceCount;
@@ -604,7 +604,7 @@ AL_API ALvoid AL_APIENTRY alDeferUpdatesSOFT(void)
continue;
}
if(Exchange_ALenum(&(*src)->NeedsUpdate, AL_FALSE) || UpdateSources)
if(ExchangeInt(&(*src)->NeedsUpdate, AL_FALSE) || UpdateSources)
ALsource_Update(*src, Context);
src++;
@@ -614,7 +614,7 @@ AL_API ALvoid AL_APIENTRY alDeferUpdatesSOFT(void)
for(e = 0;e < Context->EffectSlotMap.size;e++)
{
ALEffectSlot = Context->EffectSlotMap.array[e].value;
if(Exchange_ALenum(&ALEffectSlot->NeedsUpdate, AL_FALSE))
if(ExchangeInt(&ALEffectSlot->NeedsUpdate, AL_FALSE))
ALEffect_Update(ALEffectSlot->EffectState, Context, ALEffectSlot);
}
UnlockUIntMapRead(&Context->EffectSlotMap);
@@ -631,7 +631,7 @@ AL_API ALvoid AL_APIENTRY alProcessUpdatesSOFT(void)
Context = GetReffedContext();
if(!Context) return;
if(Exchange_ALenum(&Context->DeferUpdates, AL_FALSE))
if(ExchangeInt(&Context->DeferUpdates, AL_FALSE))
{
ALsizei pos;
@@ -645,7 +645,7 @@ AL_API ALvoid AL_APIENTRY alProcessUpdatesSOFT(void)
if(Source->lOffset != -1)
ApplyOffset(Source);
new_state = Exchange_ALenum(&Source->new_state, AL_NONE);
new_state = ExchangeInt(&Source->new_state, AL_NONE);
if(new_state)
SetSourceState(Source, Context, new_state);
}