From f56c1f8b2f41993003033ea7ff21a41aedff384e Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 2 May 2016 22:22:13 -0400 Subject: [PATCH] remove mutex from tracker_manager (#690) remove mutex from tracker_manager --- include/libtorrent/tracker_manager.hpp | 8 ++---- include/libtorrent/udp_tracker_connection.hpp | 1 + src/natpmp.cpp | 1 - src/tracker_manager.cpp | 26 +++++++++++-------- src/udp_tracker_connection.cpp | 1 + src/upnp.cpp | 6 +---- 6 files changed, 20 insertions(+), 23 deletions(-) diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 4527f1701..0bd23edde 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -41,7 +41,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include #include @@ -66,6 +65,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/io_service.hpp" #include "libtorrent/aux_/array_view.hpp" #include "libtorrent/time.hpp" +#include "libtorrent/debug.hpp" namespace libtorrent { @@ -274,9 +274,6 @@ namespace libtorrent int m_completion_timeout; - // TODO: 3 is this object really accessed from multiple threads? - mutable std::mutex m_mutex; - // used for timeouts // this is set when the request has been sent time_point m_start_time; @@ -339,6 +336,7 @@ namespace libtorrent class TORRENT_EXTRA_EXPORT tracker_manager final : boost::noncopyable + , single_threaded { public: @@ -398,8 +396,6 @@ namespace libtorrent private: - mutable std::mutex m_mutex; - // maps transactionid to the udp_tracker_connection // These must use shared_ptr to avoid a dangling reference // if a connection is erased while a timeout event is in the queue diff --git a/include/libtorrent/udp_tracker_connection.hpp b/include/libtorrent/udp_tracker_connection.hpp index be2ff0551..50e5c7868 100644 --- a/include/libtorrent/udp_tracker_connection.hpp +++ b/include/libtorrent/udp_tracker_connection.hpp @@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include diff --git a/src/natpmp.cpp b/src/natpmp.cpp index a2e2f33fb..d76912424 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -79,7 +79,6 @@ natpmp::natpmp(io_service& ios // for this array not to be reallocated, by passing // around pointers to its elements. so reserve size for now m_mappings.reserve(10); - TORRENT_ASSERT(is_single_thread()); } void natpmp::start() diff --git a/src/tracker_manager.cpp b/src/tracker_manager.cpp index b9635ee7f..96b6d5719 100644 --- a/src/tracker_manager.cpp +++ b/src/tracker_manager.cpp @@ -235,8 +235,7 @@ namespace libtorrent void tracker_manager::remove_request(tracker_connection const* c) { - std::lock_guard l(m_mutex); - + TORRENT_ASSERT(is_single_thread()); http_conns_t::iterator i = std::find_if(m_http_conns.begin() , m_http_conns.end() , boost::bind(&boost::shared_ptr::get, _1) == c); @@ -261,6 +260,7 @@ namespace libtorrent boost::shared_ptr c , boost::uint64_t tid) { + TORRENT_ASSERT(is_single_thread()); m_udp_conns.erase(c->transaction_id()); m_udp_conns[tid] = c; } @@ -270,7 +270,7 @@ namespace libtorrent , tracker_request req , boost::weak_ptr c) { - std::lock_guard l(m_mutex); + TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(req.num_want >= 0); TORRENT_ASSERT(!m_abort || req.event == tracker_request::stopped); if (m_abort && req.event != tracker_request::stopped) return; @@ -316,6 +316,7 @@ namespace libtorrent bool tracker_manager::incoming_packet(udp::endpoint const& ep , aux::array_view const buf) { + TORRENT_ASSERT(is_single_thread()); // ignore packets smaller than 8 bytes if (buf.size() < 8) { @@ -350,17 +351,17 @@ namespace libtorrent return p->on_receive(ep, buf); } - void tracker_manager::incoming_error(error_code const& ec - , udp::endpoint const& ep) + void tracker_manager::incoming_error(error_code const& + , udp::endpoint const&) { - TORRENT_UNUSED(ec); - TORRENT_UNUSED(ep); + TORRENT_ASSERT(is_single_thread()); // TODO: 2 implement } bool tracker_manager::incoming_packet(char const* hostname , aux::array_view const buf) { + TORRENT_ASSERT(is_single_thread()); // ignore packets smaller than 8 bytes if (buf.size() < 16) return false; @@ -392,6 +393,7 @@ namespace libtorrent void tracker_manager::send_hostname(char const* hostname, int const port , array_view p, error_code& ec, int const flags) { + TORRENT_ASSERT(is_single_thread()); m_send_fun_hostname(hostname, port, p, ec, flags); } @@ -399,13 +401,16 @@ namespace libtorrent , array_view p , error_code& ec, int const flags) { + TORRENT_ASSERT(is_single_thread()); m_send_fun(ep, p, ec, flags); } void tracker_manager::abort_all_requests(bool all) { + // this is called from the destructor too, which is not subject to the + // single-thread requirement. + TORRENT_ASSERT(all || is_single_thread()); // removes all connections except 'event=stopped'-requests - std::unique_lock l(m_mutex); m_abort = true; http_conns_t close_http_connections; @@ -441,7 +446,6 @@ namespace libtorrent if (rc) rc->debug_log("aborting: %s", req.url.c_str()); #endif } - l.unlock(); for (http_conns_t::iterator i = close_http_connections.begin() , end(close_http_connections.end()); i != end; ++i) @@ -459,13 +463,13 @@ namespace libtorrent bool tracker_manager::empty() const { - std::lock_guard l(m_mutex); + TORRENT_ASSERT(is_single_thread()); return m_http_conns.empty() && m_udp_conns.empty(); } int tracker_manager::num_requests() const { - std::lock_guard l(m_mutex); + TORRENT_ASSERT(is_single_thread()); return int(m_http_conns.size() + m_udp_conns.size()); } } diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index e495fdba7..9226293b6 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include #include #include diff --git a/src/upnp.cpp b/src/upnp.cpp index 30607c3ba..6b41d2300 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -90,7 +90,6 @@ upnp::upnp(io_service& ios , m_last_if_update(min_time()) { TORRENT_ASSERT(cb); - TORRENT_ASSERT(is_single_thread()); } void upnp::start() @@ -104,10 +103,7 @@ void upnp::start() m_mappings.reserve(10); } -upnp::~upnp() -{ - TORRENT_ASSERT(is_single_thread()); -} +upnp::~upnp() {} void upnp::discover_device() {