AuroraRuntime/Source/Grug/AuGrug.cpp
Jamie Reece Wilson ca2c0462ab [*] Continue to refactor allocations of synchronization primitive away
[*] NT: Fix missing CoTaskMemFree leak on startup
[*] Fix ConsoleStd restart bug
2023-06-28 10:33:12 +01:00

198 lines
5.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuGrug.cpp
Date: 2022-01-03
File: Flusher.cpp
Date: 2021-8-27
Author: Reece
Note: Grug couples async telemetry, console flushing, and log dispatching
to one lazy working thread w/ high performance thread primitives for
when urgent telemetry events are raised.
***/
#include <Source/RuntimeInternal.hpp>
#include "AuGrug.hpp"
#include <Source/Logging/AuLogger.hpp>
#include <Source/Console/ConsoleFIO/ConsoleFIO.hpp>
#include <Source/RuntimeInternal.hpp>
#include <Source/Console/Flusher.hpp>
#include <Source/Console/Console.hpp>
#include <Source/Debug/MemoryCrunch.hpp>
#if defined(AURORA_IS_LINUX_DERIVED)
void LinuxSuperSecretIOTick();
void LinuxSuperSecretFuckGlibc();
#endif
namespace Aurora::Grug
{
static const auto kGrugSleepMs = 100;
static const auto kGrugFlushMs = 500;
static AuThreads::ThreadUnique_t gGrugsBigWorld;
static AuThreadPrimitives::ConditionMutex gMutex; // ^ that
static AuThreadPrimitives::ConditionVariable gCondVar(AuUnsafeRaiiToShared(gMutex.AsPointer())); // slow logger work queue
static AuThreadPrimitives::Semaphore gArrows;
static void SlowStartupTasks()
{
Console::ConsoleFIO::FIOCleanup();
}
// grug require only 1 strand
static void GrugWorld()
{
#if defined(AURORA_IS_LINUX_DERIVED)
LinuxSuperSecretFuckGlibc();
#endif
// grug surive first night
SlowStartupTasks();
AU_LOCK_GUARD(gMutex);
AuDebug::AddMemoryCrunch(); // this thread must never encounter an out of memory condition. begin out of memory mitigations.
Utility::RateLimiter limiter;
limiter.SetNextStep(kGrugFlushMs);
// grug has cave
while (AuIsThreadRunning())
{
// * -> grug wake up from 100ms (kGrugSleepMs)
// grug smashy alarm
gCondVar->WaitForSignal(kGrugFlushMs);
// grug anoy
if (gArrows && gArrows->TryLock())
{
// grug yeet
DequeueOneArrow();
}
// grug pump all async log messages
GrugFlushWrites();
// adhd grug wonder around the log pool every 500ms (rate limited to 100ms sleeps)
if (limiter.CheckExchangePass())
{
GrugFlushFlushs();
}
// grug give up
// grug yield for at least 100ms -> *
if (gArrows)
{
// grug sleep for 100ms or until poked
if (gArrows->LockMS(kGrugSleepMs))
{
DequeueOneArrow();
}
}
else
{
// grug sleep 100ms
AuThreading::Sleep(kGrugSleepMs);
}
#if defined(AURORA_IS_LINUX_DERIVED)
::LinuxSuperSecretIOTick();
#endif
}
}
static void DestroyFlushThread()
{
gGrugsBigWorld.reset();
}
static void InitFlushThread()
{
// Startup a runner thread that will take care of all the stress of triggering IO every so often on a remote thread
gGrugsBigWorld = AuThreads::ThreadUnique(AuThreads::ThreadInfo(
AuMakeShared<AuThreads::IThreadVectorsFunctional>(AuThreads::IThreadVectorsFunctional::OnEntry_t(std::bind(GrugWorld)),
AuThreads::IThreadVectorsFunctional::OnExit_t{}),
"Cave Grug"
));
if (!gGrugsBigWorld)
{
return;
}
// he's a lazy bastard
gGrugsBigWorld->SetThrottle(AuThreads::EThreadThrottle::eEfficient);
gGrugsBigWorld->SetPriority(AuThreads::EThreadPriority::ePrioLow);
gGrugsBigWorld->Run();
}
void InitGrug()
{
InitFlushThread();
}
void DeinitGrug()
{
if (bool(gGrugsBigWorld) && gGrugsBigWorld.get() != AuThreads::GetThread())
{
gGrugsBigWorld.reset();
}
GrugFlushWrites();
GrugFlushFlushs();
DeinitArrows();
}
void NotifyGrugOfTelemetry()
{
if (gArrows)
{
gArrows->Unlock(1);
}
DrachenlordScreech();
}
void DrachenlordScreech()
{
if (!gCondVar)
{
return;
}
gCondVar->Signal();
}
bool IsGrug()
{
if (!gGrugsBigWorld) return false;
return AuThreads::GetThread() == gGrugsBigWorld.get();
}
void NotifyGrugOfLogs()
{
if (Grug::IsGrug())
{
Grug::GrugFlushWrites();
}
else
{
Grug::DrachenlordScreech();
}
}
void GrugFlushWrites()
{
Logging::ForceFlushLoggers();
}
void GrugFlushFlushs()
{
Logging::ForceFlushFlush();
Console::PumpOffMain();
Console::ForceFlush();
}
}