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

@@ -0,0 +1,104 @@
#include "CoreMinimal.h"
///////////////////////////////////////////////
//
// TRIGGERED TASKS
//
// This is the situation where this class is
// useful:
//
// * You have a function to run in the background.
// * It should start the instant a foreground thread says "NOW".
// * It should be run exactly once for each "NOW".
//
// To use this class, construct an FTriggeredTask,
// and provide a runnable object. The runnable
// object's "Run" method will get executed in
// each time you call 'Trigger' on the FTriggeredTask.
//
// The run method will never get called reentrantly,
// even if you call 'Trigger' rapidly in succession.
//
///////////////////////////////////////////////
class FTriggeredTask : public FRunnable
{
private:
// Mutex used by control routines.
//
FCriticalSection Mutex;
// The worker thread.
//
FRunnableThread* Thread;
// Used to tell the worker thread to stop.
//
bool ThreadStopRequested;
// This event is used to wake up the thread.
//
// Normally, this means we want the worker to run the task
// once. But if ThreadStopRequested is true, it means we
// want the thread to exit.
//
FEvent* ThreadEvent;
// The client whose task we're triggering.
//
FRunnable* Client;
private:
/////////////////////////////////////////////
//
// This section contains routines that are
// executed by the thread itself.
//
/////////////////////////////////////////////
// Method of FRunnable, called by the Luprex thread.
//
virtual uint32 Run() override;
public:
/////////////////////////////////////////////
//
// This section contains thread control routines.
// These are not invoked by the thread, but by the
// outside entity that started the thread.
//
/////////////////////////////////////////////
// Constructor.
//
// The client must outlive the FTriggeredTask.
//
FTriggeredTask();
// Startup.
//
// Bring the system online, put it in ready mode.
//
void Startup(FRunnable* client);
// Shutdown.
//
// Bring the system down, deallocate all threads.
//
void Shutdown();
// Trigger
//
// Run the client's 'RUN' method once, in the
// background. Trigger is a no-op if the system
// is shut down. If you trigger when the task
// is already running, a trigger may get dropped.
//
void Trigger();
// IsRunning
//
bool IsRunning();
};