From acbf483bae5bdf81e8bbc2e98255a311c494f775 Mon Sep 17 00:00:00 2001 From: Dmitry Antipov Date: Thu, 12 Jan 2023 21:03:14 +0300 Subject: [PATCH] refactor: switch to C++11-compatible tr_wait() from tr_wait_msec() (#4576) --- cli/cli.cc | 8 +++++--- libtransmission/utils.cc | 16 ---------------- libtransmission/utils.h | 10 ++++++++-- libtransmission/verify.cc | 10 ++++++---- libtransmission/web.cc | 2 +- tests/libtransmission/test-fixtures.h | 2 +- 6 files changed, 21 insertions(+), 27 deletions(-) diff --git a/cli/cli.cc b/cli/cli.cc index a6e28d6ad..3caabb3b1 100644 --- a/cli/cli.cc +++ b/cli/cli.cc @@ -17,12 +17,14 @@ #include #include #include -#include /* tr_wait_msec */ +#include /* tr_wait() */ #include #include #include #include // tr_sessionFetch() +using namespace std::chrono_literals; + /*** **** ***/ @@ -266,7 +268,7 @@ int tr_main(int argc, char* argv[]) waitingOnWeb = true; while (waitingOnWeb) { - tr_wait_msec(1000); + tr_wait(1s); } } else @@ -308,7 +310,7 @@ int tr_main(int argc, char* argv[]) "Error:", }; - tr_wait_msec(200); + tr_wait(200ms); if (gotsig) { diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index ffe3a0b4e..857db6694 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -237,22 +237,6 @@ uint64_t tr_time_msec() return std::chrono::system_clock::now().time_since_epoch() / 1ms; } -void tr_wait_msec(long int delay_milliseconds) -{ -#ifdef _WIN32 - - Sleep((DWORD)delay_milliseconds); - -#else - - struct timespec ts = {}; - ts.tv_sec = delay_milliseconds / 1000; - ts.tv_nsec = (delay_milliseconds % 1000) * 1000000; - nanosleep(&ts, nullptr); - -#endif -} - /*** **** ***/ diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 4fc1b59a9..71ffe87e2 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -7,12 +7,14 @@ #include #include +#include #include // uint8_t, uint32_t, uint64_t #include // size_t #include // time_t #include #include #include +#include #include #include @@ -85,8 +87,12 @@ constexpr auto tr_saveFile(std::string_view filename, ContiguousRange const& x, /** @brief return the current date in milliseconds */ [[nodiscard]] uint64_t tr_time_msec(); -/** @brief sleep the specified number of milliseconds */ -void tr_wait_msec(long int delay_milliseconds); +/** @brief sleep for the specified duration */ +template +void tr_wait(std::chrono::duration const& delay) +{ + std::this_thread::sleep_for(delay); +} template::value, bool> = true> [[nodiscard]] std::optional tr_parseNum(std::string_view str, std::string_view* setme_remainder = nullptr, int base = 10); diff --git a/libtransmission/verify.cc b/libtransmission/verify.cc index 62c5a4fd6..af54f8246 100644 --- a/libtransmission/verify.cc +++ b/libtransmission/verify.cc @@ -21,13 +21,15 @@ #include "log.h" #include "torrent.h" #include "tr-assert.h" -#include "utils.h" // tr_time(), tr_wait_msec() +#include "utils.h" // tr_time(), tr_wait() #include "verify.h" +using namespace std::chrono_literals; + namespace { -auto constexpr MsecToSleepPerSecondDuringVerify = int{ 100 }; +auto constexpr SleepPerSecondDuringVerify = 100ms; } @@ -133,7 +135,7 @@ bool tr_verify_worker::verifyTorrent(tr_torrent* tor, std::atomic const& s if (auto const now = tr_time(); last_slept_at != now) { last_slept_at = now; - tr_wait_msec(MsecToSleepPerSecondDuringVerify); + tr_wait(SleepPerSecondDuringVerify); } sha->clear(); @@ -275,6 +277,6 @@ tr_verify_worker::~tr_verify_worker() while (verify_thread_id_.has_value()) { - tr_wait_msec(20); + tr_wait(20ms); } } diff --git a/libtransmission/web.cc b/libtransmission/web.cc index 2e6850241..4f2c5f05c 100644 --- a/libtransmission/web.cc +++ b/libtransmission/web.cc @@ -671,7 +671,7 @@ public: ++repeats; if (repeats > 1U) { - tr_wait_msec(100); + tr_wait(100ms); } } else diff --git a/tests/libtransmission/test-fixtures.h b/tests/libtransmission/test-fixtures.h index 4c8755f4b..5636a5da4 100644 --- a/tests/libtransmission/test-fixtures.h +++ b/tests/libtransmission/test-fixtures.h @@ -89,7 +89,7 @@ inline bool waitFor(std::function const& test, std::chrono::milliseconds return false; } - tr_wait_msec(10); + tr_wait(10ms); } }