Skip to content

Commit

Permalink
Avoid unnecessarily held lock in backlog_population (#4126)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Feb 14, 2023
1 parent 3e0d54f commit 96aa938
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions nano/node/backlog_population.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ void nano::backlog_population::start ()

void nano::backlog_population::stop ()
{
nano::unique_lock<nano::mutex> lock{ mutex };
stopped = true;
lock.unlock ();
{
nano::lock_guard<nano::mutex> lock{ mutex };
stopped = true;
}
notify ();
nano::join_or_pass (thread);
}
Expand Down Expand Up @@ -85,6 +86,7 @@ void nano::backlog_population::populate_backlog (nano::unique_lock<nano::mutex>
while (!stopped && !done)
{
lock.unlock ();

{
auto transaction = store.tx_begin_read ();

Expand All @@ -101,9 +103,11 @@ void nano::backlog_population::populate_backlog (nano::unique_lock<nano::mutex>
}
done = store.account.begin (transaction, next) == end;
}

lock.lock ();

// Give the rest of the node time to progress without holding database lock
std::this_thread::sleep_for (std::chrono::milliseconds (1000 / config_m.frequency));
condition.wait_for (lock, std::chrono::milliseconds{ 1000 / config_m.frequency });
}
}

Expand Down

0 comments on commit 96aa938

Please sign in to comment.