CULambdaRunnable.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2018-current Getnamo. All Rights Reserved
  2. #pragma once
  3. #include "Engine/LatentActionManager.h"
  4. #include "LatentActions.h"
  5. #include "Async/TaskGraphInterfaces.h"
  6. /** A simple latent action where we don't hold the value, expect capturing value in lambdas */
  7. class COREUTILITY_API FCULatentAction : public FPendingLatentAction
  8. {
  9. public:
  10. TFunction<void()> OnCancelNotification = nullptr;
  11. /** Note that you need to use the resulting call to cleanly exit */
  12. static FCULatentAction* CreateLatentAction(struct FLatentActionInfo& LatentInfo, UObject* WorldContext);
  13. FCULatentAction(const FLatentActionInfo& LatentInfo);
  14. virtual void UpdateOperation(FLatentResponse& Response) override;
  15. void Call();
  16. void Cancel();
  17. virtual void NotifyObjectDestroyed() override;
  18. virtual void NotifyActionAborted() override;
  19. #if WITH_EDITOR
  20. virtual FString GetDescription() const override;
  21. #endif
  22. const FName ExecutionFunction;
  23. const int32 OutputLink;
  24. const FWeakObjectPtr CallbackTarget;
  25. private:
  26. bool Called;
  27. };
  28. /**
  29. * Convenience wrappers for common thread/task work flow. Run background task on thread, callback via task graph on game thread
  30. */
  31. class COREUTILITY_API FCULambdaRunnable
  32. {
  33. public:
  34. /**
  35. * Runs the passed lambda on the background thread, new thread per call
  36. */
  37. static void RunLambdaOnBackGroundThread(TFunction< void()> InFunction);
  38. /**
  39. * Runs the passed lambda on the background thread pool
  40. */
  41. static void RunLambdaOnBackGroundThreadPool(TFunction< void()> InFunction);
  42. /**
  43. * Runs a short lambda on the game thread via task graph system
  44. */
  45. static FGraphEventRef RunShortLambdaOnGameThread(TFunction< void()> InFunction);
  46. /**
  47. * Runs a short lambda on background thread via task graph system
  48. */
  49. static FGraphEventRef RunShortLambdaOnBackGroundTask(TFunction< void()> InFunction);
  50. /**
  51. * Runs a thread with idle for duration before calling back on game thread.
  52. * Due to context cost recommended for >0.1sec durations.
  53. */
  54. static void SetTimeout(TFunction<void()>OnDone, float DurationInSec, bool bCallbackOnGameThread = true);
  55. };