Use C++11 thread-safe static initializers

This commit is contained in:
Pieter Wuille 2016-06-07 16:29:03 +02:00
parent c31b24f745
commit 888483098e
4 changed files with 6 additions and 17 deletions

View file

@ -56,11 +56,7 @@ void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); } bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); } CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); }
SaltedTxidHasher::SaltedTxidHasher() SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
{
GetRandBytes((unsigned char*)&k0, sizeof(k0));
GetRandBytes((unsigned char*)&k1, sizeof(k1));
}
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), hasModifier(false), cachedCoinsUsage(0) { } CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), hasModifier(false), cachedCoinsUsage(0) { }

View file

@ -269,7 +269,7 @@ class SaltedTxidHasher
{ {
private: private:
/** Salt */ /** Salt */
uint64_t k0, k1; const uint64_t k0, k1;
public: public:
SaltedTxidHasher(); SaltedTxidHasher();

View file

@ -4784,11 +4784,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
LOCK(cs_vNodes); LOCK(cs_vNodes);
// Use deterministic randomness to send to the same nodes for 24 hours // Use deterministic randomness to send to the same nodes for 24 hours
// at a time so the addrKnowns of the chosen nodes prevent repeats // at a time so the addrKnowns of the chosen nodes prevent repeats
static uint64_t salt0 = 0, salt1 = 0; static const uint64_t salt0 = GetRand(std::numeric_limits<uint64_t>::max());
while (salt0 == 0 && salt1 == 0) { static const uint64_t salt1 = GetRand(std::numeric_limits<uint64_t>::max());
GetRandBytes((unsigned char*)&salt0, sizeof(salt0));
GetRandBytes((unsigned char*)&salt1, sizeof(salt1));
}
uint64_t hashAddr = addr.GetHash(); uint64_t hashAddr = addr.GetHash();
multimap<uint64_t, CNode*> mapMix; multimap<uint64_t, CNode*> mapMix;
const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60)); const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));

View file

@ -2601,12 +2601,8 @@ int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
/* static */ uint64_t CNode::CalculateKeyedNetGroup(const CAddress& ad) /* static */ uint64_t CNode::CalculateKeyedNetGroup(const CAddress& ad)
{ {
static uint64_t k0 = 0, k1 = 0; static const uint64_t k0 = GetRand(std::numeric_limits<uint64_t>::max());
while (k0 == 0 && k1 == 0) { static const uint64_t k1 = GetRand(std::numeric_limits<uint64_t>::max());
// Make sure this only runs on the first invocation.
GetRandBytes((unsigned char*)&k0, sizeof(k0));
GetRandBytes((unsigned char*)&k1, sizeof(k1));
}
std::vector<unsigned char> vchNetGroup(ad.GetGroup()); std::vector<unsigned char> vchNetGroup(ad.GetGroup());