/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: AsyncApp.cpp Date: 2021-6-26 Author: Reece ***/ #include #include "Async.hpp" #include "AsyncApp.hpp" #include "WorkItem.hpp" #include "AuSchedular.hpp" #include namespace Aurora::Async { static AuSPtr gAsyncAppMem; void InitApp() { gAsyncAppMem = AuMakeShared(); 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 AsyncApp::GetShutdownEvent() { return ThreadPool::GetShutdownEvent(); } void AsyncApp::AddDependency(AuSPtr 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 optWorkerId) { return ThreadPool::QueryAbortFence(optWorkerId); } bool AsyncApp::QueryShouldAbort(AuOptional optWorkerId, AuUInt64 uFenceMagic) { return ThreadPool::QueryShouldAbort(optWorkerId, uFenceMagic); } AUKN_SYM IAsyncApp *GetAsyncApp() { return gAsyncAppMem.get(); } AUKN_SYM AuSPtr 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 AsyncApp::NewWorkItem(const WorkerId_t &worker, const AuSPtr &task) { return ThreadPool::NewWorkItem(worker, task); } AuSPtr AsyncApp::NewWorkFunction(const WorkerId_t &worker, AuVoidFunc callback) { return ThreadPool::NewWorkFunction(worker, callback); } AuSPtr AsyncApp::NewFence() { return ThreadPool::NewFence(); } Threading::Threads::ThreadShared_t AsyncApp::ResolveHandle(WorkerId_t id) { return ThreadPool::ResolveHandle(id); } AuBST> 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 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 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 AsyncApp::GetIOProcessor(WorkerId_t pid) { return ThreadPool::GetIOProcessor(pid); } AuSPtr AsyncApp::GetIONetInterface(WorkerId_t pid) { return ThreadPool::GetIONetInterface(pid); } AuSPtr AsyncApp::GetIONetWorker(WorkerId_t pid) { return ThreadPool::GetIONetWorker(pid); } AuSPtr AsyncApp::ToKernelWorkQueue() { return ThreadPool::ToKernelWorkQueue(); } AuSPtr AsyncApp::ToKernelWorkQueue(WorkerId_t workerId) { return ThreadPool::ToKernelWorkQueue(workerId); } }