From 0e4b877654eebf497c629880a23c7bd7df071373 Mon Sep 17 00:00:00 2001 From: scorpion-26 <58082714+scorpion-26@users.noreply.github.com> Date: Tue, 21 Feb 2023 21:19:22 +0100 Subject: [PATCH] PulseAudio: Make FlushLoop more robust When PA_CONTEXT_READY is called multiple times, PulseAudio::FlusLoop() would get stuck to to integer underflow. Now there are multiple things preventing that. Possible fix for https://github.com/scorpion-26/gBar/issues/7 --- src/PulseAudio.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/PulseAudio.h b/src/PulseAudio.h index 79af7ca..e34e6c8 100644 --- a/src/PulseAudio.h +++ b/src/PulseAudio.h @@ -11,14 +11,15 @@ namespace PulseAudio static pa_mainloop* mainLoop; static pa_context* context; - static uint32_t pending = 0; + static int32_t pending = 0; inline void FlushLoop() { - while (pending) + while (pending > 0) { pa_mainloop_iterate(mainLoop, 0, nullptr); } + pending = 0; } inline void Init() @@ -41,7 +42,11 @@ namespace PulseAudio case PA_CONTEXT_CONNECTING: // Don't care break; - case PA_CONTEXT_READY: pending--; break; + case PA_CONTEXT_READY: + // Prevent pending from going negative when PA_CONTEXT_READY is called multiple times + if (pending > 0) + pending--; + break; } };