Use atomic flags for the thunk array

This commit is contained in:
Chris Robinson 2017-04-14 15:29:30 -07:00
parent 6476f3277a
commit 9e60eea93b

View File

@ -28,8 +28,8 @@
#include "almalloc.h"
static ATOMIC(ALenum) *ThunkArray;
static ALuint ThunkArraySize;
static ATOMIC_FLAG *ThunkArray;
static ALsizei ThunkArraySize;
static RWLock ThunkLock;
void ThunkInit(void)
@ -49,12 +49,12 @@ void ThunkExit(void)
ALenum NewThunkEntry(ALuint *index)
{
void *NewList;
ALuint i;
ALsizei i;
ReadLock(&ThunkLock);
for(i = 0;i < ThunkArraySize;i++)
{
if(ATOMIC_EXCHANGE(ALenum, &ThunkArray[i], AL_TRUE, almemory_order_acq_rel) == AL_FALSE)
if(!ATOMIC_FLAG_TEST_AND_SET(&ThunkArray[i], almemory_order_acq_rel))
{
ReadUnlock(&ThunkLock);
*index = i+1;
@ -69,7 +69,7 @@ ALenum NewThunkEntry(ALuint *index)
*/
for(;i < ThunkArraySize;i++)
{
if(ATOMIC_EXCHANGE(ALenum, &ThunkArray[i], AL_TRUE, almemory_order_acq_rel) == AL_FALSE)
if(!ATOMIC_FLAG_TEST_AND_SET(&ThunkArray[i], almemory_order_acq_rel))
{
WriteUnlock(&ThunkLock);
*index = i+1;
@ -89,17 +89,20 @@ ALenum NewThunkEntry(ALuint *index)
ThunkArray = NewList;
ThunkArraySize *= 2;
ATOMIC_STORE_SEQ(&ThunkArray[i], AL_TRUE);
ATOMIC_FLAG_TEST_AND_SET(&ThunkArray[i], almemory_order_seq_cst);
*index = ++i;
for(;i < ThunkArraySize;i++)
ATOMIC_FLAG_CLEAR(&ThunkArray[i], almemory_order_relaxed);
WriteUnlock(&ThunkLock);
*index = i+1;
return AL_NO_ERROR;
}
void FreeThunkEntry(ALuint index)
{
ReadLock(&ThunkLock);
if(index > 0 && index <= ThunkArraySize)
ATOMIC_STORE_SEQ(&ThunkArray[index-1], AL_FALSE);
if(index > 0 && (ALsizei)index <= ThunkArraySize)
ATOMIC_FLAG_CLEAR(&ThunkArray[index-1], almemory_order_release);
ReadUnlock(&ThunkLock);
}