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

118 lines
3.1 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: AuConditionVariable.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 "AuConditionVariable.Generic.hpp"
#include <Source/Time/Time.hpp>
2021-06-27 21:25:29 +00:00
#if !defined(_AURUNTIME_GENERICCV)
namespace Aurora::Threading::Primitives
{
ConditionVariableImpl::ConditionVariableImpl(const AuSPtr<IConditionMutex> &mutex) : mutex_(std::dynamic_pointer_cast<IConditionMutexEx>(mutex))
2021-06-27 21:25:29 +00:00
{
pthread_condattr_t attr;
::pthread_condattr_init(&attr);
::pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
auto ret = ::pthread_cond_init(&this->pthreadCv_, &attr);
2021-06-27 21:25:29 +00:00
SysAssert(ret == 0, "Couldn't initialize CV");
}
ConditionVariableImpl::~ConditionVariableImpl()
{
::pthread_cond_destroy(&this->pthreadCv_);
2021-06-27 21:25:29 +00:00
}
AuSPtr<IConditionMutex> ConditionVariableImpl::GetMutex()
2021-06-27 21:25:29 +00:00
{
return this->mutex_;
2021-06-27 21:25:29 +00:00
}
bool ConditionVariableImpl::WaitForSignal(AuUInt32 uTimeout)
{
return WaitForSignalNS(AuMSToNS<AuUInt64>(uTimeout));
}
bool ConditionVariableImpl::WaitForSignalNS(AuUInt64 qwTimeout)
2021-06-27 21:25:29 +00:00
{
auto mutex = reinterpret_cast<pthread_mutex_t*>(this->mutex_->GetOSHandle());
2021-06-27 21:25:29 +00:00
if (gRuntimeRunLevel >= 5)
{
return true;
}
2021-06-27 21:25:29 +00:00
if (timeout == 0)
{
int ret {};
do
{
if ((ret = ::pthread_cond_wait(&this->pthreadCv_, mutex)) == 0)
{
return true;
}
} while (ret == EINTR);
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "conditional wait failed: {}", ret)
return false;
2021-06-27 21:25:29 +00:00
}
else
{
struct timespec tspec;
Time::auabsns2ts(&tspec, AuTime::CurrentClockNS() + qwTimeout);
2021-06-27 21:25:29 +00:00
int ret {};
do
2021-06-27 21:25:29 +00:00
{
ret = ::pthread_cond_timedwait(&this->pthreadCv_, mutex, &tspec);
if (ret == 0)
{
return true;
}
if (ret == ETIMEDOUT)
{
return false;
}
} while (ret == EINTR);
2021-06-27 21:25:29 +00:00
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "conditional timed wait failed: {}", ret)
return false;
2021-06-27 21:25:29 +00:00
}
}
void ConditionVariableImpl::Signal()
{
auto ret = ::pthread_cond_signal(&this->pthreadCv_);
2021-06-27 21:25:29 +00:00
SysAssert(ret == 0, "Couldn't wake any CV waiters");
}
void ConditionVariableImpl::Broadcast()
{
auto ret = ::pthread_cond_broadcast(&this->pthreadCv_);
2021-06-27 21:25:29 +00:00
SysAssert(ret == 0, "Couldn't wake any CV waiters");
}
AUKN_SYM IConditionVariable *ConditionVariableNew(const AuSPtr<IConditionMutex> &mutex)
2021-06-27 21:25:29 +00:00
{
return _new ConditionVariableImpl(mutex);
}
AUKN_SYM void ConditionVariableRelease(IConditionVariable *mutex)
{
AuSafeDelete<ConditionVariableImpl *>(mutex);
2021-06-27 21:25:29 +00:00
}
AUROXTL_INTERFACE_SOO_SRC(ConditionVariable, ConditionVariableImpl, (const AuSPtr<IConditionMutex> &, pMutex))
2021-06-27 21:25:29 +00:00
}
#endif