@@ -510,6 +510,21 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
510510 boost::thread t (runCommand, strCmd); // thread runs free
511511}
512512
513+ static bool fHaveGenesis = false ;
514+ static boost::mutex cs_GenesisWait;
515+ static CConditionVariable condvar_GenesisWait;
516+
517+ static void BlockNotifyGenesisWait (bool , const CBlockIndex *pBlockIndex)
518+ {
519+ if (pBlockIndex != NULL ) {
520+ {
521+ boost::unique_lock<boost::mutex> lock_GenesisWait (cs_GenesisWait);
522+ fHaveGenesis = true ;
523+ }
524+ condvar_GenesisWait.notify_all ();
525+ }
526+ }
527+
513528struct CImportingNow
514529{
515530 CImportingNow () {
@@ -1286,7 +1301,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
12861301 break ;
12871302 }
12881303
1289- if (!fReindex ) {
1304+ if (!fReindex && chainActive. Tip () != NULL ) {
12901305 uiInterface.InitMessage (_ (" Rewinding blocks..." ));
12911306 if (!RewindBlockIndex (chainparams)) {
12921307 strLoadError = _ (" Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain" );
@@ -1403,6 +1418,14 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
14031418
14041419 // ********************************************************* Step 10: import blocks
14051420
1421+ // Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.
1422+ // No locking, as this happens before any background thread is started.
1423+ if (chainActive.Tip () == NULL ) {
1424+ uiInterface.NotifyBlockTip .connect (BlockNotifyGenesisWait);
1425+ } else {
1426+ fHaveGenesis = true ;
1427+ }
1428+
14061429 if (mapArgs.count (" -blocknotify" ))
14071430 uiInterface.NotifyBlockTip .connect (BlockNotifyCallback);
14081431
@@ -1412,19 +1435,16 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
14121435 BOOST_FOREACH (const std::string& strFile, mapMultiArgs[" -loadblock" ])
14131436 vImportFiles.push_back (strFile);
14141437 }
1438+
14151439 threadGroup.create_thread (boost::bind (&ThreadImport, vImportFiles));
14161440
14171441 // Wait for genesis block to be processed
1418- bool fHaveGenesis = false ;
1419- while (!fHaveGenesis && !fRequestShutdown ) {
1420- {
1421- LOCK (cs_main);
1422- fHaveGenesis = (chainActive.Tip () != NULL );
1423- }
1424-
1425- if (!fHaveGenesis ) {
1426- MilliSleep (10 );
1442+ {
1443+ boost::unique_lock<boost::mutex> lock (cs_GenesisWait);
1444+ while (!fHaveGenesis ) {
1445+ condvar_GenesisWait.wait (lock);
14271446 }
1447+ uiInterface.NotifyBlockTip .disconnect (BlockNotifyGenesisWait);
14281448 }
14291449
14301450 // ********************************************************* Step 11: start node
0 commit comments