/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: Flusher.cpp Date: 2021-8-27 Author: Reece ***/ #include #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::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()); InitFlushThread(); } void DeinitFlusher() { DestroyFlushThread(); } void ForceFlush() { ConsoleFIO::Flush(); } }