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);