This commit is contained in:
@ -94,7 +94,7 @@ private:
|
|||||||
|
|
||||||
void set_read_state(boost::dynamic_bitset<> read_state);
|
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:
|
public:
|
||||||
auto close() -> bool override;
|
auto close() -> bool override;
|
||||||
|
@ -449,7 +449,7 @@ auto open_file::native_operation(
|
|||||||
auto read_state = get_read_state();
|
auto read_state = get_read_state();
|
||||||
if (not is_empty_file && (last_chunk < read_state.size())) {
|
if (not is_empty_file && (last_chunk < read_state.size())) {
|
||||||
rw_lock.unlock();
|
rw_lock.unlock();
|
||||||
update_background_reader(0U);
|
update_reader(0U);
|
||||||
|
|
||||||
download_chunk(last_chunk, false, true);
|
download_chunk(last_chunk, false, true);
|
||||||
if (get_api_error() != api_error::success) {
|
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 =
|
auto end_chunk =
|
||||||
static_cast<std::size_t>((read_size + read_offset) / get_chunk_size());
|
static_cast<std::size_t>((read_size + read_offset) / get_chunk_size());
|
||||||
|
|
||||||
update_background_reader(begin_chunk);
|
update_reader(begin_chunk);
|
||||||
|
|
||||||
download_range(begin_chunk, end_chunk, true);
|
download_range(begin_chunk, end_chunk, true);
|
||||||
if (get_api_error() != api_error::success) {
|
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);
|
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_);
|
recur_mutex_lock rw_lock(rw_mtx_);
|
||||||
read_chunk_ = read_chunk;
|
read_chunk_ = chunk;
|
||||||
|
|
||||||
if (reader_thread_ || stop_requested_) {
|
if (reader_thread_ || stop_requested_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
reader_thread_ = std::make_unique<std::thread>([this]() {
|
reader_thread_ = std::make_unique<std::thread>([this]() {
|
||||||
std::size_t next_chunk{};
|
unique_recur_mutex_lock lock(rw_mtx_);
|
||||||
|
auto next_chunk{read_chunk_};
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
while (not stop_requested_) {
|
while (not stop_requested_) {
|
||||||
unique_recur_mutex_lock lock(rw_mtx_);
|
lock.lock();
|
||||||
|
|
||||||
auto read_state = get_read_state();
|
auto read_state = get_read_state();
|
||||||
if ((get_file_size() == 0U) || read_state.all()) {
|
if ((get_file_size() == 0U) || read_state.all()) {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
@ -671,12 +675,7 @@ void open_file::update_background_reader(std::size_t read_chunk) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
next_chunk = next_chunk + 1U >= read_state.size() ? 0U : next_chunk + 1U;
|
||||||
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()));
|
|
||||||
|
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
download_chunk(next_chunk, true, false);
|
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 =
|
auto end_chunk =
|
||||||
static_cast<std::size_t>((write_offset + data.size()) / get_chunk_size());
|
static_cast<std::size_t>((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),
|
download_range(begin_chunk, std::min(get_read_state().size() - 1U, end_chunk),
|
||||||
true);
|
true);
|
||||||
|
@ -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() {
|
void ring_buffer_base::reader_thread() {
|
||||||
unique_mutex_lock chunk_lock(chunk_mtx_);
|
unique_mutex_lock chunk_lock(chunk_mtx_);
|
||||||
auto next_chunk = ring_pos_;
|
auto next_chunk{ring_pos_};
|
||||||
chunk_notify_.notify_all();
|
chunk_notify_.notify_all();
|
||||||
chunk_lock.unlock();
|
chunk_lock.unlock();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user