From f0646f2928cf4f1dca42e4f1be81ffb54a8c0666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Thu, 18 Apr 2024 12:52:06 +0200 Subject: [PATCH] Start/stop pattern for `unchecked_map` --- nano/node/node.cpp | 1 + nano/node/unchecked_map.cpp | 41 +++++++++++++++++++++++-------------- nano/node/unchecked_map.hpp | 6 +++++- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/nano/node/node.cpp b/nano/node/node.cpp index a7115d15bd..d43147ba77 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -674,6 +674,7 @@ void nano::node::start () { port_mapping.start (); } + unchecked.start (); wallets.start (); rep_tiers.start (); vote_processor.start (); diff --git a/nano/node/unchecked_map.cpp b/nano/node/unchecked_map.cpp index 4d65a40d2f..447d4e8b51 100644 --- a/nano/node/unchecked_map.cpp +++ b/nano/node/unchecked_map.cpp @@ -9,15 +9,37 @@ nano::unchecked_map::unchecked_map (unsigned const max_unchecked_blocks, nano::stats & stats, bool const & disable_delete) : max_unchecked_blocks{ max_unchecked_blocks }, stats{ stats }, - disable_delete{ disable_delete }, - thread{ [this] () { run (); } } + disable_delete{ disable_delete } { } nano::unchecked_map::~unchecked_map () { - stop (); - thread.join (); + debug_assert (!thread.joinable ()); +} + +void nano::unchecked_map::start () +{ + debug_assert (!thread.joinable ()); + + thread = std::thread ([this] () { + nano::thread_role::set (nano::thread_role::name::unchecked); + run (); + }); +} + +void nano::unchecked_map::stop () +{ + { + nano::lock_guard lock{ mutex }; + stopped = true; + } + condition.notify_all (); + + if (thread.joinable ()) + { + thread.join (); + } } void nano::unchecked_map::put (nano::hash_or_account const & dependency, nano::unchecked_info const & info) @@ -85,16 +107,6 @@ std::size_t nano::unchecked_map::count () const return entries.size (); } -void nano::unchecked_map::stop () -{ - nano::unique_lock lock{ mutex }; - if (!stopped) - { - stopped = true; - condition.notify_all (); // Notify flush (), run () - } -} - void nano::unchecked_map::flush () { nano::unique_lock lock{ mutex }; @@ -122,7 +134,6 @@ void nano::unchecked_map::process_queries (decltype (buffer) const & back_buffer void nano::unchecked_map::run () { - nano::thread_role::set (nano::thread_role::name::unchecked); nano::unique_lock lock{ mutex }; while (!stopped) { diff --git a/nano/node/unchecked_map.hpp b/nano/node/unchecked_map.hpp index 9a548c203d..579c61c464 100644 --- a/nano/node/unchecked_map.hpp +++ b/nano/node/unchecked_map.hpp @@ -25,6 +25,9 @@ class unchecked_map unchecked_map (unsigned const max_unchecked_blocks, nano::stats &, bool const & do_delete); ~unchecked_map (); + void start (); + void stop (); + void put (nano::hash_or_account const & dependency, nano::unchecked_info const & info); void for_each ( std::function action, std::function predicate = [] () { return true; }); @@ -35,7 +38,6 @@ class unchecked_map void del (nano::unchecked_key const & key); void clear (); std::size_t count () const; - void stop (); void flush (); /** @@ -58,10 +60,12 @@ class unchecked_map std::deque buffer; std::deque back_buffer; bool writing_back_buffer{ false }; + bool stopped{ false }; nano::condition_variable condition; nano::mutex mutex; std::thread thread; + unsigned const max_unchecked_blocks; void process_queries (decltype (buffer) const & back_buffer);