From d430174f0b4c02d5dfc2eb818ed79410a2e19b4b Mon Sep 17 00:00:00 2001 From: NBonaparte Date: Sat, 24 Mar 2018 00:37:01 -0700 Subject: [PATCH] fix(pulseaudio): add mutex for queue --- include/adapters/pulseaudio.hpp | 5 +++++ src/adapters/pulseaudio.cpp | 3 +++ 2 files changed, 8 insertions(+) diff --git a/include/adapters/pulseaudio.hpp b/include/adapters/pulseaudio.hpp index 1e76df92..4f5301a7 100644 --- a/include/adapters/pulseaudio.hpp +++ b/include/adapters/pulseaudio.hpp @@ -2,6 +2,7 @@ #include #include +#include #include "common.hpp" #include "settings.hpp" @@ -18,6 +19,8 @@ typedef struct pa_threaded_mainloop pa_threaded_mainloop; POLYBAR_NS class logger; +using mutex = std::mutex; + DEFINE_ERROR(pulseaudio_error); class pulseaudio { @@ -69,6 +72,8 @@ class pulseaudio { pa_threaded_mainloop* m_mainloop{nullptr}; queue m_events; + mutable mutex m_mutex; + // specified sink name string spec_s_name; diff --git a/src/adapters/pulseaudio.cpp b/src/adapters/pulseaudio.cpp index ed943ddb..2e3b6b3a 100644 --- a/src/adapters/pulseaudio.cpp +++ b/src/adapters/pulseaudio.cpp @@ -112,6 +112,8 @@ bool pulseaudio::wait() { * Process queued pulseaudio events */ int pulseaudio::process_events() { + std::lock_guard lock(m_mutex); + int ret = m_events.size(); pa_threaded_mainloop_lock(m_mainloop); pa_operation *o{nullptr}; @@ -245,6 +247,7 @@ void pulseaudio::subscribe_callback(pa_context *, pa_subscription_event_type_t t return; switch(t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) { case PA_SUBSCRIPTION_EVENT_SINK: + std::lock_guard lock(This->m_mutex); switch(t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) { case PA_SUBSCRIPTION_EVENT_NEW: This->m_events.emplace(evtype::NEW);