fixed race condition in dht

This commit is contained in:
Arvid Norberg 2008-03-24 04:38:43 +00:00
parent e1eadab1b4
commit 1d8edc0722
2 changed files with 19 additions and 3 deletions

View File

@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/filesystem/operations.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/detail/atomic_count.hpp>
#include <boost/thread/mutex.hpp>
#include "libtorrent/kademlia/node.hpp"
#include "libtorrent/kademlia/node_id.hpp"
@ -117,6 +118,12 @@ namespace libtorrent { namespace dht
dht_settings const& m_settings;
int m_refresh_bucket;
// The mutex is used to abort the dht node
// it's only used to set m_abort to true
typedef boost::mutex mutex_t;
mutable mutex_t m_mutex;
bool m_abort;
// used to resolve hostnames for nodes
udp::resolver m_host_resolver;

View File

@ -155,6 +155,7 @@ namespace libtorrent { namespace dht
, m_refresh_timer(sock.get_io_service())
, m_settings(settings)
, m_refresh_bucket(160)
, m_abort(false)
, m_host_resolver(sock.get_io_service())
, m_refs(0)
{
@ -212,6 +213,8 @@ namespace libtorrent { namespace dht
void dht_tracker::stop()
{
mutex_t::scoped_lock l(m_mutex);
m_abort = true;
m_timer.cancel();
m_connection_timer.cancel();
m_refresh_timer.cancel();
@ -228,7 +231,9 @@ namespace libtorrent { namespace dht
void dht_tracker::connection_timeout(asio::error_code const& e)
try
{
if (e) return;
mutex_t::scoped_lock l(m_mutex);
if (e || m_abort) return;
time_duration d = m_dht.connection_timeout();
m_connection_timer.expires_from_now(d);
m_connection_timer.async_wait(bind(&dht_tracker::connection_timeout, self(), _1));
@ -245,7 +250,9 @@ namespace libtorrent { namespace dht
void dht_tracker::refresh_timeout(asio::error_code const& e)
try
{
if (e) return;
mutex_t::scoped_lock l(m_mutex);
if (e || m_abort) return;
time_duration d = m_dht.refresh_timeout();
m_refresh_timer.expires_from_now(d);
m_refresh_timer.async_wait(
@ -259,7 +266,9 @@ namespace libtorrent { namespace dht
void dht_tracker::tick(asio::error_code const& e)
try
{
if (e) return;
mutex_t::scoped_lock l(m_mutex);
if (e || m_abort) return;
m_timer.expires_from_now(minutes(tick_period));
m_timer.async_wait(bind(&dht_tracker::tick, self(), _1));