[+] Harden linux sleep against interrupts

This commit is contained in:
Reece Wilson 2022-05-13 18:51:47 +01:00
parent 2ecaef35fb
commit 021959bdfc

View File

@ -28,9 +28,13 @@ namespace Aurora::Threading
AUKN_SYM void Sleep(AuUInt64 timeout)
{
#if defined(AURORA_IS_LINUX_DERIVED)
usleep(timeout * 1000);
SleepNs(AuMSToNS<AuUInt64>(timeout));
#elif defined(AURORA_IS_MODERNNT_DERIVED)
::Sleep(timeout);
#else
std::atomic<bool> value = false;
BooleanWaitable waitable(value);
@ -41,22 +45,35 @@ namespace Aurora::Threading
AUKN_SYM void SleepNs(AuUInt64 timeout)
{
#if defined(AURORA_IS_LINUX_DERIVED) || defined(AURORA_IS_BSD_DERIVED)
usleep(timeout / 1000);
auto endTime = AuTime::CurrentClockNS() + timeout;
if (usleep(timeout / 1000) == EINTR)
{
AuUInt64 now;
while ((now = AuTime::CurrentClockNS()) < endTime)
{
usleep((endTime - now) / 1000);
}
}
#else
#if defined(AURORA_IS_MODERNNT_DERIVED)
if (NtDelayExecution)
{
auto currentTime = AuTime::CurrentClockNS() + timeout;
auto targetTime = AuTime::ConvertTimestampNs(currentTime);
auto endTime = AuTime::CurrentClockNS() + timeout;
auto targetTime = AuTime::ConvertTimestampNs(endTime);
while (AuTime::CurrentClockNS() < currentTime)
while (AuTime::CurrentClockNS() < endTime)
{
LARGE_INTEGER word;
word.QuadPart = targetTime;
NtDelayExecution(FALSE, &word);
}
return;
}
#endif