Complete ring buffer and direct download support #26

This commit is contained in:
Scott E. Graves 2024-12-23 07:53:42 -06:00
parent 75a4676eac
commit ce1676f3d3
2 changed files with 13 additions and 1 deletions

View File

@ -57,6 +57,7 @@ private:
std::condition_variable chunk_notify_;
mutable std::mutex chunk_mtx_;
std::mutex read_mtx_;
std::unique_ptr<std::thread> reader_thread_;
std::size_t ring_begin_{};
std::size_t ring_end_{};
std::size_t ring_pos_{};

View File

@ -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<std::thread>([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);
}