AuroraRuntime/Source/Threading/Primitives/AuSpinLock.cpp
Reece e82ec4a343 [+] IWaitable::LockNS(...)
[+] AuThreading.WakeAllOnAddress
[+] AuThreading.WakeOnAddress
[+] AuThreading.WakeNOnAddress
[+] AuThreading.TryWaitOnAddress
[+] AuThreading.WaitOnAddress
[*] Further optimize synch primitives
[+] AuThreadPrimitives::RWRenterableLock
2023-03-12 15:27:28 +00:00

96 lines
1.9 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuSpinLock.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuSpinLock.hpp"
#include "SMPYield.hpp"
namespace Aurora::Threading::Primitives
{
static void YieldCpu(long &count)
{
int loops = (1 << count);
while (loops > 0)
{
SMPPause();
loops -= 1;
}
count++;
if (count >= 15) count = 0;
}
SpinLock::SpinLock()
{
value_ = 0;
}
bool SpinLock::HasOSHandle(AuMach &mach)
{
return false;
}
bool SpinLock::TryLock()
{
return AuAtomicTestAndSet(&this->value_, 0) == 0;
}
bool SpinLock::HasLockImplementation()
{
return true;
}
void SpinLock::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock Mutex object");
}
bool SpinLock::LockNS(AuUInt64 timeout)
{
if (timeout == 0)
{
while (AuAtomicTestAndSet(&this->value_, 0))
{
long count = 0;
while (this->value_)
{
YieldCpu(count);
}
}
}
else
{
AuUInt64 startTime = AuTime::HighResClockNS();
AuUInt64 endTime = startTime + timeout;
while (AuAtomicTestAndSet(&this->value_, 0))
{
long count = 0;
while (value_)
{
if (endTime <= AuTime::HighResClockNS())
{
return false;
}
YieldCpu(count);
}
}
}
return true;
}
bool SpinLock::Lock(AuUInt64 timeout)
{
return LockNS(AuMSToNS<AuUInt64>(timeout));
}
void SpinLock::Unlock()
{
this->value_ = 0;
}
}