[+] WOA_STRICTER_FIFO

This commit is contained in:
Reece Wilson 2024-11-21 17:17:09 +00:00
parent c21af7d3e0
commit 2440d5b835
2 changed files with 73 additions and 8 deletions

View File

@ -19,6 +19,13 @@
#define HACK_NO_INVALID_ACCESS_LEAK_SHARED_REF_ON_DESTROYED_THREAD
// WOA_ALWAYS_DUMB_OS_TARGET -> iOS, notarized MacOS, Win9x, Xbox 360, etc
// This will tank the average case:
// > fast paths will be disabled,
// > lock barriers turn into full lock guards,
// > and every sleep will require a condvar or semaphore wakeup.
// Every perf trick will be bypassed.
//#define WOA_STRICTER_FIFO
namespace Aurora::Threading
{
#if defined(HACK_NO_INVALID_ACCESS_LEAK_SHARED_REF_ON_DESTROYED_THREAD)
@ -122,20 +129,24 @@ namespace Aurora::Threading
if (state.qwNanosecondsAbs)
{
#if !defined(WOA_STRICTER_FIFO)
if (!WaitBuffer::Compare2<eMethod, true>(this->pAddress, this->uSize, state.compare.buffer, state.uDownsizeMask))
{
return true;
}
#endif
auto uNow = AuTime::SteadyClockNS();
auto uEndTime = state.qwNanosecondsAbs.value();
while (uNow < uEndTime)
{
#if !defined(WOA_STRICTER_FIFO)
if (!WaitBuffer::Compare2<eMethod, true>(this->pAddress, this->uSize, state.compare.buffer, state.uDownsizeMask))
{
return true;
}
#endif
#if defined(AURORA_PLATFORM_WIN32)
Win32DropSchedulerResolution();
@ -151,8 +162,17 @@ namespace Aurora::Threading
#if !defined(WOA_SEMAPHORE_MODE)
this->mutex.Lock();
#endif
#if defined(WOA_STRICTER_FIFO)
if (!WaitBuffer::Compare2<eMethod, true>(this->pAddress, this->uSize, state.compare.buffer, state.uDownsizeMask))
{
return true;
}
#endif
continue;
}
else
{
#if defined(WOA_SEMAPHORE_MODE)
this->semaphore->LockAbsNS(uEndTime);
@ -162,6 +182,14 @@ namespace Aurora::Threading
#endif
}
#if defined(WOA_STRICTER_FIFO)
if (!WaitBuffer::Compare2<eMethod, true>(this->pAddress, this->uSize, state.compare.buffer, state.uDownsizeMask))
{
return true;
}
#endif
uNow = AuTime::SteadyClockNS();
}
@ -169,7 +197,11 @@ namespace Aurora::Threading
}
else
{
#if !defined(WOA_STRICTER_FIFO)
while (WaitBuffer::Compare2<eMethod, true>(this->pAddress, this->uSize, state.compare.buffer, state.uDownsizeMask))
#else
do
#endif
{
if (!AuAtomicLoad(&this->bAlive))
{
@ -180,8 +212,10 @@ namespace Aurora::Threading
#if !defined(WOA_SEMAPHORE_MODE)
this->mutex.Lock();
#endif
continue;
}
else
{
#if defined(WOA_SEMAPHORE_MODE)
this->semaphore->Lock();
@ -190,6 +224,9 @@ namespace Aurora::Threading
#endif
}
}
#if defined(WOA_STRICTER_FIFO)
while (WaitBuffer::Compare2<eMethod, true>(this->pAddress, this->uSize, state.compare.buffer, state.uDownsizeMask));
#endif
return true;
}
@ -632,11 +669,14 @@ namespace Aurora::Threading
{
decltype(pCurrentHead) pBefore {};
#if !defined(WOA_SEMAPHORE_MODE)
// Insertion barrier
#if !defined(WOA_SEMAPHORE_MODE) && !defined(WOA_STRICTER_FIFO)
// Condvar wait-list insertion barrier
{
AU_LOCK_GUARD(pCurrentHead->mutex);
}
#elif !defined(WOA_SEMAPHORE_MODE)
// do { ... | bAlive consumer | } while (...) lock guard
AU_LOCK_GUARD(pCurrentHead->mutex);
#endif
auto [bCont, bRemove] = callback(*pCurrentHead);
@ -756,6 +796,11 @@ namespace Aurora::Threading
#if defined(WOA_ALWAYS_DUMB_OS_TARGET)
return false;
#endif
#if defined(WOA_STRICTER_FIFO)
return false;
#endif
static AuOptionalEx<bool> gIsWaitOnRecommendedCache {};
if (gIsWaitOnRecommendedCache)
@ -1216,11 +1261,13 @@ namespace Aurora::Threading
AuUInt64 qwNanoseconds,
AuOptional<bool> optAlreadySpun)
{
#if !defined(WOA_STRICTER_FIFO)
// Avoid SteadyTime syscall in the event of HAL retardation (missing KUSER QPC, Linux vDSO, etc)
if (!WaitBuffer::Compare2<EWaitMethod::eNotEqual, true>(pTargetAddress, uWordSize, pCompareAddress, kMax64))
{
return true;
}
#endif
return WaitOnAddressSteady(pTargetAddress,
pCompareAddress,
@ -1236,11 +1283,13 @@ namespace Aurora::Threading
AuUInt64 qwNanoseconds,
AuOptional<bool> optAlreadySpun)
{
#if !defined(WOA_STRICTER_FIFO)
// Avoid SteadyTime syscall in the event of HAL retardation (missing KUSER QPC, Linux vDSO, etc)
if (!WaitBuffer::Compare(pTargetAddress, uWordSize, pCompareAddress, kMax64, eMethod))
{
return true;
}
#endif
return WaitOnAddressSpecialSteady(eMethod,
pTargetAddress,
@ -1255,10 +1304,14 @@ namespace Aurora::Threading
const void *pCompareAddress,
AuUInt8 uWordSize)
{
#if !defined(WOA_STRICTER_FIFO)
return Primitives::DoTryIfAlderLake([&]()
{
return !WaitBuffer::Compare2<T, true>(pTargetAddress, uWordSize, pCompareAddress);
}, pTargetAddress);
#else
return false;
#endif
}
WOAFASTPUB bool TryWaitOnAddress(const void *pTargetAddress,
@ -1273,8 +1326,9 @@ namespace Aurora::Threading
const void *pCompareAddress,
AuUInt8 uWordSize)
{
#if !defined(WOA_STRICTER_FIFO)
DO_OF_METHOD_TYPE(return, TryWaitOnAddressSpecialTmpl, pTargetAddress, pCompareAddress, uWordSize);
#endif
return false;
}
@ -1305,6 +1359,7 @@ namespace Aurora::Threading
AuUInt8 uWordSize,
const AuFunction<bool(const void *, const void *, AuUInt8)> &check)
{
#if !defined(WOA_STRICTER_FIFO)
return Primitives::DoTryIfAlderLake([&]()
{
if (WaitBuffer::Compare2<T, true>(pTargetAddress, uWordSize, pCompareAddress))
@ -1314,6 +1369,9 @@ namespace Aurora::Threading
return check(pTargetAddress, pCompareAddress, uWordSize);
}, pTargetAddress);
#else
return false;
#endif
}
WOAFASTPUB bool TryWaitOnAddressSpecialEx(EWaitMethod eMethod,
@ -1327,7 +1385,9 @@ namespace Aurora::Threading
return TryWaitOnAddressSpecial(eMethod, pTargetAddress, pCompareAddress, uWordSize);
}
#if !defined(WOA_STRICTER_FIFO)
DO_OF_METHOD_TYPE(return, TryWaitOnAddressSpecialExTmpl, pTargetAddress, pCompareAddress, uWordSize, check);
#endif
return false;
}
@ -1394,7 +1454,8 @@ namespace Aurora::Threading
AuUInt8 uWordSize,
AuUInt64 qwNanoseconds,
AuOptional<bool> optAlreadySpun)
{
{
#if !defined(WOA_STRICTER_FIFO)
// Avoid emulated path dynamic TLS fetch without TLS section
// or various security checks
// or other such bloated thunks
@ -1402,6 +1463,7 @@ namespace Aurora::Threading
{
return true;
}
#endif
bool bWaitOnAddress = IsWaitOnRecommended();
if (bWaitOnAddress)
@ -1463,6 +1525,7 @@ namespace Aurora::Threading
AuUInt64 qwNanoseconds,
AuOptional<bool> optAlreadySpun)
{
#if !defined(WOA_STRICTER_FIFO)
// Avoid emulated path dynamic TLS fetch without TLS section
// or various security checks
// or other such bloated thunks
@ -1470,6 +1533,7 @@ namespace Aurora::Threading
{
return true;
}
#endif
bool bWaitOnAddress = IsWaitOnRecommended();
if (bWaitOnAddress)

View File

@ -36,8 +36,9 @@ namespace Aurora::Threading::Primitives
~Win32ConditionMutex();
bool TryLock();
void Lock();
void Unlock();
// TODO: these two aulines used to give me link errors. !todo: check against different msvc releases(, if i can be bothered)!!!
auline void Lock();
auline void Unlock();
AuUInt GetOSHandle();
auline bool TryLockNoSpin();