[*] Optimize event primitive

This commit is contained in:
Reece Wilson 2022-12-29 00:02:56 +00:00
parent 2209aeb7a8
commit d14511429a
2 changed files with 22 additions and 24 deletions

View File

@ -10,31 +10,26 @@
namespace Aurora::Threading::Primitives
{
EventImpl::EventImpl(bool trigged, bool atomicRelease, bool permitMultipleTriggers) : triggered_(trigged), atomicRelease_(atomicRelease), permitMultipleTriggers_(permitMultipleTriggers) {}
EventImpl::EventImpl(bool bTriggered, bool bAtomicRelease, bool bPermitMultipleTriggers) :
triggered_(bTriggered),
atomicRelease_(bAtomicRelease),
permitMultipleTriggers_(bAtomicRelease),
condition_(AuUnsafeRaiiToShared(&this->mutex_))
{}
EventImpl::~EventImpl() {}
bool EventImpl::Init()
{
this->mutex_ = ConditionMutexUnique();
if (!this->mutex_)
{
return false;
}
this->condition_ = ConditionVariableUnique(AuUnsafeRaiiToShared(this->mutex_));
if (!this->condition_)
{
return false;
}
return true;
}
bool EventImpl::Lock(AuUInt64 timeout /*=0*/)
{
AU_LOCK_GUARD(this->mutex_);
AuInt64 startTime = Time::SteadyClockMS();
AuInt64 endTime = startTime + timeout;
AuInt64 uStartTime = Time::SteadyClockMS();
AuInt64 uEndTime = uStartTime + timeout;
while (!AtomicIsEventSet())
{
@ -42,14 +37,14 @@ namespace Aurora::Threading::Primitives
if (timeout)
{
timeoutMs = endTime - static_cast<AuInt64>(Time::SteadyClockMS());
timeoutMs = uEndTime - Time::SteadyClockMS();
if (timeoutMs < 0)
{
return false;
}
}
if (!condition_->WaitForSignal(timeoutMs))
if (!this->condition_.WaitForSignal(timeoutMs))
{
return false;
}
@ -81,9 +76,9 @@ namespace Aurora::Threading::Primitives
void EventImpl::Reset()
{
this->mutex_->Lock();
this->mutex_.Lock();
this->triggered_ = false;
this->mutex_->Unlock();
this->mutex_.Unlock();
}
void EventImpl::Set()
@ -91,7 +86,7 @@ namespace Aurora::Threading::Primitives
AU_LOCK_GUARD(this->mutex_);
SysAssertExp((this->permitMultipleTriggers_) || (!this->triggered_), "Can not trigger an awake event object");
this->triggered_ = true;
this->condition_->Broadcast();
this->condition_.Broadcast();
}
bool EventImpl::HasOSHandle(AuMach &mach)
@ -116,9 +111,9 @@ namespace Aurora::Threading::Primitives
// Unlike the other types, unlock is ways a nop
}
AUKN_SYM IEvent *EventNew(bool trigged, bool atomicRelease, bool permitMultipleTriggers)
AUKN_SYM IEvent *EventNew(bool bTriggered, bool bAtomicRelease, bool bPermitMultipleTriggers)
{
auto event = _new EventImpl(trigged, atomicRelease, permitMultipleTriggers);
auto event = _new EventImpl(bTriggered, bAtomicRelease, bPermitMultipleTriggers);
if (!event)
{
return nullptr;

View File

@ -7,6 +7,9 @@
***/
#pragma once
#include "AuConditionVariable.Generic.hpp"
#include "AuConditionMutex.Generic.hpp"
namespace Aurora::Threading::Primitives
{
class EventImpl : public IEvent
@ -31,8 +34,8 @@ namespace Aurora::Threading::Primitives
const bool atomicRelease_{};
const bool permitMultipleTriggers_{};
ConditionMutexUnique_t mutex_;
ConditionVariableUnique_t condition_;
ConditionMutexImpl mutex_;
ConditionVariableImpl condition_;
bool triggered_;
};
}