From fa3b3cb9b5d944d34b1d5ac3e102ac333482a475 Mon Sep 17 00:00:00 2001 From: MacroFake Date: Sat, 30 Apr 2022 10:24:24 +0200 Subject: [PATCH] Expose underlying clock in CThreadInterrupt Overloading sleep_for is not needed, as * seconds and minutes can be converted to milliseconds by the compiler, not needing a duration_cast * std::condition_variable::wait_for will convert milliseconds to the duration type of the underlying clock So simply expose the clock. --- ci/test/06_script_b.sh | 1 + src/threadinterrupt.cpp | 12 +----------- src/threadinterrupt.h | 5 ++--- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/ci/test/06_script_b.sh b/ci/test/06_script_b.sh index e1032ba6bd6..bdf913a0897 100755 --- a/ci/test/06_script_b.sh +++ b/ci/test/06_script_b.sh @@ -47,6 +47,7 @@ if [ "${RUN_TIDY}" = "true" ]; then " src/rpc/fees.cpp"\ " src/rpc/signmessage.cpp"\ " src/test/fuzz/txorphan.cpp"\ + " src/threadinterrupt.cpp"\ " src/util/bip32.cpp"\ " src/util/bytevectorhash.cpp"\ " src/util/error.cpp"\ diff --git a/src/threadinterrupt.cpp b/src/threadinterrupt.cpp index 340106ed99f..e28b447c1d7 100644 --- a/src/threadinterrupt.cpp +++ b/src/threadinterrupt.cpp @@ -28,18 +28,8 @@ void CThreadInterrupt::operator()() cond.notify_all(); } -bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time) +bool CThreadInterrupt::sleep_for(Clock::duration rel_time) { WAIT_LOCK(mut, lock); return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); }); } - -bool CThreadInterrupt::sleep_for(std::chrono::seconds rel_time) -{ - return sleep_for(std::chrono::duration_cast(rel_time)); -} - -bool CThreadInterrupt::sleep_for(std::chrono::minutes rel_time) -{ - return sleep_for(std::chrono::duration_cast(rel_time)); -} diff --git a/src/threadinterrupt.h b/src/threadinterrupt.h index 992016b4f6e..363aab39ced 100644 --- a/src/threadinterrupt.h +++ b/src/threadinterrupt.h @@ -19,13 +19,12 @@ class CThreadInterrupt { public: + using Clock = std::chrono::steady_clock; CThreadInterrupt(); explicit operator bool() const; void operator()() EXCLUSIVE_LOCKS_REQUIRED(!mut); void reset(); - bool sleep_for(std::chrono::milliseconds rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut); - bool sleep_for(std::chrono::seconds rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut); - bool sleep_for(std::chrono::minutes rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut); + bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut); private: std::condition_variable cond;