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"
|
2021-10-02 16:07:33 +00:00
|
|
|
#include <Source/Time/Time.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERICCV)
|
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
2021-10-08 19:51:34 +00:00
|
|
|
ConditionVariableImpl::ConditionVariableImpl(const AuSPtr<IConditionMutex> &mutex) : mutex_(std::dynamic_pointer_cast<IConditionMutexEx>(mutex))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-11-28 16:01:08 +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()
|
|
|
|
{
|
2022-11-28 16:01:08 +00:00
|
|
|
::pthread_cond_destroy(&this->pthreadCv_);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
AuSPtr<IConditionMutex> ConditionVariableImpl::GetMutex()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-11-28 16:01:08 +00:00
|
|
|
return this->mutex_;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 00:35: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
|
|
|
{
|
2022-11-28 16:01:08 +00:00
|
|
|
auto mutex = reinterpret_cast<pthread_mutex_t*>(this->mutex_->GetOSHandle());
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-08-14 21:09:25 +00:00
|
|
|
if (gRuntimeRunLevel >= 5)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
if (timeout == 0)
|
|
|
|
{
|
2022-04-13 14:42:36 +00:00
|
|
|
int ret {};
|
|
|
|
do
|
|
|
|
{
|
2022-11-28 16:01:08 +00:00
|
|
|
if ((ret = ::pthread_cond_wait(&this->pthreadCv_, mutex)) == 0)
|
2022-04-13 14:42:36 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (ret == EINTR);
|
2022-08-14 21:09:25 +00:00
|
|
|
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "conditional wait failed: {}", ret)
|
|
|
|
return false;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-07 04:41:16 +00:00
|
|
|
struct timespec tspec;
|
2023-03-15 00:35:29 +00:00
|
|
|
Time::auabsns2ts(&tspec, AuTime::CurrentClockNS() + qwTimeout);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-04-13 14:42:36 +00:00
|
|
|
int ret {};
|
|
|
|
|
|
|
|
do
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-11-28 16:01:08 +00:00
|
|
|
ret = ::pthread_cond_timedwait(&this->pthreadCv_, mutex, &tspec);
|
2022-04-13 14:42:36 +00:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == ETIMEDOUT)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (ret == EINTR);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-08-14 21:09:25 +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()
|
|
|
|
{
|
2022-11-28 16:01:08 +00:00
|
|
|
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()
|
|
|
|
{
|
2022-11-28 16:01:08 +00:00
|
|
|
auto ret = ::pthread_cond_broadcast(&this->pthreadCv_);
|
2021-06-27 21:25:29 +00:00
|
|
|
SysAssert(ret == 0, "Couldn't wake any CV waiters");
|
|
|
|
}
|
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-03-22 03:53:08 +00:00
|
|
|
AuSafeDelete<ConditionVariableImpl *>(mutex);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2023-03-21 03:18:09 +00:00
|
|
|
|
|
|
|
AUROXTL_INTERFACE_SOO_SRC(ConditionVariable, ConditionVariableImpl, (const AuSPtr<IConditionMutex> &, pMutex))
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 03:53:08 +00:00
|
|
|
#endif
|