From bc7900f33db3d01fb93dfee7981c01ea495cd42e Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Fri, 13 Sep 2024 10:22:00 +0200 Subject: [PATCH] kernel: Move background load thread to node context The thread handle is never used by the ChainstateManager, so move it out and into the node context. Users of the kernel library now no longer have to manually join the thread when destructing the ChainstateManager. --- src/bitcoin-chainstate.cpp | 2 -- src/init.cpp | 4 ++-- src/node/context.h | 2 ++ src/validation.h | 2 -- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index ebe013b638b8d..9cbafa233dde3 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -283,8 +283,6 @@ int main(int argc, char* argv[]) epilogue: // Without this precise shutdown sequence, there will be a lot of nullptr // dereferencing and UB. - if (chainman.m_thread_load.joinable()) chainman.m_thread_load.join(); - validation_signals.FlushBackgroundCallbacks(); { LOCK(cs_main); diff --git a/src/init.cpp b/src/init.cpp index f67835d7da591..da3d99b6bf25d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -298,7 +298,7 @@ void Shutdown(NodeContext& node) StopTorControl(); - if (node.chainman && node.chainman->m_thread_load.joinable()) node.chainman->m_thread_load.join(); + if (node.background_init_thread.joinable()) node.background_init_thread.join(); // After everything has been shut down, but before things get flushed, stop the // the scheduler. After this point, SyncWithValidationInterfaceQueue() should not be called anymore // as this would prevent the shutdown from completing. @@ -1789,7 +1789,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) vImportFiles.push_back(fs::PathFromString(strFile)); } - chainman.m_thread_load = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] { + node.background_init_thread = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] { ScheduleBatchPriority(); // Import blocks ImportBlocks(chainman, vImportFiles); diff --git a/src/node/context.h b/src/node/context.h index a664fad80be87..398089ff3c0eb 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -9,6 +9,7 @@ #include #include #include +#include #include class ArgsManager; @@ -86,6 +87,7 @@ struct NodeContext { std::atomic exit_status{EXIT_SUCCESS}; //! Manages all the node warnings std::unique_ptr warnings; + std::thread background_init_thread; //! Declare default constructor and destructor that are not inline, so code //! instantiating the NodeContext struct doesn't need to #include class diff --git a/src/validation.h b/src/validation.h index 059ae52bdf4ca..6bc8a14de95a6 100644 --- a/src/validation.h +++ b/src/validation.h @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -1008,7 +1007,6 @@ class ChainstateManager const util::SignalInterrupt& m_interrupt; const Options m_options; - std::thread m_thread_load; //! A single BlockManager instance is shared across each constructed //! chainstate to avoid duplicating block metadata. node::BlockManager m_blockman;