Skip to content

Commit

Permalink
Start/stop pattern for unchecked_map
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Apr 18, 2024
1 parent 97d4905 commit f0646f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
1 change: 1 addition & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ void nano::node::start ()
{
port_mapping.start ();
}
unchecked.start ();
wallets.start ();
rep_tiers.start ();
vote_processor.start ();
Expand Down
41 changes: 26 additions & 15 deletions nano/node/unchecked_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nano::mutex> 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)
Expand Down Expand Up @@ -85,16 +107,6 @@ std::size_t nano::unchecked_map::count () const
return entries.size ();
}

void nano::unchecked_map::stop ()
{
nano::unique_lock<nano::mutex> lock{ mutex };
if (!stopped)
{
stopped = true;
condition.notify_all (); // Notify flush (), run ()
}
}

void nano::unchecked_map::flush ()
{
nano::unique_lock<nano::mutex> lock{ mutex };
Expand Down Expand Up @@ -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<nano::mutex> lock{ mutex };
while (!stopped)
{
Expand Down
6 changes: 5 additions & 1 deletion nano/node/unchecked_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void (nano::unchecked_key const &, nano::unchecked_info const &)> action, std::function<bool ()> predicate = [] () { return true; });
Expand All @@ -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 ();

/**
Expand All @@ -58,10 +60,12 @@ class unchecked_map
std::deque<nano::hash_or_account> buffer;
std::deque<nano::hash_or_account> 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);
Expand Down

0 comments on commit f0646f2

Please sign in to comment.