mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-03 09:23:01 +01:00
refactor: Use wait_for predicate to check for interrupt
Also use uint256::ZERO where appropriate for self-documenting code.
This commit is contained in:
@@ -940,19 +940,12 @@ public:
|
||||
|
||||
BlockRef waitTipChanged(uint256 current_tip, MillisecondsDouble timeout) override
|
||||
{
|
||||
// Interrupt check interval
|
||||
const MillisecondsDouble tick{1000};
|
||||
auto now{std::chrono::steady_clock::now()};
|
||||
auto deadline = now + timeout;
|
||||
// std::chrono does not check against overflow
|
||||
if (deadline < now) deadline = std::chrono::steady_clock::time_point::max();
|
||||
if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
|
||||
{
|
||||
WAIT_LOCK(notifications().m_tip_block_mutex, lock);
|
||||
while ((notifications().m_tip_block == uint256() || notifications().m_tip_block == current_tip) && !chainman().m_interrupt) {
|
||||
now = std::chrono::steady_clock::now();
|
||||
if (now >= deadline) break;
|
||||
notifications().m_tip_block_cv.wait_until(lock, std::min(deadline, now + tick));
|
||||
}
|
||||
notifications().m_tip_block_cv.wait_for(lock, timeout, [&]() EXCLUSIVE_LOCKS_REQUIRED(notifications().m_tip_block_mutex) {
|
||||
return (notifications().m_tip_block != current_tip && notifications().m_tip_block != uint256::ZERO) || chainman().m_interrupt;
|
||||
});
|
||||
}
|
||||
// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
|
||||
LOCK(::cs_main);
|
||||
|
||||
Reference in New Issue
Block a user