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"
|
|
|
|
|
|
|
|
namespace Aurora::Console
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
static AuThreads::ThreadUnique_t gWriterThread;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2021-11-05 17:34:23 +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()
|
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
auto thread = AuThreads::GetThread();
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
SlowStartupTasks();
|
|
|
|
|
|
|
|
while (!thread->Exiting())
|
|
|
|
{
|
2021-09-06 15:47:35 +00:00
|
|
|
Threading::Sleep(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
|
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
gWriterThread = AuThreads::ThreadUnique(AuThreads::ThreadInfo(
|
|
|
|
AuMakeShared<AuThreads::IThreadVectorsFunctional>(AuThreads::IThreadVectorsFunctional::OnEntry_t(std::bind(LogThreadEP)),
|
|
|
|
AuThreads::IThreadVectorsFunctional::OnExit_t{}),
|
2021-10-24 10:19:47 +00:00
|
|
|
"CasualConsoleAsyncWritter"
|
|
|
|
));
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!gWriterThread)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gWriterThread->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitFlusher()
|
|
|
|
{
|
|
|
|
// Add a 'ShutdownFlushHook' object to the main threads TLS hook
|
2021-11-05 17:34:23 +00:00
|
|
|
AuThreads::GetThread()->AddLastHopeTlsHook(AuMakeShared<ShutdownFlushHook>());
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
InitFlushThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitFlusher()
|
|
|
|
{
|
|
|
|
DestroyFlushThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForceFlush()
|
|
|
|
{
|
|
|
|
ConsoleFIO::Flush();
|
|
|
|
}
|
|
|
|
}
|