AuroraRuntime/Source/Threading/Primitives/AuMutex.Unix.cpp

141 lines
3.3 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2022-11-17 07:46:07 +00:00
File: AuMutex.Unix.cpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-12
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2022-11-17 07:46:07 +00:00
#include "AuMutex.Generic.hpp"
#include "SMPYield.hpp"
2021-06-27 21:25:29 +00:00
#if !defined(_AURUNTIME_GENERIC_MUTEX) && !defined(AURORA_IS_LINUX_DERIVED)
2021-09-30 14:57:41 +00:00
#include <Source/Time/Time.hpp>
2021-06-27 21:25:29 +00:00
namespace Aurora::Threading::Primitives
{
Mutex::Mutex()
{
pthread_condattr_t attr;
::pthread_condattr_init(&attr);
::pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
2021-06-27 21:25:29 +00:00
SysAssert(::pthread_cond_init(&this->pthreadCv_, &attr) == 0, "couldn't initialize mutex/CV");
}
2021-06-27 21:25:29 +00:00
Mutex::~Mutex()
{
::pthread_cond_destroy(&this->pthreadCv_);
2021-06-27 21:25:29 +00:00
}
bool Mutex::HasOSHandle(AuMach &mach)
{
return false;
2021-06-27 21:25:29 +00:00
}
2021-06-27 21:25:29 +00:00
bool Mutex::HasLockImplementation()
{
return true;
}
bool Mutex::TryLock()
2021-06-27 21:25:29 +00:00
{
// Assume heavyweight pthread_cond_timedwait yield is followed by an instant hit, for now
auto old = this->value_;
return (old == 0 && AuAtomicCompareExchange(&this->value_, 1, old) == old);
2021-06-27 21:25:29 +00:00
}
bool Mutex::Lock(AuUInt64 uTimeout)
2021-06-27 21:25:29 +00:00
{
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool Mutex::LockNS(AuUInt64 uTimeout)
{
if (DoTryIf([=]()
{
auto old = this->value_;
return (old == 0 && AuAtomicCompareExchange(&this->value_, 1, old) == old);
}))
2021-06-27 21:25:29 +00:00
{
return true;
2021-06-27 21:25:29 +00:00
}
AuUInt64 uStart = AuTime::SteadyClockNS();
AuUInt64 uEnd = uStart + uTimeout;
2022-04-13 15:06:26 +00:00
AU_LOCK_GUARD(this->mutex_);
auto mutex = reinterpret_cast<pthread_mutex_t*>(this->mutex_.GetOSHandle());
struct timespec tspec;
if (uTimeout != 0)
{
Time::auabsns2ts(&tspec, uEnd);
}
2022-04-13 15:06:26 +00:00
2023-03-12 20:59:03 +00:00
int ret {};
while (!this->TryLock())
{
if (uTimeout != 0)
{
uStart = Time::SteadyClockNS();
if (uStart >= uEnd)
2022-04-13 15:06:26 +00:00
{
return false;
2022-04-13 15:06:26 +00:00
}
do
2022-04-13 15:06:26 +00:00
{
ret = ::pthread_cond_timedwait(&this->pthreadCv_, mutex, &tspec);
2022-04-13 15:06:26 +00:00
}
while (ret == EINTR);
}
else
{
2023-03-12 20:59:03 +00:00
bool bStatus {};
do
{
if ((ret = ::pthread_cond_wait(&this->pthreadCv_, mutex)) == 0)
{
2023-03-12 20:59:03 +00:00
bStatus = true;
break;
}
}
while (ret == EINTR);
2021-06-27 21:25:29 +00:00
2023-03-12 20:59:03 +00:00
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "Mutex wait failed: {}", ret)
}
2021-06-27 21:25:29 +00:00
}
return true;
}
void Mutex::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock mutex");
}
2021-06-27 21:25:29 +00:00
void Mutex::Unlock()
{
{
AU_LOCK_GUARD(this->mutex_);
this->value_ = 0;
}
auto ret = ::pthread_cond_signal(&this->pthreadCv_);
SysAssert(ret == 0, "Couldn't wake any mutex waiter");
2021-06-27 21:25:29 +00:00
}
AUKN_SYM IWaitable *MutexNew()
{
return _new Mutex();
}
AUKN_SYM void MutexRelease(IWaitable *pMutex)
2021-06-27 21:25:29 +00:00
{
AuSafeDelete<Mutex *>(pMutex);
2021-06-27 21:25:29 +00:00
}
}
#endif