run peerMgrPeerStats in session thread (#5992)

* run peerMgrPeerStats in session thread

* code-review: add a TODO: for refactoring
This commit is contained in:
Cœur 2024-03-16 08:04:43 +08:00 committed by GitHub
parent a72a93764e
commit 8709ec60e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 7 deletions

View File

@ -1841,13 +1841,22 @@ tr_peer_stat* tr_peerMgrPeerStats(tr_torrent const* tor, size_t* setme_count)
auto const n = std::size(peers);
auto* const ret = new tr_peer_stat[n];
auto const now = tr_time();
auto const now_msec = tr_time_msec();
std::transform(
std::begin(peers),
std::end(peers),
ret,
[&now, &now_msec](auto const* peer) { return get_peer_stats(peer, now, now_msec); });
// TODO: re-implement as a callback solution (similar to tr_sessionSetCompletenessCallback) in case present call to run_in_session_thread is causing hangs when the peers info window is displayed.
auto done_promise = std::promise<void>{};
auto done_future = done_promise.get_future();
tor->session->run_in_session_thread(
[&peers, &ret, &done_promise]()
{
auto const now = tr_time();
auto const now_msec = tr_time_msec();
std::transform(
std::begin(peers),
std::end(peers),
ret,
[&now, &now_msec](auto const* peer) { return get_peer_stats(peer, now, now_msec); });
done_promise.set_value();
});
done_future.wait();
*setme_count = n;
return ret;