fixed race condition when saving DHT state

This commit is contained in:
Arvid Norberg 2009-01-23 16:40:00 +00:00
parent 2b4a49ea19
commit 9b5aa967b0
3 changed files with 23 additions and 2 deletions

View File

@ -47,6 +47,7 @@ release 0.14.2
* fixed bug in http_connection when binding to a particular IP
* fixed typo in python binding (torrent_handle::piece_prioritize should
be torrent_handle::piece_priorities)
* fixed race condition when saving DHT state
release 0.14.1

View File

@ -53,6 +53,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/filesystem/path.hpp>
#include <boost/thread.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
@ -176,6 +177,7 @@ namespace libtorrent
dht_settings const& get_dht_settings() const { return m_dht_settings; }
void start_dht(entry const& startup_state);
void stop_dht();
entry dht_state() const;
void maybe_update_udp_mapping(int nat, int local_port, int external_port);
#endif
@ -327,6 +329,8 @@ namespace libtorrent
// private:
void session_impl::dht_state_callback(boost::condition_variable_any& c
, entry& e, bool& done) const;
void on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih);
#ifndef TORRENT_DISABLE_POOL_ALLOCATOR
@ -347,7 +351,7 @@ namespace libtorrent
// this is where all active sockets are stored.
// the selector can sleep while there's no activity on
// them
io_service m_io_service;
mutable io_service m_io_service;
// handles disk io requests asynchronously
// peers have pointers into the disk buffer

View File

@ -2331,11 +2331,27 @@ namespace aux {
m_dht_settings.service_port = m_listen_interface.port();
}
void session_impl::dht_state_callback(boost::condition_variable_any& c
, entry& e, bool& done) const
{
if (m_dht) e = m_dht->state();
mutex_t::scoped_lock l(m_mutex);
done = true;
l.unlock();
c.notify_all();
}
entry session_impl::dht_state() const
{
boost::condition_variable_any cond;
mutex_t::scoped_lock l(m_mutex);
if (!m_dht) return entry();
return m_dht->state();
entry e;
bool done = false;
m_io_service.post(boost::bind(&session_impl::dht_state_callback
, this, boost::ref(cond), boost::ref(e), boost::ref(done)));
while (!done) cond.wait(l);
return e;
}
void session_impl::add_dht_node(std::pair<std::string, int> const& node)