From 1d5f42d6d0e2c2f6d06ec5fb3a06d394a91b9383 Mon Sep 17 00:00:00 2001 From: arvidn Date: Wed, 23 Nov 2016 01:43:57 -0500 Subject: [PATCH] convert remaining disk io jobs --- include/libtorrent/disk_interface.hpp | 18 +++++--- include/libtorrent/disk_io_job.hpp | 10 ++--- include/libtorrent/disk_io_thread.hpp | 10 ++--- include/libtorrent/torrent.hpp | 16 ++++---- src/disk_io_job.cpp | 10 ++++- src/disk_io_thread.cpp | 14 +++---- src/peer_connection.cpp | 9 ++-- src/torrent.cpp | 59 +++++++++++++-------------- 8 files changed, 75 insertions(+), 71 deletions(-) diff --git a/include/libtorrent/disk_interface.hpp b/include/libtorrent/disk_interface.hpp index bac67e891..3772250aa 100644 --- a/include/libtorrent/disk_interface.hpp +++ b/include/libtorrent/disk_interface.hpp @@ -39,7 +39,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - struct disk_io_job; struct storage_interface; struct peer_request; struct disk_observer; @@ -59,6 +58,13 @@ namespace libtorrent file_exist = -4 }; + enum flags_t + { + // this flag is set on a job when a read operation did + // not hit the disk, but found the data in the read cache. + cache_hit = 0x2, + }; + virtual void async_read(storage_interface* storage, peer_request const& r , std::function handler, void* requester, int flags = 0) = 0; @@ -79,17 +85,17 @@ namespace libtorrent virtual void async_flush_piece(storage_interface* storage, int piece , std::function handler = std::function()) = 0; virtual void async_stop_torrent(storage_interface* storage - , std::function handler)= 0; + , std::function handler = std::function()) = 0; virtual void async_rename_file(storage_interface* storage, int index, std::string const& name - , std::function handler) = 0; + , std::function handler) = 0; virtual void async_delete_files(storage_interface* storage, int options - , std::function handler) = 0; + , std::function handler) = 0; virtual void async_set_file_priority(storage_interface* storage , std::vector const& prio - , std::function handler) = 0; + , std::function handler) = 0; virtual void async_clear_piece(storage_interface* storage, int index - , std::function handler) = 0; + , std::function handler) = 0; virtual void clear_piece(storage_interface* storage, int index) = 0; virtual void update_stats_counters(counters& c) const = 0; diff --git a/include/libtorrent/disk_io_job.hpp b/include/libtorrent/disk_io_job.hpp index 86dcb1837..da7db5143 100644 --- a/include/libtorrent/disk_io_job.hpp +++ b/include/libtorrent/disk_io_job.hpp @@ -102,10 +102,6 @@ namespace libtorrent { sequential_access = 0x1, - // this flag is set on a job when a read operation did - // not hit the disk, but found the data in the read cache. - cache_hit = 0x2, - // force making a copy of the cached block, rather // than getting a reference to the block already in // the cache. @@ -160,7 +156,8 @@ namespace libtorrent using move_handler = std::function; using release_handler = std::function; using check_handler = std::function; - using generic_handler = std::function; + using rename_handler = std::function; + using clear_piece_handler = std::function; boost::variant callback; + , rename_handler + , clear_piece_handler> callback; // the error code from the file operation // on error, this also contains the path of the diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 31874f68a..ff11d465f 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -305,23 +305,23 @@ namespace libtorrent void async_release_files(storage_interface* storage , std::function handler = std::function()) override; void async_delete_files(storage_interface* storage, int options - , std::function handler) override; + , std::function handler) override; void async_check_files(storage_interface* storage , add_torrent_params const* resume_data , std::vector& links , std::function handler) override; void async_rename_file(storage_interface* storage, int index, std::string const& name - , std::function handler) override; + , std::function handler) override; void async_stop_torrent(storage_interface* storage - , std::function handler) override; + , std::function handler) override; void async_flush_piece(storage_interface* storage, int piece , std::function handler = std::function()) override; void async_set_file_priority(storage_interface* storage , std::vector const& prio - , std::function handler) override; + , std::function handler) override; void async_clear_piece(storage_interface* storage, int index - , std::function handler) override; + , std::function handler) override; // this is not asynchronous and requires that the piece does not // have any pending buffers. It's meant to be used for pieces that // were just read and hashed and failed the hash check. diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 64ed71861..9c882fbd7 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -92,7 +92,6 @@ namespace libtorrent struct storage_interface; class bt_peer_connection; struct listen_socket_t; - struct disk_io_job; enum class waste_reason { @@ -381,7 +380,6 @@ namespace libtorrent void add_piece(int piece, char const* data, int flags = 0); void on_disk_write_complete(storage_error const& error , peer_request p); - void on_disk_tick_done(disk_io_job const* j); void set_progress_ppm(int p) { m_progress_ppm = std::uint32_t(p); } struct read_piece_struct @@ -537,7 +535,7 @@ namespace libtorrent void set_file_priority(int index, int priority); int file_priority(int index) const; - void on_file_priority(); + void on_file_priority(storage_error const&); void prioritize_files(std::vector const& files); void file_priorities(std::vector*) const; @@ -887,10 +885,10 @@ namespace libtorrent // this is the handler for hash failure piece synchronization // i.e. resetting the piece - void on_piece_sync(disk_io_job const* j); + void on_piece_sync(int piece); // this is the handler for write failure piece synchronization - void on_piece_fail_sync(disk_io_job const* j, piece_block b); + void on_piece_fail_sync(int piece, piece_block b); void add_redundant_bytes(int b, waste_reason reason); void add_failed_bytes(int b); @@ -1116,11 +1114,13 @@ namespace libtorrent void construct_storage(); void update_list(int list, bool in); - void on_files_deleted(disk_io_job const* j); - void on_torrent_paused(disk_io_job const* j); + void on_files_deleted(storage_error const& error); + void on_torrent_paused(); void on_storage_moved(int status, std::string const& path , storage_error const& error); - void on_file_renamed(disk_io_job const* j); + void on_file_renamed(std::string const& filename + , int const file_idx + , storage_error const& error); void on_cache_flushed(); // upload and download rate limits for the torrent diff --git a/src/disk_io_job.cpp b/src/disk_io_job.cpp index ddb3c6dc7..b66ffe9c6 100644 --- a/src/disk_io_job.cpp +++ b/src/disk_io_job.cpp @@ -76,10 +76,16 @@ namespace libtorrent h(m_job.ret, m_job.error); } - void operator()(disk_io_job::generic_handler& h) const + void operator()(disk_io_job::rename_handler& h) const { if (!h) return; - h(&m_job); + h(m_job.buffer.string, m_job.piece, m_job.error); + } + + void operator()(disk_io_job::clear_piece_handler& h) const + { + if (!h) return; + h(m_job.piece); } private: diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 5ac1ca914..470d85bea 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -1372,7 +1372,7 @@ namespace libtorrent // cache-hit m_stats_counters.inc_stats_counter(counters::num_blocks_cache_hits); DLOG("do_read: cache hit\n"); - j->flags |= disk_io_job::cache_hit; + j->flags |= disk_interface::cache_hit; j->ret = ret; completed_jobs.push_back(j); } @@ -1562,7 +1562,7 @@ namespace libtorrent { m_stats_counters.inc_stats_counter(counters::num_blocks_cache_hits); DLOG("do_read: cache hit\n"); - j->flags |= disk_io_job::cache_hit; + j->flags |= disk_interface::cache_hit; j->ret = ret; return 0; } @@ -1784,7 +1784,7 @@ namespace libtorrent void disk_io_thread::async_delete_files(storage_interface* storage , int const options - , std::function handler) + , std::function handler) { // remove cache blocks belonging to this torrent jobqueue_t completed_jobs; @@ -1847,7 +1847,7 @@ namespace libtorrent } void disk_io_thread::async_rename_file(storage_interface* storage, int index, std::string const& name - , std::function handler) + , std::function handler) { disk_io_job* j = allocate_job(disk_io_job::rename_file); j->storage = storage->shared_from_this(); @@ -1858,7 +1858,7 @@ namespace libtorrent } void disk_io_thread::async_stop_torrent(storage_interface* storage - , std::function handler) + , std::function handler) { // remove outstanding hash jobs belonging to this torrent std::unique_lock l2(m_job_mutex); @@ -1913,7 +1913,7 @@ namespace libtorrent void disk_io_thread::async_set_file_priority(storage_interface* storage , std::vector const& prios - , std::function handler) + , std::function handler) { std::vector* p = new std::vector(prios); @@ -1926,7 +1926,7 @@ namespace libtorrent } void disk_io_thread::async_clear_piece(storage_interface* storage, int index - , std::function handler) + , std::function handler) { disk_io_job* j = allocate_job(disk_io_job::clear_piece); j->storage = storage->shared_from_this(); diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 411aef49d..763bf7605 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -61,7 +61,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/ip_filter.hpp" #include "libtorrent/kademlia/node_id.hpp" #include "libtorrent/close_reason.hpp" -#include "libtorrent/disk_io_job.hpp" #include "libtorrent/aux_/has_block.hpp" #include "libtorrent/aux_/time.hpp" @@ -3032,9 +3031,7 @@ namespace libtorrent // is m_abort true? if so, we should probably just // exit this function early, no need to keep the picker // state up-to-date, right? - disk_io_job sj; - sj.piece = p.piece; - t->on_piece_fail_sync(&sj, block_finished); + t->on_piece_fail_sync(p.piece, block_finished); } } t->update_gauge(); @@ -5236,7 +5233,7 @@ namespace libtorrent , "piece: %d s: %x l: %x b: %p c: %s e: %s rtt: %d us" , r.piece, r.start, r.length , static_cast(disk_block) - , (flags & disk_io_job::cache_hit ? "cache hit" : "cache miss") + , (flags & disk_interface::cache_hit ? "cache hit" : "cache miss") , error.ec.message().c_str(), disk_rtt); } #endif @@ -5303,7 +5300,7 @@ namespace libtorrent // if it's rare enough to make it into the suggested piece // push another piece out if (m_settings.get_int(settings_pack::suggest_mode) == settings_pack::suggest_read_cache - && (flags & disk_io_job::cache_hit) == 0) + && (flags & disk_interface::cache_hit) == 0) { t->add_suggest_piece(r.piece); } diff --git a/src/torrent.cpp b/src/torrent.cpp index 2f7f3f295..ae3b1fa8e 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1097,11 +1097,8 @@ namespace libtorrent pause(); } - void torrent::on_piece_fail_sync(disk_io_job const* j, piece_block b) try + void torrent::on_piece_fail_sync(int, piece_block) try { - TORRENT_UNUSED(j); - TORRENT_UNUSED(b); - if (m_abort) return; update_gauge(); @@ -4139,9 +4136,7 @@ namespace libtorrent // it doesn't really matter what we do // here, since we're about to destruct the // torrent anyway. - disk_io_job j; - j.piece = index; - on_piece_sync(&j); + on_piece_sync(index); } #if TORRENT_USE_ASSERTS @@ -4176,7 +4171,7 @@ namespace libtorrent c.send_block_requests(); } - void torrent::on_piece_sync(disk_io_job const* j) try + void torrent::on_piece_sync(int const piece) try { // the user may have called force_recheck, which clears // the piece picker @@ -4184,12 +4179,12 @@ namespace libtorrent // unlock the piece and restore it, as if no block was // ever downloaded for it. - m_picker->restore_piece(j->piece); + m_picker->restore_piece(piece); // we have to let the piece_picker know that // this piece failed the check as it can restore it // and mark it as being interesting for download - TORRENT_ASSERT(m_picker->have_piece(j->piece) == false); + TORRENT_ASSERT(m_picker->have_piece(piece) == false); // loop over all peers and re-request potential duplicate // blocks to this piece @@ -4203,14 +4198,14 @@ namespace libtorrent , end2(dq.end()); k != end2; ++k) { if (k->timed_out || k->not_wanted) continue; - if (int(k->block.piece_index) != j->piece) continue; + if (int(k->block.piece_index) != piece) continue; m_picker->mark_as_downloading(k->block, p->peer_info_struct() , p->picker_options()); } for (std::vector::const_iterator k = rq.begin() , end2(rq.end()); k != end2; ++k) { - if (int(k->block.piece_index) != j->piece) continue; + if (int(k->block.piece_index) != piece) continue; m_picker->mark_as_downloading(k->block, p->peer_info_struct() , p->picker_options()); } @@ -4412,15 +4407,15 @@ namespace libtorrent return avail_vec[random(avail_vec.size() - 1)]; } - void torrent::on_files_deleted(disk_io_job const* j) try + void torrent::on_files_deleted(storage_error const& error) try { TORRENT_ASSERT(is_single_thread()); - if (j->ret != 0) + if (error) { if (alerts().should_post()) alerts().emplace_alert(get_handle() - , j->error.ec, m_torrent_file->info_hash()); + , error.ec, m_torrent_file->info_hash()); } else { @@ -4429,27 +4424,29 @@ namespace libtorrent } catch (...) { handle_exception(); } - void torrent::on_file_renamed(disk_io_job const* j) try + void torrent::on_file_renamed(std::string const& filename + , int const file_idx + , storage_error const& error) try { TORRENT_ASSERT(is_single_thread()); - if (j->ret == 0) - { - if (alerts().should_post()) - alerts().emplace_alert(get_handle() - , j->buffer.string, j->piece); - m_torrent_file->rename_file(j->piece, j->buffer.string); - } - else + if (error) { if (alerts().should_post()) alerts().emplace_alert(get_handle() - , j->piece, j->error.ec); + , file_idx, error.ec); + } + else + { + if (alerts().should_post()) + alerts().emplace_alert(get_handle() + , filename, file_idx); + m_torrent_file->rename_file(file_idx, filename); } } catch (...) { handle_exception(); } - void torrent::on_torrent_paused(disk_io_job const*) try + void torrent::on_torrent_paused() try { TORRENT_ASSERT(is_single_thread()); @@ -4912,7 +4909,7 @@ namespace libtorrent } } - void torrent::on_file_priority() {} + void torrent::on_file_priority(storage_error const&) {} void torrent::prioritize_files(std::vector const& files) { @@ -4945,7 +4942,7 @@ namespace libtorrent if (m_torrent_file->num_pieces() > 0 && m_storage) { m_ses.disk_thread().async_set_file_priority(m_storage.get() - , m_file_priority, std::bind(&torrent::on_file_priority, this)); + , m_file_priority, std::bind(&torrent::on_file_priority, this, _1)); } update_piece_priorities(); @@ -4983,7 +4980,7 @@ namespace libtorrent if (m_storage) { m_ses.disk_thread().async_set_file_priority(m_storage.get() - , m_file_priority, std::bind(&torrent::on_file_priority, this)); + , m_file_priority, std::bind(&torrent::on_file_priority, this, _1)); } update_piece_priorities(); } @@ -7725,7 +7722,7 @@ namespace libtorrent } m_ses.disk_thread().async_rename_file(m_storage.get(), index, name - , std::bind(&torrent::on_file_renamed, shared_from_this(), _1)); + , std::bind(&torrent::on_file_renamed, shared_from_this(), _1, _2, _3)); return; } @@ -8639,7 +8636,7 @@ namespace libtorrent { // the torrent_paused alert will be posted from on_torrent_paused m_ses.disk_thread().async_stop_torrent(m_storage.get() - , std::bind(&torrent::on_torrent_paused, shared_from_this(), _1)); + , std::bind(&torrent::on_torrent_paused, shared_from_this())); } else {