2021-06-27 21:33:58 +00:00
|
|
|
/***
|
|
|
|
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;
|
2021-07-15 16:16:23 +00:00
|
|
|
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-07-15 16:16:23 +00:00
|
|
|
|
2021-06-30 12:00:32 +00:00
|
|
|
static void SchedThread()
|
|
|
|
{
|
2021-07-01 13:49:32 +00:00
|
|
|
AuUInt32 counter {};
|
2021-06-30 12:00:32 +00:00
|
|
|
AuList<SchedEntry> pending;
|
2021-07-01 13:49:32 +00:00
|
|
|
|
|
|
|
auto thread = Threading::Threads::GetThread();
|
2021-06-30 12:00:32 +00:00
|
|
|
|
|
|
|
while (!thread->Exiting())
|
|
|
|
{
|
|
|
|
AuList<SchedEntry> pending;
|
2021-07-15 16:28:45 +00:00
|
|
|
|
|
|
|
Threading::SleepNs(1000000 / 2 * gRuntimeConfig.async.schedularFrequency);
|
|
|
|
|
2021-06-30 12:00:32 +00:00
|
|
|
GetDispatchableTasks(pending);
|
|
|
|
|
|
|
|
for (auto &entry : pending)
|
|
|
|
{
|
2021-07-15 16:16:23 +00:00
|
|
|
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
|
|
|
}
|
2021-07-01 13:49:32 +00:00
|
|
|
|
|
|
|
counter++;
|
2021-09-06 10:58:08 +00:00
|
|
|
if ((!gRuntimeConfig.async.sysPumpFrequency) || ((gRuntimeConfig.async.sysPumpFrequency) && (counter % gRuntimeConfig.async.sysPumpFrequency) == 0))
|
2021-07-01 13:49:32 +00:00
|
|
|
{
|
2021-07-15 16:16:23 +00:00
|
|
|
try
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!std::exchange(gLockedPump, true))
|
|
|
|
{
|
|
|
|
NewWorkItem({0, 0}, AuMakeShared<BasicWorkStdFunc>(PumpSysThread))->Dispatch();
|
|
|
|
}
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
LogWarn("Dropped SysRuntimePump");
|
|
|
|
Debug::PrintError();
|
|
|
|
}
|
2021-07-01 13:49:32 +00:00
|
|
|
}
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
2021-07-15 16:16:23 +00:00
|
|
|
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitSched()
|
|
|
|
{
|
2021-07-15 16:16:23 +00:00
|
|
|
gSchedLock = Threading::Primitives::MutexUnique();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitSched()
|
|
|
|
{
|
|
|
|
gThread.reset();
|
|
|
|
gSchedLock.reset();
|
|
|
|
}
|
2021-06-30 12:00:32 +00:00
|
|
|
|
2021-07-15 16:16:23 +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();
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:16:23 +00:00
|
|
|
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});
|
|
|
|
}
|
2021-07-15 16:16:23 +00:00
|
|
|
|
|
|
|
void TerminateSceduledTasks(DispatchTarget_t target)
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
AU_LOCK_GUARD(gSchedLock);
|
2021-07-15 16:16:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|