2023-08-12 10:11:12 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuConditionMutex.Linux.cpp
|
|
|
|
Date: 2023-8-11
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "AuConditionMutex.Generic.hpp"
|
|
|
|
#include "SMTYield.hpp"
|
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERICCV)
|
|
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
|
|
|
LinuxConditionMutex::LinuxConditionMutex()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LinuxConditionMutex::~LinuxConditionMutex()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-21 18:17:05 +00:00
|
|
|
bool LinuxConditionMutex::LockMS(AuUInt64 uTimeout)
|
|
|
|
{
|
|
|
|
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxConditionMutex::LockNS(AuUInt64 uTimeout)
|
|
|
|
{
|
|
|
|
if (TryLockNoSpin())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LockNS(AuTime::SteadyClockNS() + uTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxConditionMutex::LockAbsMS(AuUInt64 uTimeout)
|
|
|
|
{
|
|
|
|
return LockAbsNS(AuMSToNS<AuUInt64>(uTimeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxConditionMutex::LockAbsNS(AuUInt64 uTimeout)
|
|
|
|
{
|
|
|
|
struct timespec tspec;
|
|
|
|
|
|
|
|
if (TryLockNoSpin())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
|
|
|
Time::monoabsns2ts(&tspec, uTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TryLockHeavy())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuAtomicAdd(&this->uSleeping_, 1u);
|
|
|
|
|
|
|
|
auto state = this->uState_;
|
|
|
|
while (!(state == 0 &&
|
|
|
|
AuAtomicCompareExchange<AuUInt32>(&this->uState_, 1, state) == state))
|
|
|
|
{
|
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
|
|
|
if (Time::SteadyClockNS() >= uTimeout)
|
|
|
|
{
|
|
|
|
AuAtomicSub(&this->uSleeping_, 1u);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret {};
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = futex_wait(&this->uState_, state, &tspec);
|
|
|
|
}
|
|
|
|
while (ret == EINTR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ret {};
|
|
|
|
bool bStatus {};
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((ret = futex_wait(&this->uState_, state)) == 0)
|
|
|
|
{
|
|
|
|
bStatus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == EAGAIN || errno == EAGAIN)
|
|
|
|
{
|
|
|
|
bStatus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
while (ret == EINTR);
|
|
|
|
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "Mutex wait failed: {}", ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
state = this->uState_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuAtomicSub(&this->uSleeping_, 1u);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-12 10:11:12 +00:00
|
|
|
bool LinuxConditionMutex::TryLock()
|
2023-08-19 17:33:54 +00:00
|
|
|
{
|
|
|
|
if (gRuntimeConfig.threadingConfig.bPreferLinuxCondMutexSpinTryLock)
|
|
|
|
{
|
|
|
|
return TryLockHeavy();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return TryLockNoSpin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxConditionMutex::TryLockNoSpin()
|
|
|
|
{
|
|
|
|
return AuAtomicTestAndSet(&this->uState_, 0) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxConditionMutex::TryLockHeavy()
|
2023-08-12 10:11:12 +00:00
|
|
|
{
|
|
|
|
return DoTryIf([=]()
|
|
|
|
{
|
2023-08-19 17:33:54 +00:00
|
|
|
return TryLockNoSpin();
|
2023-08-12 10:11:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxConditionMutex::Lock()
|
|
|
|
{
|
2023-08-19 17:33:54 +00:00
|
|
|
if (TryLockHeavy())
|
2023-08-12 10:11:12 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuAtomicAdd(&this->uSleeping_, 1u);
|
|
|
|
|
|
|
|
auto state = this->uState_;
|
|
|
|
while (!(state == 0 &&
|
|
|
|
AuAtomicCompareExchange<AuUInt32>(&this->uState_, 1, state) == state))
|
|
|
|
{
|
|
|
|
|
|
|
|
int ret {};
|
|
|
|
bool bStatus {};
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((ret = futex_wait(&this->uState_, state)) == 0)
|
|
|
|
{
|
|
|
|
bStatus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == EAGAIN || errno == EAGAIN)
|
|
|
|
{
|
|
|
|
bStatus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
while (ret == EINTR);
|
|
|
|
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "Mutex wait failed: {}", ret)
|
|
|
|
|
|
|
|
state = this->uState_;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuAtomicSub(&this->uSleeping_, 1u);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxConditionMutex::Unlock()
|
|
|
|
{
|
2023-08-23 19:20:12 +00:00
|
|
|
AuAtomicClearU8Lock(&this->uState_);
|
|
|
|
if (AuAtomicLoad(&this->uSleeping_))
|
2023-08-12 10:11:12 +00:00
|
|
|
{
|
|
|
|
futex_wake(&this->uState_, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt LinuxConditionMutex::GetOSHandle()
|
|
|
|
{
|
|
|
|
return AuMach(&this->uState_);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM IConditionMutex *ConditionMutexNew()
|
|
|
|
{
|
2023-08-21 18:17:05 +00:00
|
|
|
return _new ConditionMutexImpl();
|
2023-08-12 10:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void ConditionMutexRelease(IConditionMutex *mutex)
|
|
|
|
{
|
2023-08-21 18:17:05 +00:00
|
|
|
AuSafeDelete<ConditionMutexImpl *>(mutex);
|
2023-08-12 10:11:12 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 18:17:05 +00:00
|
|
|
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, ConditionMutex, ConditionMutexImpl)
|
2023-08-12 10:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|