From ba0f894c7ca87b183951f1d158d6025a1ed7d792 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 8 Feb 2009 01:29:09 +0000 Subject: [PATCH] avoid adding a torrent to the checking queue twice --- include/libtorrent/torrent.hpp | 7 +++++++ src/torrent.cpp | 35 +++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index e12782683..1dad5981e 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -714,6 +714,9 @@ namespace libtorrent void update_peer_interest(bool was_finished); void prioritize_udp_trackers(); + void queue_torrent_check(); + void dequeue_torrent_check(); + policy m_policy; // total time we've been available on this torrent @@ -1030,6 +1033,10 @@ namespace libtorrent // connect to peers bool m_files_checked:1; + // this is true if the torrent has been added to + // checking queue in the session + bool m_queued_for_checking:1; + // this is true while tracker announcing is enabled // is is disabled while paused and checking files bool m_announcing:1; diff --git a/src/torrent.cpp b/src/torrent.cpp index e05cc5b1d..df8625559 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -186,6 +186,7 @@ namespace libtorrent , m_super_seeding(false) , m_has_incoming(false) , m_files_checked(false) + , m_queued_for_checking(false) , m_announcing(false) , m_waiting_tracker(false) , m_seed_mode(p.seed_mode && m_torrent_file->is_valid()) @@ -753,13 +754,27 @@ namespace libtorrent // some files set_state(torrent_status::queued_for_checking); if (should_check_files()) - m_ses.check_torrent(shared_from_this()); + queue_torrent_check(); } std::vector().swap(m_resume_data); lazy_entry().swap(m_resume_entry); } + void torrent::queue_torrent_check() + { + if (m_queued_for_checking) return; + m_queued_for_checking = true; + m_ses.check_torrent(shared_from_this()); + } + + void torrent::dequeue_torrent_check() + { + if (!m_queued_for_checking) return; + m_queued_for_checking = false; + m_ses.done_checking(shared_from_this()); + } + void torrent::force_recheck() { // if the torrent is already queued to check its files @@ -824,7 +839,7 @@ namespace libtorrent if (!is_torrent_paused() || is_auto_managed()) { set_state(torrent_status::queued_for_checking); - m_ses.check_torrent(shared_from_this()); + queue_torrent_check(); } } @@ -1851,7 +1866,7 @@ namespace libtorrent if (m_state == torrent_status::checking_files) { - m_ses.done_checking(shared_from_this()); + dequeue_torrent_check(); set_state(torrent_status::queued_for_checking); } @@ -4288,7 +4303,7 @@ namespace libtorrent m_ses.m_auto_manage_time_scaler = 2; m_error.clear(); if (!checking_files && should_check_files()) - m_ses.check_torrent(shared_from_this()); + queue_torrent_check(); } void torrent::set_error(std::string const& msg) @@ -4299,7 +4314,7 @@ namespace libtorrent { // stop checking m_storage->abort_disk_io(); - m_ses.done_checking(shared_from_this()); + dequeue_torrent_check(); set_state(torrent_status::queued_for_checking); } } @@ -4316,12 +4331,14 @@ namespace libtorrent m_ses.m_auto_manage_time_scaler = 0; if (!checking_files && should_check_files()) - m_ses.check_torrent(shared_from_this()); + { + queue_torrent_check(); + } else if (checking_files && !should_check_files()) { // stop checking m_storage->abort_disk_io(); - m_ses.done_checking(shared_from_this()); + dequeue_torrent_check(); set_state(torrent_status::queued_for_checking); } } @@ -4445,7 +4462,7 @@ namespace libtorrent { // stop checking m_storage->abort_disk_io(); - m_ses.done_checking(shared_from_this()); + dequeue_torrent_check(); set_state(torrent_status::queued_for_checking); } } @@ -4504,7 +4521,7 @@ namespace libtorrent m_paused = false; do_resume(); if (!checking_files && should_check_files()) - m_ses.check_torrent(shared_from_this()); + queue_torrent_check(); } void torrent::do_resume()