diff --git a/common/include/eoskeygen/key_search.hpp b/common/include/eoskeygen/key_search.hpp index f757002..eb358da 100644 --- a/common/include/eoskeygen/key_search.hpp +++ b/common/include/eoskeygen/key_search.hpp @@ -83,15 +83,21 @@ protected : #ifdef EOSIOKEYGEN_HAVE_THREADS void _thr_proc(); - void _search_mt(size_t n); + void _search_mt(); #endif /* EOSIOKEYGEN_HAVE_THREADS */ - void _search_linear(size_t n); + void _search_linear(); protected : // List of words to search for. strlist_t m_words; + // Max keys to search for. + std::size_t m_max; + + // Current number of keys found. + std::size_t m_count; + #ifdef EOSIOKEYGEN_HAVE_THREADS // Number of threads to use. size_t m_threads; diff --git a/common/src/key_search.cpp b/common/src/key_search.cpp index 810f884..68803a9 100644 --- a/common/src/key_search.cpp +++ b/common/src/key_search.cpp @@ -31,10 +31,12 @@ namespace eoskeygen { KeySearch::KeySearch() : + m_max (0), + m_count (0), #ifdef EOSIOKEYGEN_HAVE_THREADS - m_threads(0), + m_threads (0), #endif - m_callback(NULL) + m_callback (NULL) { } @@ -67,32 +69,34 @@ void KeySearch::setCallback(IKeySearchResult* callback) m_callback = callback; } -void KeySearch::_search_linear(size_t n) { - - size_t count = 0; +void KeySearch::_search_linear() +{ struct libeosio::ec_keypair pair; - while (count < n) { + while (m_count < m_max) { struct result res; libeosio::ec_generate_key(&pair); if (_contains_word(&pair, res)) { m_callback->onResult(&pair, res); - count++; + m_count++; } } } -void KeySearch::find(size_t num_results) { +void KeySearch::find(size_t num_results) +{ + m_count = 0; + m_max = num_results; #ifdef EOSIOKEYGEN_HAVE_THREADS // Only do multithread if number of threads makes sense. if (m_threads >= 2) { - _search_mt(num_results); + _search_mt(); return; } #endif /* HAVE_THREADS */ - _search_linear(num_results); + _search_linear(); } bool KeySearch::_contains_word(const struct libeosio::ec_keypair* key, struct result& result) { diff --git a/common/src/key_search_mt.cpp b/common/src/key_search_mt.cpp index 00e50e3..fc643b0 100644 --- a/common/src/key_search_mt.cpp +++ b/common/src/key_search_mt.cpp @@ -31,39 +31,33 @@ namespace eoskeygen { -// Max keys to search for, -std::size_t g_max; - -// How many keys we have found so far. -std::size_t g_count; - -// Mutex guard for g_count. +// Mutex guard for m_count. std::mutex g_count_mtx; // Thread process. -void KeySearch::_thr_proc() { - +void KeySearch::_thr_proc() +{ struct libeosio::ec_keypair pair; - while (g_count < g_max) { + while (m_count < m_max) { struct result res; libeosio::ec_generate_key(&pair); if (_contains_word(&pair, res)) { // Guard output with mutex, so we don't get - // interrupted mid write and can write to g_count and res safely. + // interrupted mid write and can write to m_count and res safely. const std::lock_guard lock(g_count_mtx); - // It is possible g_count was updated by another thread + // It is possible m_count was updated by another thread // after we checked it in the while loop. // So while we have the lock, we need to check it again. - if (g_count >= g_max) { + if (m_count >= m_max) { return; } // Update count and call result function. - g_count++; + m_count++; m_callback->onResult(&pair, res); } } @@ -79,16 +73,12 @@ size_t KeySearch::max_threads() return std::thread::hardware_concurrency(); } -void KeySearch::_search_mt(size_t n) +void KeySearch::_search_mt() { std::vector t; t.resize(m_threads - 1); - // Setup global variables for the threads. - g_max = n; - g_count = 0; - // Launch them. for(std::size_t i = 0; i < t.size(); i++) { t[i] = std::thread(&KeySearch::_thr_proc, this);