AuroraRuntime/Source/IO/Loop/LSLocalSemaphore.cpp

336 lines
8.7 KiB
C++
Raw Normal View History

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: LSLocalSemaphore.cpp
Date: 2023-10-21
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "LSLocalSemaphore.hpp"
#include <Source/Threading/Primitives/SMTYield.hpp>
namespace Aurora::IO::Loop
{
LSLocalSemaphore::LSLocalSemaphore()
{
}
LSLocalSemaphore::~LSLocalSemaphore()
{
}
bool LSLocalSemaphore::TryInit(AuUInt32 initialCount)
{
2023-10-22 05:11:39 +00:00
if (!LSSemaphore::TryInit(initialCount))
{
return false;
}
this->uAtomicSemaphore = initialCount;
2023-10-22 05:11:39 +00:00
this->uAtomicKernelSemaphore = initialCount;
return true;
}
void LSLocalSemaphore::DoParanoia()
{
if (auto uCount = AuAtomicLoad(&this->uAtomicSemaphore))
{
AuAtomicAdd(&this->uAtomicKernelSemaphore, uCount);
LSSemaphore::AddMany(uCount);
}
}
bool LSLocalSemaphore::OnTrigger(AuUInt handle)
{
2023-10-21 17:12:23 +00:00
auto bRet = this->TryTakeNoSpin();
bool bTriggerLater = !bRet;
2023-10-21 17:12:23 +00:00
while (true)
{
auto uOld = AuAtomicLoad(&this->uAtomicKernelSemaphore);
2023-10-21 17:12:23 +00:00
if (uOld == 0)
{
DoParanoia();
2023-10-21 17:12:23 +00:00
break;
}
2023-10-22 05:11:39 +00:00
if (AuAtomicCompareExchange(&this->uAtomicKernelSemaphore, uOld - 1, uOld) == uOld)
2023-10-21 17:12:23 +00:00
{
auto uCount = AuAtomicLoad(&this->uAtomicSemaphore);
if (uOld - 1 == 0)
2023-10-21 17:12:23 +00:00
{
#if defined(AURORA_PLATFORM_LINUX)
if (uCount == 1)
{
// Don't acknowledge?
// Don't write into?
// saves two syscalls for nothang
}
else if (uCount)
{
AuAtomicAdd(&this->uAtomicKernelSemaphore, uCount - 1);
LSSemaphore::AddMany(uCount - 1);
}
#else
if (uCount)
{
AuAtomicAdd(&this->uAtomicKernelSemaphore, uCount);
LSSemaphore::AddMany(uCount);
}
#endif
else
{
(void)LSSemaphore::OnTrigger(0);
bTriggerLater = false;
}
}
else if (uOld || !bRet)
{
(void)LSSemaphore::OnTrigger(0);
bTriggerLater = false;
2023-10-21 17:12:23 +00:00
}
break;
}
}
#if 0
if (bTriggerLater)
{
(void)LSSemaphore::OnTrigger(0);
}
#endif
2023-10-21 17:12:23 +00:00
return bRet;
}
bool LSLocalSemaphore::AddOne()
{
2023-10-21 17:12:23 +00:00
auto uNext = AuAtomicAdd(&this->uAtomicSemaphore, 1u);
#if 0
2023-10-22 05:11:39 +00:00
if (AuAtomicLoad(&this->uAtomicKernelSemaphore) >= uNext)
{
2023-10-21 17:12:23 +00:00
return true;
}
2023-10-21 17:12:23 +00:00
2023-10-22 05:11:39 +00:00
AuAtomicAdd(&this->uAtomicKernelSemaphore, 1u);
#else
while (true)
{
auto uCurrentValue = AuAtomicLoad(&this->uAtomicKernelSemaphore);
auto uNextValue = uCurrentValue;
bool bCanReturn = false;
if (uCurrentValue < uNext)
{
uNextValue = uNext;
}
else
{
bCanReturn = true;
}
if (AuAtomicCompareExchange(&this->uAtomicKernelSemaphore, uNextValue, uCurrentValue) == uCurrentValue)
{
if (bCanReturn)
{
return true;
}
else
{
break;
}
}
}
#endif
2023-10-21 17:12:23 +00:00
LSSemaphore::AddOne();
return true;
}
2023-10-22 05:11:39 +00:00
bool LSLocalSemaphore::AddMany(AuUInt32 uCount)
{
auto uNext = AuAtomicAdd(&this->uAtomicSemaphore, uCount);
2023-10-22 05:11:39 +00:00
#if 0
2023-10-22 05:11:39 +00:00
if (AuAtomicLoad(&this->uAtomicKernelSemaphore) >= uNext)
{
return true;
}
else
{
/* if AddMany add/load/kernel-wake race condition, it's the next AddMany persons problem. */
/* uAtomicKernelSemaphore cannot be lower than uAtomicSemaphore, at the epilogue of the last unlock/adds tick. */
2023-10-22 05:11:39 +00:00
/* If it somehow is, ::OnTrigger will check that the final kernel negative increment does not occur just before (linux) after (win32) (bool(this->uAtomicSemaphore)). */
/* Remember: this->uAtomicKernelSemaphore should only be decremented after uAtomicSemaphore and uAtomicKernelSemaphore have already been incremented together... */
/* ...therefore, the last kernel waker should always see bool(this->uAtomicSemaphore), unless stolen by another thread. */
/* if stolen, it's a race condition we dont care about; we avoided the kernel object and state entirely. We have to wait for another AddMany to wake us up. */
/* if not stolen, uAtomicSemaphore is read as non-zero, and is readded to the kernel semaphore */
/* Most users just use the IO event objects LSAsync and LSLocalEvent. These are known good. */
}
2023-10-22 05:11:39 +00:00
AuAtomicAdd(&this->uAtomicKernelSemaphore, uCount);
#else
while (true)
{
auto uCurrentValue = AuAtomicLoad(&this->uAtomicKernelSemaphore);
auto uNextValue = uCurrentValue;
bool bCanReturn = false;
if (uCurrentValue < uNext)
{
uNextValue = uNext;
}
else
{
bCanReturn = true;
}
if (AuAtomicCompareExchange(&this->uAtomicKernelSemaphore, uNextValue, uCurrentValue) == uCurrentValue)
{
if (bCanReturn)
{
return true;
}
else
{
uCount = uNext - uCurrentValue;
break;
}
}
}
#endif
2023-10-22 05:11:39 +00:00
LSSemaphore::AddMany(uCount);
return true;
}
bool LSLocalSemaphore::IsSignaled()
{
return this->TryTake();
}
2023-12-01 10:33:55 +00:00
bool LSLocalSemaphore::IsSignaledNoSpinIfUserland()
{
return this->TryTakeNoSpin();
}
bool LSLocalSemaphore::WaitOn(AuUInt32 timeout)
{
return this->TryTakeWaitMS(timeout);
}
ELoopSource LSLocalSemaphore::GetType()
{
2023-12-18 07:37:48 +00:00
return ELoopSource::eSourceFastSemaphore;
}
bool LSLocalSemaphore::TryTakeNoSpin()
{
AuUInt32 uOld {};
while ((uOld = this->uAtomicSemaphore))
{
if (AuAtomicCompareExchange(&this->uAtomicSemaphore, uOld - 1, uOld) == uOld)
{
return true;
}
}
return false;
}
bool LSLocalSemaphore::TryTakeSpin()
{
return Threading::Primitives::DoTryIfAlderLake([&]
{
return this->TryTakeNoSpin();
}, &this->uAtomicSemaphore);
}
bool LSLocalSemaphore::TryTake()
{
2023-12-01 10:33:55 +00:00
return this->TryTakeSpin();
}
bool LSLocalSemaphore::TryTakeWaitMS(AuUInt32 timeout)
{
auto uEndTime = timeout ?
AuTime::SteadyClockNS() + AuMSToNS<AuUInt64>(timeout) :
0;
if (this->TryTakeSpin())
{
return true;
}
while (!this->TryTakeNoSpin())
{
if (!timeout)
{
if (LSSemaphore::WaitOn(0))
{
return true;
}
}
else
{
auto uStartTime = Time::SteadyClockNS();
if (uStartTime >= uEndTime)
{
return false;
}
auto uDeltaMs = AuNSToMS<AuInt64>(uEndTime - uStartTime);
if (uDeltaMs &&
LSSemaphore::WaitOn(uDeltaMs))
{
return true;
}
2023-10-26 16:25:40 +00:00
else if (!uDeltaMs)
{
if (this->TryTakeSpin())
{
return true;
}
}
}
}
return true;
}
2023-10-21 17:12:23 +00:00
void LSLocalSemaphore::OnPresleep()
{
}
void LSLocalSemaphore::OnFinishSleep()
{
2023-10-22 05:11:39 +00:00
2023-10-21 17:12:23 +00:00
}
AUKN_SYM AuSPtr<ILSSemaphore> NewLSSemaphore(AuUInt32 initialCount)
{
auto pMutex = AuMakeShared<LSLocalSemaphore>();
if (!pMutex)
{
SysPushErrorGeneric();
return {};
}
if (!pMutex->TryInit(initialCount))
{
SysPushErrorNested();
return {};
}
return pMutex;
}
}