mirror of
https://github.com/eosswedenorg/antelope-keygen
synced 2026-07-04 12:03:41 +02:00
common/include/eoskeygen/key_search.hpp: add max and count variables to the class.
This is done so that m_max can be set to zero from another method. stopping a find() operation. (for threaded operations)
This commit is contained in:
parent
3cc6a2530e
commit
2a528e0eb0
3 changed files with 31 additions and 31 deletions
|
|
@ -83,15 +83,21 @@ protected :
|
||||||
#ifdef EOSIOKEYGEN_HAVE_THREADS
|
#ifdef EOSIOKEYGEN_HAVE_THREADS
|
||||||
void _thr_proc();
|
void _thr_proc();
|
||||||
|
|
||||||
void _search_mt(size_t n);
|
void _search_mt();
|
||||||
#endif /* EOSIOKEYGEN_HAVE_THREADS */
|
#endif /* EOSIOKEYGEN_HAVE_THREADS */
|
||||||
|
|
||||||
void _search_linear(size_t n);
|
void _search_linear();
|
||||||
|
|
||||||
protected :
|
protected :
|
||||||
// List of words to search for.
|
// List of words to search for.
|
||||||
strlist_t m_words;
|
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
|
#ifdef EOSIOKEYGEN_HAVE_THREADS
|
||||||
// Number of threads to use.
|
// Number of threads to use.
|
||||||
size_t m_threads;
|
size_t m_threads;
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,8 @@
|
||||||
namespace eoskeygen {
|
namespace eoskeygen {
|
||||||
|
|
||||||
KeySearch::KeySearch() :
|
KeySearch::KeySearch() :
|
||||||
|
m_max (0),
|
||||||
|
m_count (0),
|
||||||
#ifdef EOSIOKEYGEN_HAVE_THREADS
|
#ifdef EOSIOKEYGEN_HAVE_THREADS
|
||||||
m_threads (0),
|
m_threads (0),
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -67,32 +69,34 @@ void KeySearch::setCallback(IKeySearchResult* callback)
|
||||||
m_callback = callback;
|
m_callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeySearch::_search_linear(size_t n) {
|
void KeySearch::_search_linear()
|
||||||
|
{
|
||||||
size_t count = 0;
|
|
||||||
struct libeosio::ec_keypair pair;
|
struct libeosio::ec_keypair pair;
|
||||||
|
|
||||||
while (count < n) {
|
while (m_count < m_max) {
|
||||||
struct result res;
|
struct result res;
|
||||||
libeosio::ec_generate_key(&pair);
|
libeosio::ec_generate_key(&pair);
|
||||||
if (_contains_word(&pair, res)) {
|
if (_contains_word(&pair, res)) {
|
||||||
m_callback->onResult(&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
|
#ifdef EOSIOKEYGEN_HAVE_THREADS
|
||||||
// Only do multithread if number of threads makes sense.
|
// Only do multithread if number of threads makes sense.
|
||||||
if (m_threads >= 2) {
|
if (m_threads >= 2) {
|
||||||
_search_mt(num_results);
|
_search_mt();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif /* HAVE_THREADS */
|
#endif /* HAVE_THREADS */
|
||||||
|
|
||||||
_search_linear(num_results);
|
_search_linear();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KeySearch::_contains_word(const struct libeosio::ec_keypair* key, struct result& result) {
|
bool KeySearch::_contains_word(const struct libeosio::ec_keypair* key, struct result& result) {
|
||||||
|
|
|
||||||
|
|
@ -31,39 +31,33 @@
|
||||||
|
|
||||||
namespace eoskeygen {
|
namespace eoskeygen {
|
||||||
|
|
||||||
// Max keys to search for,
|
// Mutex guard for m_count.
|
||||||
std::size_t g_max;
|
|
||||||
|
|
||||||
// How many keys we have found so far.
|
|
||||||
std::size_t g_count;
|
|
||||||
|
|
||||||
// Mutex guard for g_count.
|
|
||||||
std::mutex g_count_mtx;
|
std::mutex g_count_mtx;
|
||||||
|
|
||||||
// Thread process.
|
// Thread process.
|
||||||
void KeySearch::_thr_proc() {
|
void KeySearch::_thr_proc()
|
||||||
|
{
|
||||||
struct libeosio::ec_keypair pair;
|
struct libeosio::ec_keypair pair;
|
||||||
|
|
||||||
while (g_count < g_max) {
|
while (m_count < m_max) {
|
||||||
struct result res;
|
struct result res;
|
||||||
|
|
||||||
libeosio::ec_generate_key(&pair);
|
libeosio::ec_generate_key(&pair);
|
||||||
if (_contains_word(&pair, res)) {
|
if (_contains_word(&pair, res)) {
|
||||||
|
|
||||||
// Guard output with mutex, so we don't get
|
// 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<std::mutex> lock(g_count_mtx);
|
const std::lock_guard<std::mutex> 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.
|
// after we checked it in the while loop.
|
||||||
// So while we have the lock, we need to check it again.
|
// So while we have the lock, we need to check it again.
|
||||||
if (g_count >= g_max) {
|
if (m_count >= m_max) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update count and call result function.
|
// Update count and call result function.
|
||||||
g_count++;
|
m_count++;
|
||||||
m_callback->onResult(&pair, res);
|
m_callback->onResult(&pair, res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -79,16 +73,12 @@ size_t KeySearch::max_threads()
|
||||||
return std::thread::hardware_concurrency();
|
return std::thread::hardware_concurrency();
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeySearch::_search_mt(size_t n)
|
void KeySearch::_search_mt()
|
||||||
{
|
{
|
||||||
std::vector<std::thread> t;
|
std::vector<std::thread> t;
|
||||||
|
|
||||||
t.resize(m_threads - 1);
|
t.resize(m_threads - 1);
|
||||||
|
|
||||||
// Setup global variables for the threads.
|
|
||||||
g_max = n;
|
|
||||||
g_count = 0;
|
|
||||||
|
|
||||||
// Launch them.
|
// Launch them.
|
||||||
for(std::size_t i = 0; i < t.size(); i++) {
|
for(std::size_t i = 0; i < t.size(); i++) {
|
||||||
t[i] = std::thread(&KeySearch::_thr_proc, this);
|
t[i] = std::thread(&KeySearch::_thr_proc, this);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue