From 9ae56c17a13d87a79ba3c5ad75655130ea21f713 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 28 Dec 2024 13:52:31 -0600 Subject: [PATCH] refactor --- .../include/file_manager/open_file.hpp | 2 +- .../src/file_manager/open_file.cpp | 25 +++++++++---------- .../src/file_manager/ring_buffer_base.cpp | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/repertory/librepertory/include/file_manager/open_file.hpp b/repertory/librepertory/include/file_manager/open_file.hpp index 715832e7..a38d1ec3 100644 --- a/repertory/librepertory/include/file_manager/open_file.hpp +++ b/repertory/librepertory/include/file_manager/open_file.hpp @@ -94,7 +94,7 @@ private: void set_read_state(boost::dynamic_bitset<> read_state); - void update_background_reader(std::size_t read_chunk); + void update_reader(std::size_t chunk); public: auto close() -> bool override; diff --git a/repertory/librepertory/src/file_manager/open_file.cpp b/repertory/librepertory/src/file_manager/open_file.cpp index e9922974..d3dbad7a 100644 --- a/repertory/librepertory/src/file_manager/open_file.cpp +++ b/repertory/librepertory/src/file_manager/open_file.cpp @@ -449,7 +449,7 @@ auto open_file::native_operation( auto read_state = get_read_state(); if (not is_empty_file && (last_chunk < read_state.size())) { rw_lock.unlock(); - update_background_reader(0U); + update_reader(0U); download_chunk(last_chunk, false, true); if (get_api_error() != api_error::success) { @@ -575,7 +575,7 @@ auto open_file::read(std::size_t read_size, std::uint64_t read_offset, auto end_chunk = static_cast((read_size + read_offset) / get_chunk_size()); - update_background_reader(begin_chunk); + update_reader(begin_chunk); download_range(begin_chunk, end_chunk, true); if (get_api_error() != api_error::success) { @@ -652,18 +652,22 @@ void open_file::set_read_state(boost::dynamic_bitset<> read_state) { read_state_ = std::move(read_state); } -void open_file::update_background_reader(std::size_t read_chunk) { +void open_file::update_reader(std::size_t chunk) { recur_mutex_lock rw_lock(rw_mtx_); - read_chunk_ = read_chunk; + read_chunk_ = chunk; if (reader_thread_ || stop_requested_) { return; } reader_thread_ = std::make_unique([this]() { - std::size_t next_chunk{}; + unique_recur_mutex_lock lock(rw_mtx_); + auto next_chunk{read_chunk_}; + lock.unlock(); + while (not stop_requested_) { - unique_recur_mutex_lock lock(rw_mtx_); + lock.lock(); + auto read_state = get_read_state(); if ((get_file_size() == 0U) || read_state.all()) { lock.unlock(); @@ -671,12 +675,7 @@ void open_file::update_background_reader(std::size_t read_chunk) { continue; } - do { - next_chunk = read_chunk_ = - ((read_chunk_ + 1U) >= read_state.size()) ? 0U : read_chunk_ + 1U; - } while ((next_chunk != 0U) && (get_active_downloads().find(next_chunk) != - get_active_downloads().end())); - + next_chunk = next_chunk + 1U >= read_state.size() ? 0U : next_chunk + 1U; lock.unlock(); download_chunk(next_chunk, true, false); @@ -711,7 +710,7 @@ auto open_file::write(std::uint64_t write_offset, const data_buffer &data, auto end_chunk = static_cast((write_offset + data.size()) / get_chunk_size()); - update_background_reader(begin_chunk); + update_reader(begin_chunk); download_range(begin_chunk, std::min(get_read_state().size() - 1U, end_chunk), true); diff --git a/repertory/librepertory/src/file_manager/ring_buffer_base.cpp b/repertory/librepertory/src/file_manager/ring_buffer_base.cpp index d8eefd81..7e2f7c33 100644 --- a/repertory/librepertory/src/file_manager/ring_buffer_base.cpp +++ b/repertory/librepertory/src/file_manager/ring_buffer_base.cpp @@ -252,7 +252,7 @@ auto ring_buffer_base::read(std::size_t read_size, std::uint64_t read_offset, void ring_buffer_base::reader_thread() { unique_mutex_lock chunk_lock(chunk_mtx_); - auto next_chunk = ring_pos_; + auto next_chunk{ring_pos_}; chunk_notify_.notify_all(); chunk_lock.unlock();