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

258 lines
5.9 KiB
C++
Raw Normal View History

/***
Copyright (C) 2022-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuSemaphore.Linux.cpp
Date: 2022-12-28
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuSemaphore.Generic.hpp"
#include "SMTYield.hpp"
#if !defined(_AURUNTIME_GENERIC_SEMAPHORE)
#include <Source/Time/Time.hpp>
namespace Aurora::Threading::Primitives
{
SemaphoreImpl::SemaphoreImpl(AuUInt16 uIntialValue) : dwState_(uIntialValue)
{
}
SemaphoreImpl::~SemaphoreImpl()
{
}
bool SemaphoreImpl::HasOSHandle(AuMach &mach)
{
return false;
}
bool SemaphoreImpl::HasLockImplementation()
{
return true;
}
2023-08-17 22:06:02 +00:00
bool SemaphoreImpl::TryLockNoSpin()
{
auto old = this->dwState_;
return (old != 0 && AuAtomicCompareExchange(&this->dwState_, old - 1, old) == old);
}
bool SemaphoreImpl::TryLock()
2023-08-19 17:33:54 +00:00
{
if (ThrdCfg::gPreferLinuxSemaphoreSpinTryLock)
2023-08-19 17:33:54 +00:00
{
2023-08-27 11:41:51 +00:00
return this->TryLockHeavy();
2023-08-19 17:33:54 +00:00
}
else
{
2023-08-27 11:41:51 +00:00
return this->TryLockNoSpin();
2023-08-19 17:33:54 +00:00
}
}
bool SemaphoreImpl::TryLockHeavy()
{
return DoTryIfAlderLake([=]()
{
2023-09-09 18:46:08 +00:00
return this->TryLockNoSpin();
}, &this->dwState_);
}
bool SemaphoreImpl::LockMS(AuUInt64 uTimeout)
{
2023-09-09 18:46:08 +00:00
return this->LockNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool SemaphoreImpl::LockNS(AuUInt64 uTimeout)
{
AuUInt64 uStart {};
AuUInt64 uEnd {};
2023-08-22 14:28:09 +00:00
if (this->TryLockNoSpin())
{
return true;
}
errno = 0;
struct timespec tspec;
if (uTimeout != 0)
{
uStart = AuTime::SteadyClockNS();
uEnd = uStart + uTimeout;
Time::monoabsns2ts(&tspec, uEnd);
}
if (!ThrdCfg::gPreferLinuxPrimitivesFutexNoSpin)
2023-08-27 11:41:51 +00:00
{
if (this->TryLockHeavy())
{
return true;
}
2023-08-22 14:28:09 +00:00
}
2023-08-17 22:06:02 +00:00
AuAtomicAdd<AuUInt32>(&this->dwSleeping_, 1u);
while (!this->TryLockNoSpin())
{
if (uTimeout != 0)
{
if (Time::SteadyClockNS() >= uEnd)
{
2023-08-27 11:41:51 +00:00
AuAtomicSub<AuUInt32>(&this->dwSleeping_, 1u);
return false;
}
int ret {};
do
{
2023-08-17 22:06:02 +00:00
ret = futex_wait(&this->dwState_, 0, &tspec);
}
while (ret == -EINTR);
}
else
{
int ret {};
2023-03-12 20:59:03 +00:00
bool bStatus {};
do
{
2023-08-17 22:06:02 +00:00
if ((ret = futex_wait(&this->dwState_, 0)) == 0)
{
2023-03-12 20:59:03 +00:00
bStatus = true;
continue;
}
if (ret == -EAGAIN)
{
2023-03-12 20:59:03 +00:00
bStatus = true;
continue;
}
}
while (ret == -EINTR);
2023-08-22 14:28:09 +00:00
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "semaphore wait failed: {}", ret)
}
}
AuAtomicSub<AuUInt32>(&this->dwSleeping_, 1u);
return true;
}
bool SemaphoreImpl::LockAbsMS(AuUInt64 timeout)
{
return this->LockAbsNS(AuMSToNS<AuUInt64>(timeout));
}
bool SemaphoreImpl::LockAbsNS(AuUInt64 uTimeout)
{
AuUInt64 uEnd {};
2023-08-27 11:41:51 +00:00
if (ThrdCfg::gPreferLinuxPrimitivesFutexNoSpin)
2023-08-22 14:28:09 +00:00
{
2023-08-27 11:41:51 +00:00
if (this->TryLockNoSpin())
{
return true;
}
}
else
{
if (this->TryLockHeavy())
{
return true;
}
2023-08-22 14:28:09 +00:00
}
struct timespec tspec;
if (uTimeout != 0)
{
uEnd = uTimeout;
Time::monoabsns2ts(&tspec, uEnd);
}
AuAtomicAdd<AuUInt32>(&this->dwSleeping_, 1u);
while (!this->TryLockNoSpin())
2023-08-22 14:28:09 +00:00
{
if (uTimeout != 0)
{
if (Time::SteadyClockNS() >= uEnd)
{
AuAtomicSub<AuUInt32>(&this->dwSleeping_, 1u);
return false;
}
int ret {};
do
{
ret = futex_wait(&this->dwState_, 0, &tspec);
}
while (ret == -EINTR);
2023-08-22 14:28:09 +00:00
}
else
{
int ret {};
bool bStatus {};
do
{
if ((ret = futex_wait(&this->dwState_, 0)) == 0)
{
bStatus = true;
continue;
}
if (ret == -EAGAIN)
2023-08-22 14:28:09 +00:00
{
bStatus = true;
continue;
}
}
while (ret == -EINTR);
2023-03-12 20:59:03 +00:00
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "semaphore wait failed: {}", ret)
}
}
2023-08-17 22:06:02 +00:00
AuAtomicSub<AuUInt32>(&this->dwSleeping_, 1u);
return true;
}
void SemaphoreImpl::Lock()
{
auto status = LockNS(0);
SysAssert(status, "Couldn't lock semaphore");
}
void SemaphoreImpl::Unlock(AuUInt16 count)
{
2023-08-17 22:06:02 +00:00
AuAtomicAdd<AuUInt32>(&this->dwState_, count);
2023-08-27 11:41:51 +00:00
2023-08-23 22:45:26 +00:00
if (AuAtomicLoad(&this->dwSleeping_))
2023-08-17 22:06:02 +00:00
{
futex_wake(&this->dwState_, count);
}
}
void SemaphoreImpl::Unlock()
{
Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(AuUInt16 uInitialCount)
{
return _new SemaphoreImpl(uInitialCount);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)
{
AuSafeDelete<SemaphoreImpl *>(pSemaphore);
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, Semaphore, SemaphoreImpl)
}
#endif