2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
File: AuSleep.cpp
|
2021-06-27 21:25:29 +00:00
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-11-17 07:46:07 +00:00
|
|
|
#include "AuSleep.hpp"
|
|
|
|
#include "AuWaitFor.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
2021-06-27 21:25:29 +00:00
|
|
|
#include <unistd.h>
|
2022-05-13 17:44:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
#include <Time/Time.hpp>
|
|
|
|
#endif
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
namespace Aurora::Threading
|
|
|
|
{
|
2022-11-17 07:46:07 +00:00
|
|
|
AUKN_SYM void Sleep(AuUInt64 qwTimeout)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED)
|
2022-05-13 17:51:47 +00:00
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
SleepNs(AuMSToNS<AuUInt64>(qwTimeout));
|
2022-05-13 17:51:47 +00:00
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
#elif defined(AURORA_IS_MODERNNT_DERIVED)
|
2022-05-13 17:51:47 +00:00
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
::Sleep(qwTimeout);
|
2022-05-13 17:51:47 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
#else
|
|
|
|
std::atomic<bool> value = false;
|
|
|
|
BooleanWaitable waitable(value);
|
2022-11-17 07:46:07 +00:00
|
|
|
WaitFor(&waitable, qwTimeout);
|
2021-06-27 21:25:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
AUKN_SYM void SleepNs(AuUInt64 qwTimeout)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_LINUX_DERIVED) || defined(AURORA_IS_BSD_DERIVED)
|
2022-05-13 17:51:47 +00:00
|
|
|
|
2023-01-23 15:23:43 +00:00
|
|
|
auto qwEndTime = AuTime::SteadyClockNS() + qwTimeout;
|
2022-05-13 17:51:47 +00:00
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
if ((usleep(qwTimeout / 1000) == -1) &&
|
2022-08-02 04:52:17 +00:00
|
|
|
(errno == EINTR))
|
2022-05-13 17:51:47 +00:00
|
|
|
{
|
2023-01-23 15:23:43 +00:00
|
|
|
AuUInt64 qwNow;
|
|
|
|
|
|
|
|
while ((qwNow = AuTime::SteadyClockNS()) < qwEndTime)
|
2022-05-13 17:51:47 +00:00
|
|
|
{
|
2023-01-23 15:23:43 +00:00
|
|
|
usleep((qwEndTime - qwNow) / 1000);
|
2022-05-13 17:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
#else
|
2022-05-13 17:44:44 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
|
2023-02-16 16:01:21 +00:00
|
|
|
if (pNtDelayExecution)
|
2022-05-13 17:44:44 +00:00
|
|
|
{
|
2023-01-23 15:23:43 +00:00
|
|
|
auto endTimeSteady = AuTime::SteadyClockNS() + qwTimeout;
|
|
|
|
auto endTimeWall = AuTime::CurrentClockNS() + qwTimeout;
|
|
|
|
auto targetTimeNt = AuTime::ConvertTimestampNs(endTimeWall);
|
2022-05-13 17:44:44 +00:00
|
|
|
|
2023-10-19 01:48:56 +00:00
|
|
|
Win32DropSchedulerResolution();
|
|
|
|
|
2023-01-23 15:23:43 +00:00
|
|
|
while (AuTime::SteadyClockNS() < endTimeSteady)
|
2022-05-13 17:44:44 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER word;
|
2023-01-23 15:23:43 +00:00
|
|
|
word.QuadPart = targetTimeNt;
|
2023-02-16 16:01:21 +00:00
|
|
|
pNtDelayExecution(FALSE, &word);
|
2022-05-13 17:44:44 +00:00
|
|
|
}
|
2022-05-13 17:51:47 +00:00
|
|
|
|
|
|
|
return;
|
2022-05-13 17:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-01-23 15:23:43 +00:00
|
|
|
auto status = YieldPollNs(true, qwTimeout + Time::SteadyClockNS(), [=]()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
});
|
2022-05-13 17:44:44 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitSleep()
|
|
|
|
{
|
2023-02-16 16:01:21 +00:00
|
|
|
// ...
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|