AuroraRuntime/Source/Async/AuThreadStateSingletons.cpp

97 lines
2.3 KiB
C++

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuThreadStateSingletons.cpp
Date: 2023-11-04
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuThreadStateSingletons.hpp"
#include <Source/IO/CompletionGroup/CompletionGroup.hpp>
namespace Aurora::Async
{
AuSPtr<AuIO::IIOProcessor> ThreadStateSingletons::GetIOProcessor(AuWorkerPId_t pid)
{
if (this->pIOProcessors)
{
return this->pIOProcessors;
}
{
AU_LOCK_GUARD(this->mutex);
if (this->pIOProcessors)
{
return this->pIOProcessors;
}
return this->pIOProcessors = AuIO::NewIOProcessorOnThread(false, pid);
}
}
AuSPtr<AuIO::CompletionGroup::ICompletionGroup> ThreadStateSingletons::GetIOGroup(AuWorkerPId_t pid)
{
if (this->pGroup)
{
return this->pGroup;
}
{
AU_LOCK_GUARD(this->mutex);
if (this->pGroup)
{
return this->pGroup;
}
this->pGroup = AuIO::CompletionGroup::NewCompletionGroup();
if (!this->pGroup)
{
return {};
}
AuStaticCast<IO::CompletionGroup::CompletionGroup>(this->pGroup)->MakeInternalAsync(pid);
return this->pGroup;
}
}
void ThreadStateSingletons::TryInitNet(AuWorkerPId_t pid)
{
if (this->pNetInterface)
{
return;
}
AU_LOCK_GUARD(this->mutex);
auto pNetProcessor = AuNet::NewNetworkInterface();
if (!pNetProcessor)
{
return;
}
auto pNetWorker = pNetProcessor->GetWorkersService()->Attach(this->GetIOProcessor(pid));
if (!pNetWorker)
{
return;
}
this->pNetWorker = pNetWorker;
this->pNetInterface = pNetProcessor;
}
AuSPtr<AuIO::Net::INetInterface> ThreadStateSingletons::GetIONetInterface(AuWorkerPId_t pid)
{
this->TryInitNet(pid);
return this->pNetInterface;
}
AuSPtr<AuIO::Net::INetWorker> ThreadStateSingletons::GetIONetWorker(AuWorkerPId_t pid)
{
this->TryInitNet(pid);
return this->pNetWorker;
}
}