fixed race condition

This commit is contained in:
orignal 2014-10-05 21:59:05 -04:00
parent 78fc3876e6
commit 0f3a68cd8e
4 changed files with 11 additions and 0 deletions

View File

@ -182,9 +182,12 @@ namespace data
RouterInfo * r = new RouterInfo (buf, len);
m_RouterInfos[r->GetIdentHash ()] = r;
if (r->IsFloodfill ())
{
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
m_Floodfills.push_back (r);
}
}
}
void NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len)
{
@ -852,6 +855,7 @@ namespace data
XORMetric minMetric;
RoutingKey destKey = CreateRoutingKey (destination);
minMetric.SetMax ();
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
for (auto it: m_Floodfills)
{
if (!it->IsUnreachable () && !excluded.count (it->GetIdentHash ()))

View File

@ -7,6 +7,7 @@
#include <vector>
#include <string>
#include <thread>
#include <mutex>
#include <boost/filesystem.hpp>
#include "Queue.h"
#include "I2NPProtocol.h"
@ -114,6 +115,7 @@ namespace data
std::map<IdentHash, LeaseSet *> m_LeaseSets;
std::map<IdentHash, RouterInfo *> m_RouterInfos;
mutable std::mutex m_FloodfillsMutex;
std::vector<RouterInfo *> m_Floodfills;
std::mutex m_RequestedDestinationsMutex;
std::map<IdentHash, RequestedDestination *> m_RequestedDestinations;

View File

@ -582,6 +582,7 @@ namespace stream
{
SAMSession session;
session.localDestination = localDestination;
std::unique_lock<std::mutex> l(m_SessionsMutex);
auto ret = m_Sessions.insert (std::pair<std::string, SAMSession>(id, session));
if (!ret.second)
LogPrint ("Session ", id, " already exists");
@ -592,6 +593,7 @@ namespace stream
void SAMBridge::CloseSession (const std::string& id)
{
std::unique_lock<std::mutex> l(m_SessionsMutex);
auto it = m_Sessions.find (id);
if (it != m_Sessions.end ())
{
@ -605,6 +607,7 @@ namespace stream
SAMSession * SAMBridge::FindSession (const std::string& id)
{
std::unique_lock<std::mutex> l(m_SessionsMutex);
auto it = m_Sessions.find (id);
if (it != m_Sessions.end ())
return &it->second;

2
SAM.h
View File

@ -6,6 +6,7 @@
#include <map>
#include <list>
#include <thread>
#include <mutex>
#include <boost/asio.hpp>
#include "Identity.h"
#include "LeaseSet.h"
@ -144,6 +145,7 @@ namespace stream
boost::asio::io_service m_Service;
boost::asio::ip::tcp::acceptor m_Acceptor;
SAMSocket * m_NewSocket;
std::mutex m_SessionsMutex;
std::map<std::string, SAMSession> m_Sessions;
};
}