From c8c2fbe07f1a5475aea3a2680af9130558c7e5c8 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Wed, 6 Mar 2013 22:16:05 -0500 Subject: Shutdown cleanup prep-work Create a boost::thread_group object at the qt/bitcoind main-loop level that will hold pointers to all the main-loop threads. This will replace the vnThreadsRunning[] array. For testing, ported the BitcoinMiner threads to use its own boost::thread_group. --- src/init.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index aac46d489d..a5015adc48 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -122,6 +122,16 @@ void Shutdown(void* parg) } } +// +// Signal handlers are very limited in what they are allowed to do, so: +// +void DetectShutdownThread(boost::thread_group* threadGroup) +{ + while (fRequestShutdown == false) + Sleep(200); + threadGroup->interrupt_all(); +} + void HandleSIGTERM(int) { fRequestShutdown = true; @@ -143,6 +153,7 @@ void HandleSIGHUP(int) #if !defined(QT_GUI) bool AppInit(int argc, char* argv[]) { + boost::thread_group threadGroup; bool fRet = false; try { @@ -185,7 +196,7 @@ bool AppInit(int argc, char* argv[]) exit(ret); } - fRet = AppInit2(); + fRet = AppInit2(threadGroup); } catch (std::exception& e) { PrintExceptionContinue(&e, "AppInit()"); @@ -193,7 +204,11 @@ bool AppInit(int argc, char* argv[]) PrintExceptionContinue(NULL, "AppInit()"); } if (!fRet) + { Shutdown(NULL); + threadGroup.interrupt_all(); + threadGroup.join_all(); + } return fRet; } @@ -405,7 +420,7 @@ void ThreadImport(void *data) { /** Initialize bitcoin. * @pre Parameters should be parsed and config file should be read. */ -bool AppInit2() +bool AppInit2(boost::thread_group& threadGroup) { // ********************************************************* Step 1: setup #ifdef _MSC_VER @@ -449,6 +464,8 @@ bool AppInit2() sigaction(SIGHUP, &sa_hup, NULL); #endif + threadGroup.create_thread(boost::bind(&DetectShutdownThread, &threadGroup)); + // ********************************************************* Step 2: parameter interactions fTestNet = GetBoolArg("-testnet"); -- cgit v1.2.3