2022-12-28 23:44:45 +00:00
|
|
|
/***
|
2024-01-29 14:09:59 +00:00
|
|
|
Copyright (C) 2022-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2022-12-28 23:44:45 +00:00
|
|
|
|
|
|
|
File: AuMutex.Linux.cpp
|
|
|
|
Date: 2022-12-28
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "AuMutex.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_MUTEX)
|
|
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
MutexImpl::MutexImpl()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-12-29 15:35:25 +00:00
|
|
|
this->state_ = 0;
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
MutexImpl::~MutexImpl()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool MutexImpl::HasOSHandle(AuMach &mach)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool MutexImpl::HasLockImplementation()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool MutexImpl::TryLock()
|
2023-08-19 17:33:54 +00:00
|
|
|
{
|
2024-05-03 11:14:52 +00:00
|
|
|
if (this->TryLockNoSpin())
|
2023-08-19 17:33:54 +00:00
|
|
|
{
|
2024-05-03 11:14:52 +00:00
|
|
|
return true;
|
2023-08-19 17:33:54 +00:00
|
|
|
}
|
2024-05-03 11:14:52 +00:00
|
|
|
|
|
|
|
if (!ThrdCfg::gPreferLinuxMutexSpinTryLock)
|
2023-08-19 17:33:54 +00:00
|
|
|
{
|
2024-05-03 11:14:52 +00:00
|
|
|
return false;
|
2023-08-19 17:33:54 +00:00
|
|
|
}
|
2024-05-03 11:14:52 +00:00
|
|
|
|
|
|
|
return this->TryLockHeavy();
|
2023-08-19 17:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MutexImpl::TryLockNoSpin()
|
|
|
|
{
|
|
|
|
return AuAtomicTestAndSet(&this->state_, 0) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MutexImpl::TryLockHeavy()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2024-05-03 11:14:52 +00:00
|
|
|
return DoTryIfAlderLake([=]()
|
2023-03-12 15:27:28 +00:00
|
|
|
{
|
2023-09-09 18:46:08 +00:00
|
|
|
return this->TryLockNoSpin();
|
2024-05-03 11:14:52 +00:00
|
|
|
}, &this->state_);
|
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 MutexImpl::LockMS(AuUInt64 uTimeout)
|
2023-03-12 15:27:28 +00:00
|
|
|
{
|
2023-09-09 18:46:08 +00:00
|
|
|
return this->LockNS(AuMSToNS<AuUInt64>(uTimeout));
|
2023-03-12 15:27:28 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool MutexImpl::LockNS(AuUInt64 uTimeout)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-06-26 07:33:47 +00:00
|
|
|
AuUInt64 uStart {};
|
|
|
|
AuUInt64 uEnd {};
|
|
|
|
|
2023-12-23 03:10:17 +00:00
|
|
|
if (this->TryLockNoSpin())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timespec tspec;
|
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
|
|
|
uStart = AuTime::SteadyClockNS();
|
|
|
|
uEnd = uStart + uTimeout;
|
|
|
|
|
|
|
|
Time::monoabsns2ts(&tspec, uEnd);
|
|
|
|
}
|
|
|
|
|
2024-01-21 21:15:57 +00:00
|
|
|
if (!ThrdCfg::gPreferLinuxPrimitivesFutexNoSpin)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-08-27 11:41:51 +00:00
|
|
|
if (this->TryLockHeavy())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
AuAtomicAdd(&this->dwSleeping_, 1u);
|
2022-12-28 23:44:45 +00:00
|
|
|
|
2023-09-12 15:12:54 +00:00
|
|
|
while (!this->TryLockNoSpin())
|
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-11 15:51:42 +00:00
|
|
|
AuAtomicSub(&this->dwSleeping_, 1u);
|
2022-12-28 23:44:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret {};
|
|
|
|
do
|
|
|
|
{
|
2023-09-12 15:12:54 +00:00
|
|
|
ret = futex_wait(&this->state_, 1, &tspec);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
2023-08-26 11:18:10 +00:00
|
|
|
while (ret == -EINTR);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ret {};
|
2023-03-12 20:59:03 +00:00
|
|
|
bool bStatus {};
|
|
|
|
|
2022-12-28 23:44:45 +00:00
|
|
|
do
|
|
|
|
{
|
2023-09-12 15:12:54 +00:00
|
|
|
if ((ret = futex_wait(&this->state_, 1)) == 0)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-03-12 20:59:03 +00:00
|
|
|
bStatus = true;
|
|
|
|
break;
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 20:35:40 +00:00
|
|
|
if (ret == -EAGAIN)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-03-12 20:59:03 +00:00
|
|
|
bStatus = true;
|
|
|
|
break;
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-08-26 11:18:10 +00:00
|
|
|
while (ret == -EINTR);
|
2022-12-28 23:44:45 +00:00
|
|
|
|
2023-03-12 20:59:03 +00:00
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "Mutex wait failed: {}", ret)
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
2023-09-12 15:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuAtomicSub(&this->dwSleeping_, 1u);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MutexImpl::LockAbsNS(AuUInt64 uTimeout)
|
|
|
|
{
|
|
|
|
struct timespec tspec;
|
|
|
|
|
|
|
|
if (this->TryLockNoSpin())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-28 23:44:45 +00:00
|
|
|
|
2023-09-12 15:12:54 +00:00
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
|
|
|
Time::monoabsns2ts(&tspec, uTimeout);
|
|
|
|
}
|
|
|
|
|
2024-01-21 21:15:57 +00:00
|
|
|
if (!ThrdCfg::gPreferLinuxPrimitivesFutexNoSpin)
|
2023-09-12 15:12:54 +00:00
|
|
|
{
|
|
|
|
if (this->TryLockHeavy())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AuAtomicAdd(&this->dwSleeping_, 1u);
|
|
|
|
|
|
|
|
while (!this->TryLockNoSpin())
|
|
|
|
{
|
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
|
|
|
if (Time::SteadyClockNS() >= uTimeout)
|
|
|
|
{
|
|
|
|
AuAtomicSub(&this->dwSleeping_, 1u);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret {};
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = futex_wait(&this->state_, 1, &tspec);
|
|
|
|
}
|
|
|
|
while (ret == -EINTR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ret {};
|
|
|
|
bool bStatus {};
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((ret = futex_wait(&this->state_, 1)) == 0)
|
|
|
|
{
|
|
|
|
bStatus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == -EAGAIN)
|
|
|
|
{
|
|
|
|
bStatus = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
while (ret == -EINTR);
|
|
|
|
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "Mutex wait failed: {}", ret)
|
|
|
|
}
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
AuAtomicSub(&this->dwSleeping_, 1u);
|
2022-12-28 23:44:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
void MutexImpl::SlowLock()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-04-03 07:21:44 +00:00
|
|
|
auto status = LockMS(0);
|
2022-12-28 23:44:45 +00:00
|
|
|
SysAssert(status, "Couldn't lock mutex");
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void MutexImpl::Unlock()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-08-23 19:20:12 +00:00
|
|
|
AuAtomicClearU8Lock(&this->state_);
|
|
|
|
|
|
|
|
if (AuAtomicLoad(&this->dwSleeping_))
|
2023-08-11 15:51:42 +00:00
|
|
|
{
|
|
|
|
futex_wake(&this->state_, 1);
|
|
|
|
}
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
AUKN_SYM IHyperWaitable *MutexNew()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
return _new MutexImpl();
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:51:42 +00:00
|
|
|
AUKN_SYM void MutexRelease(IHyperWaitable *pMutex)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
AuSafeDelete<MutexImpl *>(pMutex);
|
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, Mutex, MutexImpl)
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
#endif
|