miner: fix race when stopping mining with start mining enabled

This commit is contained in:
moneromooo-monero 2019-03-15 19:08:22 +00:00
parent 4308a2e173
commit def4016171
2 changed files with 10 additions and 6 deletions

View File

@ -106,6 +106,7 @@ namespace cryptonote
m_thread_index(0), m_thread_index(0),
m_phandler(phandler), m_phandler(phandler),
m_height(0), m_height(0),
m_threads_active(0),
m_pausers_count(0), m_pausers_count(0),
m_threads_total(0), m_threads_total(0),
m_starter_nonce(0), m_starter_nonce(0),
@ -264,8 +265,8 @@ namespace cryptonote
{ {
CRITICAL_REGION_LOCAL(m_threads_lock); CRITICAL_REGION_LOCAL(m_threads_lock);
boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1); boost::interprocess::ipcdetail::atomic_write32(&m_stop, 1);
for(boost::thread& th: m_threads) while (m_threads_active > 0)
th.join(); misc_utils::sleep_no_w(100);
m_threads.clear(); m_threads.clear();
} }
boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0); boost::interprocess::ipcdetail::atomic_write32(&m_stop, 0);
@ -447,10 +448,11 @@ namespace cryptonote
// In case background mining was active and the miner threads are waiting // In case background mining was active and the miner threads are waiting
// on the background miner to signal start. // on the background miner to signal start.
while (m_threads_active > 0)
{
m_is_background_mining_started_cond.notify_all(); m_is_background_mining_started_cond.notify_all();
misc_utils::sleep_no_w(100);
for(boost::thread& th: m_threads) }
th.join();
// The background mining thread could be sleeping for a long time, so we // The background mining thread could be sleeping for a long time, so we
// interrupt it just in case // interrupt it just in case
@ -589,6 +591,7 @@ namespace cryptonote
} }
slow_hash_free_state(); slow_hash_free_state();
MGINFO("Miner thread stopped ["<< th_local_index << "]"); MGINFO("Miner thread stopped ["<< th_local_index << "]");
--m_threads_active;
return true; return true;
} }
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------

View File

@ -125,6 +125,7 @@ namespace cryptonote
uint64_t m_height; uint64_t m_height;
volatile uint32_t m_thread_index; volatile uint32_t m_thread_index;
volatile uint32_t m_threads_total; volatile uint32_t m_threads_total;
std::atomic<uint32_t> m_threads_active;
std::atomic<int32_t> m_pausers_count; std::atomic<int32_t> m_pausers_count;
epee::critical_section m_miners_count_lock; epee::critical_section m_miners_count_lock;