@@ -64,6 +64,9 @@ map<CInv, int64> mapAlreadyAskedFor;
64
64
set<CNetAddr> setservAddNodeAddresses;
65
65
CCriticalSection cs_setservAddNodeAddresses;
66
66
67
+ static CWaitableCriticalSection csOutbound;
68
+ static int nOutbound = 0;
69
+ static CConditionVariable condOutbound;
67
70
68
71
69
72
unsigned short GetListenPort()
@@ -460,6 +463,8 @@ CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
460
463
pnode->AddRef();
461
464
CRITICAL_BLOCK(cs_vNodes)
462
465
vNodes.push_back(pnode);
466
+ WAITABLE_CRITICAL_BLOCK(csOutbound)
467
+ nOutbound++;
463
468
464
469
pnode->nTimeConnected = GetTime();
465
470
return pnode;
@@ -610,6 +615,15 @@ void ThreadSocketHandler2(void* parg)
610
615
// remove from vNodes
611
616
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
612
617
618
+ if (!pnode->fInbound)
619
+ WAITABLE_CRITICAL_BLOCK(csOutbound)
620
+ {
621
+ nOutbound--;
622
+
623
+ // Connection slot(s) were removed, notify connection creator(s)
624
+ NOTIFY(condOutbound);
625
+ }
626
+
613
627
// close socket and cleanup
614
628
pnode->CloseSocketDisconnect();
615
629
pnode->Cleanup();
@@ -1278,32 +1292,20 @@ void ThreadOpenConnections2(void* parg)
1278
1292
int64 nStart = GetTime();
1279
1293
loop
1280
1294
{
1281
- int nOutbound = 0;
1282
-
1283
1295
vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
1284
1296
Sleep(500);
1285
1297
vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
1286
1298
if (fShutdown)
1287
1299
return;
1288
1300
1289
1301
// Limit outbound connections
1290
- loop
1291
- {
1292
- nOutbound = 0;
1293
- CRITICAL_BLOCK(cs_vNodes)
1294
- BOOST_FOREACH(CNode* pnode, vNodes)
1295
- if (!pnode->fInbound)
1296
- nOutbound++;
1297
- int nMaxOutboundConnections = MAX_OUTBOUND_CONNECTIONS;
1298
- nMaxOutboundConnections = min(nMaxOutboundConnections, (int)GetArg("-maxconnections", 125));
1299
- if (nOutbound < nMaxOutboundConnections)
1300
- break;
1301
- vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
1302
- Sleep(2000);
1303
- vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
1304
- if (fShutdown)
1305
- return;
1306
- }
1302
+ int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)GetArg("-maxconnections", 125));
1303
+ vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
1304
+ WAITABLE_CRITICAL_BLOCK(csOutbound)
1305
+ WAIT(condOutbound, fShutdown || nOutbound < nMaxOutbound);
1306
+ vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
1307
+ if (fShutdown)
1308
+ return;
1307
1309
1308
1310
bool fAddSeeds = false;
1309
1311
@@ -1752,6 +1754,7 @@ bool StopNode()
1752
1754
fShutdown = true;
1753
1755
nTransactionsUpdated++;
1754
1756
int64 nStart = GetTime();
1757
+ NOTIFY_ALL(condOutbound);
1755
1758
do
1756
1759
{
1757
1760
int nThreadsRunning = 0;
0 commit comments