AuroraRuntime/Source/IO/Async/AuIOAPCLessWaitable.cpp

135 lines
2.9 KiB
C++

/***
Copyright (C) 2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuIOAPCLessWaitable.cpp
Date: 2024-2-25
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuIOAPCLessWaitable.hpp"
namespace Aurora::IO::Async
{
void APCLessEvent::OnFinishSleep()
{
if (auto pThat = AuTryLockMemoryType(this->wpParent))
{
pThat->CheckLocal();
}
}
APCLessWaitable::APCLessWaitable()
{
this->threadId = AuThreads::GetThreadId();
}
AuSPtr<Loop::ILoopSource> APCLessWaitable::GetLoopSource()
{
AU_LOCK_GUARD(this->mutex);
if (this->pCompletionGroup)
{
return this->pCompletionGroup->ToAnyLoopSource();
}
else if (this->pEvent)
{
return this->pEvent;
}
else
{
this->pEvent = AuMakeShared<APCLessEvent>();
SysCheckNotNullMemory(this->pEvent, {});
SysCheckReturn(this->pEvent->TryInit(false, false, true), {});
this->pEvent->wpParent = this->SharedFromThis();
return this->pEvent;
}
}
bool APCLessWaitable::HasCompletedForGCWI()
{
return this->bHasFinished;
}
bool APCLessWaitable::HasNonTrivialCleanup()
{
return true;
}
void APCLessWaitable::CleanupForGCWI()
{
this->CheckLocal();
AuResetMember(this->pCompletionGroup);
}
bool APCLessWaitable::HasBeenSignaled()
{
return this->bHasFinished;
}
void APCLessWaitable::SignalComplete()
{
AU_LOCK_GUARD(this->mutex);
this->bHasFinished = true;
if (this->pEvent)
{
this->pEvent->Set();
}
if (this->pCompletionGroup)
{
if (auto pOtherEvent = this->pCompletionGroup->GetTriggerLoopSource())
{
pOtherEvent->Set();
}
}
}
void APCLessWaitable::Reset()
{
if (this->pEvent)
{
this->pEvent->Reset();
}
this->bHasFinished = false;
}
void APCLessWaitable::CheckLocal()
{
if (!this->bHasFinished)
{
return;
}
if (this->threadId == AuThreads::GetThreadId())
{
this->OnOriginThreadComplete();
}
}
bool APCLessWaitable::TryAttachToCompletionGroup(const AuSPtr<CompletionGroup::ICompletionGroup> &pCompletionGroup)
{
AU_LOCK_GUARD(this->mutex);
if (!pCompletionGroup)
{
return false;
}
if (!this->pCompletionGroup)
{
return false;
}
this->pCompletionGroup = pCompletionGroup;
return true;
}
AuSPtr<CompletionGroup::ICompletionGroup> APCLessWaitable::GetCompletionGroup()
{
AU_LOCK_GUARD(this->mutex);
return this->pCompletionGroup;
}
}