From 5882d9546c4633fa539b81b1cf16e3b229995d34 Mon Sep 17 00:00:00 2001 From: terminx Date: Sun, 13 Jan 2019 23:26:50 +0000 Subject: [PATCH] Add mutex to protect against S_Callback() being called from more than one thread at the same time git-svn-id: https://svn.eduke32.com/eduke32@7310 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/duke3d/src/sounds.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/duke3d/src/sounds.cpp b/source/duke3d/src/sounds.cpp index d1dafa4d7..946f52600 100644 --- a/source/duke3d/src/sounds.cpp +++ b/source/duke3d/src/sounds.cpp @@ -38,6 +38,7 @@ static bool SoundPaused = false; std::atomic dnum; uint32_t dq[DQSIZE]; +static mutex_t m_callback; void S_SoundStartup(void) { @@ -78,6 +79,7 @@ void S_SoundStartup(void) S_MusicVolume(ud.config.MusicVolume); FX_SetReverseStereo(ud.config.ReverseStereo); + mutex_init(&m_callback); FX_SetCallBack(S_Callback); FX_SetPrintf(OSD_Printf); } @@ -915,14 +917,18 @@ void S_Update(void) } while (++sndnum <= highest); } +// S_Callback() can be called from either the audio thread when a sound ends, or the main thread +// when playing back a new sound needs an existing sound to be stopped first void S_Callback(uint32_t num) { if ((int32_t)num == MUSIC_ID) return; + mutex_lock(&m_callback); int const ldnum = dnum; dq[ldnum & (DQSIZE - 1)] = num; dnum++; + mutex_unlock(&m_callback); } void S_ClearSoundLocks(void)