Skip to content

Commit c052719

Browse files
committed
Merge pull request #1260 from sipa/splitsync
Split synchronization mechanisms from util.{h,cpp}
2 parents b34c5f3 + 7f3ccb5 commit c052719

16 files changed

+388
-281
lines changed

bitcoin-qt.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ HEADERS += src/qt/bitcoingui.h \
109109
src/bignum.h \
110110
src/checkpoints.h \
111111
src/compat.h \
112+
src/sync.h \
112113
src/util.h \
113114
src/uint256.h \
114115
src/serialize.h \
@@ -172,6 +173,7 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
172173
src/qt/editaddressdialog.cpp \
173174
src/qt/bitcoinaddressvalidator.cpp \
174175
src/version.cpp \
176+
src/sync.cpp \
175177
src/util.cpp \
176178
src/netbase.cpp \
177179
src/key.cpp \

src/addrman.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "netbase.h"
88
#include "protocol.h"
99
#include "util.h"
10+
#include "sync.h"
1011

1112

1213
#include <map>

src/keystore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define BITCOIN_KEYSTORE_H
77

88
#include "crypter.h"
9-
#include "util.h"
9+
#include "sync.h"
1010
#include "base58.h"
1111

1212
class CScript;

src/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_MAIN_H
77

88
#include "bignum.h"
9+
#include "sync.h"
910
#include "net.h"
1011
#include "script.h"
1112

src/makefile.linux-mingw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ OBJS= \
6161
obj/bitcoinrpc.o \
6262
obj/rpcdump.o \
6363
obj/script.o \
64+
obj/sync.o \
6465
obj/util.o \
6566
obj/wallet.o \
6667
obj/walletdb.o \

src/makefile.mingw

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ OBJS= \
5858
obj/bitcoinrpc.o \
5959
obj/rpcdump.o \
6060
obj/script.o \
61+
obj/sync.o \
6162
obj/util.o \
6263
obj/wallet.o \
6364
obj/walletdb.o \

src/makefile.osx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ OBJS= \
8585
obj/bitcoinrpc.o \
8686
obj/rpcdump.o \
8787
obj/script.o \
88+
obj/sync.o \
8889
obj/util.o \
8990
obj/wallet.o \
9091
obj/walletdb.o \

src/makefile.unix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ OBJS= \
102102
obj/bitcoinrpc.o \
103103
obj/rpcdump.o \
104104
obj/script.o \
105+
obj/sync.o \
105106
obj/util.o \
106107
obj/wallet.o \
107108
obj/walletdb.o \

src/net.cpp

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void ThreadOpenAddedConnections2(void* parg);
3535
void ThreadMapPort2(void* parg);
3636
#endif
3737
void ThreadDNSAddressSeed2(void* parg);
38-
bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest = NULL, bool fOneShot = false);
38+
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
3939

4040

4141

@@ -66,10 +66,7 @@ CCriticalSection cs_vOneShots;
6666
set<CNetAddr> setservAddNodeAddresses;
6767
CCriticalSection cs_setservAddNodeAddresses;
6868

69-
static CWaitableCriticalSection csOutbound;
70-
static int nOutbound = 0;
71-
static CConditionVariable condOutbound;
72-
69+
static CSemaphore *semOutbound = NULL;
7370

7471
void AddOneShot(string strDest)
7572
{
@@ -463,10 +460,6 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest, int64 nTimeout)
463460
LOCK(cs_vNodes);
464461
vNodes.push_back(pnode);
465462
}
466-
{
467-
WAITABLE_LOCK(csOutbound);
468-
nOutbound++;
469-
}
470463

471464
pnode->nTimeConnected = GetTime();
472465
return pnode;
@@ -612,14 +605,8 @@ void ThreadSocketHandler2(void* parg)
612605
// remove from vNodes
613606
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
614607

615-
if (!pnode->fInbound)
616-
{
617-
WAITABLE_LOCK(csOutbound);
618-
nOutbound--;
619-
620-
// Connection slot(s) were removed, notify connection creator(s)
621-
NOTIFY(condOutbound);
622-
}
608+
// release outbound grant (if any)
609+
pnode->grantOutbound.Release();
623610

624611
// close socket and cleanup
625612
pnode->CloseSocketDisconnect();
@@ -1295,8 +1282,11 @@ void static ProcessOneShot()
12951282
vOneShots.pop_front();
12961283
}
12971284
CAddress addr;
1298-
if (!OpenNetworkConnection(addr, strDest.c_str(), true))
1299-
AddOneShot(strDest);
1285+
CSemaphoreGrant grant(*semOutbound, true);
1286+
if (grant) {
1287+
if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true))
1288+
AddOneShot(strDest);
1289+
}
13001290
}
13011291

