mirror of
https://github.com/libretro/RetroArch
synced 2025-01-29 18:32:44 +00:00
Add missing locks in audio_mixer.c (#13858)
This commit is contained in:
parent
141bba0464
commit
eaf98919a9
@ -1153,6 +1153,8 @@ ifeq ($(HAVE_THREADS), 1)
|
||||
ifeq ($(findstring Haiku,$(OS)),)
|
||||
LIBS += $(THREADS_LIBS)
|
||||
endif
|
||||
else
|
||||
OBJ += $(LIBRETRO_COMM_DIR)/rthreads/single_threaded.o
|
||||
endif
|
||||
|
||||
ifeq ($(HAVE_THREAD_STORAGE), 1)
|
||||
|
@ -1321,6 +1321,8 @@ THREAD
|
||||
#include "../libretro-common/rthreads/rthreads.c"
|
||||
#include "../gfx/video_thread_wrapper.c"
|
||||
#include "../audio/audio_thread_wrapper.c"
|
||||
#else
|
||||
#include "../libretro-common/rthreads/single_threaded.c"
|
||||
#endif
|
||||
|
||||
/* needed for playlists, netplay lobbies and achievements */
|
||||
|
@ -26,6 +26,7 @@
|
||||
|
||||
#include <audio/audio_mixer.h>
|
||||
#include <audio/audio_resampler.h>
|
||||
#include <rthreads/rthreads.h>
|
||||
|
||||
#ifdef HAVE_RWAV
|
||||
#include <formats/rwav.h>
|
||||
@ -183,6 +184,7 @@ struct audio_mixer_voice
|
||||
unsigned type;
|
||||
float volume;
|
||||
bool repeat;
|
||||
slock_t *lock;
|
||||
};
|
||||
|
||||
/* TODO/FIXME - static globals */
|
||||
@ -311,16 +313,21 @@ void audio_mixer_init(unsigned rate)
|
||||
|
||||
s_rate = rate;
|
||||
|
||||
for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++)
|
||||
for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++) {
|
||||
s_voices[i].type = AUDIO_MIXER_TYPE_NONE;
|
||||
s_voices[i].lock = slock_new();
|
||||
}
|
||||
}
|
||||
|
||||
void audio_mixer_done(void)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++)
|
||||
for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++) {
|
||||
slock_lock(s_voices[i].lock);
|
||||
audio_mixer_release(&s_voices[i]);
|
||||
slock_unlock(s_voices[i].lock);
|
||||
}
|
||||
}
|
||||
|
||||
audio_mixer_sound_t* audio_mixer_load_wav(void *buffer, int32_t size,
|
||||
@ -837,6 +844,14 @@ audio_mixer_voice_t* audio_mixer_play(audio_mixer_sound_t* sound,
|
||||
if (voice->type != AUDIO_MIXER_TYPE_NONE)
|
||||
continue;
|
||||
|
||||
slock_lock(voice->lock);
|
||||
|
||||
if (voice->type != AUDIO_MIXER_TYPE_NONE)
|
||||
{
|
||||
slock_unlock(voice->lock);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* claim the voice, also helps with cleanup on error */
|
||||
voice->type = sound->type;
|
||||
|
||||
@ -881,16 +896,22 @@ audio_mixer_voice_t* audio_mixer_play(audio_mixer_sound_t* sound,
|
||||
voice->volume = volume;
|
||||
voice->sound = sound;
|
||||
voice->stop_cb = stop_cb;
|
||||
slock_unlock(voice->lock);
|
||||
}
|
||||
else
|
||||
{
|
||||
audio_mixer_release(voice);
|
||||
if(i < AUDIO_MIXER_MAX_VOICES)
|
||||
{
|
||||
audio_mixer_release(voice);
|
||||
slock_unlock(voice->lock);
|
||||
}
|
||||
voice = NULL;
|
||||
}
|
||||
|
||||
return voice;
|
||||
}
|
||||
|
||||
/* Need to hold lock for voice. */
|
||||
static void audio_mixer_release(audio_mixer_voice_t* voice)
|
||||
{
|
||||
if (!voice)
|
||||
@ -933,11 +954,14 @@ void audio_mixer_stop(audio_mixer_voice_t* voice)
|
||||
|
||||
if (voice)
|
||||
{
|
||||
slock_lock(voice->lock);
|
||||
stop_cb = voice->stop_cb;
|
||||
sound = voice->sound;
|
||||
|
||||
audio_mixer_release(voice);
|
||||
|
||||
slock_unlock(voice->lock);
|
||||
|
||||
if (stop_cb)
|
||||
stop_cb(sound, AUDIO_MIXER_SOUND_STOPPED);
|
||||
}
|
||||
@ -998,6 +1022,9 @@ static void audio_mixer_mix_ogg(float* buffer, size_t num_frames,
|
||||
unsigned temp_samples = 0;
|
||||
float* pcm = NULL;
|
||||
|
||||
if (!voice->types.ogg.stream)
|
||||
return;
|
||||
|
||||
if (voice->types.ogg.position == voice->types.ogg.samples)
|
||||
{
|
||||
again:
|
||||
@ -1284,7 +1311,11 @@ void audio_mixer_mix(float* buffer, size_t num_frames,
|
||||
|
||||
for (i = 0; i < AUDIO_MIXER_MAX_VOICES; i++, voice++)
|
||||
{
|
||||
float volume = (override) ? volume_override : voice->volume;
|
||||
float volume;
|
||||
|
||||
slock_lock(voice->lock);
|
||||
|
||||
volume = (override) ? volume_override : voice->volume;
|
||||
|
||||
switch (voice->type)
|
||||
{
|
||||
@ -1314,6 +1345,8 @@ void audio_mixer_mix(float* buffer, size_t num_frames,
|
||||
case AUDIO_MIXER_TYPE_NONE:
|
||||
break;
|
||||
}
|
||||
|
||||
slock_unlock(voice->lock);
|
||||
}
|
||||
|
||||
for (j = 0, sample = buffer; j < num_frames * 2; j++, sample++)
|
||||
@ -1338,5 +1371,7 @@ void audio_mixer_voice_set_volume(audio_mixer_voice_t *voice, float val)
|
||||
if (!voice)
|
||||
return;
|
||||
|
||||
slock_lock(voice->lock);
|
||||
voice->volume = val;
|
||||
slock_unlock(voice->lock);
|
||||
}
|
||||
|
24
libretro-common/rthreads/single_threaded.c
Normal file
24
libretro-common/rthreads/single_threaded.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <rthreads/rthreads.h>
|
||||
|
||||
uintptr_t sthread_get_thread_id(sthread_t *thread)
|
||||
{
|
||||
return thread ? 1 : 0;
|
||||
}
|
||||
|
||||
void slock_unlock(slock_t *lock)
|
||||
{
|
||||
}
|
||||
|
||||
void slock_lock(slock_t *lock)
|
||||
{
|
||||
}
|
||||
|
||||
void slock_free(slock_t *lock)
|
||||
{
|
||||
}
|
||||
|
||||
slock_t *slock_new(void)
|
||||
{
|
||||
static uintptr_t ctr = 1;
|
||||
return (slock_t*)ctr++;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user