From 2952cc8373b1b8af0dedf24111f92befb407a095 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sun, 22 Dec 2024 08:14:23 -0600 Subject: [PATCH] refactor --- .../src/file_manager/open_file.cpp | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/repertory/librepertory/src/file_manager/open_file.cpp b/repertory/librepertory/src/file_manager/open_file.cpp index 4827b990..900d91e8 100644 --- a/repertory/librepertory/src/file_manager/open_file.cpp +++ b/repertory/librepertory/src/file_manager/open_file.cpp @@ -309,11 +309,10 @@ void open_file::download_chunk(std::size_t chunk, bool skip_active, void open_file::download_range(std::size_t start_chunk, std::size_t end_chunk, bool should_reset) { - for (std::size_t chunk = start_chunk; chunk <= end_chunk; ++chunk) { + for (std::size_t chunk = start_chunk; + (get_api_error() == api_error::success) && (chunk <= end_chunk); + ++chunk) { download_chunk(chunk, false, should_reset); - if (get_api_error() != api_error::success) { - return; - } } } @@ -558,35 +557,36 @@ void open_file::update_background_reader(std::size_t read_chunk) { recur_mutex_lock reader_lock(file_mtx_); read_chunk_ = read_chunk; - if (not reader_thread_ && not stop_requested_) { - reader_thread_ = std::make_unique([this]() { - std::size_t next_chunk{}; - while (not stop_requested_) { - unique_recur_mutex_lock file_lock(file_mtx_); - if ((fsi_.size == 0U) || read_state_.all()) { - file_lock.unlock(); - - unique_mutex_lock io_lock(io_thread_mtx_); - if (not stop_requested_ && io_thread_queue_.empty()) { - io_thread_notify_.wait(io_lock); - } - io_thread_notify_.notify_all(); - io_lock.unlock(); - continue; - } - - do { - next_chunk = read_chunk_ = ((read_chunk_ + 1U) >= read_state_.size()) - ? 0U - : read_chunk_ + 1U; - } while ((next_chunk != 0U) && (active_downloads_.find(next_chunk) != - active_downloads_.end())); - - file_lock.unlock(); - download_chunk(next_chunk, true, false); - } - }); + if (reader_thread_ || stop_requested_) { + return; } + + reader_thread_ = std::make_unique([this]() { + std::size_t next_chunk{}; + while (not stop_requested_) { + unique_recur_mutex_lock file_lock(file_mtx_); + if ((fsi_.size == 0U) || read_state_.all()) { + file_lock.unlock(); + + unique_mutex_lock io_lock(io_thread_mtx_); + if (not stop_requested_ && io_thread_queue_.empty()) { + io_thread_notify_.wait(io_lock); + } + io_thread_notify_.notify_all(); + io_lock.unlock(); + continue; + } + + do { + next_chunk = read_chunk_ = + ((read_chunk_ + 1U) >= read_state_.size()) ? 0U : read_chunk_ + 1U; + } while ((next_chunk != 0U) && + (active_downloads_.find(next_chunk) != active_downloads_.end())); + + file_lock.unlock(); + download_chunk(next_chunk, true, false); + } + }); } auto open_file::write(std::uint64_t write_offset, const data_buffer &data,