From 2a82bb5880d447dd6a9af4f1e3dc82955901c333 Mon Sep 17 00:00:00 2001 From: gubatron Date: Wed, 4 Apr 2018 23:55:23 -0600 Subject: [PATCH] disk_io_thread abort_hash_jobs duplicate code refactor --- include/libtorrent/disk_io_thread.hpp | 1 + src/disk_io_thread.cpp | 45 ++++++++++----------------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 70d383142..8ebde42ad 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -488,6 +488,7 @@ namespace aux { void execute_job(disk_io_job* j); void immediate_execute(); void abort_jobs(); + void abort_hash_jobs(storage_index_t const storage); // returns the maximum number of threads // the actual number of threads may be less diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 3ab21dffb..7b9bbceed 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -1830,25 +1830,25 @@ constexpr disk_job_flags_t disk_interface::cache_hit; add_fence_job(j); } + void disk_io_thread::abort_hash_jobs(storage_index_t const storage) { + // abort outstanding hash jobs belonging to this torrent + std::unique_lock l(m_job_mutex); + + std::shared_ptr st + = m_torrents[storage]->shared_from_this(); + // hash jobs + for (auto i = m_hash_io_jobs.m_queued_jobs.iterate(); i.get(); i.next()) { + disk_io_job *j = i.get(); + if (j->storage != st) continue; + j->flags |= disk_io_job::aborted; + } + } + void disk_io_thread::async_delete_files(storage_index_t const storage , remove_flags_t const options , std::function handler) { - { - // abort outstanding hash jobs belonging to this torrent - std::unique_lock l(m_job_mutex); - - std::shared_ptr st - = m_torrents[storage]->shared_from_this(); - // hash jobs - for (auto i = m_hash_io_jobs.m_queued_jobs.iterate(); i.get(); i.next()) - { - disk_io_job* j = i.get(); - if (j->storage != st) continue; - j->flags |= disk_io_job::aborted; - } - } - + abort_hash_jobs(storage); disk_io_job* j = allocate_job(job_action_t::delete_files); j->storage = m_torrents[storage]->shared_from_this(); j->callback = std::move(handler); @@ -1888,20 +1888,7 @@ constexpr disk_job_flags_t disk_interface::cache_hit; void disk_io_thread::async_stop_torrent(storage_index_t const storage , std::function handler) { - { - // abort outstanding hash jobs belonging to this torrent - std::unique_lock l(m_job_mutex); - - std::shared_ptr st - = m_torrents[storage]->shared_from_this(); - // hash jobs - for (auto i = m_hash_io_jobs.m_queued_jobs.iterate(); i.get(); i.next()) - { - disk_io_job* j = i.get(); - if (j->storage != st) continue; - j->flags |= disk_io_job::aborted; - } - } + abort_hash_jobs(storage); disk_io_job* j = allocate_job(job_action_t::stop_torrent); j->storage = m_torrents[storage]->shared_from_this(); j->callback = std::move(handler);