AuroraRuntime/Source/Async/AsyncApp.cpp

240 lines
5.5 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 "AuSchedular.hpp"
#include <Source/Console/Commands/Commands.hpp>
namespace Aurora::Async
{
static AuSPtr<AsyncApp> gAsyncAppMem;
void InitApp()
{
gAsyncAppMem = AuMakeShared<AsyncApp>();
gAsyncApp = gAsyncAppMem.get();
}
void ReleaseApp()
{
if (gAsyncAppMem)
{
gAsyncAppMem->Shutdown();
AuResetMember(gAsyncAppMem);
}
}
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::AddDependency(AuSPtr<IThreadPool> pPool)
{
ThreadPool::AddDependency(pPool);
}
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();
}
void AsyncApp::IncrementAbortFenceOnPool()
{
ThreadPool::IncrementAbortFenceOnPool();
}
void AsyncApp::IncrementAbortFenceOnWorker(WorkerId_t workerId)
{
ThreadPool::IncrementAbortFenceOnWorker(workerId);
}
AuUInt64 AsyncApp::QueryAbortFence(AuOptional<WorkerId_t> optWorkerId)
{
return ThreadPool::QueryAbortFence(optWorkerId);
}
bool AsyncApp::QueryShouldAbort(AuOptional<WorkerId_t> optWorkerId, AuUInt64 uFenceMagic)
{
return ThreadPool::QueryShouldAbort(optWorkerId, uFenceMagic);
}
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)
{
return ThreadPool::NewWorkItem(worker, task);
}
AuSPtr<IWorkItem> AsyncApp::NewWorkFunction(const WorkerId_t &worker, AuVoidFunc callback)
{
return ThreadPool::NewWorkFunction(worker, callback);
}
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);
}
}