From 4f419be42dba9dbc8ec43df17b53f2f9db6b08bd Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Mon, 23 Dec 2024 08:51:49 -0600 Subject: [PATCH] Complete ring buffer and direct download support #26 --- .../file_manager/ring_buffer_open_file.cpp | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 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 28528c18..3f3f8c1f 100644 --- a/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp +++ b/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp @@ -101,6 +101,38 @@ ring_buffer_open_file::~ring_buffer_open_file() { reader_thread_.reset(); } +void ring_buffer_open_file::background_reader_thread() { + unique_mutex_lock chunk_lock(chunk_mtx_); + auto next_chunk = ring_pos_; + chunk_notify_.notify_all(); + chunk_lock.unlock(); + + 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(); + }; + + if (not ring_state_[next_chunk % ring_state_.size()]) { + check_and_wait(); + continue; + } + + chunk_notify_.notify_all(); + chunk_lock.unlock(); + + download_chunk(next_chunk, true); + } +} + auto ring_buffer_open_file::can_handle_file(std::uint64_t file_size, std::size_t chunk_size, std::size_t ring_size) -> bool { @@ -371,36 +403,4 @@ void ring_buffer_open_file::set_api_path(const std::string &api_path) { open_file_base::set_api_path(api_path); chunk_notify_.notify_all(); } - -void ring_buffer_open_file::background_reader_thread() { - unique_mutex_lock chunk_lock(chunk_mtx_); - auto next_chunk = ring_pos_; - chunk_notify_.notify_all(); - chunk_lock.unlock(); - - 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(); - }; - - if (not ring_state_[next_chunk % ring_state_.size()]) { - check_and_wait(); - continue; - } - - chunk_notify_.notify_all(); - chunk_lock.unlock(); - - download_chunk(next_chunk, true); - } -} } // namespace repertory