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)
|
2024-04-29 16:08:31 +00:00
|
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
#include <Source/Threading/Primitives/SMTYield.hpp>
|
2022-05-13 17:44:44 +00:00
|
|
|
#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
|
|
|
{
|
2024-04-29 16:08:31 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2022-11-17 07:46:07 +00:00
|
|
|
::Sleep(qwTimeout);
|
2021-06-27 21:25:29 +00:00
|
|
|
#else
|
2024-04-29 16:08:31 +00:00
|
|
|
SleepNs(AuMSToNS<AuUInt64>(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;
|
2024-04-29 16:08:31 +00:00
|
|
|
if (auto uSleepMnSec = qwTimeout / 1000)
|
2022-05-13 17:51:47 +00:00
|
|
|
{
|
2024-04-29 16:08:31 +00:00
|
|
|
if ((usleep(uSleepMnSec) == -1) &&
|
|
|
|
(errno == EINTR))
|
2022-05-13 17:51:47 +00:00
|
|
|
{
|
2024-04-29 16:08:31 +00:00
|
|
|
AuUInt64 qwNow;
|
|
|
|
|
|
|
|
while ((qwNow = AuTime::SteadyClockNS()) < qwEndTime)
|
|
|
|
{
|
|
|
|
if (auto uSleepMnSec = (qwEndTime - qwNow) / 1000)
|
|
|
|
{
|
|
|
|
usleep(uSleepMnSec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-05-13 17:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
#else
|
2024-04-29 16:08:31 +00:00
|
|
|
|
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
|
|
|
{
|
2024-04-29 16:08:31 +00:00
|
|
|
auto uEndTimeSteadyNS = AuTime::SteadyClockNS() + qwTimeout;
|
|
|
|
auto uEndTimeSteadyNS2 = uEndTimeSteadyNS;
|
|
|
|
|
2024-04-23 22:17:40 +00:00
|
|
|
#if defined(_AURORA_WANT_STRICT_NT_WAKEUP_TIME_NO_WALL_CLOCK_SHIFT_ALLOWED)
|
|
|
|
auto uEndTimeWall = AuTime::CurrentClockNS() + qwTimeout;
|
|
|
|
auto uTargetTimeNt = AuTime::ConvertTimestampNs(uEndTimeWall);
|
2022-05-13 17:44:44 +00:00
|
|
|
|
2023-10-19 01:48:56 +00:00
|
|
|
Win32DropSchedulerResolution();
|
2024-04-29 16:08:31 +00:00
|
|
|
#else
|
|
|
|
if (!AuSwInfo::IsWindows8Point1OrGreater())
|
|
|
|
{
|
|
|
|
if (qwTimeout < 500000)
|
|
|
|
{
|
|
|
|
uEndTimeSteadyNS -= 10'000ull;
|
|
|
|
|
|
|
|
(void)YieldPollNs(true, uEndTimeSteadyNS, [=]()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-10 07:14:54 +00:00
|
|
|
if (qwTimeout > AuMSToNS<AuUInt64>(60))
|
2024-04-29 16:08:31 +00:00
|
|
|
{
|
2024-09-10 07:14:54 +00:00
|
|
|
uEndTimeSteadyNS -= 20'000'000ull;
|
|
|
|
}
|
|
|
|
else if (qwTimeout > AuMSToNS<AuUInt64>(40))
|
|
|
|
{
|
|
|
|
uEndTimeSteadyNS -= 10'000'000ull;
|
|
|
|
}
|
|
|
|
else if (qwTimeout > AuMSToNS<AuUInt64>(10))
|
|
|
|
{
|
|
|
|
uEndTimeSteadyNS -= 4'000'000ull;
|
|
|
|
}
|
|
|
|
else if (qwTimeout < AuMSToNS<AuUInt64>(1'300))
|
|
|
|
{
|
|
|
|
uEndTimeSteadyNS -= 500'000ull;
|
2024-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Win32DropSchedulerResolution();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (qwTimeout < AuMSToNS<AuUInt64>(1))
|
|
|
|
{
|
2024-09-10 07:14:54 +00:00
|
|
|
uEndTimeSteadyNS -= 250'000ull;
|
2024-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2023-10-19 01:48:56 +00:00
|
|
|
|
2024-04-23 22:17:40 +00:00
|
|
|
AuUInt64 uNowNS {};
|
2024-04-29 16:08:31 +00:00
|
|
|
while ((uNowNS = AuTime::SteadyClockNS()) < uEndTimeSteadyNS2)
|
2022-05-13 17:44:44 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER word;
|
2024-04-23 22:17:40 +00:00
|
|
|
#if defined(_AURORA_WANT_STRICT_NT_WAKEUP_TIME_NO_WALL_CLOCK_SHIFT_ALLOWED)
|
|
|
|
word.QuadPart = uTargetTimeNt;
|
|
|
|
#else
|
2024-04-29 16:08:31 +00:00
|
|
|
if (AuThreadPrimitives::ThrdCfg::gPlatformIsSMPProcessorOptimized)
|
|
|
|
{
|
2024-09-10 07:14:54 +00:00
|
|
|
if (uEndTimeSteadyNS2 - uNowNS <= 230000ull)
|
2024-04-29 16:08:31 +00:00
|
|
|
{
|
2024-05-05 18:42:10 +00:00
|
|
|
auto uNow = AuAtomicAdd(&AuThreadPrimitives::gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
if (!AuThreadPrimitives::gSpinAdaptiveThreshold || uNow <= AuThreadPrimitives::gSpinAdaptiveThreshold)
|
2024-04-29 16:08:31 +00:00
|
|
|
{
|
2024-05-05 18:42:10 +00:00
|
|
|
#if (defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86))
|
|
|
|
if (AuThreadPrimitives::ThrdCfg::gIsIntelAlderLakeOrGreater)
|
|
|
|
{
|
2024-05-06 21:04:26 +00:00
|
|
|
_tpause(0, __rdtsc() + 10000);
|
2024-05-05 18:42:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
for (AU_ITERATE_N(i, 32))
|
|
|
|
{
|
|
|
|
AuThreadPrimitives::SMPPause();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// shit compiler wont unwrap
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
// 32 or 16?
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
AuAtomicSub(&AuThreadPrimitives::gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AuAtomicSub(&AuThreadPrimitives::gSpinAdaptiveCurrentCount, 1u);
|
2024-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSInt sDelta1 = uEndTimeSteadyNS - uNowNS;
|
|
|
|
word.QuadPart = 0;
|
|
|
|
if (sDelta1 > 0)
|
|
|
|
{
|
|
|
|
word.QuadPart = -(sDelta1 / 100ull);
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:17:40 +00:00
|
|
|
if (!word.QuadPart)
|
|
|
|
{
|
2024-04-29 16:08:31 +00:00
|
|
|
if (AuThreadPrimitives::ThrdCfg::gPlatformIsSMPProcessorOptimized)
|
|
|
|
{
|
2024-05-05 18:42:10 +00:00
|
|
|
auto uNow = AuAtomicAdd(&AuThreadPrimitives::gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
if (!AuThreadPrimitives::gSpinAdaptiveThreshold || uNow <= AuThreadPrimitives::gSpinAdaptiveThreshold)
|
2024-04-29 16:08:31 +00:00
|
|
|
{
|
2024-05-05 18:42:10 +00:00
|
|
|
#if (defined(AURORA_ARCH_X64) || defined(AURORA_ARCH_X86))
|
|
|
|
if (AuThreadPrimitives::ThrdCfg::gIsIntelAlderLakeOrGreater)
|
|
|
|
{
|
2024-05-06 21:04:26 +00:00
|
|
|
_tpause(0, __rdtsc() + 1000);
|
2024-05-05 18:42:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
for (AU_ITERATE_N(i, 32))
|
|
|
|
{
|
|
|
|
AuThreadPrimitives::SMPPause();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// shit compiler wont unwrap
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
// 32 or 16?
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause(); AuThreadPrimitives::SMPPause();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
AuAtomicSub(&AuThreadPrimitives::gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AuAtomicSub(&AuThreadPrimitives::gSpinAdaptiveCurrentCount, 1u);
|
|
|
|
break;
|
2024-04-29 16:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-10 07:14:54 +00:00
|
|
|
if (!uEndTimeSteadyNS)
|
|
|
|
{
|
|
|
|
ContextYield();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2024-04-29 16:08:31 +00:00
|
|
|
}
|
2024-04-23 22:17:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
}
|