AuroraRuntime/Source/Async/AsyncApp.cpp

175 lines
4.0 KiB
C++
Raw Normal View History

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AsyncApp.cpp
Date: 2021-6-26
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2021-06-30 09:28:52 +00:00
#include "Async.hpp"
2021-06-27 21:25:29 +00:00
#include "AsyncApp.hpp"
2021-06-30 09:28:52 +00:00
#include "WorkItem.hpp"
#include "Schedular.hpp"
2021-09-30 14:57:41 +00:00
#include <Source/Console/Commands/Commands.hpp>
2021-06-27 21:25:29 +00:00
2021-06-27 21:25:29 +00:00
namespace Aurora::Async
{
static AsyncApp *gAsyncApp;
static AuSPtr<AsyncApp> gAsyncAppMem;
2021-06-30 12:00:32 +00:00
void InitApp()
2021-06-30 12:00:32 +00:00
{
gAsyncAppMem = AuMakeShared<AsyncApp>();
gAsyncApp = gAsyncAppMem.get();
2021-06-30 12:00:32 +00:00
}
void ReleaseApp()
2021-06-30 12:00:32 +00:00
{
gAsyncAppMem.reset();
2021-06-30 12:00:32 +00:00
}
void AsyncApp::Start()
2021-06-30 09:28:52 +00:00
{
ThreadPool::SetRunningMode(true);
SysAssert(ThreadPool::Create({0, 0}));
StartSched(); // this is now an init once function
2021-06-30 09:28:52 +00:00
}
void AsyncApp::Main()
2021-06-30 09:28:52 +00:00
{
ThreadPool::Entrypoint({0, 0});
2021-06-30 09:28:52 +00:00
}
void AsyncApp::SetConsoleCommandDispatcher(WorkerId_t id)
2021-06-30 09:28:52 +00:00
{
commandDispatcher_ = id;
Console::Commands::UpdateDispatcher(commandDispatcher_);
2021-06-30 09:28:52 +00:00
}
void AsyncApp::CleanUpWorker(WorkerId_t id)
2021-06-30 09:28:52 +00:00
{
// 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({});
}
2021-06-30 09:28:52 +00:00
}
void AsyncApp::CleanWorkerPoolReservedZeroFree()
{
ThreadPool::Shutdown();
}
AUKN_SYM IAsyncApp *GetAsyncApp()
{
return gAsyncApp;
}
bool AsyncApp::Spawn(WorkerId_t workerId)
{
return ThreadPool::Spawn(workerId);
}
2021-06-30 09:28:52 +00:00
void AsyncApp::SetRunningMode(bool eventRunning)
{
ThreadPool::SetRunningMode(eventRunning);
}
bool AsyncApp::Create(WorkerId_t workerId)
{
return ThreadPool::Create(workerId);
}
bool AsyncApp::InRunnerMode()
2021-06-30 09:28:52 +00:00
{
return ThreadPool::InRunnerMode();
2021-06-30 09:28:52 +00:00
}
bool AsyncApp::Poll()
2021-06-30 09:28:52 +00:00
{
return ThreadPool::Poll();
2021-06-30 09:28:52 +00:00
}
bool AsyncApp::RunOnce()
2021-06-30 09:28:52 +00:00
{
return ThreadPool::RunOnce();
2021-06-30 09:28:52 +00:00
}
bool AsyncApp::Run()
{
return ThreadPool::Run();
}
2021-06-30 09:28:52 +00:00
void AsyncApp::Shutdown()
{
ThreadPool::Shutdown();
2021-06-30 09:28:52 +00:00
}
bool AsyncApp::Exiting()
{
return ThreadPool::Exiting();
2021-06-30 09:28:52 +00:00
}
AuSPtr<IWorkItem> AsyncApp::NewWorkItem(const WorkerId_t &worker, const AuSPtr<IWorkItemHandler> &task, bool supportsBlocking)
{
return ThreadPool::NewWorkItem(worker, task, supportsBlocking);
2021-06-30 09:28:52 +00:00
}
AuSPtr<IWorkItem> AsyncApp::NewFence()
2021-06-30 09:28:52 +00:00
{
return ThreadPool::NewFence();
}
Threading::Threads::ThreadShared_t AsyncApp::ResolveHandle(WorkerId_t id)
{
return ThreadPool::ResolveHandle(id);
2021-06-30 09:28:52 +00:00
}
AuBST<ThreadGroup_t, AuList<ThreadId_t>> AsyncApp::GetThreads()
{
return ThreadPool::GetThreads();
2021-06-30 09:28:52 +00:00
}
WorkerId_t AsyncApp::GetCurrentThread()
{
return ThreadPool::GetCurrentThread();
2021-06-30 09:28:52 +00:00
}
bool AsyncApp::Sync(WorkerId_t workerId, AuUInt32 timeoutMs, bool requireSignal)
2021-06-30 09:28:52 +00:00
{
return ThreadPool::Sync(workerId, timeoutMs, requireSignal);
2021-06-30 09:28:52 +00:00
}
void AsyncApp::Signal(WorkerId_t workerId)
2021-06-30 09:28:52 +00:00
{
ThreadPool::Signal(workerId);
2021-06-30 09:28:52 +00:00
}
void AsyncApp::SyncAllSafe()
{
ThreadPool::SyncAllSafe();
}
2021-06-30 09:28:52 +00:00
void AsyncApp::AddFeature(WorkerId_t id, AuSPtr<Threading::Threads::IThreadFeature> feature, bool async)
{
ThreadPool::AddFeature(id, feature, async);
2021-06-30 09:28:52 +00:00
}
void AsyncApp::AssertInThreadGroup(ThreadGroup_t group)
{
ThreadPool::AssertInThreadGroup(group);
2021-06-30 09:28:52 +00:00
}
void AsyncApp::AssertWorker(WorkerId_t id)
{
ThreadPool::AssertWorker(id);
2021-06-30 09:28:52 +00:00
}
bool AsyncApp::ScheduleLoopSource(const AuSPtr<Loop::ILoopSource> &loopSource, WorkerId_t workerId, AuUInt32 timeout, const AuConsumer<AuSPtr<Loop::ILoopSource>, bool> &callback)
2021-06-30 09:28:52 +00:00
{
return ThreadPool::ScheduleLoopSource(loopSource, workerId, timeout, callback);
2021-06-30 09:28:52 +00:00
}
2021-06-27 21:25:29 +00:00
}