diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2024-09-25 10:08:22 +0200 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2024-10-01 09:11:08 +0200 |
commit | fa7f52af1a47ebe3d1bc00e3bac70bf9f6fa769b (patch) | |
tree | 331023d454235e3679731d554bafb02aeacdfd68 /src | |
parent | 5ca28ef28bcca1775ff49921fc2528d9439b71ab (diff) |
refactor: Use wait_for predicate to check for interrupt
Also use uint256::ZERO where appropriate for self-documenting code.
Diffstat (limited to 'src')
-rw-r--r-- | src/interfaces/mining.h | 3 | ||||
-rw-r--r-- | src/node/interfaces.cpp | 15 | ||||
-rw-r--r-- | src/node/kernel_notifications.h | 4 |
3 files changed, 9 insertions, 13 deletions
diff --git a/src/interfaces/mining.h b/src/interfaces/mining.h index 2977811aa6..f71f7d7251 100644 --- a/src/interfaces/mining.h +++ b/src/interfaces/mining.h @@ -61,7 +61,8 @@ public: virtual std::optional<BlockRef> getTip() = 0; /** - * Waits for the tip to change + * Waits for the connected tip to change. If the tip was not connected on + * startup, this will wait. * * @param[in] current_tip block hash of the current chain tip. Function waits * for the chain tip to differ from this. diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 25a9029f06..ec4777ba7f 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -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); diff --git a/src/node/kernel_notifications.h b/src/node/kernel_notifications.h index a39a20e2e2..296b9c426d 100644 --- a/src/node/kernel_notifications.h +++ b/src/node/kernel_notifications.h @@ -57,7 +57,9 @@ public: Mutex m_tip_block_mutex; std::condition_variable m_tip_block_cv GUARDED_BY(m_tip_block_mutex); //! The block for which the last blockTip notification was received for. - uint256 m_tip_block GUARDED_BY(m_tip_block_mutex); + //! The initial ZERO means that no block has been connected yet, which may + //! be true even long after startup, until shutdown. + uint256 m_tip_block GUARDED_BY(m_tip_block_mutex){uint256::ZERO}; private: const std::function<bool()>& m_shutdown_request; |