AuroraRuntime/Source/Async/AsyncApp.cpp
2023-05-24 08:19:47 +01:00

222 lines
5.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AsyncApp.cpp
Date: 2021-6-26
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Async.hpp"
#include "AsyncApp.hpp"
#include "WorkItem.hpp"
#include "Schedular.hpp"
#include <Source/Console/Commands/Commands.hpp>
namespace Aurora::Async
{
static AuSPtr<AsyncApp> gAsyncAppMem;
void InitApp()
{
gAsyncAppMem = AuMakeShared<AsyncApp>();
gAsyncApp = gAsyncAppMem.get();
}
void ReleaseApp()
{
gAsyncAppMem->Shutdown();
gAsyncAppMem.reset();
}
void AsyncApp::Start()
{
ThreadPool::SetRunningMode(true);
SysAssert(ThreadPool::Create({0, 0}));
StartSched(); // this is now an init once function
}
void AsyncApp::Main()
{
ThreadPool::Entrypoint({0, 0});
}
void AsyncApp::SetConsoleCommandDispatcher(WorkerId_t id)
{
this->commandDispatcher_ = id;
Console::Commands::UpdateDispatcher(WorkerPId_t(GetSharedAsyncApp(), id));
}
AuSPtr<Aurora::Threading::IWaitable> AsyncApp::GetShutdownEvent()
{
return ThreadPool::GetShutdownEvent();
}
void AsyncApp::CleanUpWorker(WorkerId_t id)
{
// This shouldn't be a problem; however, we're going to handle the one edge case where
// some angry sysadmin is spamming commands
if ((commandDispatcher_.has_value())
&& (commandDispatcher_.value() == id))
{
Console::Commands::UpdateDispatcher({});
}
}
void AsyncApp::CleanWorkerPoolReservedZeroFree()
{
// TODO: this is a hack. we need to reference count
DeinitSched();
ThreadPool::Shutdown();
}
AUKN_SYM IAsyncApp *GetAsyncApp()
{
return gAsyncAppMem.get();
}
AUKN_SYM AuSPtr<IAsyncApp> GetSharedAsyncApp()
{
return gAsyncAppMem;
}
bool AsyncApp::Spawn(WorkerId_t workerId)
{
return ThreadPool::Spawn(workerId);
}
void AsyncApp::SetRunningMode(bool eventRunning)
{
ThreadPool::SetRunningMode(eventRunning);
}
bool AsyncApp::Create(WorkerId_t workerId)
{
return ThreadPool::Create(workerId);
}
bool AsyncApp::InRunnerMode()
{
return ThreadPool::InRunnerMode();
}
bool AsyncApp::Poll()
{
return ThreadPool::Poll();
}
bool AsyncApp::RunOnce()
{
return ThreadPool::RunOnce();
}
bool AsyncApp::Run()
{
return ThreadPool::Run();
}
AuUInt32 AsyncApp::PollAndCount(bool bStrict)
{
return ThreadPool::PollAndCount(bStrict);
}
AuUInt32 AsyncApp::RunAllPending()
{
return ThreadPool::RunAllPending();
}
void AsyncApp::Shutdown()
{
ThreadPool::Shutdown();
}
bool AsyncApp::Exiting()
{
return ThreadPool::Exiting();
}
AuSPtr<IWorkItem> AsyncApp::NewWorkItem(const WorkerId_t &worker, const AuSPtr<IWorkItemHandler> &task, bool supportsBlocking)
{
return ThreadPool::NewWorkItem(worker, task, supportsBlocking);
}
AuSPtr<IWorkItem> AsyncApp::NewFence()
{
return ThreadPool::NewFence();
}
Threading::Threads::ThreadShared_t AsyncApp::ResolveHandle(WorkerId_t id)
{
return ThreadPool::ResolveHandle(id);
}
AuBST<ThreadGroup_t, AuList<ThreadId_t>> AsyncApp::GetThreads()
{
return ThreadPool::GetThreads();
}
WorkerId_t AsyncApp::GetCurrentThread()
{
return ThreadPool::GetCurrentThread();
}
bool AsyncApp::Sync(WorkerId_t workerId, AuUInt32 timeoutMs, bool requireSignal)
{
return ThreadPool::Sync(workerId, timeoutMs, requireSignal);
}
AuSPtr<AuLoop::ILoopSource> AsyncApp::WorkerToLoopSource(WorkerId_t id)
{
return ThreadPool::WorkerToLoopSource(id);
}
void AsyncApp::Signal(WorkerId_t workerId)
{
ThreadPool::Signal(workerId);
}
void AsyncApp::SyncAllSafe()
{
ThreadPool::SyncAllSafe();
}
void AsyncApp::AddFeature(WorkerId_t id, AuSPtr<Threading::Threads::IThreadFeature> feature, bool async)
{
ThreadPool::AddFeature(id, feature, async);
}
void AsyncApp::AssertInThreadGroup(ThreadGroup_t group)
{
ThreadPool::AssertInThreadGroup(group);
}
void AsyncApp::AssertWorker(WorkerId_t id)
{
ThreadPool::AssertWorker(id);
}
AuSPtr<AuLoop::ILoopQueue> AsyncApp::ToKernelWorkQueue()
{
return ThreadPool::ToKernelWorkQueue();
}
AuSPtr<AuLoop::ILoopQueue> AsyncApp::ToKernelWorkQueue(WorkerId_t workerId)
{
return ThreadPool::ToKernelWorkQueue(workerId);
}
void AsyncApp::UpdateWorkMode(WorkerId_t workerId, RunMode mode)
{
ThreadPool::UpdateWorkMode(workerId, mode);
}
ERunMode AsyncApp::GetCurrentThreadRunMode()
{
return ThreadPool::GetCurrentThreadRunMode();
}
ERunMode AsyncApp::GetThreadRunMode(WorkerId_t workerId)
{
return ThreadPool::GetThreadRunMode(workerId);
}
}