Skip to content

Commit

Permalink
Start/stop pattern for unchecked_map (#4577)
Browse files Browse the repository at this point in the history
* Start/stop pattern for `unchecked_map`

* Remove flush function
  • Loading branch information
pwojcikdev authored Apr 18, 2024
1 parent 97d4905 commit e51b276
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 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
51 changes: 27 additions & 24 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 All @@ -30,6 +52,7 @@ void nano::unchecked_map::put (nano::hash_or_account const & dependency, nano::u
{
entries.get<tag_sequenced> ().pop_front ();
}

stats.inc (nano::stat::type::unchecked, nano::stat::detail::put);
}

Expand Down Expand Up @@ -85,24 +108,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 };
condition.wait (lock, [this] () {
return stopped || (buffer.empty () && back_buffer.empty () && !writing_back_buffer);
});
}

void nano::unchecked_map::trigger (nano::hash_or_account const & dependency)
{
nano::unique_lock<nano::mutex> lock{ mutex };
Expand All @@ -122,7 +127,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 All @@ -138,7 +142,6 @@ void nano::unchecked_map::run ()
}
else
{
condition.notify_all (); // Notify flush ()
condition.wait (lock, [this] () {
return stopped || !buffer.empty ();
});
Expand Down
7 changes: 5 additions & 2 deletions 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,8 +38,6 @@ class unchecked_map
void del (nano::unchecked_key const & key);
void clear ();
std::size_t count () const;
void stop ();
void flush ();

/**
* Trigger requested dependencies
Expand All @@ -58,10 +59,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 e51b276

Please sign in to comment.