@@ -511,6 +511,21 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
511
511
boost::thread t (runCommand, strCmd); // thread runs free
512
512
}
513
513
514
+ static bool fHaveGenesis = false ;
515
+ static boost::mutex cs_GenesisWait;
516
+ static CConditionVariable condvar_GenesisWait;
517
+
518
+ static void BlockNotifyGenesisWait (bool , const CBlockIndex *pBlockIndex)
519
+ {
520
+ if (pBlockIndex != NULL ) {
521
+ {
522
+ boost::unique_lock<boost::mutex> lock_GenesisWait (cs_GenesisWait);
523
+ fHaveGenesis = true ;
524
+ }
525
+ condvar_GenesisWait.notify_all ();
526
+ }
527
+ }
528
+
514
529
struct CImportingNow
515
530
{
516
531
CImportingNow () {
@@ -1322,7 +1337,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
1322
1337
break ;
1323
1338
}
1324
1339
1325
- if (!fReindex ) {
1340
+ if (!fReindex && chainActive. Tip () != NULL ) {
1326
1341
uiInterface.InitMessage (_ (" Rewinding blocks..." ));
1327
1342
if (!RewindBlockIndex (chainparams)) {
1328
1343
strLoadError = _ (" Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain" );
@@ -1439,6 +1454,17 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
1439
1454
1440
1455
// ********************************************************* Step 10: import blocks
1441
1456
1457
+ if (!CheckDiskSpace ())
1458
+ return false ;
1459
+
1460
+ // Either install a handler to notify us when genesis activates, or set fHaveGenesis directly.
1461
+ // No locking, as this happens before any background thread is started.
1462
+ if (chainActive.Tip () == NULL ) {
1463
+ uiInterface.NotifyBlockTip .connect (BlockNotifyGenesisWait);
1464
+ } else {
1465
+ fHaveGenesis = true ;
1466
+ }
1467
+
1442
1468
if (mapArgs.count (" -blocknotify" ))
1443
1469
uiInterface.NotifyBlockTip .connect (BlockNotifyCallback);
1444
1470
@@ -1448,26 +1474,20 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
1448
1474
BOOST_FOREACH (const std::string& strFile, mapMultiArgs[" -loadblock" ])
1449
1475
vImportFiles.push_back (strFile);
1450
1476
}
1477
+
1451
1478
threadGroup.create_thread (boost::bind (&ThreadImport, vImportFiles));
1452
1479
1453
1480
// Wait for genesis block to be processed
1454
- bool fHaveGenesis = false ;
1455
- while (!fHaveGenesis && !fRequestShutdown ) {
1456
- {
1457
- LOCK (cs_main);
1458
- fHaveGenesis = (chainActive.Tip () != NULL );
1459
- }
1460
-
1461
- if (!fHaveGenesis ) {
1462
- MilliSleep (10 );
1481
+ {
1482
+ boost::unique_lock<boost::mutex> lock (cs_GenesisWait);
1483
+ while (!fHaveGenesis ) {
1484
+ condvar_GenesisWait.wait (lock);
1463
1485
}
1486
+ uiInterface.NotifyBlockTip .disconnect (BlockNotifyGenesisWait);
1464
1487
}
1465
1488
1466
1489
// ********************************************************* Step 11: start node
1467
1490
1468
- if (!CheckDiskSpace ())
1469
- return false ;
1470
-
1471
1491
if (!strErrors.str ().empty ())
1472
1492
return InitError (strErrors.str ());
1473
1493
0 commit comments