From ad222333088bae438456a205f744792d605a6a27 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Mon, 23 Dec 2024 08:46:49 -0600 Subject: [PATCH] Complete ring buffer and direct download support #26 --- .../file_manager/ring_buffer_open_file.cpp | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 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 1e964fa3..cb8b7da5 100644 --- a/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp +++ b/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp @@ -373,33 +373,31 @@ void ring_buffer_open_file::set_api_path(const std::string &api_path) { } void ring_buffer_open_file::background_reader_thread() { - unique_mutex_lock read_lock(read_mtx_); - read_lock.unlock(); - + unique_mutex_lock chunk_lock(chunk_mtx_); auto next_chunk = ring_pos_; - while (not stop_requested_) { - read_lock.lock(); + chunk_notify_.notify_all(); + chunk_lock.unlock(); - const auto check_and_wait = [this, &next_chunk]() { - unique_mutex_lock chunk_lock(chunk_mtx_); + while (not stop_requested_) { + chunk_lock.lock(); + + next_chunk = next_chunk + 1U > ring_end_ ? ring_begin_ : next_chunk + 1U; + const auto check_and_wait = [this, &chunk_lock, &next_chunk]() { if (get_read_state().all()) { chunk_notify_.wait(chunk_lock); next_chunk = ring_pos_; } + chunk_notify_.notify_all(); + chunk_lock.unlock(); }; - 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); - check_and_wait(); } }