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

162 lines
3.8 KiB
C++
Raw Normal View History

/***
Copyright (C) 2022 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
{
2023-08-17 22:06:02 +00:00
SemaphoreImpl::SemaphoreImpl(long intialValue) : dwState_(intialValue)
{
}
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()
{
return DoTryIf([=]()
{
2023-08-17 22:06:02 +00:00
return this->TryLockNoSpin();
});
}
bool SemaphoreImpl::LockMS(AuUInt64 uTimeout)
{
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool SemaphoreImpl::LockNS(AuUInt64 uTimeout)
{
AuUInt64 uStart {};
AuUInt64 uEnd {};
if (this->TryLock())
{
return true;
}
errno = 0;
struct timespec tspec;
if (uTimeout != 0)
{
uStart = AuTime::SteadyClockNS();
uEnd = uStart + uTimeout;
Time::monoabsns2ts(&tspec, uEnd);
}
2023-08-17 22:06:02 +00:00
AuAtomicAdd<AuUInt32>(&this->dwSleeping_, 1u);
auto old = this->dwState_;
//!tryLock (with old in a scope we can access)
while (!((old != 0) &&
2023-08-17 22:06:02 +00:00
(AuAtomicCompareExchange(&this->dwState_, old - 1, old) == old)))
{
if (uTimeout != 0)
{
if (Time::SteadyClockNS() >= uEnd)
{
2023-08-17 22:06:02 +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 || errno == EAGAIN)
{
2023-03-12 20:59:03 +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
old = this->dwState_;
}
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(long count)
{
2023-08-17 22:06:02 +00:00
AuAtomicAdd<AuUInt32>(&this->dwState_, count);
if (this->dwSleeping_)
{
futex_wake(&this->dwState_, count);
}
}
void SemaphoreImpl::Unlock()
{
Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(int iInitialCount)
{
return _new SemaphoreImpl(iInitialCount);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)
{
AuSafeDelete<SemaphoreImpl *>(pSemaphore);
}
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, Semaphore, SemaphoreImpl)
}
#endif