diff --git a/nano/node/blockprocessor.cpp b/nano/node/blockprocessor.cpp index 24ed806777..69ba26feb0 100644 --- a/nano/node/blockprocessor.cpp +++ b/nano/node/blockprocessor.cpp @@ -45,9 +45,21 @@ nano::block_processor::block_processor (nano::node & node_a, nano::write_databas block_processed.notify (result, context); } }); - processing_thread = std::thread ([this] () { +} + +nano::block_processor::~block_processor () +{ + // Thread must be stopped before destruction + debug_assert (!thread.joinable ()); +} + +void nano::block_processor::start () +{ + debug_assert (!thread.joinable ()); + + thread = std::thread ([this] () { nano::thread_role::set (nano::thread_role::name::block_processing); - this->process_blocks (); + run (); }); } @@ -58,7 +70,10 @@ void nano::block_processor::stop () stopped = true; } condition.notify_all (); - nano::join_or_pass (processing_thread); + if (thread.joinable ()) + { + thread.join (); + } } std::size_t nano::block_processor::size () @@ -171,7 +186,7 @@ void nano::block_processor::rollback_competitor (store::write_transaction const } } -void nano::block_processor::process_blocks () +void nano::block_processor::run () { nano::unique_lock lock{ mutex }; while (!stopped) diff --git a/nano/node/blockprocessor.hpp b/nano/node/blockprocessor.hpp index 3cc7663c21..620abcdd95 100644 --- a/nano/node/blockprocessor.hpp +++ b/nano/node/blockprocessor.hpp @@ -63,8 +63,11 @@ class block_processor final public: block_processor (nano::node &, nano::write_database_queue &); + ~block_processor (); + void start (); void stop (); + std::size_t size (); bool full (); bool half_full (); @@ -74,7 +77,7 @@ class block_processor final bool should_log (); bool have_blocks_ready (); bool have_blocks (); - void process_blocks (); + std::unique_ptr collect_container_info (std::string const & name); std::atomic flushing{ false }; @@ -89,6 +92,7 @@ class block_processor final nano::observer_set const &> rolled_back; private: + void run (); // Roll back block in the ledger that conflicts with 'block' void rollback_competitor (store::write_transaction const &, nano::block const & block); nano::block_status process_one (store::write_transaction const &, context const &, bool forced = false); @@ -111,6 +115,6 @@ class block_processor final std::chrono::steady_clock::time_point next_log; nano::condition_variable condition; nano::mutex mutex{ mutex_identifier (mutexes::block_processor) }; - std::thread processing_thread; + std::thread thread; }; } diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 578d57de70..006036ef8f 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -646,6 +646,7 @@ void nano::node::start () port_mapping.start (); } wallets.start (); + block_processor.start (); active.start (); generator.start (); final_generator.start ();