AuroraRuntime/Source/Threading/AuSleep.cpp
Reece Wilson 7be2d3fbdc [+] AuUtility::ThroughputCalculator
[+] AuNet::ISocketStats
[+] AuNet::ISocketChannel::GetRecvStats()
[+] AuNet::ISocketChannel::GetSendStats()
[+] AuIO::IOProcessor::RunTickEx(AuUInt32 dwTimeout)
[*] Refactor clock APIs
[+] Documentation in headers
[+] AuIO::IIOPipeWork::GetStartTickMS()
[+] AuIO::IIOPipeWork::GetLastTickMS()
[+] AuIO::IIOPipeWork::GetPredictedThroughput()
[+] AuIO::IIOPipeWork::GetBytesProcessed()
2022-12-06 22:58:15 +00:00

95 lines
2.2 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
{
#if defined(AURORA_IS_MODERNNT_DERIVED)
using NtDelayExecution_f = DWORD(NTAPI *)(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval);
static NtDelayExecution_f NtDelayExecution;
#endif
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 endTime = AuTime::CurrentClockNS() + qwTimeout;
if ((usleep(qwTimeout / 1000) == -1) &&
(errno == EINTR))
{
AuUInt64 now;
while ((now = AuTime::CurrentClockNS()) < endTime)
{
usleep((endTime - now) / 1000);
}
}
#else
#if defined(AURORA_IS_MODERNNT_DERIVED)
if (NtDelayExecution)
{
auto endTime = AuTime::CurrentClockNS() + qwTimeout;
auto targetTime = AuTime::ConvertTimestampNs(endTime);
while (AuTime::CurrentClockNS() < endTime)
{
LARGE_INTEGER word;
word.QuadPart = targetTime;
NtDelayExecution(FALSE, &word);
}
return;
}
#endif
auto status = YieldPollNs(true, qwTimeout + Time::HighResClockNS(), [=]()
{
return false;
});
#endif
}
void InitSleep()
{
#if defined(AURORA_IS_MODERNNT_DERIVED)
NtDelayExecution = AuReinterpretCast<NtDelayExecution_f>(GetProcAddress(LoadLibraryA("ntdll.dll"), "NtDelayExecution"));
#endif
}
}