13021292
void ThreadOpenConnections2(void* parg)
@@ -1312,7 +1302,7 @@ void ThreadOpenConnections2(void* parg)
13121302
BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
13131303
{
13141304
CAddress addr;
1315-
OpenNetworkConnection(addr, strAddr.c_str());
1305+
OpenNetworkConnection(addr, NULL, strAddr.c_str());
13161306
for (int i = 0; i < 10 && i < nLoop; i++)
13171307
{
13181308
Sleep(500);
@@ -1335,13 +1325,9 @@ void ThreadOpenConnections2(void* parg)
13351325
if (fShutdown)
13361326
return;
13371327

1338-
// Limit outbound connections
1339-
int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)GetArg("-maxconnections", 125));
1328+
13401329
vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
1341-
{
1342-
WAITABLE_LOCK(csOutbound);
1343-
WAIT(condOutbound, fShutdown || nOutbound < nMaxOutbound);
1344-
}
1330+
CSemaphoreGrant grant(*semOutbound);
13451331
vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
13461332
if (fShutdown)
13471333
return;
@@ -1374,11 +1360,15 @@ void ThreadOpenConnections2(void* parg)
13741360

13751361
// Only connect to one address per a.b.?.? range.
13761362
// Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
1363+
int nOutbound = 0;
13771364
set<vector<unsigned char> > setConnected;
13781365
{
13791366
LOCK(cs_vNodes);
1380-
BOOST_FOREACH(CNode* pnode, vNodes)
1367+
BOOST_FOREACH(CNode* pnode, vNodes) {
13811368
setConnected.insert(pnode->addr.GetGroup());
1369+
if (!pnode->fInbound)
1370+
nOutbound++;
1371+
}
13821372
}
13831373

13841374
int64 nANow = GetAdjustedTime();
@@ -1408,7 +1398,7 @@ void ThreadOpenConnections2(void* parg)
14081398
}
14091399

14101400
if (addrConnect.IsValid())
1411-
OpenNetworkConnection(addrConnect);
1401+
OpenNetworkConnection(addrConnect, &grant);
14121402
}
14131403
}
14141404

@@ -1442,7 +1432,8 @@ void ThreadOpenAddedConnections2(void* parg)
14421432
while(!fShutdown) {
14431433
BOOST_FOREACH(string& strAddNode, mapMultiArgs["-addnode"]) {
14441434
CAddress addr;
1445-
OpenNetworkConnection(addr, strAddNode.c_str());
1435+
CSemaphoreGrant grant(*semOutbound);
1436+
OpenNetworkConnection(addr, &grant, strAddNode.c_str());
14461437
Sleep(500);
14471438
}
14481439
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--;
@@ -1485,7 +1476,8 @@ void ThreadOpenAddedConnections2(void* parg)
14851476
}
14861477
BOOST_FOREACH(vector<CService>& vserv, vservConnectAddresses)
14871478
{
1488-
OpenNetworkConnection(CAddress(*(vserv.begin())));
1479+
CSemaphoreGrant grant(*semOutbound);
1480+
OpenNetworkConnection(CAddress(*(vserv.begin())), &grant);
14891481
Sleep(500);
14901482
if (fShutdown)
14911483
return;
@@ -1500,7 +1492,8 @@ void ThreadOpenAddedConnections2(void* parg)
15001492
}
15011493
}
15021494

1503-
bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest, bool fOneShot)
1495+
// if succesful, this moves the passed grant to the constructed node
1496+
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound, const char *strDest, bool fOneShot)
15041497
{
15051498
//
15061499
// Initiate outbound network connection
@@ -1522,6 +1515,8 @@ bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest, boo
15221515
return false;
15231516
if (!pnode)
15241517
return false;
1518+
if (grantOutbound)
1519+
grantOutbound->MoveTo(pnode->grantOutbound);
15251520
pnode->fNetworkNode = true;
15261521
if (fOneShot)
15271522
pnode->fOneShot = true;
@@ -1770,6 +1765,12 @@ void StartNode(void* parg)
17701765
#endif
17711766
#endif
17721767

1768+
if (semOutbound == NULL) {
1769+
// initialize semaphore
1770+
int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)GetArg("-maxconnections", 125));
1771+
semOutbound = new CSemaphore(nMaxOutbound);
1772+
}
1773+
17731774
if (pnodeLocalHost == NULL)
17741775
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));
17751776

@@ -1823,7 +1824,8 @@ bool StopNode()
18231824
fShutdown = true;
18241825
nTransactionsUpdated++;
18251826
int64 nStart = GetTime();
1826-
NOTIFY_ALL(condOutbound);
1827+
for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
1828+
semOutbound->post();
18271829
do
18281830
{
18291831
int nThreadsRunning = 0;

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class CNode
148148
bool fNetworkNode;
149149
bool fSuccessfullyConnected;
150150
bool fDisconnect;
151+
CSemaphoreGrant grantOutbound;
151152
protected:
152153
int nRefCount;
153154

0 commit comments

Comments
 (0)