From eceb037e705694a56c74b9fedc1c597659808dee Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Fri, 1 Dec 2023 12:24:18 +0000 Subject: [PATCH] [*] WaitMultipleLoopSources optimizations cont --- Source/IO/Loop/Loop.NT.cpp | 14 +++++++-- Source/IO/Loop/Loop.cpp | 59 +++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/Source/IO/Loop/Loop.NT.cpp b/Source/IO/Loop/Loop.NT.cpp index 0fd28300..28a5130f 100644 --- a/Source/IO/Loop/Loop.NT.cpp +++ b/Source/IO/Loop/Loop.NT.cpp @@ -12,7 +12,7 @@ namespace Aurora::IO::Loop { - AuList> WaitMultipleOrObjects(const AuList> &objects, AuUInt32 timeout) + AuList> WaitMultipleOrObjects(const AuList> &objects, bool bZeroTick, AuUInt32 dwTimeoutReq) { bool isWinLoop; AuList> loopSourceExs; @@ -68,15 +68,23 @@ namespace Aurora::IO::Loop source->OnPresleep(); } + AuUInt32 uTimeout {}; + if (!bZeroTick) + { + uTimeout = dwTimeoutReq ? + dwTimeoutReq : + INFINITE; + } + DWORD ret; if (isWinLoop && pMsgWaitForMultipleObjectsEx) { - ret = pMsgWaitForMultipleObjectsEx(handleArray.size(), handleArray.data(), timeout ? timeout : INFINITE, QS_ALLPOSTMESSAGE | QS_ALLINPUT, MWMO_INPUTAVAILABLE | MWMO_ALERTABLE); + ret = pMsgWaitForMultipleObjectsEx(handleArray.size(), handleArray.data(), uTimeout, QS_ALLPOSTMESSAGE | QS_ALLINPUT, MWMO_INPUTAVAILABLE | MWMO_ALERTABLE); } else { - ret = ::WaitForMultipleObjectsEx(handleArray.size(), handleArray.data(), false, timeout ? timeout : INFINITE, true); + ret = ::WaitForMultipleObjectsEx(handleArray.size(), handleArray.data(), false, uTimeout, true); } bool error = ((ret == WAIT_TIMEOUT) || diff --git a/Source/IO/Loop/Loop.cpp b/Source/IO/Loop/Loop.cpp index ba42458d..491f40ba 100644 --- a/Source/IO/Loop/Loop.cpp +++ b/Source/IO/Loop/Loop.cpp @@ -53,9 +53,9 @@ namespace Aurora::IO::Loop } #if defined(AURORA_IS_MODERNNT_DERIVED) - AuList> WaitMultipleOrObjects(const AuList> &objects, AuUInt32 timeout); + AuList> WaitMultipleOrObjects(const AuList> &objects, bool bZeroTick, AuUInt32 timeout); #endif - AuList> WaitMultipleOrObjectsFallback(const AuList> &objects, AuUInt32 timeout, bool &bTimeout); + AuList> WaitMultipleOrObjectsFallback(const AuList> &objects, AuUInt32 timeout, bool bZeroTick, bool &bTimeout); void ResetLoopSourceFalseAlarm(const AuSPtr &pLoopSource) { @@ -219,10 +219,11 @@ namespace Aurora::IO::Loop ) { auto pSource = *itr; + auto eType = pSource->GetType(); - if (pSource->GetType() == ELoopSource::eSourceMutex || - pSource->GetType() == ELoopSource::eSourceSemaphore || - pSource->GetType() == ELoopSource::eSourceEvent) + if (eType == ELoopSource::eSourceMutex || + eType == ELoopSource::eSourceSemaphore || + eType == ELoopSource::eSourceEvent) { auto pSourceEx = AuDynamicCast(pSource); @@ -263,17 +264,20 @@ namespace Aurora::IO::Loop if (lsList2.size()) { - if (signalTemp.size()) + bool bZeroTick { }; + if ((bZeroTick = bool(signalTemp.size()))) { uTimeoutMS = 1; } + #if defined(AURORA_IS_MODERNNT_DERIVED) - signaled = WaitMultipleOrObjects(lsList2, uTimeoutMS); - bTimedout = uTimeoutMS ? + signaled = WaitMultipleOrObjects(lsList2, bZeroTick, uTimeoutMS); + bTimedout = uTimeoutMS && !bZeroTick ? Time::SteadyClockNS() >= uTimeoutEnd : false; #else - signaled = WaitMultipleOrObjectsFallback(lsList2, uTimeoutMS, bTimedout); + signaled = WaitMultipleOrObjectsFallback(lsList2, uTimeoutMS, bZeroTick, bTimedout); + bTimedout &= !bZeroTick; #endif } @@ -290,7 +294,7 @@ namespace Aurora::IO::Loop } } - AuList> WaitMultipleOrObjectsFallback(const AuList> &objects, AuUInt32 timeout, bool &bTimeout) + AuList> WaitMultipleOrObjectsFallback(const AuList> &objects, AuUInt32 timeout, bool bZeroTick, bool &bTimeout) { AuList> loopSourceExs; AuList> triggered; @@ -330,9 +334,9 @@ namespace Aurora::IO::Loop continue; } - if (auto extended = AuDynamicCast(source)) + if (auto pLoopSourceEx = AuDynamicCast(source)) { - if (!AuTryInsert(loopSourceExs, extended)) + if (!AuTryInsert(loopSourceExs, pLoopSourceEx)) { return {}; } @@ -361,21 +365,36 @@ namespace Aurora::IO::Loop source->OnPresleep(); } - bTimeout = !pQueue->WaitAny(timeout); - - for (AU_ITERATE_N(i, objects.size())) + if (bZeroTick) { - auto source = loopSourceExs[i]; + (void)pQueue->PumpNonblocking(); + } + else + { + bTimeout = !pQueue->WaitAny(timeout); + } + + for (AU_ITERATE_N(i, loopSourceExs.size())) + { + auto pLoopSource = loopSourceExs[i]; - if (std::find(triggered.begin(), triggered.end(), source) == triggered.end()) + if (std::find(triggered.begin(), triggered.end(), pLoopSource) == triggered.end()) { - if (source->IsSignaled()) + auto eType = pLoopSource->GetType(); + + if (!bTimeout || + eType == ELoopSource::eSourceMutex || + eType == ELoopSource::eSourceSemaphore || + eType == ELoopSource::eSourceEvent) { - triggered.push_back(source); + if (pLoopSource->IsSignaledNoSpinIfUserland()) + { + triggered.push_back(pLoopSource); + } } } - source->OnFinishSleep(); + pLoopSource->OnFinishSleep(); } return triggered;