refactor(alsa): Acquire mutex lock for api calls

This commit is contained in:
Michael Carlberg 2016-10-11 05:43:57 +02:00
parent 565a6d7032
commit 6037947b1d
2 changed files with 21 additions and 20 deletions

View File

@ -8,7 +8,7 @@
#include "common.hpp" #include "common.hpp"
#include "config.hpp" #include "config.hpp"
// #include "utils/threading.hpp" #include "utils/threading.hpp"
LEMONBUDDY_NS LEMONBUDDY_NS
@ -62,7 +62,7 @@ class alsa_ctl_interface {
} }
~alsa_ctl_interface() { ~alsa_ctl_interface() {
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
snd_ctl_close(m_ctl); snd_ctl_close(m_ctl);
snd_hctl_close(m_hctl); snd_hctl_close(m_hctl);
} }
@ -70,7 +70,7 @@ class alsa_ctl_interface {
bool wait(int timeout = -1) { bool wait(int timeout = -1) {
assert(m_ctl); assert(m_ctl);
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
int err = 0; int err = 0;
@ -91,13 +91,11 @@ class alsa_ctl_interface {
} }
bool test_device_plugged() { bool test_device_plugged() {
// std::lock_guard<threading_util::spin_lock> lck(m_lock);
// if (!wait(0))
// return false;
assert(m_elem); assert(m_elem);
assert(m_value); assert(m_value);
std::lock_guard<threading_util::spin_lock> guard(m_lock);
int err = 0; int err = 0;
if ((err = snd_hctl_elem_read(m_elem, m_value)) < 0) if ((err = snd_hctl_elem_read(m_elem, m_value)) < 0)
throw_exception<alsa_ctl_interface_error>("Could not read control value", err); throw_exception<alsa_ctl_interface_error>("Could not read control value", err);
@ -107,7 +105,7 @@ class alsa_ctl_interface {
void process_events() {} void process_events() {}
private: private:
// threading_util::spin_lock m_lock; threading_util::spin_lock m_lock;
snd_hctl_t* m_hctl = nullptr; snd_hctl_t* m_hctl = nullptr;
snd_hctl_elem_t* m_elem = nullptr; snd_hctl_elem_t* m_elem = nullptr;
@ -149,7 +147,7 @@ class alsa_mixer {
} }
~alsa_mixer() { ~alsa_mixer() {
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
snd_mixer_elem_remove(m_mixerelement); snd_mixer_elem_remove(m_mixerelement);
snd_mixer_detach(m_hardwaremixer, ALSA_SOUNDCARD); snd_mixer_detach(m_hardwaremixer, ALSA_SOUNDCARD);
snd_mixer_close(m_hardwaremixer); snd_mixer_close(m_hardwaremixer);
@ -158,17 +156,21 @@ class alsa_mixer {
bool wait(int timeout = -1) { bool wait(int timeout = -1) {
assert(m_hardwaremixer); assert(m_hardwaremixer);
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::unique_lock<threading_util::spin_lock> guard(m_lock);
int err = 0; int err = 0;
if ((err = snd_mixer_wait(m_hardwaremixer, timeout)) < 0) if ((err = snd_mixer_wait(m_hardwaremixer, timeout)) < 0)
throw_exception<alsa_mixer_error>("Failed to wait for events", err); throw_exception<alsa_mixer_error>("Failed to wait for events", err);
guard.unlock();
return process_events() > 0; return process_events() > 0;
} }
int process_events() { int process_events() {
std::lock_guard<threading_util::spin_lock> guard(m_lock);
int num_events = snd_mixer_handle_events(m_hardwaremixer); int num_events = snd_mixer_handle_events(m_hardwaremixer);
if (num_events < 0) if (num_events < 0)
@ -178,7 +180,7 @@ class alsa_mixer {
} }
int get_volume() { int get_volume() {
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
long chan_n = 0, vol_total = 0, vol, vol_min, vol_max; 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); snd_mixer_selem_get_playback_volume_range(m_mixerelement, &vol_min, &vol_max);
@ -198,7 +200,7 @@ class alsa_mixer {
if (is_muted()) if (is_muted())
return; return;
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
long vol_min, vol_max; long vol_min, vol_max;
@ -207,19 +209,19 @@ class alsa_mixer {
} }
void set_mute(bool mode) { void set_mute(bool mode) {
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
snd_mixer_selem_set_playback_switch_all(m_mixerelement, mode); snd_mixer_selem_set_playback_switch_all(m_mixerelement, mode);
} }
void toggle_mute() { void toggle_mute() {
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
int state; int state;
snd_mixer_selem_get_playback_switch(m_mixerelement, SND_MIXER_SCHN_FRONT_LEFT, &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); snd_mixer_selem_set_playback_switch_all(m_mixerelement, !state);
} }
bool is_muted() { bool is_muted() {
// std::lock_guard<threading_util::spin_lock> lck(m_lock); std::lock_guard<threading_util::spin_lock> guard(m_lock);
int state = 0; int state = 0;
for (int i = 0; i < SND_MIXER_SCHN_LAST; i++) { 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)) { 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; return false;
} }
protected:
void error_handler(string message) {}
private: private:
// threading_util::spin_lock m_lock; threading_util::spin_lock m_lock;
snd_mixer_t* m_hardwaremixer = nullptr; snd_mixer_t* m_hardwaremixer = nullptr;
snd_mixer_elem_t* m_mixerelement = nullptr; snd_mixer_elem_t* m_mixerelement = nullptr;

View File

@ -394,7 +394,7 @@ namespace modules {
CAST_MODULE(Impl)->broadcast(); CAST_MODULE(Impl)->broadcast();
while (CONST_CAST_MODULE(Impl).enabled()) { while (CONST_CAST_MODULE(Impl).enabled()) {
std::lock_guard<threading_util::spin_lock> lck(this->update_lock); std::unique_lock<threading_util::spin_lock> lck(this->update_lock);
if (!CAST_MODULE(Impl)->has_event()) if (!CAST_MODULE(Impl)->has_event())
continue; continue;
@ -403,6 +403,8 @@ namespace modules {
continue; continue;
CAST_MODULE(Impl)->broadcast(); CAST_MODULE(Impl)->broadcast();
lck.unlock();
CAST_MODULE(Impl)->idle(); CAST_MODULE(Impl)->idle();
} }
} }