From f344665ddc7d3e1c93593a48835087b674aa2113 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Mon, 23 Dec 2024 08:43:21 -0600 Subject: [PATCH] Complete ring buffer and direct download support #26 --- .../file_manager/ring_buffer_open_file.cpp | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp b/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp index 3396d7d2..1e964fa3 100644 --- a/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp +++ b/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp @@ -376,24 +376,31 @@ void ring_buffer_open_file::background_reader_thread() { unique_mutex_lock read_lock(read_mtx_); read_lock.unlock(); + auto next_chunk = ring_pos_; while (not stop_requested_) { read_lock.lock(); - auto next_chunk = - ring_pos_ + 1U >= ring_state_.size() ? 0U : ring_pos_ + 1U; + + const auto check_and_wait = [this, &next_chunk]() { + unique_mutex_lock chunk_lock(chunk_mtx_); + if (get_read_state().all()) { + chunk_notify_.wait(chunk_lock); + next_chunk = ring_pos_; + } + chunk_notify_.notify_all(); + }; + + next_chunk = next_chunk + 1U > ring_end_ ? ring_begin_ : next_chunk + 1U; if (not ring_state_[next_chunk % ring_state_.size()]) { read_lock.unlock(); + + check_and_wait(); continue; } read_lock.unlock(); download_chunk(next_chunk, true); - unique_mutex_lock chunk_lock(chunk_mtx_); - if (get_read_state().all()) { - chunk_notify_.wait(chunk_lock); - } - - chunk_notify_.notify_all(); + check_and_wait(); } } } // namespace repertory