Skip to content

Commit 42b5014

Browse files
committed
Engine - Audio - Properly close audio background thread
1 parent c8d14ed commit 42b5014

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

neo/sound/snd_local.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ class idSoundSystemLocal : public idSoundSystem
598598

599599
bool insideLevelLoad;
600600

601+
bool systemClosing;
602+
601603
idSysMutex mutex;
602604

603605
//-------------------------
@@ -614,6 +616,7 @@ class idSoundSystemLocal : public idSoundSystem
614616
private:
615617
bool initOnce;
616618
bool useXAudio;
619+
uintptr_t threadHandle;
617620
};
618621

619622
extern idSoundSystemLocal soundSystemLocal;

neo/sound/snd_system.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ void DefaultDeviceChangeThread(void* data) {
162162
static uint64 nextCheck = 0;
163163
const uint64 waitTime = 5000;
164164
while (1) {
165+
if (soundSystemLocal.systemClosing) {
166+
break;
167+
}
165168
int now = Sys_Milliseconds();
166169
if (now >= nextCheck) {
167170
#if defined(_MSC_VER) && defined(USE_XAUDIO2)
@@ -233,14 +236,15 @@ void idSoundSystemLocal::Init()
233236
idLib::Printf("Creating Default Device Detection Thread\n");
234237
#if defined(_MSC_VER) && defined(USE_XAUDIO2)
235238
if (useXAudio) {
236-
Sys_CreateThread((xthread_t)DefaultDeviceChangeThread, ((idSoundHardware_XAudio2*)hardware)->GetSelectedDevice(), THREAD_LOWEST, "Default Audio Device Change Listener", CORE_ANY);
239+
threadHandle = Sys_CreateThread((xthread_t)DefaultDeviceChangeThread, ((idSoundHardware_XAudio2*)hardware)->GetSelectedDevice(), THREAD_LOWEST, "Default Audio Device Change Listener", CORE_ANY);
237240
}
238241
else
239242
#endif
240243
{
241-
Sys_CreateThread((xthread_t)DefaultDeviceChangeThread, this->GetInternal(), THREAD_LOWEST, "Default Audio Device Change Listener", CORE_ANY);
244+
threadHandle = Sys_CreateThread((xthread_t)DefaultDeviceChangeThread, this->GetInternal(), THREAD_LOWEST, "Default Audio Device Change Listener", CORE_ANY);
242245
}
243246
initOnce = true;
247+
systemClosing = false;
244248
}
245249
}
246250

@@ -296,6 +300,8 @@ idSoundSystemLocal::Shutdown
296300
*/
297301
void idSoundSystemLocal::Shutdown()
298302
{
303+
systemClosing = true;
304+
Sys_DestroyThread(threadHandle);
299305
hardware->Shutdown();
300306
// EAX or not, the list needs to be cleared
301307
EFXDatabase.Clear();

0 commit comments

Comments
 (0)