AuroraRuntime/Source/Async/Schedular.cpp

155 lines
3.9 KiB
C++
Raw Normal View History

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Schedular.cpp
Date: 2021-6-26
Author: Reece
***/
2021-06-27 21:25:29 +00:00
#include <RuntimeInternal.hpp>
2021-06-30 12:00:32 +00:00
#include "Async.hpp"
2021-06-27 21:25:29 +00:00
#include "Schedular.hpp"
2021-06-30 12:00:32 +00:00
#include "AsyncApp.hpp"
2021-06-27 21:25:29 +00:00
namespace Aurora::Async
{
2021-06-30 12:00:32 +00:00
struct SchedEntry
{
AuUInt64 ns;
DispatchTarget_t target;
AuSPtr<IAsyncRunnable> runnable;
};
static Threading::Threads::ThreadUnique_t gThread;
static Threading::Primitives::MutexUnique_t gSchedLock;
2021-06-30 12:00:32 +00:00
static AuList<SchedEntry> gEntries;
static void GetDispatchableTasks(AuList<SchedEntry> &pending)
{
2021-09-06 10:58:08 +00:00
AU_LOCK_GUARD(gSchedLock);
2021-06-30 12:00:32 +00:00
auto time = Time::CurrentClockNS();
for (auto itr = gEntries.begin(); itr != gEntries.end(); )
{
if (itr->ns <= time)
{
pending.push_back(std::move(*itr));
itr = gEntries.erase(itr);
}
else
{
itr ++;
}
}
}
2021-09-06 10:58:08 +00:00
static bool gLockedPump = false;
static void PumpSysThread()
{
RuntimeSysPump();
gLockedPump = false;
}
2021-06-30 12:00:32 +00:00
static void SchedThread()
{
AuUInt32 counter {};
2021-06-30 12:00:32 +00:00
AuList<SchedEntry> pending;
auto thread = Threading::Threads::GetThread();
2021-06-30 12:00:32 +00:00
while (!thread->Exiting())
{
AuList<SchedEntry> pending;
Threading::SleepNs(1000000 / 2 * gRuntimeConfig.async.schedularFrequency);
2021-06-30 12:00:32 +00:00
GetDispatchableTasks(pending);
for (auto &entry : pending)
{
try
{
static_cast<AsyncApp *>(GetAsyncApp())->Run(entry.target, entry.runnable);
DecRunningTasks();
}
catch (...)
{
LogWarn("Dropped scheduled task! Expect a leaky counter!");
LogWarn("Would you rather `Why u no exit?!` or `WHY DID U JUST CRASH REEEE` in production?");
Debug::PrintError();
}
2021-06-30 12:00:32 +00:00
}
counter++;
2021-09-06 10:58:08 +00:00
if ((!gRuntimeConfig.async.sysPumpFrequency) || ((gRuntimeConfig.async.sysPumpFrequency) && (counter % gRuntimeConfig.async.sysPumpFrequency) == 0))
{
try
{
2021-09-06 10:58:08 +00:00
if (!std::exchange(gLockedPump, true))
{
NewWorkItem({0, 0}, AuMakeShared<BasicWorkStdFunc>(PumpSysThread))->Dispatch();
}
}
catch (...)
{
LogWarn("Dropped SysRuntimePump");
Debug::PrintError();
}
}
2021-06-30 12:00:32 +00:00
}
2021-06-30 12:00:32 +00:00
}
void InitSched()
{
gSchedLock = Threading::Primitives::MutexUnique();
}
void DeinitSched()
{
gThread.reset();
gSchedLock.reset();
}
2021-06-30 12:00:32 +00:00
void StartSched()
{
2021-06-30 12:00:32 +00:00
Threading::Threads::AbstractThreadVectors handler;
handler.DoRun = [=](const Threading::Threads::IAuroraThread *thread)
{
SchedThread();
};
gThread = Threading::Threads::ThreadUnique(handler);
gThread->Run();
}
void StopSched()
2021-06-30 12:00:32 +00:00
{
gThread.reset();
}
void Schedule(AuUInt64 ns, DispatchTarget_t target, AuSPtr<IAsyncRunnable> runnable)
{
2021-09-06 10:58:08 +00:00
AU_LOCK_GUARD(gSchedLock);
2021-06-30 12:00:32 +00:00
IncRunningTasks();
gEntries.push_back({ns, target, runnable});
}
void TerminateSceduledTasks(DispatchTarget_t target)
{
2021-09-06 10:58:08 +00:00
AU_LOCK_GUARD(gSchedLock);
for (auto itr = gEntries.begin(); itr != gEntries.end(); )
{
if (itr->target <= target)
{
itr->runnable->CancelAsync();
itr = gEntries.erase(itr);
}
else
{
itr ++;
}
}
}
2021-06-27 21:25:29 +00:00
}