Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ static CCoinsViewDB* pcoinsdbview = NULL;
static CCoinsViewErrorCatcher* pcoinscatcher = NULL;
static boost::scoped_ptr<ECCVerifyHandle> globalVerifyHandle;

void Interrupt(boost::thread_group& threadGroup)
static boost::thread_group threadGroup;
static CScheduler scheduler;
void Interrupt()
{
InterruptHTTPServer();
InterruptHTTPRPC();
InterruptRPC();
InterruptREST();
InterruptTorControl();
threadGroup.interrupt_all();
}

/** Preparing steps before shutting down or restarting the wallet */
Expand Down Expand Up @@ -213,6 +214,11 @@ void PrepareShutdown()
DumpMasternodePayments();
UnregisterNodeSignals(GetNodeSignals());

// After everything has been shut down, but before things get flushed, stop the
// CScheduler/checkqueue threadGroup
threadGroup.interrupt_all();
threadGroup.join_all();

if (fFeeEstimatesInitialized) {
boost::filesystem::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
CAutoFile est_fileout(fopen(est_path.string().c_str(), "wb"), SER_DISK, CLIENT_VERSION);
Expand Down Expand Up @@ -703,7 +709,7 @@ bool InitSanityCheck(void)
return true;
}

bool AppInitServers(boost::thread_group& threadGroup)
bool AppInitServers()
{
RPCServer::OnStopped(&OnRPCStopped);
RPCServer::OnPreCommand(&OnRPCPreCommand);
Expand All @@ -723,7 +729,7 @@ bool AppInitServers(boost::thread_group& threadGroup)
/** Initialize pivx.
* @pre Parameters should be parsed and config file should be read.
*/
bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
bool AppInit2()
{
// ********************************************************* Step 1: setup
#ifdef _MSC_VER
Expand Down Expand Up @@ -1053,7 +1059,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
*/
if (fServer) {
uiInterface.InitMessage.connect(SetRPCWarmupStatus);
if (!AppInitServers(threadGroup))
if (!AppInitServers())
return InitError(_("Unable to start HTTP server. See debug log for details."));
}

Expand Down
4 changes: 2 additions & 2 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ extern CzPIVWallet* zwalletMain;
void StartShutdown();
bool ShutdownRequested();
/** Interrupt threads */
void Interrupt(boost::thread_group& threadGroup);
void Interrupt();
void Shutdown();
void PrepareShutdown();
bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler);
bool AppInit2();

/** The help message mode determines what help message to show */
enum HelpMessageMode {
Expand Down
20 changes: 5 additions & 15 deletions src/pivxd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "main.h"
#include "masternodeconfig.h"
#include "noui.h"
#include "scheduler.h"
#include "rpc/server.h"
#include "ui_interface.h"
#include "util.h"
Expand Down Expand Up @@ -41,18 +40,15 @@

static bool fDaemon;

void WaitForShutdown(boost::thread_group* threadGroup)
void WaitForShutdown()
{
bool fShutdown = ShutdownRequested();
// Tell the main threads to shutdown.
while (!fShutdown) {
MilliSleep(200);
fShutdown = ShutdownRequested();
}
if (threadGroup) {
Interrupt(*threadGroup);
threadGroup->join_all();
}
Interrupt();
}

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -61,9 +57,6 @@ void WaitForShutdown(boost::thread_group* threadGroup)
//
bool AppInit(int argc, char* argv[])
{
boost::thread_group threadGroup;
CScheduler scheduler;

bool fRet = false;

//
Expand Down Expand Up @@ -147,20 +140,17 @@ bool AppInit(int argc, char* argv[])
#endif
SoftSetBoolArg("-server", true);

fRet = AppInit2(threadGroup, scheduler);
fRet = AppInit2();
} catch (std::exception& e) {
PrintExceptionContinue(&e, "AppInit()");
} catch (...) {
PrintExceptionContinue(NULL, "AppInit()");
}

if (!fRet) {
Interrupt(threadGroup);
// threadGroup.join_all(); was left out intentionally here, because we didn't re-test all of
// the startup-failure cases to make sure they don't result in a hang due to some
// thread-blocking-waiting-for-another-thread-during-startup case
Interrupt();
} else {
WaitForShutdown(&threadGroup);
WaitForShutdown();
}
Shutdown();

Expand Down
12 changes: 3 additions & 9 deletions src/qt/pivx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "init.h"
#include "main.h"
#include "rpc/server.h"
#include "scheduler.h"
#include "ui_interface.h"
#include "util.h"

Expand Down Expand Up @@ -166,9 +165,6 @@ public slots:
void runawayException(const QString& message);

private:
boost::thread_group threadGroup;
CScheduler scheduler;

/// Flag indicating a restart
bool execute_restart;

Expand Down Expand Up @@ -252,7 +248,7 @@ void BitcoinCore::initialize()

try {
qDebug() << __func__ << ": Running AppInit2 in thread";
int rv = AppInit2(threadGroup, scheduler);
int rv = AppInit2();
emit initializeResult(rv);
} catch (std::exception& e) {
handleRunawayException(&e);
Expand All @@ -267,8 +263,7 @@ void BitcoinCore::restart(QStringList args)
execute_restart = false;
try {
qDebug() << __func__ << ": Running Restart in thread";
Interrupt(threadGroup);
threadGroup.join_all();
Interrupt();
PrepareShutdown();
qDebug() << __func__ << ": Shutdown finished";
emit shutdownResult(1);
Expand All @@ -288,8 +283,7 @@ void BitcoinCore::shutdown()
{
try {
qDebug() << __func__ << ": Running Shutdown in thread";
Interrupt(threadGroup);
threadGroup.join_all();
Interrupt();
Shutdown();
qDebug() << __func__ << ": Shutdown finished";
emit shutdownResult(1);
Expand Down