Skip to content

Commit

Permalink
Merge pull request #339 from lostystyg/0.21
Browse files Browse the repository at this point in the history
Improved stopping pocketcoind for 0.21
  • Loading branch information
lostystyg authored Jul 22, 2022
2 parents 9b216d7 + e55b604 commit c6fa67b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ void Shutdown(NodeContext& node)
// CScheduler/checkqueue, threadGroup and load block thread.
if (node.scheduler) node.scheduler->stop();
if (g_load_block.joinable()) g_load_block.join();
#ifdef ENABLE_WALLET
Staker::getInstance()->stop();
#endif
threadGroup.interrupt_all();
threadGroup.join_all();

Expand Down
28 changes: 17 additions & 11 deletions src/staker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void Staker::startWorkers(
{
if (workersStarted) { return; }
workersStarted = true;
m_interrupt.reset();

this->minerSleep = minerSleep;
threadGroup.create_thread(
Expand Down Expand Up @@ -104,7 +105,7 @@ void Staker::run(const util::Ref& context, CChainParams const& chainparams, boos
}
}

UninterruptibleSleep(std::chrono::milliseconds{minerSleep * 10});
m_interrupt.sleep_for(std::chrono::milliseconds{minerSleep * 10});
}
}

Expand Down Expand Up @@ -136,7 +137,7 @@ void Staker::worker(const util::Ref& context, CChainParams const& chainparams, s
while (wallet->IsLocked())
{
nLastCoinStakeSearchInterval = 0;
UninterruptibleSleep(std::chrono::milliseconds{1000});
m_interrupt.sleep_for(std::chrono::milliseconds{1000});
}

if (gArgs.GetBoolArg("-stakingrequirespeers", DEFAULT_STAKINGREQUIRESPEERS))
Expand All @@ -151,7 +152,7 @@ void Staker::worker(const util::Ref& context, CChainParams const& chainparams, s
if (!fvNodesEmpty && !::ChainstateActive().IsInitialBlockDownload())
break;

UninterruptibleSleep(std::chrono::milliseconds{1000});
m_interrupt.sleep_for(std::chrono::milliseconds{1000});
} while (true);
}

Expand All @@ -160,15 +161,15 @@ void Staker::worker(const util::Ref& context, CChainParams const& chainparams, s
if (ShutdownRequested())
break;

UninterruptibleSleep(std::chrono::milliseconds{1000});
m_interrupt.sleep_for(std::chrono::milliseconds{1000});
}

while (chainparams.GetConsensus().nPosFirstBlock > ::ChainActive().Tip()->nHeight)
{
if (ShutdownRequested())
break;

UninterruptibleSleep(std::chrono::milliseconds{30000});
m_interrupt.sleep_for(std::chrono::milliseconds{30000});
}

uint64_t nFees = 0;
Expand All @@ -193,15 +194,10 @@ void Staker::worker(const util::Ref& context, CChainParams const& chainparams, s
}
else
{
UninterruptibleSleep(std::chrono::milliseconds{minerSleep});
m_interrupt.sleep_for(std::chrono::milliseconds{minerSleep});
}
}
}
catch (const boost::thread_interrupted&)
{
LogPrintf("Staker worker thread terminated\n");
throw;
}
catch (const std::runtime_error& e)
{
LogPrintf("Staker worker thread runtime error: %s\n", e.what());
Expand Down Expand Up @@ -315,3 +311,13 @@ bool Staker::signBlock(std::shared_ptr<CBlock> block, std::shared_ptr<CWallet> w
#endif
return false;
}


void Staker::stop()
{
// TODO (losty): this should be called only after shutdown requested
// because otherwise sleeps will be just skipped but thread will
// not exited from it's loop that can result in very big cpu consumption. Probably exit
// loop by checking "if (m_interrupt)" instead of checking for ShutdownRequested?
m_interrupt();
}
4 changes: 4 additions & 0 deletions src/staker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef POCKETCOIN_STAKER_H
#define POCKETCOIN_STAKER_H

#include "threadinterrupt.h"
#include <boost/thread.hpp>
#include <chainparams.h>
#include <util/ref.h>
Expand Down Expand Up @@ -39,7 +40,10 @@ class Staker

bool signBlock(std::shared_ptr<CBlock>, std::shared_ptr<CWallet>, int64_t);

void stop();

private:
CThreadInterrupt m_interrupt;
Staker();

Staker(Staker const&);
Expand Down
5 changes: 4 additions & 1 deletion src/statistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,14 @@ namespace Statistic
void Run(boost::thread_group& threadGroup, const util::Ref& context)
{
shutdown = false;
m_interrupt.reset();
threadGroup.create_thread(boost::bind(&RequestStatEngine::PeriodicStatLogger, this, boost::cref(context)));
}

void Stop()
{
shutdown = true;
m_interrupt();
}

void PeriodicStatLogger(const util::Ref& context)
Expand All @@ -305,11 +307,12 @@ namespace Statistic
CompileStatsAsJsonSince(chunkSize, context).write(1));

RemoveSamplesBefore(chunkSize * 2);
UninterruptibleSleep(std::chrono::milliseconds{statLoggerSleep});
m_interrupt.sleep_for(std::chrono::milliseconds{statLoggerSleep});
}
}

private:
CThreadInterrupt m_interrupt;
std::vector<RequestSample> _samples;
Mutex _samplesLock;
bool shutdown = false;
Expand Down

0 comments on commit c6fa67b

Please sign in to comment.