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;