From 2cf6b776276b4e09784449bd97fe71af61bb5a1e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 16 May 2019 22:01:53 +0200 Subject: [PATCH] input/buffering: eliminate "idle" flag, automatically seek to next hole --- src/input/BufferingInputStream.cxx | 29 +++++++++++++++++------------ src/input/BufferingInputStream.hxx | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/input/BufferingInputStream.cxx b/src/input/BufferingInputStream.cxx index fed4ab0ed..ebfffd943 100644 --- a/src/input/BufferingInputStream.cxx +++ b/src/input/BufferingInputStream.cxx @@ -105,7 +105,6 @@ BufferingInputStream::Read(std::unique_lock &lock, void *ptr, size_t s) if (!IsAvailable()) { /* wake up the sleeping thread */ - idle = false; wake_cond.notify_one(); } @@ -117,12 +116,6 @@ BufferingInputStream::Read(std::unique_lock &lock, void *ptr, size_t s) std::rethrow_exception(std::exchange(read_error, {})); } - if (idle) { - /* wake up the sleeping thread */ - idle = false; - wake_cond.notify_one(); - } - client_cond.wait(lock); } } @@ -158,11 +151,10 @@ BufferingInputStream::RunThread() noexcept seek_error = std::current_exception(); } - idle = false; seek = false; read_error = {}; client_cond.notify_one(); - } else if (read_error || idle) { + } else if (read_error) { /* wait for client to consume the read error */ wake_cond.wait(lock); } else if (offset != input->GetOffset() && !IsAvailable()) { @@ -214,9 +206,22 @@ BufferingInputStream::RunThread() noexcept if (w.empty()) { if (IsAvailable()) { /* we still have enough data - for the next Read() - sleep - until we need more data */ - idle = true; + for the next Read() - seek + to the first hole */ + + size_t new_offset = FindFirstHole(); + if (new_offset == INVALID_OFFSET) + /* the file has been + read completely */ + break; + + try { + input->Seek(lock, new_offset); + } catch (...) { + read_error = std::current_exception(); + client_cond.notify_one(); + OnBufferAvailable(); + } } else { /* we need more data at our current position, because diff --git a/src/input/BufferingInputStream.hxx b/src/input/BufferingInputStream.hxx index 972c8fd6d..ee460210c 100644 --- a/src/input/BufferingInputStream.hxx +++ b/src/input/BufferingInputStream.hxx @@ -55,7 +55,7 @@ class BufferingInputStream : InputStreamHandler { SparseBuffer buffer; - bool stop = false, seek = false, idle = false; + bool stop = false, seek = false; size_t offset = 0;