refactor: switch to C++11-compatible tr_wait() from tr_wait_msec() (#4576)

This commit is contained in:
Dmitry Antipov 2023-01-12 21:03:14 +03:00 committed by GitHub
parent a5c7b65869
commit acbf483bae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 27 deletions

View File

@ -17,12 +17,14 @@
#include <libtransmission/error.h> #include <libtransmission/error.h>
#include <libtransmission/file.h> #include <libtransmission/file.h>
#include <libtransmission/tr-getopt.h> #include <libtransmission/tr-getopt.h>
#include <libtransmission/utils.h> /* tr_wait_msec */ #include <libtransmission/utils.h> /* tr_wait() */
#include <libtransmission/variant.h> #include <libtransmission/variant.h>
#include <libtransmission/version.h> #include <libtransmission/version.h>
#include <libtransmission/web-utils.h> #include <libtransmission/web-utils.h>
#include <libtransmission/web.h> // tr_sessionFetch() #include <libtransmission/web.h> // tr_sessionFetch()
using namespace std::chrono_literals;
/*** /***
**** ****
***/ ***/
@ -266,7 +268,7 @@ int tr_main(int argc, char* argv[])
waitingOnWeb = true; waitingOnWeb = true;
while (waitingOnWeb) while (waitingOnWeb)
{ {
tr_wait_msec(1000); tr_wait(1s);
} }
} }
else else
@ -308,7 +310,7 @@ int tr_main(int argc, char* argv[])
"Error:", "Error:",
}; };
tr_wait_msec(200); tr_wait(200ms);
if (gotsig) if (gotsig)
{ {

View File

@ -237,22 +237,6 @@ uint64_t tr_time_msec()
return std::chrono::system_clock::now().time_since_epoch() / 1ms; 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
}
/*** /***
**** ****
***/ ***/

View File

@ -7,12 +7,14 @@
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <chrono>
#include <cstdint> // uint8_t, uint32_t, uint64_t #include <cstdint> // uint8_t, uint32_t, uint64_t
#include <cstddef> // size_t #include <cstddef> // size_t
#include <ctime> // time_t #include <ctime> // time_t
#include <optional> #include <optional>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <thread>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
@ -85,8 +87,12 @@ constexpr auto tr_saveFile(std::string_view filename, ContiguousRange const& x,
/** @brief return the current date in milliseconds */ /** @brief return the current date in milliseconds */
[[nodiscard]] uint64_t tr_time_msec(); [[nodiscard]] uint64_t tr_time_msec();
/** @brief sleep the specified number of milliseconds */ /** @brief sleep for the specified duration */
void tr_wait_msec(long int delay_milliseconds); template<class rep, class period>
void tr_wait(std::chrono::duration<rep, period> const& delay)
{
std::this_thread::sleep_for(delay);
}
template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true> template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
[[nodiscard]] std::optional<T> tr_parseNum(std::string_view str, std::string_view* setme_remainder = nullptr, int base = 10); [[nodiscard]] std::optional<T> tr_parseNum(std::string_view str, std::string_view* setme_remainder = nullptr, int base = 10);

View File

@ -21,13 +21,15 @@
#include "log.h" #include "log.h"
#include "torrent.h" #include "torrent.h"
#include "tr-assert.h" #include "tr-assert.h"
#include "utils.h" // tr_time(), tr_wait_msec() #include "utils.h" // tr_time(), tr_wait()
#include "verify.h" #include "verify.h"
using namespace std::chrono_literals;
namespace namespace
{ {
auto constexpr MsecToSleepPerSecondDuringVerify = int{ 100 }; auto constexpr SleepPerSecondDuringVerify = 100ms;
} }
@ -133,7 +135,7 @@ bool tr_verify_worker::verifyTorrent(tr_torrent* tor, std::atomic<bool> const& s
if (auto const now = tr_time(); last_slept_at != now) if (auto const now = tr_time(); last_slept_at != now)
{ {
last_slept_at = now; last_slept_at = now;
tr_wait_msec(MsecToSleepPerSecondDuringVerify); tr_wait(SleepPerSecondDuringVerify);
} }
sha->clear(); sha->clear();
@ -275,6 +277,6 @@ tr_verify_worker::~tr_verify_worker()
while (verify_thread_id_.has_value()) while (verify_thread_id_.has_value())
{ {
tr_wait_msec(20); tr_wait(20ms);
} }
} }

View File

@ -671,7 +671,7 @@ public:
++repeats; ++repeats;
if (repeats > 1U) if (repeats > 1U)
{ {
tr_wait_msec(100); tr_wait(100ms);
} }
} }
else else

View File

@ -89,7 +89,7 @@ inline bool waitFor(std::function<bool()> const& test, std::chrono::milliseconds
return false; return false;
} }
tr_wait_msec(10); tr_wait(10ms);
} }
} }