diff --git a/Tunnel.cpp b/Tunnel.cpp index 7a17b61e..a372452c 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -243,7 +243,6 @@ namespace tunnel Tunnel * Tunnels::GetPendingTunnel (uint32_t replyMsgID) { - std::unique_lock l(m_PendingTunnelsMutex); auto it = m_PendingTunnels.find(replyMsgID); if (it != m_PendingTunnels.end () && it->second->GetState () == eTunnelStatePending) { @@ -298,19 +297,7 @@ namespace tunnel void Tunnels::DeleteTunnelPool (TunnelPool * pool) { - if (pool) - { - { - std::unique_lock l(m_PoolsMutex); - m_Pools.erase (pool->GetIdentHash ()); - } - { - std::unique_lock l(m_PendingTunnelsMutex); - for (auto it: m_PendingTunnels) - if (it.second->GetTunnelPool () == pool) it.second->SetTunnelPool (nullptr); - } - delete pool; - } + if (pool) pool->SetDeleted (); } void Tunnels::AddTransitTunnel (TransitTunnel * tunnel) @@ -394,7 +381,6 @@ namespace tunnel { // check pending tunnel. delete failed or timeout uint64_t ts = i2p::util::GetSecondsSinceEpoch (); - std::unique_lock l(m_PendingTunnelsMutex); for (auto it = m_PendingTunnels.begin (); it != m_PendingTunnels.end ();) { auto tunnel = it->second; @@ -546,10 +532,22 @@ namespace tunnel void Tunnels::ManageTunnelPools () { std::unique_lock l(m_PoolsMutex); - for (auto& it: m_Pools) + for (auto it = m_Pools.begin (); it != m_Pools.end ();) { - it.second->CreateTunnels (); - it.second->TestTunnels (); + TunnelPool * pool = it->second; + if (!pool->IsDeleted ()) + { + pool->CreateTunnels (); + pool->TestTunnels (); + it++; + } + else + { + it = m_Pools.erase (it); + for (auto it1: m_PendingTunnels) + if (it1.second->GetTunnelPool () == pool) it1.second->SetTunnelPool (nullptr); + delete pool; + } } } diff --git a/Tunnel.h b/Tunnel.h index 7ce1c906..c333edb1 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -148,7 +148,6 @@ namespace tunnel bool m_IsRunning; std::thread * m_Thread; - std::mutex m_PendingTunnelsMutex; std::map m_PendingTunnels; // by replyMsgID std::mutex m_InboundTunnelsMutex; std::map m_InboundTunnels; diff --git a/TunnelPool.cpp b/TunnelPool.cpp index a82e8934..c95f6fb3 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -11,7 +11,8 @@ namespace i2p namespace tunnel { TunnelPool::TunnelPool (i2p::garlic::GarlicDestination& localDestination, int numHops, int numTunnels): - m_LocalDestination (localDestination), m_NumHops (numHops), m_NumTunnels (numTunnels) + m_LocalDestination (localDestination), m_NumHops (numHops), m_NumTunnels (numTunnels), + m_IsDeleted (false) { } diff --git a/TunnelPool.h b/TunnelPool.h index 1a231a94..816decf3 100644 --- a/TunnelPool.h +++ b/TunnelPool.h @@ -48,6 +48,9 @@ namespace tunnel void TestTunnels (); void ProcessDeliveryStatus (I2NPMessage * msg); + bool IsDeleted () const { return m_IsDeleted; }; + void SetDeleted () { m_IsDeleted = true; } + private: void CreateInboundTunnel (); @@ -68,6 +71,7 @@ namespace tunnel mutable std::mutex m_OutboundTunnelsMutex; std::set m_OutboundTunnels; std::map > m_Tests; + bool m_IsDeleted; public: