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: AuSpinLock.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 "AuSpinLock.hpp"
|
2023-03-21 03:18:09 +00:00
|
|
|
#include "SMTYield.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
|
|
|
SpinLock::SpinLock()
|
|
|
|
{
|
2024-05-07 15:46:12 +00:00
|
|
|
this->state_ = 0;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpinLock::HasOSHandle(AuMach &mach)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpinLock::TryLock()
|
|
|
|
{
|
2024-05-07 15:46:12 +00:00
|
|
|
return DoTryIfAlderLake([=]()
|
|
|
|
{
|
|
|
|
return AuAtomicTestAndSet(&this->state_, 0) == 0;
|
|
|
|
}, &this->state_);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpinLock::HasLockImplementation()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-03 07:21:44 +00:00
|
|
|
void SpinLock::SlowLock()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-04-03 07:21:44 +00:00
|
|
|
auto status = LockNS(0);
|
2021-06-27 21:25:29 +00:00
|
|
|
SysAssert(status, "Couldn't lock Mutex object");
|
|
|
|
}
|
|
|
|
|
2024-05-07 15:46:12 +00:00
|
|
|
bool SpinLock::LockAbsNS(AuUInt64 timeout)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-02-18 17:39:01 +00:00
|
|
|
if (timeout == 0)
|
2024-05-07 15:46:12 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (DoTryIfAlderLake([=]()
|
|
|
|
{
|
|
|
|
return AuAtomicTestAndSet(&this->state_, 0) == 0;
|
|
|
|
}, &this->state_))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-02-18 17:39:01 +00:00
|
|
|
{
|
2023-04-03 07:21:44 +00:00
|
|
|
while (AuAtomicTestAndSet(&this->state_, 0))
|
2022-02-18 17:39:01 +00:00
|
|
|
{
|
2024-05-07 15:46:12 +00:00
|
|
|
if (DoTryIfAlderLake([=]()
|
|
|
|
{
|
|
|
|
return AuAtomicTestAndSet(&this->state_, 0) == 0;
|
|
|
|
}, &this->state_))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout <= AuTime::SteadyClockNS())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpinLock::LockNS(AuUInt64 timeout)
|
|
|
|
{
|
|
|
|
if (timeout == 0)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (DoTryIfAlderLake([=]()
|
|
|
|
{
|
|
|
|
return AuAtomicTestAndSet(&this->state_, 0) == 0;
|
|
|
|
}, &this->state_))
|
2022-02-18 17:39:01 +00:00
|
|
|
{
|
2024-05-07 15:46:12 +00:00
|
|
|
return true;
|
2022-02-18 17:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2024-05-07 15:46:12 +00:00
|
|
|
timeout += AuTime::SteadyClockNS();
|
2022-02-18 17:39:01 +00:00
|
|
|
|
2023-04-03 07:21:44 +00:00
|
|
|
while (AuAtomicTestAndSet(&this->state_, 0))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2024-05-07 15:46:12 +00:00
|
|
|
if (DoTryIfAlderLake([=]()
|
2022-02-18 17:39:01 +00:00
|
|
|
{
|
2024-05-07 15:46:12 +00:00
|
|
|
return AuAtomicTestAndSet(&this->state_, 0) == 0;
|
|
|
|
}, &this->state_))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout <= AuTime::SteadyClockNS())
|
|
|
|
{
|
|
|
|
return false;
|
2022-02-18 17:39:01 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2024-05-07 15:46:12 +00:00
|
|
|
|
|
|
|
return true;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-03 07:21:44 +00:00
|
|
|
bool SpinLock::LockMS(AuUInt64 timeout)
|
2023-03-12 15:27:28 +00:00
|
|
|
{
|
|
|
|
return LockNS(AuMSToNS<AuUInt64>(timeout));
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
void SpinLock::Unlock()
|
|
|
|
{
|
2023-04-03 07:21:44 +00:00
|
|
|
this->state_ = 0;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|