Skip to content

Commit

Permalink
Use unique_lock to guarantee all exit paths unlock the mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Feb 4, 2024
1 parent 465a0e1 commit 0cb53ae
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions SurrealEngine/Audio/AudioDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,9 @@ class OpenALAudioDevice : public AudioDevice

void PlayMusic(std::unique_ptr<AudioSource> source)
{
playbackMutex.lock();
std::unique_lock lock(playbackMutex);
music = std::move(source);
musicUpdate = true;
playbackMutex.unlock();
}

int PlaySound(int channel, USound* sound, vec3& location, float volume, float radius, float pitch)
Expand Down Expand Up @@ -393,13 +392,13 @@ class OpenALAudioDevice : public AudioDevice

void PlayMusicBuffer() override
{
playbackMutex.lock();
std::unique_lock lock(playbackMutex);
if (!music)
return;

int format = (music->GetChannels() == 1) ? AL_FORMAT_MONO_FLOAT32 : AL_FORMAT_STEREO_FLOAT32;
int freq = music->GetFrequency();
playbackMutex.unlock();
lock.unlock();

ALenum error;
for (int i = 0; i < musicBufferCount; i++)
Expand Down Expand Up @@ -428,14 +427,14 @@ class OpenALAudioDevice : public AudioDevice
ALuint buffer;
alSourceUnqueueBuffers(alMusicSource, 1, &buffer);

playbackMutex.lock();
std::unique_lock lock(playbackMutex);

if (!music)
return;

int format = (music->GetChannels() == 1) ? AL_FORMAT_MONO_FLOAT32 : AL_FORMAT_STEREO_FLOAT32;
int freq = music->GetFrequency();
playbackMutex.unlock();
lock.unlock();

alBufferData(buffer, format, musicQueue.Pop(), musicBufferSize*4, freq);
if (alGetError() != AL_NO_ERROR)
Expand Down Expand Up @@ -490,7 +489,7 @@ class OpenALAudioDevice : public AudioDevice
{
while (!bExit)
{
playbackMutex.lock();
std::unique_lock lock(playbackMutex);
if (musicUpdate)
{
musicUpdate = false;
Expand All @@ -510,7 +509,7 @@ class OpenALAudioDevice : public AudioDevice
music->ReadSamples(musicQueue.GetNextFree(), musicBufferSize);
musicQueue.Push(musicQueue.GetNextFree());
}
playbackMutex.unlock();
lock.unlock();

if (!bMusicPlaying)
{
Expand All @@ -526,7 +525,7 @@ class OpenALAudioDevice : public AudioDevice
}
else
{
playbackMutex.unlock();
lock.unlock();
bMusicPlaying = false;
}
using namespace std::chrono_literals;
Expand Down

0 comments on commit 0cb53ae

Please sign in to comment.