[+] Future proof IWaitable with Abs waits

(polyfil for now)
This commit is contained in:
Reece Wilson 2023-06-05 11:40:31 +01:00
parent 301f1a6025
commit 07ce6d8974
2 changed files with 45 additions and 10 deletions

View File

@ -7,23 +7,58 @@
***/
#pragma once
namespace Aurora::Time
{
AUKN_SYM AuUInt64 SteadyClockNS();
}
namespace Aurora::Threading
{
/**
IWaitable represents a generic waitable primitive <br>
There is no guarantee of any specific underlying primitive or backing by the operating system directly <br>
*All* methods **must** be supported on all platforms. If they don't, think harder! <br>
Implementable on: NX, Win32 via CVs, and others via pthreads <br>
Zero timeout = infinity <br>
*All* methods **must** be supported on all platforms. <br>
*/
struct IWaitable
struct AUKN_SYM IWaitable
{
// Mostly worthless. Do not access
// Used by internals
virtual bool HasOSHandle(AuMach &mach) = 0;
// Do not access
// Used by internals
virtual bool HasLockImplementation() = 0;
virtual void Lock() = 0;
virtual bool LockMS(AuUInt64 timeout /*=0*/) = 0;
virtual bool LockNS(AuUInt64 timeout /*=0*/) = 0;
inline virtual bool LockMS(AuUInt64 qwRelTimeoutInMs /* = 0, infinity - use TryLock for an opportunistic lock-spin-once operation */)
{
return this->LockAbsNS(qwRelTimeoutInMs ? Aurora::Time::SteadyClockNS() + AuMSToNS<AuUInt64>(qwRelTimeoutInMs) : 0);
}
inline virtual bool LockNS(AuUInt64 qwRelTimeoutInNs /* = 0, infinity - use TryLock for an opportunistic lock-spin-once operation */)
{
return this->LockAbsNS(qwRelTimeoutInNs ? Aurora::Time::SteadyClockNS() + qwRelTimeoutInNs : 0);
}
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);
}
inline virtual bool LockAbsNS(AuUInt64 qwAbsTimeoutInNs /* = 0, infinity*/)
{
if (!qwAbsTimeoutInNs) return this->LockNS(0);
auto iTimeDelta = AuInt64(qwAbsTimeoutInNs) - AuInt64(Aurora::Time::SteadyClockNS());
if (iTimeDelta <= 0) return TryLock();
return this->LockNS(iTimeDelta);
}
virtual bool TryLock() = 0;
virtual void Unlock() = 0;
};
}

View File

@ -33,9 +33,9 @@ namespace Aurora::Threading::Primitives
static const auto kPrimitiveSizeRWLock = kPrimitiveSizeNTRWLock;
static const auto kPrimitiveSizeCond = kPrimitiveSizeNTCond;
static const auto kPrimitiveSizeCondMutex = kPrimitiveSizeNTCondMutex;
// fuck you, its time to overtake the STL in even Windows 11 micro-benchmarks
#define AURT_ENABLE_HYPER_MUTEX
// fuck you, its time to overtake the STL in even Windows 11 micro-benchmarks
#define AURT_ENABLE_HYPER_MUTEX
//#elif defined(AURORA_IS_LINUX_DERIVED)
//
@ -73,7 +73,7 @@ namespace Aurora::Threading::Primitives
#if defined(AURT_ENABLE_HYPER_MUTEX)
using IHyperWaitable = HyperWaitable;
#else
using IHyperWaitable = IWaitable;
using IHyperWaitable = IWaitable;
#endif
}