AuroraRuntime/Source/Threading/Primitives/AuSemaphore.Unix.cpp
Jamie Reece Wilson 17c50eff64 [*] fix old unix sync regressions
do not hold switching lock while spinning as originally written and intended
2023-06-13 12:05:55 +01:00

164 lines
4.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuSemaphore.Unix.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuSemaphore.Generic.hpp"
#include "SMTYield.hpp"
#if !defined(_AURUNTIME_GENERIC_SEMAPHORE) && !defined(AURORA_IS_LINUX_DERIVED)
#include <Source/Time/Time.hpp>
namespace Aurora::Threading::Primitives
{
SemaphoreImpl::SemaphoreImpl(long intialValue) : value_(intialValue)
{
pthread_condattr_t attr;
::pthread_condattr_init(&attr);
::pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
SysAssert(::pthread_cond_init(&this->pthreadCv_, &attr) == 0, "couldn't initialize sema/CV");
}
SemaphoreImpl::~SemaphoreImpl()
{
::pthread_cond_destroy(&this->pthreadCv_);
}
bool SemaphoreImpl::HasOSHandle(AuMach &mach)
{
return false;
}
bool SemaphoreImpl::HasLockImplementation()
{
return true;
}
bool SemaphoreImpl::TryLock()
{
return DoTryIf([=]()
{
auto old = this->value_;
return (old != 0 && AuAtomicCompareExchange(&this->value_, old - 1, old) == old);
});
}
bool SemaphoreImpl::LockMS(AuUInt64 uTimeout)
{
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool SemaphoreImpl::LockNS(AuUInt64 uTimeout)
{
if (this->TryLock())
{
return true;
}
errno = 0;
AuUInt64 uStart = AuTime::SteadyClockNS();
AuUInt64 uEnd = uStart + uTimeout;
auto mutex = reinterpret_cast<pthread_mutex_t*>(this->mutex_.GetOSHandle());
struct timespec tspec;
if (uTimeout != 0)
{
Time::auabsns2ts(&tspec, uEnd);
}
while (!this->TryLock())
{
AU_LOCK_GUARD(this->mutex_);
{
auto old = this->value_;
if ((old != 0 && AuAtomicCompareExchange(&this->value_, old - 1, old) == old))
{
return true;
}
}
if (uTimeout != 0)
{
uStart = Time::SteadyClockNS();
if (uStart >= uEnd)
{
return false;
}
int ret {};
do
{
ret = ::pthread_cond_timedwait(&this->pthreadCv_, mutex, &tspec);
}
while (ret == EINTR);
}
else
{
int ret {};
bool bStatus {};
do
{
if ((ret = ::pthread_cond_wait(&this->pthreadCv_, mutex)) == 0)
{
bStatus = true;
break;
}
}
while (ret == EINTR);
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "semaphore wait failed: {}", ret)
}
}
return true;
}
void SemaphoreImpl::Lock()
{
auto status = LockNS(0);
SysAssert(status, "Couldn't lock semaphore");
}
void SemaphoreImpl::Unlock(long count)
{
AU_LOCK_GUARD(this->mutex_);
AuAtomicAdd<AuInt32>(&this->value_, count);
if (count == 1)
{
auto ret = ::pthread_cond_signal(&this->pthreadCv_);
SysAssert(ret == 0, "Couldn't wake any semaphore waiter");
}
else
{
auto ret = ::pthread_cond_broadcast(&this->pthreadCv_);
SysAssert(ret == 0, "Couldn't wake any semaphore waiters");
}
}
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