AuroraRuntime/Source/Console/Flusher.cpp
2021-10-24 11:19:47 +01:00

90 lines
2.1 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
gWriterThread = Threading::Threads::ThreadUnique(AuThreading::Threads::ThreadInfo(
AuMakeShared<AuThreading::Threads::IThreadVectorsFunctional>(AuThreading::Threads::IThreadVectorsFunctional::OnEntry_t(std::bind(LogThreadEP)),
AuThreading::Threads::IThreadVectorsFunctional::OnExit_t{}),
"CasualConsoleAsyncWritter"
));
if (!gWriterThread)
{
return;
}
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();
}
}