From 4d1119c45fb894850eec460719c3467ca0d3d97b Mon Sep 17 00:00:00 2001 From: clemahieu Date: Thu, 29 Jul 2021 13:10:29 +0100 Subject: [PATCH] Removing static mutex for the random pool and replacing the random pool with a thread_local version so it is thread safe. (#3408) --- nano/crypto_lib/random_pool.cpp | 7 +------ nano/crypto_lib/random_pool.hpp | 3 +-- nano/crypto_lib/random_pool_shuffle.hpp | 1 - 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/nano/crypto_lib/random_pool.cpp b/nano/crypto_lib/random_pool.cpp index 4b57d14f..398466dc 100644 --- a/nano/crypto_lib/random_pool.cpp +++ b/nano/crypto_lib/random_pool.cpp @@ -2,31 +2,26 @@ #include -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; } diff --git a/nano/crypto_lib/random_pool.hpp b/nano/crypto_lib/random_pool.hpp index 2afd591f..eddfcc0c 100644 --- a/nano/crypto_lib/random_pool.hpp +++ b/nano/crypto_lib/random_pool.hpp @@ -22,10 +22,9 @@ public: random_pool & operator= (random_pool const &) = delete; private: - static std::mutex mutex; static CryptoPP::AutoSeededRandomPool & get_pool (); template friend void random_pool_shuffle (Iter begin, Iter end); }; -} \ No newline at end of file +} diff --git a/nano/crypto_lib/random_pool_shuffle.hpp b/nano/crypto_lib/random_pool_shuffle.hpp index 00334810..e718f047 100644 --- a/nano/crypto_lib/random_pool_shuffle.hpp +++ b/nano/crypto_lib/random_pool_shuffle.hpp @@ -9,7 +9,6 @@ namespace nano template void random_pool_shuffle (Iter begin, Iter end) { - std::lock_guard guard (random_pool::mutex); random_pool::get_pool ().Shuffle (begin, end); } }