fix possible race condition in OnBufferEndCallback

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