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:
parent
9278124907
commit
4d1119c45f
3 changed files with 2 additions and 9 deletions
|
|
@ -2,31 +2,26 @@
|
||||||
|
|
||||||
#include <crypto/cryptopp/osrng.h>
|
#include <crypto/cryptopp/osrng.h>
|
||||||
|
|
||||||
std::mutex nano::random_pool::mutex;
|
|
||||||
|
|
||||||
void nano::random_pool::generate_block (unsigned char * output, size_t size)
|
void nano::random_pool::generate_block (unsigned char * output, size_t size)
|
||||||
{
|
{
|
||||||
auto & pool = get_pool ();
|
auto & pool = get_pool ();
|
||||||
std::lock_guard guard (mutex);
|
|
||||||
pool.GenerateBlock (output, size);
|
pool.GenerateBlock (output, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned nano::random_pool::generate_word32 (unsigned min, unsigned max)
|
unsigned nano::random_pool::generate_word32 (unsigned min, unsigned max)
|
||||||
{
|
{
|
||||||
auto & pool = get_pool ();
|
auto & pool = get_pool ();
|
||||||
std::lock_guard guard (mutex);
|
|
||||||
return pool.GenerateWord32 (min, max);
|
return pool.GenerateWord32 (min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char nano::random_pool::generate_byte ()
|
unsigned char nano::random_pool::generate_byte ()
|
||||||
{
|
{
|
||||||
auto & pool = get_pool ();
|
auto & pool = get_pool ();
|
||||||
std::lock_guard guard (mutex);
|
|
||||||
return pool.GenerateByte ();
|
return pool.GenerateByte ();
|
||||||
}
|
}
|
||||||
|
|
||||||
CryptoPP::AutoSeededRandomPool & nano::random_pool::get_pool ()
|
CryptoPP::AutoSeededRandomPool & nano::random_pool::get_pool ()
|
||||||
{
|
{
|
||||||
static CryptoPP::AutoSeededRandomPool pool;
|
static thread_local CryptoPP::AutoSeededRandomPool pool;
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,10 +22,9 @@ public:
|
||||||
random_pool & operator= (random_pool const &) = delete;
|
random_pool & operator= (random_pool const &) = delete;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::mutex mutex;
|
|
||||||
static CryptoPP::AutoSeededRandomPool & get_pool ();
|
static CryptoPP::AutoSeededRandomPool & get_pool ();
|
||||||
|
|
||||||
template <class Iter>
|
template <class Iter>
|
||||||
friend void random_pool_shuffle (Iter begin, Iter end);
|
friend void random_pool_shuffle (Iter begin, Iter end);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ namespace nano
|
||||||
template <class Iter>
|
template <class Iter>
|
||||||
void random_pool_shuffle (Iter begin, Iter end)
|
void random_pool_shuffle (Iter begin, Iter end)
|
||||||
{
|
{
|
||||||
std::lock_guard guard (random_pool::mutex);
|
|
||||||
random_pool::get_pool ().Shuffle (begin, end);
|
random_pool::get_pool ().Shuffle (begin, end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue