Removing static mutex for the random pool and replacing the random pool with a thread_local version so it is thread safe. (#3408)

This commit is contained in:
clemahieu 2021-07-29 13:10:29 +01:00 committed by GitHub
commit 4d1119c45f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 2 additions and 9 deletions

View file

@ -2,31 +2,26 @@
#include <crypto/cryptopp/osrng.h>
std::mutex nano::random_pool::mutex;
void nano::random_pool::generate_block (unsigned char * output, size_t size)
{
auto & pool = get_pool ();
std::lock_guard guard (mutex);
pool.GenerateBlock (output, size);
}
unsigned nano::random_pool::generate_word32 (unsigned min, unsigned max)
{
auto & pool = get_pool ();
std::lock_guard guard (mutex);
return pool.GenerateWord32 (min, max);
}
unsigned char nano::random_pool::generate_byte ()
{
auto & pool = get_pool ();
std::lock_guard guard (mutex);
return pool.GenerateByte ();
}
CryptoPP::AutoSeededRandomPool & nano::random_pool::get_pool ()
{
static CryptoPP::AutoSeededRandomPool pool;
static thread_local CryptoPP::AutoSeededRandomPool pool;
return pool;
}

View file

@ -22,10 +22,9 @@ public:
random_pool & operator= (random_pool const &) = delete;
private:
static std::mutex mutex;
static CryptoPP::AutoSeededRandomPool & get_pool ();
template <class Iter>
friend void random_pool_shuffle (Iter begin, Iter end);
};
}
}

View file

@ -9,7 +9,6 @@ namespace nano
template <class Iter>
void random_pool_shuffle (Iter begin, Iter end)
{
std::lock_guard guard (random_pool::mutex);
random_pool::get_pool ().Shuffle (begin, end);
}
}