fix possible race condition in OnBufferEndCallback

remotes/1734709060101541481/main
cosmonaut 2021-12-15 14:24:00 -08:00
parent 4a3d2e25d4
commit 3b39ed9d10
2 changed files with 13 additions and 2 deletions

Binary file not shown.

View File

@ -268,6 +268,8 @@ typedef struct FAudioGMS_Device
IdStack effectChainIndexStack;
double timestep;
SDL_mutex *destroyMutex;
} FAudioGMS_Device;
static FAudioGMS_Device *device = NULL;
@ -386,9 +388,11 @@ static void FAudioGMS_INTERNAL_OnBufferEndCallback(
if (instance->destroyOnFinish)
{
SDL_LockMutex(device->destroyMutex);
/* this is a callback so we don't want to immediately destroy and screw up data */
instance->destroyTimerActive = 1;
instance->destroyTimer = 0;
instance->destroyTimer = 10; /* a little wiggle room */
if (instance->voice.effectChainAttached)
{
@ -403,6 +407,8 @@ static void FAudioGMS_INTERNAL_OnBufferEndCallback(
}
}
}
SDL_UnlockMutex(device->destroyMutex);
}
}
}
@ -542,6 +548,8 @@ void FAudioGMS_Init(double spatialDistanceScale, double timestep)
device->timestep = timestep;
device->destroyMutex = SDL_CreateMutex();
Log("FAudio initialized successfully!");
}
@ -2115,6 +2123,7 @@ void FAudioGMS_Update()
FAudioGMS_INTERNAL_SoundInstance_SetVolume(instance, volume);
}
SDL_LockMutex(device->destroyMutex);
if (instance->destroyTimerActive)
{
instance->destroyTimer -= device->timestep;
@ -2124,6 +2133,7 @@ void FAudioGMS_Update()
FAudioGMS_INTERNAL_SoundInstance_Destroy(instance);
}
}
SDL_UnlockMutex(device->destroyMutex);
}
}
}
@ -2195,6 +2205,7 @@ void FAudioGMS_Destroy()
SDL_free(device->fauxMasteringVoice.sends.pSends);
FAudio_Release(device->handle);
SDL_DestroyMutex(device->destroyMutex);
SDL_free(device);
device = NULL;