From d859eb23191d7c5fa15b1591c35afba04684412c Mon Sep 17 00:00:00 2001 From: Justin Marshall Date: Sat, 25 Apr 2026 10:07:29 -0700 Subject: [PATCH] Sound system is now in another thread. --- neo/framework/Common.cpp | 2 -- neo/sound/snd_system.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/neo/framework/Common.cpp b/neo/framework/Common.cpp index 46320e79..a21f4969 100644 --- a/neo/framework/Common.cpp +++ b/neo/framework/Common.cpp @@ -2576,8 +2576,6 @@ void idCommonLocal::Frame(void) { idAsyncNetwork::RunFrame(); - soundSystem->AsyncUpdate(Sys_Milliseconds()); - if (idAsyncNetwork::IsActive()) { if (idAsyncNetwork::serverDedicated.GetInteger() != 1) { session->GuiFrameEvents(); diff --git a/neo/sound/snd_system.cpp b/neo/sound/snd_system.cpp index 6e65f324..fd1420c8 100644 --- a/neo/sound/snd_system.cpp +++ b/neo/sound/snd_system.cpp @@ -32,6 +32,9 @@ If you have questions concerning this license or the applicable additional terms #include "snd_local.h" #include +#include +#include +#include #ifdef ID_DEDICATED idCVar idSoundSystemLocal::s_noSound( "s_noSound", "1", CVAR_SOUND | CVAR_BOOL | CVAR_ROM, "" ); @@ -100,6 +103,34 @@ DEFINE_GUID(EAXPROPERTYID_EAX40_Source, DEFINE_GUID(EAXPROPERTYID_EAX40_FXSlot0, 0xA8FA6882, 0x9FDA, 0x11D2, 0xBA, 0xA0, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96); +static std::thread g_soundAsyncThread; +static std::atomic g_soundAsyncRunning{ false }; + +void StartSoundAsyncThread() { + if (g_soundAsyncRunning.exchange(true)) { + return; // already running + } + + g_soundAsyncThread = std::thread([]() { + while (g_soundAsyncRunning.load()) { + soundSystem->AsyncUpdate(Sys_Milliseconds()); + + // Prevent burning a full CPU core. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + }); +} + +void StopSoundAsyncThread() { + if (!g_soundAsyncRunning.exchange(false)) { + return; // already stopped + } + + if (g_soundAsyncThread.joinable()) { + g_soundAsyncThread.join(); + } +} + /* =============== SoundReloadSounds_f @@ -427,6 +458,8 @@ void idSoundSystemLocal::Init() { useOpenAL = idSoundSystemLocal::s_useOpenAL.GetBool(); useEAXReverb = idSoundSystemLocal::s_useEAXReverb.GetBool(); + StartSoundAsyncThread(); + cmdSystem->AddCommand( "listSounds", ListSounds_f, CMD_FL_SOUND, "lists all sounds" ); cmdSystem->AddCommand( "listSoundDecoders", ListSoundDecoders_f, CMD_FL_SOUND, "list active sound decoders" ); cmdSystem->AddCommand( "reloadSounds", SoundReloadSounds_f, CMD_FL_SOUND|CMD_FL_CHEAT, "reloads all sounds" ); @@ -443,6 +476,8 @@ idSoundSystemLocal::Shutdown =============== */ void idSoundSystemLocal::Shutdown() { + StopSoundAsyncThread(); + ShutdownHW(); // EAX or not, the list needs to be cleared