AuroraRuntime/Source/Threading/Primitives/AuSemaphore.Generic.cpp

184 lines
4.2 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2022-11-17 07:46:07 +00:00
File: AuSemaphore.Generic.cpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-12
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2022-11-17 07:46:07 +00:00
#include "AuSemaphore.Generic.hpp"
#include "../AuWakeInternal.hpp"
#include "SMTYield.hpp"
2021-06-27 21:25:29 +00:00
#if defined(_AURUNTIME_GENERIC_SEMAPHORE)
namespace Aurora::Threading::Primitives
{
SemaphoreGeneric::SemaphoreGeneric(AuUInt16 uIntialValue) :
uAtomicState(uIntialValue),
uAtomicSleeping(0)
2021-06-27 21:25:29 +00:00
{
2021-06-27 21:25:29 +00:00
}
SemaphoreGeneric::~SemaphoreGeneric()
2021-06-27 21:25:29 +00:00
{
2021-06-27 21:25:29 +00:00
}
bool SemaphoreGeneric::TryLockNoSpin()
2021-06-27 21:25:29 +00:00
{
auto uState = this->uAtomicState;
return (uState != 0 && AuAtomicCompareExchange(&this->uAtomicState, uState - 1, uState) == uState);
2021-06-27 21:25:29 +00:00
}
bool SemaphoreGeneric::TryLockHeavy()
{
return DoTryIf([=]()
{
return this->TryLockNoSpin();
});
}
bool SemaphoreGeneric::TryLock()
{
if (ThrdCfg::gPreferUnixPrimitivesNoSpin)
{
return this->TryLockNoSpin();
}
else
{
return this->TryLockHeavy();
}
}
bool SemaphoreGeneric::HasOSHandle(AuMach &mach)
2021-06-27 21:25:29 +00:00
{
return false;
}
bool SemaphoreGeneric::HasLockImplementation()
{
return true;
}
void SemaphoreGeneric::Unlock()
{
this->Unlock(1);
}
void SemaphoreGeneric::Unlock(AuUInt16 uThreads)
{
AuAtomicAdd(&this->uAtomicState, AuUInt32(uThreads));
if (auto uSleeping = AuAtomicLoad(&this->uAtomicSleeping))
{
InternalLTSWakeCount((const void *)&this->uAtomicState, AuMin(uSleeping, uThreads));
}
}
void SemaphoreGeneric::Lock()
2021-06-27 21:25:29 +00:00
{
static const AuUInt32 kRef { 0 };
if (this->TryLock())
{
return;
}
while (!this->TryLockNoSpin())
{
AuAtomicAdd(&this->uAtomicSleeping, 1u);
InternalLTSWaitOnAddressHighRes((const void *)&this->uAtomicState, &kRef, sizeof(kRef), 0);
AuAtomicSub(&this->uAtomicSleeping, 1u);
}
2021-06-27 21:25:29 +00:00
}
bool SemaphoreGeneric::LockMS(AuUInt64 qwTimeout)
2021-06-27 21:25:29 +00:00
{
return LockNS(AuMSToNS<AuUInt64>(qwTimeout));
2021-06-27 21:25:29 +00:00
}
bool SemaphoreGeneric::LockAbsMS(AuUInt64 qwTimeout)
2021-06-27 21:25:29 +00:00
{
return LockAbsNS(AuMSToNS<AuUInt64>(qwTimeout));
2021-06-27 21:25:29 +00:00
}
bool SemaphoreGeneric::LockNS(AuUInt64 qwTimeout)
2021-06-27 21:25:29 +00:00
{
static const AuUInt32 kRef { 0 };
if (this->TryLockNoSpin())
{
return true;
}
auto qwEndTime = qwTimeout ?
Time::SteadyClockNS() + qwTimeout :
0;
if (!ThrdCfg::gPreferUnixPrimitivesNoSpin)
{
if (this->TryLockHeavy())
{
return true;
}
}
while (!this->TryLockNoSpin())
{
bool bStatus {};
AuAtomicAdd(&this->uAtomicSleeping, 1u);
bStatus = InternalLTSWaitOnAddressHighRes((const void *)&this->uAtomicState, &kRef, sizeof(kRef), qwEndTime);
AuAtomicSub(&this->uAtomicSleeping, 1u);
if (!bStatus)
{
return TryLockNoSpin();
}
}
return true;
2021-06-27 21:25:29 +00:00
}
bool SemaphoreGeneric::LockAbsNS(AuUInt64 qwTimeoutAbs)
2021-06-27 21:25:29 +00:00
{
static const AuUInt32 kRef { 0 };
if (!ThrdCfg::gPreferUnixPrimitivesNoSpin)
{
if (this->TryLockHeavy())
{
return true;
}
}
while (!TryLockNoSpin())
{
bool bStatus {};
AuAtomicAdd(&this->uAtomicSleeping, 1u);
bStatus = InternalLTSWaitOnAddressHighRes((const void *)&this->uAtomicState, &kRef, sizeof(kRef), qwTimeoutAbs);
AuAtomicSub(&this->uAtomicSleeping, 1u);
if (!bStatus)
{
return TryLockNoSpin();
}
}
return true;
2021-06-27 21:25:29 +00:00
}
AUKN_SYM ISemaphore *SemaphoreNew(AuUInt16 uIntialValue)
2021-06-27 21:25:29 +00:00
{
return _new SemaphoreGeneric(uIntialValue);
2021-06-27 21:25:29 +00:00
}
AUKN_SYM void SemaphoreRelease(ISemaphore *waitable)
{
AuSafeDelete<SemaphoreGeneric *>(waitable);
2021-06-27 21:25:29 +00:00
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, Semaphore, SemaphoreGeneric)
2021-06-27 21:25:29 +00:00
}
#endif