2022-12-28 23:44:45 +00:00
|
|
|
/***
|
|
|
|
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"
|
2023-03-21 03:18:09 +00:00
|
|
|
#include "SMTYield.hpp"
|
2022-12-28 23:44:45 +00:00
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERIC_SEMAPHORE)
|
|
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
2023-08-18 14:25:54 +00:00
|
|
|
SemaphoreImpl::SemaphoreImpl(AuUInt16 uIntialValue) : dwState_(uIntialValue)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
SemaphoreImpl::~SemaphoreImpl()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::HasOSHandle(AuMach &mach)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::HasLockImplementation()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::TryLock()
|
2023-08-19 17:33:54 +00:00
|
|
|
{
|
|
|
|
if (gRuntimeConfig.threadingConfig.bPreferLinuxSemaphoreSpinTryLock)
|
|
|
|
{
|
|
|
|
return TryLockHeavy();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return TryLockNoSpin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SemaphoreImpl::TryLockHeavy()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-03-12 15:27:28 +00:00
|
|
|
return DoTryIf([=]()
|
|
|
|
{
|
2023-08-19 17:33:54 +00:00
|
|
|
return TryLockNoSpin();
|
2023-03-12 15:27:28 +00:00
|
|
|
});
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
2023-03-12 15:27:28 +00:00
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::LockMS(AuUInt64 uTimeout)
|
2023-03-12 15:27:28 +00:00
|
|
|
{
|
|
|
|
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::LockNS(AuUInt64 uTimeout)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-06-26 07:33:47 +00:00
|
|
|
AuUInt64 uStart {};
|
|
|
|
AuUInt64 uEnd {};
|
|
|
|
|
2023-08-19 17:33:54 +00:00
|
|
|
if (this->TryLockHeavy())
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
struct timespec tspec;
|
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
2023-06-26 07:33:47 +00:00
|
|
|
uStart = AuTime::SteadyClockNS();
|
|
|
|
uEnd = uStart + uTimeout;
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
Time::monoabsns2ts(&tspec, uEnd);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 22:06:02 +00:00
|
|
|
AuAtomicAdd<AuUInt32>(&this->dwSleeping_, 1u);
|
|
|
|
|
|
|
|
auto old = this->dwState_;
|
2022-12-28 23:44:45 +00:00
|
|
|
//!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)))
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
2023-06-26 07:33:47 +00:00
|
|
|
if (Time::SteadyClockNS() >= uEnd)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-08-17 22:06:02 +00:00
|
|
|
AuAtomicSub<AuUInt32>(&this->dwSleeping_, 1u);
|
2022-12-28 23:44:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret {};
|
|
|
|
do
|
|
|
|
{
|
2023-08-17 22:06:02 +00:00
|
|
|
ret = futex_wait(&this->dwState_, 0, &tspec);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
while (ret == EINTR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ret {};
|
2023-03-12 20:59:03 +00:00
|
|
|
bool bStatus {};
|
|
|
|
|
2022-12-28 23:44:45 +00:00
|
|
|
do
|
|
|
|
{
|
2023-08-17 22:06:02 +00:00
|
|
|
if ((ret = futex_wait(&this->dwState_, 0)) == 0)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-03-12 20:59:03 +00:00
|
|
|
bStatus = true;
|
2022-12-28 23:44:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == EAGAIN || errno == EAGAIN)
|
|
|
|
{
|
2023-03-12 20:59:03 +00:00
|
|
|
bStatus = true;
|
2022-12-28 23:44:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
while (ret == EINTR);
|
|
|
|
|
2023-03-12 20:59:03 +00:00
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "semaphore wait failed: {}", ret)
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 22:06:02 +00:00
|
|
|
old = this->dwState_;
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 22:06:02 +00:00
|
|
|
AuAtomicSub<AuUInt32>(&this->dwSleeping_, 1u);
|
2022-12-28 23:44:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void SemaphoreImpl::Lock()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-04-03 07:21:44 +00:00
|
|
|
auto status = LockNS(0);
|
2022-12-28 23:44:45 +00:00
|
|
|
SysAssert(status, "Couldn't lock semaphore");
|
|
|
|
}
|
|
|
|
|
2023-08-18 14:25:54 +00:00
|
|
|
void SemaphoreImpl::Unlock(AuUInt16 count)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-08-17 22:06:02 +00:00
|
|
|
AuAtomicAdd<AuUInt32>(&this->dwState_, count);
|
|
|
|
if (this->dwSleeping_)
|
|
|
|
{
|
|
|
|
futex_wake(&this->dwState_, count);
|
|
|
|
}
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void SemaphoreImpl::Unlock()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-06-13 10:33:29 +00:00
|
|
|
Unlock(1);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 14:25:54 +00:00
|
|
|
AUKN_SYM ISemaphore *SemaphoreNew(AuUInt16 uInitialCount)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-08-18 14:25:54 +00:00
|
|
|
return _new SemaphoreImpl(uInitialCount);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)
|
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
AuSafeDelete<SemaphoreImpl *>(pSemaphore);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
2023-03-21 03:18:09 +00:00
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, Semaphore, SemaphoreImpl)
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
#endif
|