From d7212624b099b9f4e39b39ac9189cdb5613ef5b3 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 29 Jan 2025 12:54:42 +0100 Subject: [PATCH] input/{async,thread}: clear the CircularBuffer when it becomes empty First step towards fixing https://github.com/MusicPlayerDaemon/MPD/issues/2186 --- src/input/AsyncInputStream.cxx | 6 ++++++ src/input/ThreadInputStream.cxx | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/input/AsyncInputStream.cxx b/src/input/AsyncInputStream.cxx index ec59680df..da41d3989 100644 --- a/src/input/AsyncInputStream.cxx +++ b/src/input/AsyncInputStream.cxx @@ -200,6 +200,12 @@ AsyncInputStream::Read(std::unique_lock &lock, memcpy(ptr, r.data, nbytes); buffer.Consume(nbytes); + if (buffer.empty()) + /* when the buffer becomes empty, reset its head and + tail so the next write can fill the whole buffer + and not just the part after the tail */ + buffer.Clear(); + offset += (offset_type)nbytes; if (paused && buffer.GetSize() < resume_at) diff --git a/src/input/ThreadInputStream.cxx b/src/input/ThreadInputStream.cxx index de9bae5ae..382ec8fdc 100644 --- a/src/input/ThreadInputStream.cxx +++ b/src/input/ThreadInputStream.cxx @@ -146,6 +146,14 @@ ThreadInputStream::Read(std::unique_lock &lock, size_t nbytes = std::min(read_size, r.size); memcpy(ptr, r.data, nbytes); buffer.Consume(nbytes); + + if (buffer.empty()) + /* when the buffer becomes empty, + reset its head and tail so the next + write can fill the whole buffer and + not just the part after the tail */ + buffer.Clear(); + wake_cond.notify_all(); offset += nbytes; return nbytes;