Replace sound callback mutex with atomic operation

git-svn-id: https://svn.eduke32.com/eduke32@6839 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-04-21 06:04:56 +00:00
parent d49300eeff
commit daa033d56a

View file

@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "duke3d.h" #include "duke3d.h"
#include "renderlayer.h" // for win_gethwnd() #include "renderlayer.h" // for win_gethwnd()
#include <atomic>
#define DQSIZE 128 #define DQSIZE 128
@ -35,8 +36,8 @@ static int32_t MusicVoice = -1;
static int32_t MusicPaused = 0; static int32_t MusicPaused = 0;
static int32_t SoundPaused = 0; static int32_t SoundPaused = 0;
static mutex_t s_mutex; std::atomic<uint32_t> dnum;
static volatile uint32_t dq[DQSIZE], dnum = 0; uint32_t dq[DQSIZE];
void S_SoundStartup(void) void S_SoundStartup(void)
{ {
@ -79,7 +80,6 @@ void S_SoundStartup(void)
FX_SetReverseStereo(ud.config.ReverseStereo); FX_SetReverseStereo(ud.config.ReverseStereo);
FX_SetCallBack(S_Callback); FX_SetCallBack(S_Callback);
FX_SetPrintf(initprintf); FX_SetPrintf(initprintf);
mutex_init(&s_mutex);
} }
void S_SoundShutdown(void) void S_SoundShutdown(void)
@ -379,31 +379,11 @@ void S_StopMusic(void)
void S_Cleanup(void) void S_Cleanup(void)
{ {
static uint32_t ldq[DQSIZE]; static uint32_t ldnum;
// process from our own local copy of the delete queue so we don't hold the lock long while (ldnum < dnum)
mutex_lock(&s_mutex);
uint32_t ldnum = dnum;
if (!ldnum)
{ {
mutex_unlock(&s_mutex); uint32_t num = dq[ldnum++ & (DQSIZE - 1)];
return;
}
dnum = 0;
for (uint32_t i = 0; i < ldnum; i++)
ldq[i] = dq[i];
mutex_unlock(&s_mutex);
ldnum--;
do
{
uint32_t num = ldq[ldnum];
// negative index is RTS playback // negative index is RTS playback
if ((int32_t)num < 0) if ((int32_t)num < 0)
@ -445,7 +425,6 @@ void S_Cleanup(void)
g_soundlocks[num]--; g_soundlocks[num]--;
} }
while (ldnum--);
} }
// returns number of bytes read // returns number of bytes read
@ -517,9 +496,8 @@ static int32_t S_TakeSlot(int32_t num)
if (FX_SoundActive(g_sounds[num].instances[i].voice)) if (FX_SoundActive(g_sounds[num].instances[i].voice))
FX_StopSound(g_sounds[num].instances[i].voice); FX_StopSound(g_sounds[num].instances[i].voice);
mutex_lock(&s_mutex); dq[dnum & (DQSIZE - 1)] = (num * MAXSOUNDINSTANCES) + i;
dq[dnum++] = (num * MAXSOUNDINSTANCES) + i; dnum++;
mutex_unlock(&s_mutex);
S_Cleanup(); S_Cleanup();
return i; return i;
@ -947,9 +925,8 @@ void S_Callback(uint32_t num)
if ((int32_t)num == MUSIC_ID) if ((int32_t)num == MUSIC_ID)
return; return;
mutex_lock(&s_mutex); dq[dnum & (DQSIZE - 1)] = num;
dq[dnum++] = num; dnum++;
mutex_unlock(&s_mutex);
} }
void S_ClearSoundLocks(void) void S_ClearSoundLocks(void)