From 6d0b02717187b0f3714520434115ae56ca079d37 Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Fri, 17 Jan 2025 19:41:12 -0500 Subject: [PATCH] lock pulseaudio mainloop while freeing sound devices --- src/monocoque/devices/sound/usb_generic_shaker.h | 3 ++- .../devices/sound/usb_generic_shaker_pulse.c | 4 +++- src/monocoque/devices/sounddevice.c | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/monocoque/devices/sound/usb_generic_shaker.h b/src/monocoque/devices/sound/usb_generic_shaker.h index 909ba9b..7ecfa3e 100644 --- a/src/monocoque/devices/sound/usb_generic_shaker.h +++ b/src/monocoque/devices/sound/usb_generic_shaker.h @@ -5,9 +5,10 @@ #ifdef USE_PULSEAUDIO int usb_generic_shaker_init(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop, pa_context* context, const char* devname, int volume, int pan, int channels, const char* streamname); +int usb_generic_shaker_free(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop); #else int usb_generic_shaker_init(SoundDevice* sounddevice); -#endif int usb_generic_shaker_free(SoundDevice* sounddevice); +#endif #endif diff --git a/src/monocoque/devices/sound/usb_generic_shaker_pulse.c b/src/monocoque/devices/sound/usb_generic_shaker_pulse.c index 72c3cd8..9df4056 100644 --- a/src/monocoque/devices/sound/usb_generic_shaker_pulse.c +++ b/src/monocoque/devices/sound/usb_generic_shaker_pulse.c @@ -94,8 +94,9 @@ void stream_state_cb(pa_stream *s, void *mainloop) { pa_threaded_mainloop_signal(mainloop, 0); } -int usb_generic_shaker_free(SoundDevice* sounddevice) +int usb_generic_shaker_free(SoundDevice* sounddevice, pa_threaded_mainloop* mainloop) { + pa_threaded_mainloop_lock(mainloop); int err = 0; if (sounddevice->stream) { @@ -104,6 +105,7 @@ int usb_generic_shaker_free(SoundDevice* sounddevice) // why is this wrong //pa_xfree(sounddevice->stream); } + pa_threaded_mainloop_unlock(mainloop); return err; } diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 48b3969..783f2d8 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -102,6 +102,21 @@ int sounddev_gearshift_update(SimDevice* this, SimData* simdata) gear_sound_set(sounddevice, simdata); } + +#ifdef USE_PULSEAUDIO + +int sounddev_free(SimDevice* this) +{ + SoundDevice* sounddevice = (void *) this->derived; + + usb_generic_shaker_free(sounddevice, mainloop); + + free(sounddevice); + + return 0; +} + +#else int sounddev_free(SimDevice* this) { SoundDevice* sounddevice = (void *) this->derived; @@ -112,6 +127,7 @@ int sounddev_free(SimDevice* this) return 0; } +#endif int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int frequency, int pan, int channels, double duration, double threshold) {