AuroraRuntime/Source/Threading/Primitives/ConditionVariable.Unix.cpp
Reece f559897b42 [+] Added WorkerId_t structure
[-] Removed WorkerId_t typedef
[*] Added shared support to some older threading apis that have yet to be updated
2021-10-08 20:51:34 +01:00

84 lines
2.2 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ConditionVariable.Unix.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "ConditionVariable.Generic.hpp"
#include <Source/Time/Time.hpp>
#if !defined(_AURUNTIME_GENERICCV)
namespace Aurora::Threading::Primitives
{
ConditionVariableImpl::ConditionVariableImpl(const AuSPtr<IConditionMutex> &mutex) : mutex_(std::dynamic_pointer_cast<IConditionMutexEx>(mutex))
{
auto ret = pthread_cond_init(&pthreadCv_, NULL);
SysAssert(ret == 0, "Couldn't initialize CV");
}
ConditionVariableImpl::~ConditionVariableImpl()
{
pthread_cond_destroy(&pthreadCv_);
}
AuSPtr<IConditionMutex> ConditionVariableImpl::GetMutex()
{
return mutex_;
}
bool ConditionVariableImpl::WaitForSignal(AuUInt32 timeout)
{
auto mutex = reinterpret_cast<pthread_mutex_t*>(mutex_->GetOSHandle());
if (timeout == 0)
{
auto ret = pthread_cond_wait(&pthreadCv_, mutex);
SysAssert(ret == 0, "Couldn't wait on CV");
return true;
}
else
{
struct timespec tspec;
Time::ms2tsabs(&tspec, timeout);
auto ret = pthread_cond_timedwait(&pthreadCv_, mutex, &tspec);
if (ret != 0)
{
SysAssert(errno == ETIMEDOUT, "conditional timed wait failed");
return false;
}
return true;
}
}
void ConditionVariableImpl::Signal()
{
auto ret = pthread_cond_signal(&pthreadCv_);
SysAssert(ret == 0, "Couldn't wake any CV waiters");
}
void ConditionVariableImpl::Broadcast()
{
auto ret = pthread_cond_broadcast(&pthreadCv_);
SysAssert(ret == 0, "Couldn't wake any CV waiters");
}
AUKN_SYM IConditionVariable *ConditionVariableNew(const AuSPtr<IConditionMutex> &mutex)
{
return _new ConditionVariableImpl(mutex);
}
AUKN_SYM void ConditionVariableRelease(IConditionVariable *mutex)
{
SafeDelete<ConditionVariableImpl *>(mutex);
}
}
#endif