171 lines
4.1 KiB
C++
171 lines
4.1 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(AuUInt16 uIntialValue) : 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::TryLockNoSpin()
|
|
{
|
|
auto old = this->value_;
|
|
return (old != 0 && AuAtomicCompareExchange(&this->value_, old - 1, old) == old);
|
|
}
|
|
|
|
bool SemaphoreImpl::TryLock()
|
|
{
|
|
return DoTryIf([=]()
|
|
{
|
|
return TryLockNoSpin();
|
|
});
|
|
}
|
|
|
|
bool SemaphoreImpl::LockMS(AuUInt64 uTimeout)
|
|
{
|
|
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
|
|
}
|
|
|
|
bool SemaphoreImpl::LockNS(AuUInt64 uTimeout)
|
|
{
|
|
AuUInt64 uStart {};
|
|
AuUInt64 uEnd {};
|
|
|
|
if (this->TryLock())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
auto mutex = reinterpret_cast<pthread_mutex_t*>(this->mutex_.GetOSHandle());
|
|
|
|
struct timespec tspec;
|
|
if (uTimeout != 0)
|
|
{
|
|
uStart = AuTime::SteadyClockNS();
|
|
uEnd = uStart + uTimeout;
|
|
|
|
Time::monoabsns2ts(&tspec, uEnd);
|
|
}
|
|
|
|
while (!this->TryLock())
|
|
{
|
|
AU_LOCK_GUARD(this->mutex_);
|
|
|
|
if (this->TryLockNoSpin())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (uTimeout != 0)
|
|
{
|
|
if (Time::SteadyClockNS() >= 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(AuUInt16 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(AuUInt16 uIntialValue)
|
|
{
|
|
return _new SemaphoreImpl(uIntialValue);
|
|
}
|
|
|
|
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)
|
|
{
|
|
AuSafeDelete<SemaphoreImpl *>(pSemaphore);
|
|
}
|
|
|
|
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, Semaphore, SemaphoreImpl)
|
|
}
|
|
#endif |