Use RAII to manage SDL_LockAudio state

This commit is contained in:
Kp 2023-05-13 15:02:55 +00:00
parent f2e65abbdb
commit e7d73e8493
4 changed files with 33 additions and 12 deletions

26
common/main/d_sdl_audio.h Normal file
View File

@ -0,0 +1,26 @@
/*
* Portions of this file are copyright Rebirth contributors and licensed as
* described in COPYING.txt.
* See COPYING.txt for license details.
*/
#pragma once
#include <SDL_audio.h>
namespace dcx {
struct RAII_SDL_LockAudio
{
RAII_SDL_LockAudio()
{
SDL_LockAudio();
}
~RAII_SDL_LockAudio()
{
SDL_UnlockAudio();
}
RAII_SDL_LockAudio(const RAII_SDL_LockAudio &) = delete;
RAII_SDL_LockAudio &operator=(const RAII_SDL_LockAudio &) = delete;
};
}

View File

@ -33,6 +33,7 @@
#endif
#include "config.h"
#include "d_sdl_audio.h"
#include "mvelib.h"
#include "mve_audio.h"
#include "byteutil.h"
@ -409,8 +410,9 @@ int MVESTREAM::handle_mve_segment_audioframedata(const mve_opcode major, const u
static const int selected_chan=1;
if (mve_audio_spec)
{
if (mve_audio_playing)
SDL_LockAudio();
std::optional<RAII_SDL_LockAudio> lock_audio{
mve_audio_playing ? std::optional<RAII_SDL_LockAudio>(std::in_place) : std::nullopt
};
const auto chan = get_ushort(data + 2);
unsigned nsamp = get_ushort(data + 4);
@ -500,9 +502,6 @@ int MVESTREAM::handle_mve_segment_audioframedata(const mve_opcode major, const u
if (mve_audio_buftail == mve_audio_bufhead)
con_printf(CON_CRITICAL, "d'oh! buffer ring overrun (%d)", mve_audio_bufhead);
}
if (mve_audio_playing)
SDL_UnlockAudio();
}
return 1;

View File

@ -26,6 +26,7 @@
#include "piggy.h"
#include "compiler-range_for.h"
#include "d_sdl_audio.h"
#include "d_underlying_value.h"
namespace dcx {
@ -147,7 +148,7 @@ static void audio_mixcallback(void *, Uint8 *stream, int len)
memset(stream, 0x80, len); // fix "static" sound bug on Mac OS X
SDL_LockAudio();
RAII_SDL_LockAudio lock_audio{};
range_for (auto &sl, SoundSlots)
{
@ -184,8 +185,6 @@ static void audio_mixcallback(void *, Uint8 *stream, int len)
sl.position = std::distance(sl.samples.begin(), sldata);
}
}
SDL_UnlockAudio();
}
}
@ -256,7 +255,7 @@ sound_channel digi_audio_start_sound(short soundnum, fix volume, sound_pan pan,
if (soundnum < 0)
return sound_channel::None;
SDL_LockAudio();
RAII_SDL_LockAudio lock_audio{};
const auto starting_channel = next_channel;
@ -271,7 +270,6 @@ sound_channel digi_audio_start_sound(short soundnum, fix volume, sound_pan pan,
next_channel = next(next_channel);
if (next_channel == starting_channel)
{
SDL_UnlockAudio();
return sound_channel::None;
}
}
@ -304,7 +302,6 @@ sound_channel digi_audio_start_sound(short soundnum, fix volume, sound_pan pan,
const auto i = next_channel;
next_channel = next(next_channel);
SDL_UnlockAudio();
return i;
}

View File

@ -21,7 +21,6 @@
#include <string.h>
#include <SDL.h>
#include <SDL_audio.h>
#include <SDL_mixer.h>
#include "pstypes.h"