91 lines
2.0 KiB
C++
91 lines
2.0 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: AuSleep.cpp
|
|
Date: 2021-6-12
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "AuSleep.hpp"
|
|
#include "AuWaitFor.hpp"
|
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
#include <Time/Time.hpp>
|
|
#endif
|
|
|
|
namespace Aurora::Threading
|
|
{
|
|
AUKN_SYM void Sleep(AuUInt64 qwTimeout)
|
|
{
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
|
|
|
SleepNs(AuMSToNS<AuUInt64>(qwTimeout));
|
|
|
|
#elif defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
::Sleep(qwTimeout);
|
|
|
|
#else
|
|
std::atomic<bool> value = false;
|
|
BooleanWaitable waitable(value);
|
|
WaitFor(&waitable, qwTimeout);
|
|
#endif
|
|
}
|
|
|
|
AUKN_SYM void SleepNs(AuUInt64 qwTimeout)
|
|
{
|
|
#if defined(AURORA_IS_LINUX_DERIVED) || defined(AURORA_IS_BSD_DERIVED)
|
|
|
|
auto qwEndTime = AuTime::SteadyClockNS() + qwTimeout;
|
|
|
|
if ((usleep(qwTimeout / 1000) == -1) &&
|
|
(errno == EINTR))
|
|
{
|
|
AuUInt64 qwNow;
|
|
|
|
while ((qwNow = AuTime::SteadyClockNS()) < qwEndTime)
|
|
{
|
|
usleep((qwEndTime - qwNow) / 1000);
|
|
}
|
|
}
|
|
|
|
#else
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
if (pNtDelayExecution)
|
|
{
|
|
auto endTimeSteady = AuTime::SteadyClockNS() + qwTimeout;
|
|
auto endTimeWall = AuTime::CurrentClockNS() + qwTimeout;
|
|
auto targetTimeNt = AuTime::ConvertTimestampNs(endTimeWall);
|
|
|
|
Win32DropSchedulerResolution();
|
|
|
|
while (AuTime::SteadyClockNS() < endTimeSteady)
|
|
{
|
|
LARGE_INTEGER word;
|
|
word.QuadPart = targetTimeNt;
|
|
pNtDelayExecution(FALSE, &word);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
#endif
|
|
|
|
auto status = YieldPollNs(true, qwTimeout + Time::SteadyClockNS(), [=]()
|
|
{
|
|
return false;
|
|
});
|
|
#endif
|
|
}
|
|
|
|
void InitSleep()
|
|
{
|
|
// ...
|
|
}
|
|
} |