2021-06-27 21:33:58 +00:00
|
|
|
/***
|
2023-11-11 10:11:09 +00:00
|
|
|
Copyright (C) 2021-2023 Jamie Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2021-06-27 21:33:58 +00:00
|
|
|
|
2023-11-11 10:11:09 +00:00
|
|
|
File: AuSchedular.cpp
|
2021-06-27 21:33:58 +00:00
|
|
|
Date: 2021-6-26
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-30 12:00:32 +00:00
|
|
|
#include "Async.hpp"
|
2023-11-11 10:11:09 +00:00
|
|
|
#include "AuSchedular.hpp"
|
2021-11-05 17:34:23 +00:00
|
|
|
//#include "AsyncApp.hpp"
|
|
|
|
#include "ThreadPool.hpp"
|
2022-05-10 13:22:37 +00:00
|
|
|
#include <Console/Commands/Commands.hpp>
|
2023-12-07 09:20:23 +00:00
|
|
|
#include "IAsyncRunnable.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
namespace Aurora::Async
|
|
|
|
{
|
2021-06-30 12:00:32 +00:00
|
|
|
struct SchedEntry
|
|
|
|
{
|
|
|
|
AuUInt64 ns;
|
2021-09-30 09:19:04 +00:00
|
|
|
WorkerId_t target;
|
2021-06-30 12:00:32 +00:00
|
|
|
AuSPtr<IAsyncRunnable> runnable;
|
2021-11-05 17:34:23 +00:00
|
|
|
IThreadPoolInternal *pool;
|
2021-06-30 12:00:32 +00:00
|
|
|
};
|
|
|
|
|
2023-08-10 00:10:59 +00:00
|
|
|
// sched thread threading:
|
2021-11-05 17:34:23 +00:00
|
|
|
static AuThreads::ThreadUnique_t gThread;
|
2023-08-09 02:21:14 +00:00
|
|
|
static AuThreadPrimitives::ConditionMutex gSchedLock;
|
|
|
|
static AuThreadPrimitives::ConditionVariable gSchedCondvar(AuUnsafeRaiiToShared(gSchedLock.AsPointer()));
|
2023-08-10 00:10:59 +00:00
|
|
|
|
|
|
|
// next tick timing:
|
2023-08-09 02:21:14 +00:00
|
|
|
static AuUInt64 uNextSysTickGuessed {};
|
|
|
|
static AuUInt64 uNextWakeuptimeRateLimit {};
|
2023-08-10 00:10:59 +00:00
|
|
|
static AuBST<AuUInt64, SchedEntry> gOrderedEntries;
|
|
|
|
// prevents stupid tick issues
|
|
|
|
static bool gLockedPump = false;
|
2022-05-09 18:27:36 +00:00
|
|
|
|
2023-08-10 00:10:59 +00:00
|
|
|
// old sewage to be cleaned up
|
|
|
|
static bool gBOriginal;
|
2023-09-15 17:08:02 +00:00
|
|
|
static AuOptionalEx<AuWorkerPId_t> gMainThread;
|
2022-05-09 18:27:36 +00:00
|
|
|
void StartSched2();
|
2023-08-10 00:10:59 +00:00
|
|
|
static void SchedNextTime(AuUInt64 uNSAbs);
|
2021-06-30 12:00:32 +00:00
|
|
|
|
|
|
|
static void GetDispatchableTasks(AuList<SchedEntry> &pending)
|
|
|
|
{
|
2023-08-09 02:21:14 +00:00
|
|
|
auto time = Time::SteadyClockNS();
|
2021-06-30 12:00:32 +00:00
|
|
|
|
2023-08-10 00:10:59 +00:00
|
|
|
for (auto itr = gOrderedEntries.begin(); itr != gOrderedEntries.end(); )
|
2021-06-30 12:00:32 +00:00
|
|
|
{
|
2023-08-10 00:10:59 +00:00
|
|
|
auto &[ns, entry] = *itr;
|
2021-12-26 15:51:31 +00:00
|
|
|
|
2023-08-10 00:10:59 +00:00
|
|
|
if (ns > time)
|
|
|
|
{
|
2023-12-06 17:29:31 +00:00
|
|
|
SchedNextTime(ns);
|
2023-08-10 00:10:59 +00:00
|
|
|
break;
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
2023-08-10 00:10:59 +00:00
|
|
|
|
|
|
|
if (!AuTryInsert(pending, AuMove(entry)))
|
2021-06-30 12:00:32 +00:00
|
|
|
{
|
2023-08-10 00:10:59 +00:00
|
|
|
SchedNextTime(time + AuMSToNS<AuUInt64>(3));
|
|
|
|
break;
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
2023-08-10 00:10:59 +00:00
|
|
|
|
|
|
|
itr = gOrderedEntries.erase(itr);
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
static void PumpSysThread()
|
|
|
|
{
|
2022-05-18 20:22:58 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
RuntimeSysPump();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch("SysPump failed");
|
|
|
|
}
|
2023-08-10 00:10:59 +00:00
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
gLockedPump = false;
|
|
|
|
}
|
2021-07-15 16:16:23 +00:00
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
static void SchedNextTime(AuUInt64 uNSAbs)
|
|
|
|
{
|
|
|
|
if (uNextSysTickGuessed > uNSAbs ||
|
|
|
|
!uNextSysTickGuessed)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
auto uCurrent = uNextSysTickGuessed;
|
|
|
|
|
|
|
|
if (uCurrent &&
|
|
|
|
uCurrent <= uNSAbs)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uNextWakeuptimeRateLimit > uNSAbs)
|
|
|
|
{
|
|
|
|
uNSAbs = uNextWakeuptimeRateLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AuAtomicCompareExchange(&uNextSysTickGuessed, uNSAbs, uCurrent) == uCurrent)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 17:29:31 +00:00
|
|
|
gSchedCondvar->Signal();
|
2023-08-09 02:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 12:00:32 +00:00
|
|
|
static void SchedThread()
|
|
|
|
{
|
2023-08-09 02:21:14 +00:00
|
|
|
Aurora::Utility::RateLimiter limiter(AuMSToNS<AuUInt64>(gRuntimeConfig.async.dwLegacyMainThreadSystemTickMS));
|
2021-06-30 12:00:32 +00:00
|
|
|
|
2023-08-10 00:10:59 +00:00
|
|
|
// Intentional: no Dec used on this thread; we should never run out of memory to schedule tasks
|
|
|
|
AuDebug::AddMemoryCrunch();
|
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
while (AuIsThreadRunning())
|
2021-06-30 12:00:32 +00:00
|
|
|
{
|
|
|
|
AuList<SchedEntry> pending;
|
2021-07-15 16:28:45 +00:00
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gSchedLock);
|
|
|
|
|
|
|
|
auto uNextTick = uNextSysTickGuessed;
|
|
|
|
auto uNow = AuTime::SteadyClockNS();
|
|
|
|
|
|
|
|
if (uNow < uNextTick)
|
|
|
|
{
|
2023-12-06 17:29:31 +00:00
|
|
|
uNextSysTickGuessed = 0;
|
2023-08-09 02:21:14 +00:00
|
|
|
gSchedCondvar->WaitForSignalNS(uNextTick - uNow);
|
|
|
|
}
|
2023-12-06 17:29:31 +00:00
|
|
|
else if (uNow >= uNextTick)
|
2023-08-10 00:10:59 +00:00
|
|
|
{
|
|
|
|
uNextSysTickGuessed = 0;
|
|
|
|
}
|
2023-12-06 17:29:31 +00:00
|
|
|
else if (uNextSysTickGuessed == 0)
|
2023-08-09 02:21:14 +00:00
|
|
|
{
|
2023-09-16 17:08:27 +00:00
|
|
|
gSchedCondvar->WaitForSignalNS(gRuntimeConfig.async.bEnableLegacyTicks ?
|
|
|
|
AuMSToNS<AuUInt64>(gRuntimeConfig.async.dwLegacyMainThreadSystemTickMS) :
|
|
|
|
0);
|
2023-08-09 02:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GetDispatchableTasks(pending);
|
2021-07-15 16:28:45 +00:00
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
uNextWakeuptimeRateLimit = AuTime::SteadyClockNS() + AuMSToNS<AuUInt64>(gRuntimeConfig.async.dwSchedulerRateLimitMS);
|
|
|
|
}
|
2021-06-30 12:00:32 +00:00
|
|
|
|
|
|
|
for (auto &entry : pending)
|
|
|
|
{
|
2021-07-15 16:16:23 +00:00
|
|
|
try
|
|
|
|
{
|
2023-08-09 02:21:14 +00:00
|
|
|
AuStaticCast<AuAsync::ThreadPool>(entry.pool)->Run(entry.target, entry.runnable, false);
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Debug::PrintError();
|
|
|
|
}
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
2021-07-01 13:49:32 +00:00
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
if (gRuntimeConfig.async.bEnableLegacyTicks)
|
2021-07-01 13:49:32 +00:00
|
|
|
{
|
2023-08-09 02:21:14 +00:00
|
|
|
if (limiter.CheckExchangePass())
|
2021-07-15 16:16:23 +00:00
|
|
|
{
|
2023-08-10 00:10:59 +00:00
|
|
|
if (!AuExchange(gLockedPump, true))
|
2022-05-09 18:27:36 +00:00
|
|
|
{
|
2023-09-22 05:05:39 +00:00
|
|
|
DispatchOn(gMainThread.value(), PumpSysThread);
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
2023-08-10 00:10:59 +00:00
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
SchedNextTime(AuMSToNS<AuUInt64>(gRuntimeConfig.async.dwLegacyMainThreadSystemTickMS) + AuTime::SteadyClockNS());
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
2021-07-01 13:49:32 +00:00
|
|
|
}
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitSched()
|
|
|
|
{
|
2023-08-09 02:21:14 +00:00
|
|
|
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitSched()
|
|
|
|
{
|
2023-08-11 15:51:42 +00:00
|
|
|
if (gThread)
|
|
|
|
{
|
|
|
|
gThread->SendExitSignal();
|
|
|
|
}
|
2023-08-10 00:10:59 +00:00
|
|
|
gSchedCondvar->Broadcast();
|
2021-07-15 16:16:23 +00:00
|
|
|
gThread.reset();
|
|
|
|
}
|
2021-06-30 12:00:32 +00:00
|
|
|
|
2022-05-09 18:27:36 +00:00
|
|
|
AUKN_SYM void SetMainThreadForSysPumpScheduling(AuWorkerPId_t pid)
|
|
|
|
{
|
2023-12-07 09:20:23 +00:00
|
|
|
if (!pid)
|
2022-05-09 18:27:36 +00:00
|
|
|
{
|
2023-08-09 02:21:14 +00:00
|
|
|
gRuntimeConfig.async.bEnableLegacyTicks = gBOriginal;
|
2023-09-15 17:08:02 +00:00
|
|
|
AuResetMember(gMainThread);
|
2022-05-10 13:22:37 +00:00
|
|
|
Console::Commands::UpdateDispatcher(gMainThread);
|
2022-05-09 18:27:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gMainThread = pid;
|
2022-05-10 13:22:37 +00:00
|
|
|
Console::Commands::UpdateDispatcher(gMainThread);
|
2023-08-09 02:21:14 +00:00
|
|
|
gRuntimeConfig.async.bEnableLegacyTicks = true;
|
2022-05-09 18:27:36 +00:00
|
|
|
|
|
|
|
StartSched2();
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:16:23 +00:00
|
|
|
void StartSched()
|
2022-05-09 18:27:36 +00:00
|
|
|
{
|
2023-08-09 02:21:14 +00:00
|
|
|
if (!gRuntimeConfig.async.bStartSchedularOnStartup)
|
2022-05-09 18:27:36 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-09 02:21:14 +00:00
|
|
|
if (gRuntimeConfig.async.dwLegacyMainThreadSystemTickMS && !gMainThread)
|
2022-05-09 18:27:36 +00:00
|
|
|
{
|
|
|
|
gMainThread = AuWorkerPId_t(AuAsync::GetSharedAsyncApp(), AuWorkerId_t {0, 0});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StartSched2();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartSched2()
|
2021-07-15 16:16:23 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
AU_LOCK_GUARD(gSchedLock);
|
2022-05-09 18:27:36 +00:00
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
if (gThread) return;
|
2022-05-09 18:27:36 +00:00
|
|
|
|
2023-08-09 02:21:14 +00:00
|
|
|
gBOriginal = gRuntimeConfig.async.bEnableLegacyTicks;
|
2022-05-09 18:27:36 +00:00
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
gThread = AuThreads::ThreadUnique(AuThreads::ThreadInfo(
|
|
|
|
AuMakeShared<AuThreads::IThreadVectorsFunctional>(AuThreads::IThreadVectorsFunctional::OnEntry_t(std::bind(SchedThread)),
|
2022-05-09 18:27:36 +00:00
|
|
|
AuThreads::IThreadVectorsFunctional::OnExit_t {})
|
2021-10-24 10:44:43 +00:00
|
|
|
));
|
2021-11-05 17:34:23 +00:00
|
|
|
|
2021-06-30 12:00:32 +00:00
|
|
|
gThread->Run();
|
|
|
|
}
|
2023-08-10 02:34:44 +00:00
|
|
|
|
2023-08-10 00:10:59 +00:00
|
|
|
bool Schedule(AuUInt64 ns, IThreadPoolInternal *pool,
|
|
|
|
WorkerId_t target,
|
|
|
|
AuSPtr<IAsyncRunnable> runnable)
|
2021-06-30 12:00:32 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
AU_LOCK_GUARD(gSchedLock);
|
2023-08-10 02:34:44 +00:00
|
|
|
AU_DEBUG_MEMCRUNCH;
|
2023-08-09 02:21:14 +00:00
|
|
|
AuAtomicAdd(&pool->uAtomicCounter, 1u);
|
|
|
|
SchedNextTime(ns);
|
2023-08-10 00:10:59 +00:00
|
|
|
return AuTryInsert(gOrderedEntries,
|
|
|
|
AuConstReference(ns),
|
|
|
|
SchedEntry { ns, target, runnable, pool });
|
2021-06-30 12:00:32 +00:00
|
|
|
}
|
2021-07-15 16:16:23 +00:00
|
|
|
|
2023-08-10 00:10:59 +00:00
|
|
|
void TerminateSceduledTasks(IThreadPoolInternal *pool,
|
|
|
|
WorkerId_t target)
|
2021-07-15 16:16:23 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
AU_LOCK_GUARD(gSchedLock);
|
2021-07-15 16:16:23 +00:00
|
|
|
|
2023-08-10 00:10:59 +00:00
|
|
|
for (auto itr = gOrderedEntries.begin(); itr != gOrderedEntries.end(); )
|
2021-07-15 16:16:23 +00:00
|
|
|
{
|
2023-08-10 00:10:59 +00:00
|
|
|
auto &[ns, entry] = *itr;
|
|
|
|
|
|
|
|
if ((entry.pool == pool) &&
|
|
|
|
((entry.target == target) ||
|
|
|
|
(target.second == Async::kThreadIdAny && target.first == entry.target.first)))
|
2021-07-15 16:16:23 +00:00
|
|
|
{
|
2023-08-10 00:10:59 +00:00
|
|
|
entry.runnable->CancelAsync();
|
|
|
|
itr = gOrderedEntries.erase(itr);
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-06 17:29:31 +00:00
|
|
|
itr++;
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|