Refactor all thread stuff into its own file

This commit is contained in:
2023-09-04 23:19:10 -04:00
parent 01c37cf2c9
commit cfc536011e
4 changed files with 190 additions and 55 deletions

View File

@@ -11,11 +11,8 @@ using namespace DebugPrint;
AIntegrationGameModeBase::AIntegrationGameModeBase()
{
Thread = nullptr;
ThreadStopRequested = false;
EngineSeconds = 0.0;
NextThreadTrigger = 1.0;
ThreadEvent = nullptr;
//PrimaryActorTick.bCanEverTick = true; // Probably wrong
//PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong
//PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong
@@ -29,43 +26,21 @@ AIntegrationGameModeBase::~AIntegrationGameModeBase()
ResetToInitialState();
}
// Run routine called by the worker thread.
uint32 AIntegrationGameModeBase::Run()
{
while (true)
{
bool triggered = ThreadEvent->Wait(3000);
if (ThreadStopRequested) {
DPrint("Thread stopping as requested");
break;
}
if (!triggered) {
DPrint("Thread waiting a long time...");
continue;
}
{
FLockedWrapper lockedwrap(LockableWrapper);
Sockets->Update(lockedwrap);
lockedwrap->play_invoke_event_update(lockedwrap.Get(), EngineSeconds);
Sockets->Update(lockedwrap);
}
}
// This method runs in the background thread,
// at the moment we trigger it.
//
uint32 AIntegrationGameModeBase::Run() {
FLockedWrapper lockedwrap(LockableWrapper);
Sockets->Update(lockedwrap);
lockedwrap->play_invoke_event_update(lockedwrap.Get(), EngineSeconds);
Sockets->Update(lockedwrap);
return 0;
}
void AIntegrationGameModeBase::ResetToInitialState()
{
// Shut down the thread and release the ThreadEvent
if (Thread != nullptr)
{
ThreadStopRequested = true;
ThreadEvent->Trigger();
delete Thread; // This waits for the thread to complete.
Thread = nullptr;
FPlatformProcess::ReturnSynchEventToPool(ThreadEvent);
ThreadEvent = nullptr;
}
ThreadStopRequested = false;
// Shut down the thread
LuprexUpdateTask.Shutdown();
// Now that the thread's gone, we should be able to
// just claim and hold the lock on the wrapper.
@@ -123,9 +98,9 @@ void AIntegrationGameModeBase::Tick(float DeltaSeconds)
ConsoleSetOutput(ConsoleOutput.Get());
ConsoleOutput.ClearDirty();
}
if ((Thread != nullptr) && (EngineSeconds >= NextThreadTrigger))
if (EngineSeconds >= NextThreadTrigger)
{
ThreadEvent->Trigger();
LuprexUpdateTask.Trigger();
NextThreadTrigger += 0.05;
}
}
@@ -162,7 +137,7 @@ void AIntegrationGameModeBase::BeginPlay()
FLockedWrapper w(LockableWrapper);
// Sanity checks. Make sure everything is clean.
checkf(Thread == nullptr, TEXT("There should be no thread here."));
checkf(!LuprexUpdateTask.IsRunning(), TEXT("There should be no thread here."));
checkf(w->engine == nullptr, TEXT("There should be no engine here."));
// Try to initialize the wrapper.
@@ -203,8 +178,7 @@ void AIntegrationGameModeBase::BeginPlay()
Sockets.Reset(FLpxSockets::Create(w));
std::string error = Sockets->GetError();
check(error.empty());
ThreadEvent = FPlatformProcess::GetSynchEventFromPool(false);
Thread = FRunnableThread::Create(this, TEXT("Worker Thread"));
LuprexUpdateTask.Startup(this);
}
// Create a tangible.