diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index bb7c2542e..b6b2b7bab 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -472,7 +472,6 @@ public: uint8_t optimistic_unchoke_time_scaler = 0; bool is_running = false; - bool needs_completeness_check = true; tr_peerMgr* const manager; @@ -927,8 +926,8 @@ void tr_peerMgrPieceCompleted(tr_torrent* tor, tr_piece_index_t p) tr_announcerAddBytes(tor, TR_ANN_DOWN, tor->pieceSize(p)); } - /* bookkeeping */ - s->needs_completeness_check = true; + // bookkeeping + tor->set_needs_completeness_check(); } static void peerCallbackFunc(tr_peer* peer, tr_peer_event const& event, void* vs) @@ -2501,23 +2500,9 @@ void tr_peerMgr::bandwidthPulse() static auto constexpr Msec = std::chrono::duration_cast(BandwidthPeriod).count(); session->top_bandwidth_.allocate(Msec); - /* torrent upkeep */ - for (auto* const tor : session->torrents()) - { - // run the completeness check for any torrents that need it - if (auto& needs_check = tor->swarm->needs_completeness_check; needs_check) - { - needs_check = false; - tor->recheckCompleteness(); - } - - /* stop torrents that are ready to stop, but couldn't be stopped - earlier during the peer-io callback call chain */ - if (tor->isStopping) - { - tr_torrentStop(tor); - } - } + // torrent upkeep + auto& torrents = session->torrents(); + std::for_each(std::begin(torrents), std::end(torrents), [](auto* tor) { tor->do_idle_work(); }); /* pump the queues */ queuePulse(session, TR_UP); diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 38fb08d98..c9605828d 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -1672,6 +1672,8 @@ void tr_torrent::recheckCompleteness() { auto const lock = unique_lock(); + needs_completeness_check_ = false; + auto const new_completeness = completion.status(); if (new_completeness != completeness) diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 91cea3955..7aa2aca44 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -769,6 +769,25 @@ public: return n_secs; } + constexpr void set_needs_completeness_check() noexcept + { + needs_completeness_check_ = true; + } + + void do_idle_work() + { + if (needs_completeness_check_) + { + needs_completeness_check_ = false; + recheckCompleteness(); + } + + if (isStopping) + { + tr_torrentStop(this); + } + } + tr_torrent_metainfo metainfo_; tr_bandwidth bandwidth_; @@ -886,10 +905,6 @@ public: bool magnetVerify = false; private: - tr_verify_state verify_state_ = TR_VERIFY_NONE; - float verify_progress_ = -1; - tr_interned_string bandwidth_group_; - void setFilesWanted(tr_file_index_t const* files, size_t n_files, bool wanted, bool is_bootstrapping) { auto const lock = unique_lock(); @@ -903,6 +918,12 @@ private: recheckCompleteness(); } } + + tr_verify_state verify_state_ = TR_VERIFY_NONE; + float verify_progress_ = -1; + tr_interned_string bandwidth_group_; + + bool needs_completeness_check_ = true; }; /***