[*] Ehhh. I never liked how I implemented the fallback for MS absolute waits.

We should calculate it via a delta between current NS time to avoid NS/MS clock overflows.
Some platforms and points in time may be more resistant than others. Let's assume monotonic time could be really high, this should help mitigate some bad math and branching caused by timeout overflows caused by stupid MS-precise abs waits (not sure why people would favour these over abs ns)
This commit is contained in:
Reece Wilson 2023-09-12 16:25:42 +01:00
parent 4b0a7c651a
commit f08f4a476a

View File

@ -10,6 +10,7 @@
namespace Aurora::Time
{
AUKN_SYM AuUInt64 SteadyClockNS();
AUKN_SYM AuUInt64 SteadyClockMS();
}
namespace Aurora::Threading
@ -43,17 +44,33 @@ namespace Aurora::Threading
inline virtual bool LockAbsMS(AuUInt64 qwAbsTimeoutInMs /* = 0, infinity*/)
{
if (!qwAbsTimeoutInMs) return this->LockNS(0);
auto iTimeDelta = AuInt64(AuMSToNS<AuUInt64>(qwAbsTimeoutInMs)) - AuInt64(Aurora::Time::SteadyClockNS());
if (iTimeDelta <= 0) return TryLock();
return this->LockNS(iTimeDelta);
if (!qwAbsTimeoutInMs)
{
return this->LockNS(0);
}
auto iTimeMSDelta = AuInt64(qwAbsTimeoutInMs) - AuInt64(Aurora::Time::SteadyClockMS());
if (iTimeMSDelta <= 0)
{
return this->TryLock();
}
return this->LockAbsNS(AuMSToNS<AuUInt64>(iTimeMSDelta) + Aurora::Time::SteadyClockNS());
}
inline virtual bool LockAbsNS(AuUInt64 qwAbsTimeoutInNs /* = 0, infinity*/)
{
if (!qwAbsTimeoutInNs) return this->LockNS(0);
if (!qwAbsTimeoutInNs)
{
return this->LockNS(0);
}
auto iTimeDelta = AuInt64(qwAbsTimeoutInNs) - AuInt64(Aurora::Time::SteadyClockNS());
if (iTimeDelta <= 0) return TryLock();
if (iTimeDelta <= 0)
{
return this->TryLock();
}
return this->LockNS(iTimeDelta);
}