Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shared lock for wallet registration functions. #2828

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using namespace boost;
// Global state
//

CCriticalSection cs_setpwalletRegistered;
boost::shared_mutex cs_setpwalletRegistered;
set<CWallet*> setpwalletRegistered;

CCriticalSection cs_main;
Expand Down Expand Up @@ -85,30 +85,26 @@ int64 nTransactionFee = 0;

void RegisterWallet(CWallet* pwalletIn)
{
{
LOCK(cs_setpwalletRegistered);
setpwalletRegistered.insert(pwalletIn);
}
boost::unique_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
setpwalletRegistered.insert(pwalletIn);
}

void UnregisterWallet(CWallet* pwalletIn)
{
{
LOCK(cs_setpwalletRegistered);
setpwalletRegistered.erase(pwalletIn);
}
boost::unique_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
setpwalletRegistered.erase(pwalletIn);
}

void UnregisterAllWallets()
{
LOCK(cs_setpwalletRegistered);
boost::unique_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
setpwalletRegistered.clear();
}

// get the wallet transaction with the given hash (if it exists)
bool static GetTransaction(const uint256& hashTx, CWalletTx& wtx)
{
LOCK(cs_setpwalletRegistered);
boost::shared_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
if (pwallet->GetTransaction(hashTx,wtx))
return true;
Expand All @@ -118,55 +114,55 @@ bool static GetTransaction(const uint256& hashTx, CWalletTx& wtx)
// erases transaction with the given hash from all wallets
void static EraseFromWallets(uint256 hash)
{
LOCK(cs_setpwalletRegistered);
boost::shared_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
pwallet->EraseFromWallet(hash);
}

// make sure all wallets know about the given transaction, in the given block
void SyncWithWallets(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate)
{
LOCK(cs_setpwalletRegistered);
boost::shared_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
pwallet->AddToWalletIfInvolvingMe(hash, tx, pblock, fUpdate);
}

// notify wallets about a new best chain
void static SetBestChain(const CBlockLocator& loc)
{
LOCK(cs_setpwalletRegistered);
boost::shared_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
pwallet->SetBestChain(loc);
}

// notify wallets about an updated transaction
void static UpdatedTransaction(const uint256& hashTx)
{
LOCK(cs_setpwalletRegistered);
boost::shared_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
pwallet->UpdatedTransaction(hashTx);
}

// dump all wallets
void static PrintWallets(const CBlock& block)
{
LOCK(cs_setpwalletRegistered);
boost::shared_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
pwallet->PrintWallet(block);
}

// notify wallets about an incoming inventory (for request counts)
void static Inventory(const uint256& hash)
{
LOCK(cs_setpwalletRegistered);
boost::shared_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
pwallet->Inventory(hash);
}

// ask wallets to resend their transactions
void static ResendWalletTransactions()
{
LOCK(cs_setpwalletRegistered);
boost::shared_lock<boost::shared_mutex> lock(cs_setpwalletRegistered);
BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered)
pwallet->ResendWalletTransactions();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extern const std::string strMessageMagic;
extern double dHashesPerSec;
extern int64 nHPSTimerStart;
extern int64 nTimeBestReceived;
extern CCriticalSection cs_setpwalletRegistered;
extern boost::shared_mutex cs_setpwalletRegistered;
extern std::set<CWallet*> setpwalletRegistered;
extern bool fImporting;
extern bool fReindex;
Expand Down