From 783bed0d85432a420ebe90edac33fc74913a6a1e Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sat, 9 Dec 2023 10:05:20 +0000 Subject: [PATCH] [*] LSLocalEvent: finish optimized impl (?) --- Source/IO/Loop/LSLocalEvent.cpp | 30 ++++++++++++++++++++++++++++- Source/IO/Loop/LSLocalEvent.hpp | 1 + Source/IO/Loop/LSLocalMutex.cpp | 4 +++- Source/IO/Loop/LSLocalSemaphore.cpp | 4 +++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Source/IO/Loop/LSLocalEvent.cpp b/Source/IO/Loop/LSLocalEvent.cpp index aff8e4db..74f9e138 100644 --- a/Source/IO/Loop/LSLocalEvent.cpp +++ b/Source/IO/Loop/LSLocalEvent.cpp @@ -58,6 +58,7 @@ namespace Aurora::IO::Loop else { (void)LSSemaphore::OnTrigger(0); + AuAtomicAdd(&this->uLaterAlwaysOn, 1u); return false; } } @@ -71,6 +72,7 @@ namespace Aurora::IO::Loop else { (void)LSSemaphore::OnTrigger(0); + AuAtomicAdd(&this->uLaterAlwaysOn, 1u); return false; } } @@ -137,6 +139,30 @@ namespace Aurora::IO::Loop void LSLocalEvent::OnPresleep() { AuAtomicAdd(&this->uApproxSleepCount, 1u); + + EventBits bits; + bits.state = AuAtomicLoad(&this->state_); + if (!bits.bTriggered) + { + return; + } + + while (true) + { + auto uState = AuAtomicLoad(&this->uLaterAlwaysOn); + + if (!uState) + { + return; + } + + if (AuAtomicCompareExchange(&this->uLaterAlwaysOn, uState - 1, uState) == uState) + { + break; + } + } + + LSSemaphore::AddOne(); } void LSLocalEvent::OnFinishSleep() @@ -210,7 +236,9 @@ namespace Aurora::IO::Loop bool LSLocalEvent::TryTakeWaitMS(AuUInt32 timeout) { - auto uEndTime = AuTime::SteadyClockNS() + AuMSToNS(timeout); + auto uEndTime = timeout ? + AuTime::SteadyClockNS() + AuMSToNS(timeout) : + 0; if (this->TryTakeSpin()) { diff --git a/Source/IO/Loop/LSLocalEvent.hpp b/Source/IO/Loop/LSLocalEvent.hpp index 0ec367b2..ddc76aae 100644 --- a/Source/IO/Loop/LSLocalEvent.hpp +++ b/Source/IO/Loop/LSLocalEvent.hpp @@ -60,5 +60,6 @@ namespace Aurora::IO::Loop }; AuUInt32 uApproxSleepCount {}; + AuUInt32 uLaterAlwaysOn {}; }; } \ No newline at end of file diff --git a/Source/IO/Loop/LSLocalMutex.cpp b/Source/IO/Loop/LSLocalMutex.cpp index d6403d5d..2f94bf73 100644 --- a/Source/IO/Loop/LSLocalMutex.cpp +++ b/Source/IO/Loop/LSLocalMutex.cpp @@ -121,7 +121,9 @@ namespace Aurora::IO::Loop bool LSLocalMutex::TryTakeWaitMS(AuUInt32 timeout) { - auto uEndTime = AuTime::SteadyClockNS() + AuMSToNS(timeout); + auto uEndTime = timeout ? + AuTime::SteadyClockNS() + AuMSToNS(timeout) : + 0; if (this->TryTakeSpin()) { diff --git a/Source/IO/Loop/LSLocalSemaphore.cpp b/Source/IO/Loop/LSLocalSemaphore.cpp index ebf68253..7aafa388 100644 --- a/Source/IO/Loop/LSLocalSemaphore.cpp +++ b/Source/IO/Loop/LSLocalSemaphore.cpp @@ -163,7 +163,9 @@ namespace Aurora::IO::Loop bool LSLocalSemaphore::TryTakeWaitMS(AuUInt32 timeout) { - auto uEndTime = AuTime::SteadyClockNS() + AuMSToNS(timeout); + auto uEndTime = timeout ? + AuTime::SteadyClockNS() + AuMSToNS(timeout) : + 0; if (this->TryTakeSpin()) {