116 lines
4.2 KiB
C++
116 lines
4.2 KiB
C++
// Copyright UnexGames 2025. All Rights Reserved.
|
|
|
|
#pragma once
|
|
|
|
#include "CoreMinimal.h"
|
|
#include "Kismet/BlueprintFunctionLibrary.h"
|
|
#include "ThreadTasks.h"
|
|
#include "ThreadPool.h"
|
|
#include "ThreadMutex.h"
|
|
#include "ThreadTasksLoop.h"
|
|
#include "Async/Async.h"
|
|
#include "Engine/EngineTypes.h"
|
|
#include "ThreadLibrary.generated.h"
|
|
|
|
/**
|
|
*
|
|
*/
|
|
|
|
|
|
|
|
|
|
UCLASS()
|
|
class MULTITHREADLIBRARY_API UThreadLibrary : public UBlueprintFunctionLibrary
|
|
{
|
|
GENERATED_BODY()
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
* Execute task on Thread os taskGraph, not thread pool.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Meta = (Latent, DisplayName = "Created And Execute Task On Thread", LatentInfo = "LatentInfo", ExpandEnumAsExecs = "Out", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "MultiThreadLibrary")
|
|
static void CreateAndExecuteTaskOnThread(UObject* WorldContextObject, FLatentActionInfo LatentInfo, UThreadBase*& Task, EtaskExecutionBranches& Out, ETaskExecutionType ExecutionType = ETaskExecutionType::TaskGraph);
|
|
|
|
/**
|
|
*Execute task on thread pool
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Meta = (Latent, DisplayName = "Execute Task On Thread Pool", LatentInfo = "LatentInfo", ExpandEnumAsExecs = "Out", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "MultiThreadLibrary")
|
|
static void ExecuteTaskOnThreadPool(UObject* WorldContextObject, FLatentActionInfo LatentInfo, UThreadBase*& Task, EtaskExecutionBranches& Out, UThreadPool* ThreadPool);
|
|
|
|
/*
|
|
* Create one Custom Thread and Execute task on thread body with tick.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Category = "MultiThreadLibrary", Meta = (DisplayName = "Create And Execute Task Loop Nr On Thread", Latent, LatentInfo = "LatentInfo", ExpandEnumAsExecs = "Out", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"))
|
|
static void CreateAndExecuteTaskLoopNrOnThread(UObject* WorldContextObject, FLatentActionInfo LatentInfo, int LoopNumber, float LoopInterval, EtaskExecutionBranches& Out);
|
|
|
|
/**
|
|
* Attempts to destroy a Thread Pool immediately.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Meta = (DisplayName = "Destroy Thread Pool Immediately"), Category = "MultiThreadLibrary")
|
|
static void DestroyThreadPoolImmediately(UThreadPool* ThreadPool);
|
|
|
|
/**
|
|
*Return Thread name of thread from where called this function.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Meta = (DisplayName = "Get Current Thread Name"), Category = "MultiThreadLibrary")
|
|
static FString GetCurrentThreadName(int32 threadID);
|
|
|
|
/**
|
|
*Return Thread ID of thread from where called this function.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Meta = (DisplayName = "Get Current Thread ID"), Category = "MultiThreadLibrary")
|
|
static int32 GetCurrentThreadID();
|
|
|
|
/**
|
|
*leep the Thread for any time in seconds.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Category = "MultiThreadLibrary")
|
|
static void Sleep(float Seconds);
|
|
|
|
/**
|
|
*Get the thread stack Size, where are actived this function.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "MultiThreadLibrary")
|
|
static int32 GetThreadStackSize();
|
|
|
|
/**
|
|
* Return the number of CPU cores used by Thread.
|
|
*/
|
|
UFUNCTION(BlueprintPure, Category = "MultiThreadLibrary")
|
|
static int32 GetNumberOfCores();
|
|
|
|
/**
|
|
*Return the number of CPU cores including Hyperthreads.
|
|
*/
|
|
UFUNCTION(BlueprintPure, Category = "MultiThreadLibrary")
|
|
static int32 GetNumberOfCoresIncludingHyperthreads();
|
|
|
|
/**
|
|
*Return whether it's called from game thread or not.
|
|
*/
|
|
UFUNCTION(BlueprintPure, Category = "MultiThreadLibrary")
|
|
static bool IsInGameThread();
|
|
|
|
|
|
|
|
|
|
/**
|
|
*Create Thread pool for use on BP.
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Meta = (HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "MultiThreadLibrary")
|
|
static UThreadPool* CreateThreadPool(UObject* WorldContextObject, int32 NumQueuedThreads = 1, int32 StackSize = 32768, EThreadPoolPriority ThreadPriority = EThreadPoolPriority::Normal, FString Name = "CustomThreadPool");
|
|
|
|
/**
|
|
*Create the Mutex for controling the Thread (Lock/TryLock or UnLock)
|
|
*/
|
|
UFUNCTION(BlueprintCallable, Meta = (HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "MultiThreadLibrary")
|
|
static UThreadMutex* CreateMutex(UObject* WorldContextObject);
|
|
|
|
|
|
|
|
|
|
|
|
};
|