Avoid unnecessarily held lock in backlog_population (#4126)

This commit is contained in:
Piotr Wójcik 2023-02-14 18:52:34 +01:00 committed by GitHub
commit 96aa938827
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,9 +29,10 @@ void nano::backlog_population::start ()
void nano::backlog_population::stop () void nano::backlog_population::stop ()
{ {
nano::unique_lock<nano::mutex> lock{ mutex }; {
stopped = true; nano::lock_guard<nano::mutex> lock{ mutex };
lock.unlock (); stopped = true;
}
notify (); notify ();
nano::join_or_pass (thread); nano::join_or_pass (thread);
} }
@ -85,6 +86,7 @@ void nano::backlog_population::populate_backlog (nano::unique_lock<nano::mutex>
while (!stopped && !done) while (!stopped && !done)
{ {
lock.unlock (); lock.unlock ();
{ {
auto transaction = store.tx_begin_read (); auto transaction = store.tx_begin_read ();
@ -101,9 +103,11 @@ void nano::backlog_population::populate_backlog (nano::unique_lock<nano::mutex>
} }
done = store.account.begin (transaction, next) == end; done = store.account.begin (transaction, next) == end;
} }
lock.lock (); lock.lock ();
// Give the rest of the node time to progress without holding database 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 });
} }
} }