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

154 lines
3.7 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: AuSemaphore.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 "AuSemaphore.Generic.hpp"
2021-06-27 21:25:29 +00:00
#if !defined(_AURUNTIME_GENERIC_SEMAPHORE) && !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
{
Semaphore::Semaphore(long intialValue) : value_(intialValue)
2021-06-27 21:25:29 +00:00
{
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");
2021-06-27 21:25:29 +00:00
}
Semaphore::~Semaphore()
{
::pthread_cond_destroy(&this->pthreadCv_);
2021-06-27 21:25:29 +00:00
}
bool Semaphore::HasOSHandle(AuMach &mach)
{
return false;
2021-06-27 21:25:29 +00:00
}
bool Semaphore::HasLockImplementation()
{
return true;
}
bool Semaphore::TryLock()
{
auto old = this->value_;
return (old != 0 && AuAtomicCompareExchange(&this->value_, old - 1, old) == old);
2021-06-27 21:25:29 +00:00
}
bool Semaphore::Lock(AuUInt64 uTimeout)
2021-06-27 21:25:29 +00:00
{
if (this->TryLock())
2021-06-27 21:25:29 +00:00
{
return true;
}
AuUInt64 uStart = AuTime::SteadyClockMS();
AuUInt64 uEnd = uStart + uTimeout;
AU_LOCK_GUARD(this->mutex_);
auto mutex = reinterpret_cast<pthread_mutex_t*>(this->mutex_.GetOSHandle());
struct timespec tspec;
if (uTimeout != 0)
2021-06-27 21:25:29 +00:00
{
Time::ms2tsabs(&tspec, uTimeout);
}
while (!this->TryLock())
{
if (uTimeout != 0)
2021-06-27 21:25:29 +00:00
{
uStart = Time::SteadyClockMS();
if (uStart >= uEnd)
{
return false;
}
int ret {};
do
{
ret = ::pthread_cond_timedwait(&this->pthreadCv_, mutex, &tspec);
if (ret == 0)
{
continue;
}
if (ret == ETIMEDOUT)
{
return false;
}
}
while (ret == EINTR);
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "semaphore timed wait failed: {}", ret)
return false;
}
else
{
int ret {};
do
{
if ((ret = ::pthread_cond_wait(&this->pthreadCv_, mutex)) == 0)
{
continue;
}
}
while (ret == EINTR);
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "conditional wait failed: {}", ret)
return false;
}
2021-06-27 21:25:29 +00:00
}
return true;
2021-06-27 21:25:29 +00:00
}
void Semaphore::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock semaphore");
}
void Semaphore::Unlock(long count)
{
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
2021-06-27 21:25:29 +00:00
{
auto ret = ::pthread_cond_broadcast(&this->pthreadCv_);
SysAssert(ret == 0, "Couldn't wake any semaphore waiters");
2021-06-27 21:25:29 +00:00
}
}
void Semaphore::Unlock()
{
Unlock(0);
2021-06-27 21:25:29 +00:00
}
AUKN_SYM ISemaphore *SemaphoreNew(int iInitialCount)
2021-06-27 21:25:29 +00:00
{
return _new Semaphore(iInitialCount);
2021-06-27 21:25:29 +00:00
}
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)
2021-06-27 21:25:29 +00:00
{
AuSafeDelete<Semaphore *>(pSemaphore);
2021-06-27 21:25:29 +00:00
}
}
#endif