AuroraRuntime/Source/Console/Flusher.cpp

109 lines
2.5 KiB
C++
Raw Normal View History

2021-09-06 10:58:08 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Flusher.cpp
Date: 2021-8-27
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2021-09-06 10:58:08 +00:00
#include "Flusher.hpp"
#include "ConsoleFIO/ConsoleFIO.hpp"
#include "Logging/Logger.hpp"
2021-09-06 10:58:08 +00:00
namespace Aurora::Console
{
static AuThreads::ThreadUnique_t gWriterThread;
static AuThreadPrimitives::ConditionVariableUnique_t gCondVar;
static AuThreadPrimitives::ConditionMutexUnique_t gMutex;
2021-09-06 10:58:08 +00:00
class ShutdownFlushHook : public AuThreads::IThreadFeature
2021-09-06 10:58:08 +00:00
{
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()
{
SlowStartupTasks();
AU_LOCK_GUARD(gMutex);
while (AuIsThreadRunning())
2021-09-06 10:58:08 +00:00
{
gCondVar->WaitForSignal(500);
2021-09-06 10:58:08 +00:00
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 = AuThreads::ThreadUnique(AuThreads::ThreadInfo(
AuMakeShared<AuThreads::IThreadVectorsFunctional>(AuThreads::IThreadVectorsFunctional::OnEntry_t(std::bind(LogThreadEP)),
AuThreads::IThreadVectorsFunctional::OnExit_t{}),
"CasualConsoleAsyncWritter"
));
2021-09-06 10:58:08 +00:00
if (!gWriterThread)
{
return;
}
2021-09-06 10:58:08 +00:00
gWriterThread->Run();
}
void InitFlusher()
{
// Add a 'ShutdownFlushHook' object to the main threads TLS hook
AuThreads::GetThread()->AddLastHopeTlsHook(AuMakeShared<ShutdownFlushHook>());
2021-09-06 10:58:08 +00:00
gMutex = AuThreadPrimitives::ConditionMutexUnique();
SysAssert(gMutex);
gCondVar = AuThreadPrimitives::ConditionVariableUnique(AuUnsafeRaiiToShared(gMutex));
SysAssert(gCondVar);
2021-09-06 10:58:08 +00:00
InitFlushThread();
}
void PingFlushers()
{
if (gCondVar)
{
gCondVar->Signal();
}
}
2021-09-06 10:58:08 +00:00
void DeinitFlusher()
{
DestroyFlushThread();
}
void ForceFlush()
{
Logging::ForceFlushLoggers();
2021-09-06 10:58:08 +00:00
ConsoleFIO::Flush();
}
}