From 6037947b1dcae74fe8d0208f0bcca734b1cd46f0 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 11 Oct 2016 05:43:57 +0200 Subject: [PATCH] refactor(alsa): Acquire mutex lock for api calls --- include/adapters/alsa.hpp | 37 ++++++++++++++++++------------------- include/modules/meta.hpp | 4 +++- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/include/adapters/alsa.hpp b/include/adapters/alsa.hpp index 0974c24c..b78ba09e 100644 --- a/include/adapters/alsa.hpp +++ b/include/adapters/alsa.hpp @@ -8,7 +8,7 @@ #include "common.hpp" #include "config.hpp" -// #include "utils/threading.hpp" +#include "utils/threading.hpp" LEMONBUDDY_NS @@ -62,7 +62,7 @@ class alsa_ctl_interface { } ~alsa_ctl_interface() { - // std::lock_guard lck(m_lock); + std::lock_guard guard(m_lock); snd_ctl_close(m_ctl); snd_hctl_close(m_hctl); } @@ -70,7 +70,7 @@ class alsa_ctl_interface { bool wait(int timeout = -1) { assert(m_ctl); - // std::lock_guard lck(m_lock); + std::lock_guard guard(m_lock); int err = 0; @@ -91,13 +91,11 @@ class alsa_ctl_interface { } bool test_device_plugged() { - // std::lock_guard lck(m_lock); - // if (!wait(0)) - // return false; - assert(m_elem); assert(m_value); + std::lock_guard guard(m_lock); + int err = 0; if ((err = snd_hctl_elem_read(m_elem, m_value)) < 0) throw_exception("Could not read control value", err); @@ -107,7 +105,7 @@ class alsa_ctl_interface { void process_events() {} private: - // threading_util::spin_lock m_lock; + threading_util::spin_lock m_lock; snd_hctl_t* m_hctl = nullptr; snd_hctl_elem_t* m_elem = nullptr; @@ -149,7 +147,7 @@ class alsa_mixer { } ~alsa_mixer() { - // std::lock_guard lck(m_lock); + std::lock_guard guard(m_lock); snd_mixer_elem_remove(m_mixerelement); snd_mixer_detach(m_hardwaremixer, ALSA_SOUNDCARD); snd_mixer_close(m_hardwaremixer); @@ -158,17 +156,21 @@ class alsa_mixer { bool wait(int timeout = -1) { assert(m_hardwaremixer); - // std::lock_guard lck(m_lock); + std::unique_lock guard(m_lock); int err = 0; if ((err = snd_mixer_wait(m_hardwaremixer, timeout)) < 0) throw_exception("Failed to wait for events", err); + guard.unlock(); + return process_events() > 0; } int process_events() { + std::lock_guard guard(m_lock); + int num_events = snd_mixer_handle_events(m_hardwaremixer); if (num_events < 0) @@ -178,7 +180,7 @@ class alsa_mixer { } int get_volume() { - // std::lock_guard lck(m_lock); + std::lock_guard guard(m_lock); long chan_n = 0, vol_total = 0, vol, vol_min, vol_max; snd_mixer_selem_get_playback_volume_range(m_mixerelement, &vol_min, &vol_max); @@ -198,7 +200,7 @@ class alsa_mixer { if (is_muted()) return; - // std::lock_guard lck(m_lock); + std::lock_guard guard(m_lock); long vol_min, vol_max; @@ -207,19 +209,19 @@ class alsa_mixer { } void set_mute(bool mode) { - // std::lock_guard lck(m_lock); + std::lock_guard guard(m_lock); snd_mixer_selem_set_playback_switch_all(m_mixerelement, mode); } void toggle_mute() { - // std::lock_guard lck(m_lock); + std::lock_guard guard(m_lock); int state; snd_mixer_selem_get_playback_switch(m_mixerelement, SND_MIXER_SCHN_FRONT_LEFT, &state); snd_mixer_selem_set_playback_switch_all(m_mixerelement, !state); } bool is_muted() { - // std::lock_guard lck(m_lock); + std::lock_guard guard(m_lock); int state = 0; for (int i = 0; i < SND_MIXER_SCHN_LAST; i++) { if (snd_mixer_selem_has_playback_channel(m_mixerelement, (snd_mixer_selem_channel_id_t)i)) { @@ -232,11 +234,8 @@ class alsa_mixer { return false; } - protected: - void error_handler(string message) {} - private: - // threading_util::spin_lock m_lock; + threading_util::spin_lock m_lock; snd_mixer_t* m_hardwaremixer = nullptr; snd_mixer_elem_t* m_mixerelement = nullptr; diff --git a/include/modules/meta.hpp b/include/modules/meta.hpp index 7e45265b..97353c21 100644 --- a/include/modules/meta.hpp +++ b/include/modules/meta.hpp @@ -394,7 +394,7 @@ namespace modules { CAST_MODULE(Impl)->broadcast(); while (CONST_CAST_MODULE(Impl).enabled()) { - std::lock_guard lck(this->update_lock); + std::unique_lock lck(this->update_lock); if (!CAST_MODULE(Impl)->has_event()) continue; @@ -403,6 +403,8 @@ namespace modules { continue; CAST_MODULE(Impl)->broadcast(); + + lck.unlock(); CAST_MODULE(Impl)->idle(); } }