From ce1676f3d3d702641ac0df20302cb7b05b8f6f5d Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Mon, 23 Dec 2024 07:53:42 -0600 Subject: [PATCH] Complete ring buffer and direct download support #26 --- .../include/file_manager/ring_buffer_open_file.hpp | 1 + .../src/file_manager/ring_buffer_open_file.cpp | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/repertory/librepertory/include/file_manager/ring_buffer_open_file.hpp b/repertory/librepertory/include/file_manager/ring_buffer_open_file.hpp index 9e158b9f..cb68f5ae 100644 --- a/repertory/librepertory/include/file_manager/ring_buffer_open_file.hpp +++ b/repertory/librepertory/include/file_manager/ring_buffer_open_file.hpp @@ -57,6 +57,7 @@ private: std::condition_variable chunk_notify_; mutable std::mutex chunk_mtx_; std::mutex read_mtx_; + std::unique_ptr reader_thread_; std::size_t ring_begin_{}; std::size_t ring_end_{}; std::size_t ring_pos_{}; 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 25b3a552..a2fc5e64 100644 --- a/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp +++ b/repertory/librepertory/src/file_manager/ring_buffer_open_file.cpp @@ -81,6 +81,9 @@ ring_buffer_open_file::ring_buffer_open_file(std::string buffer_directory, throw std::runtime_error(fmt::format("failed to resize buffer file|err|{}", utils::get_last_error_code())); } + + reader_thread_ = + std::make_unique([this]() { background_reader_thread(); }); } ring_buffer_open_file::~ring_buffer_open_file() { @@ -94,6 +97,9 @@ ring_buffer_open_file::~ring_buffer_open_file() { function_name, fsi_.api_path, source_path_, utils::get_last_error_code(), "failed to delete file"); } + + reader_thread_->join(); + reader_thread_.reset(); } auto ring_buffer_open_file::can_handle_file(std::uint64_t file_size, @@ -104,6 +110,11 @@ auto ring_buffer_open_file::can_handle_file(std::uint64_t file_size, auto ring_buffer_open_file::close() -> bool { stop_requested_ = true; + + unique_mutex_lock chunk_lock(chunk_mtx_); + chunk_notify_.notify_all(); + chunk_lock.unlock(); + return open_file_base::close(); } @@ -375,7 +386,7 @@ void ring_buffer_open_file::background_reader_thread() { download_chunk(next_chunk, true); unique_mutex_lock chunk_lock(chunk_mtx_); - if (ring_state_.none()) { + if (get_read_state().all()) { chunk_notify_.wait(chunk_lock); }