From e87e18c8be3f11439f91d163a8fb30cea1e3e75f Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 11 Jan 2023 13:52:46 +0300 Subject: [PATCH] Use busy-waiting instead of short Sleep on Windows. --- tdutils/td/utils/port/sleep.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tdutils/td/utils/port/sleep.cpp b/tdutils/td/utils/port/sleep.cpp index 448f9bf8e..ee693537c 100644 --- a/tdutils/td/utils/port/sleep.cpp +++ b/tdutils/td/utils/port/sleep.cpp @@ -14,14 +14,23 @@ #else #include #endif +#else +#include "td/utils/port/Clocks.h" #endif namespace td { void usleep_for(int32 microseconds) { #if TD_PORT_WINDOWS - int32 milliseconds = microseconds / 1000 + (microseconds % 1000 ? 1 : 0); - Sleep(milliseconds); + if (microseconds < 2000) { + auto end_time = Clocks::monotonic() + microseconds * 1e-6; + do { + SwitchToThread(); + } while (Clocks::monotonic() < end_time); + } else { + int32 milliseconds = microseconds / 1000 + (microseconds % 1000 ? 1 : 0); + Sleep(milliseconds); + } #else #if _POSIX_C_SOURCE >= 199309L timespec ts;