Skip to content

Commit

Permalink
Removing static mutex for the random pool and replacing the random po…
Browse files Browse the repository at this point in the history
…ol with a thread_local version so it is thread safe. (#3408)
  • Loading branch information
clemahieu committed Jul 29, 2021
1 parent 9278124 commit 4d1119c
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 9 deletions.
7 changes: 1 addition & 6 deletions nano/crypto_lib/random_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 1 addition & 2 deletions nano/crypto_lib/random_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ class random_pool
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);
};
}
}
1 change: 0 additions & 1 deletion nano/crypto_lib/random_pool_shuffle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4d1119c

Please sign in to comment.