AuroraRuntime/Source/Console/Flusher.cpp

93 lines
2.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Flusher.cpp
Date: 2021-8-27
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Flusher.hpp"
#include "ConsoleFIO/ConsoleFIO.hpp"
namespace Aurora::Console
{
static Threading::Threads::ThreadUnique_t gWriterThread;
class ShutdownFlushHook : public Threading::Threads::IThreadFeature
{
public:
void Init() override;
void Cleanup() override;
};
void ShutdownFlushHook::Init()
{
}
void ShutdownFlushHook::Cleanup()
{
// Flush all internal consoles on shutdown
ForceFlush();
}
static void SlowStartupTasks()
{
ConsoleFIO::FIOCleanup();
}
static void LogThreadEP()
{
auto thread = Threading::Threads::GetThread();
SlowStartupTasks();
while (!thread->Exiting())
{
Threading::Sleep(500);
ForceFlush();
}
}
static void DestroyFlushThread()
{
gWriterThread.reset();
}
static void InitFlushThread()
{
// Startup a runner thread that will take care of all the stress inducing IO every so often on a remote thread
Threading::Threads::AbstractThreadVectors handler;
handler.DoRun = [](Threading::Threads::IAuroraThread *)
{
LogThreadEP();
};
gWriterThread = Threading::Threads::ThreadUnique(handler);
if (!gWriterThread)
{
return;
}
gWriterThread->SetName("CasualConsoleAsyncWritter");
gWriterThread->Run();
}
void InitFlusher()
{
// Add a 'ShutdownFlushHook' object to the main threads TLS hook
Threading::Threads::GetThread()->AddLastHopeTlsHook(AuMakeShared<ShutdownFlushHook>());
InitFlushThread();
}
void DeinitFlusher()
{
DestroyFlushThread();
}
void ForceFlush()
{
ConsoleFIO::Flush();
}
}