From 1abb7c161df4439394cf3d59f9ae99815eb48f4a Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Sat, 8 Sep 2007 19:05:43 +0000 Subject: [PATCH] session_impl and connection_queue fixes --- libtorrent/include/libtorrent/alert_types.hpp | 1 - libtorrent/include/libtorrent/connection_queue.hpp | 5 +++++ libtorrent/include/libtorrent/piece_picker.hpp | 1 - libtorrent/src/connection_queue.cpp | 8 ++++++++ libtorrent/src/peer_connection.cpp | 1 - libtorrent/src/piece_picker.cpp | 1 - libtorrent/src/session_impl.cpp | 3 ++- 7 files changed, 15 insertions(+), 5 deletions(-) diff --git a/libtorrent/include/libtorrent/alert_types.hpp b/libtorrent/include/libtorrent/alert_types.hpp index 672096466..c810dd3f6 100755 --- a/libtorrent/include/libtorrent/alert_types.hpp +++ b/libtorrent/include/libtorrent/alert_types.hpp @@ -384,4 +384,3 @@ namespace libtorrent #endif - diff --git a/libtorrent/include/libtorrent/connection_queue.hpp b/libtorrent/include/libtorrent/connection_queue.hpp index 17be248bf..b3b7cde86 100644 --- a/libtorrent/include/libtorrent/connection_queue.hpp +++ b/libtorrent/include/libtorrent/connection_queue.hpp @@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "libtorrent/socket.hpp" #include "libtorrent/time.hpp" @@ -88,6 +89,10 @@ private: int m_half_open_limit; deadline_timer m_timer; + + typedef boost::recursive_mutex mutex_t; + mutable mutex_t m_mutex; + #ifndef NDEBUG bool m_in_timeout_function; #endif diff --git a/libtorrent/include/libtorrent/piece_picker.hpp b/libtorrent/include/libtorrent/piece_picker.hpp index 66c91e2ab..08cfebd96 100755 --- a/libtorrent/include/libtorrent/piece_picker.hpp +++ b/libtorrent/include/libtorrent/piece_picker.hpp @@ -456,4 +456,3 @@ namespace libtorrent #endif // TORRENT_PIECE_PICKER_HPP_INCLUDED - diff --git a/libtorrent/src/connection_queue.cpp b/libtorrent/src/connection_queue.cpp index 859205ed0..544ebec6b 100644 --- a/libtorrent/src/connection_queue.cpp +++ b/libtorrent/src/connection_queue.cpp @@ -54,6 +54,8 @@ namespace libtorrent , boost::function const& on_timeout , time_duration timeout) { + mutex_t::scoped_lock l(m_mutex); + INVARIANT_CHECK; m_queue.push_back(entry()); @@ -68,6 +70,8 @@ namespace libtorrent void connection_queue::done(int ticket) { + mutex_t::scoped_lock l(m_mutex); + INVARIANT_CHECK; std::list::iterator i = std::find_if(m_queue.begin() @@ -105,6 +109,8 @@ namespace libtorrent void connection_queue::try_connect() { + mutex_t::scoped_lock l(m_mutex); + INVARIANT_CHECK; if (!free_slots() || m_queue.empty()) @@ -148,6 +154,8 @@ namespace libtorrent void connection_queue::on_timeout(asio::error_code const& e) { + mutex_t::scoped_lock l(m_mutex); + INVARIANT_CHECK; #ifndef NDEBUG function_guard guard_(m_in_timeout_function); diff --git a/libtorrent/src/peer_connection.cpp b/libtorrent/src/peer_connection.cpp index 410197372..a3d522b89 100755 --- a/libtorrent/src/peer_connection.cpp +++ b/libtorrent/src/peer_connection.cpp @@ -3003,4 +3003,3 @@ namespace libtorrent } } - diff --git a/libtorrent/src/piece_picker.cpp b/libtorrent/src/piece_picker.cpp index d2eb652fd..6d2fc2218 100755 --- a/libtorrent/src/piece_picker.cpp +++ b/libtorrent/src/piece_picker.cpp @@ -1789,4 +1789,3 @@ namespace libtorrent } - diff --git a/libtorrent/src/session_impl.cpp b/libtorrent/src/session_impl.cpp index 55c5cc32c..3d687eaf8 100755 --- a/libtorrent/src/session_impl.cpp +++ b/libtorrent/src/session_impl.cpp @@ -1239,7 +1239,8 @@ namespace detail if (num_connections() >= max_connections() && !m_torrents.empty()) { torrent_map::iterator i = std::max_element(m_torrents.begin(), m_torrents.end() - , bind(&torrent::num_peers, bind(&torrent_map::value_type::second, _1))); + , bind(&torrent::num_peers, bind(&torrent_map::value_type::second, _1)) + < bind(&torrent::num_peers, bind(&torrent_map::value_type::second, _2))); assert(i != m_torrents.end()); i->second->get_policy().disconnect_one_peer();