@@ -510,6 +510,21 @@ static void BlockNotifyCallback(bool initialSync, const CBlockIndex *pBlockIndex
510
510
boost::thread t (runCommand, strCmd); // thread runs free
511
511
}
512
512
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
+
513
528
struct CImportingNow
514
529
{
515
530
CImportingNow () {
@@ -1286,7 +1301,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
1286
1301
break ;
1287
1302
}
1288
1303
1289
- if (!fReindex ) {
1304
+ if (!fReindex && chainActive. Tip () != NULL ) {
1290
1305
uiInterface.InitMessage (_ (" Rewinding blocks..." ));
1291
1306
if (!RewindBlockIndex (chainparams)) {
1292
1307
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)
1403
1418
1404
1419
// ********************************************************* Step 10: import blocks
1405
1420
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
+
1406
1429
if (mapArgs.count (" -blocknotify" ))
1407
1430
uiInterface.NotifyBlockTip .connect (BlockNotifyCallback);
1408
1431
@@ -1412,19 +1435,16 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
1412
1435
BOOST_FOREACH (const std::string& strFile, mapMultiArgs[" -loadblock" ])
1413
1436
vImportFiles.push_back (strFile);
1414
1437
}
1438
+
1415
1439
threadGroup.create_thread (boost::bind (&ThreadImport, vImportFiles));
1416
1440
1417
1441
// 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);
1427
1446
}
1447
+ uiInterface.NotifyBlockTip .disconnect (BlockNotifyGenesisWait);
1428
1448
}
1429
1449
1430
1450
// ********************************************************* Step 11: start node
0 commit comments