A modded EditSaber for making beat saber maps.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
3.0 KiB

  1. // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
  2. #pragma once
  3. /*
  4. Long duration lambda wrapper, which are generally not supported by the taskgraph system. New thread per lambda and they will auto-delete upon
  5. completion.
  6. */
  7. class WINDOWSFILEUTILITY_API WFULambdaRunnable : public FRunnable
  8. {
  9. private:
  10. /** Thread to run the worker FRunnable on */
  11. FRunnableThread* Thread;
  12. // Used to give each thread a unique stat group
  13. static uint64 ThreadNumber;
  14. //Lambda function pointer
  15. TFunction< void()> FunctionPointer;
  16. // A queued threadpool used to run lambda's in the background. This has lazy initialization and is meant to be used with
  17. // - AddLambdaToQueue
  18. // - RemoveLAmbdaFromQueue
  19. static FQueuedThreadPool* ThreadPool;
  20. public:
  21. //Constructor / Destructor
  22. WFULambdaRunnable(TFunction< void()> InFunction);
  23. virtual ~WFULambdaRunnable();
  24. // Begin FRunnable interface.
  25. virtual uint32 Run() override;
  26. virtual void Exit() override;
  27. // End FRunnable interface
  28. // Initializes the queued thread pool. This is called lazily when the first task is added to the queue
  29. // but can also be called by hand to initialize with a specific number of threads. The default number
  30. // of threads is FPlatformMisc::NumberOfIOWorkerThreadsToSpawn() which last I checked was hard-coded
  31. // at 4. <NOTE> that if you want to call this by hand, you need to do so before ever calling AddLambdaToQueue.
  32. static void InitThreadPool(int32 NumberOfThreads);
  33. /** Makes sure this thread has stopped properly */
  34. void EnsureCompletion();
  35. // Runs the passed lambda on the background thread, new thread per call
  36. static WFULambdaRunnable* RunLambdaOnBackGroundThread(TFunction< void()> InFunction);
  37. // Adds a lambda to be ran on the queued thread pool. Returns a pointer to IQueuedWork which
  38. // can be used to later remove the queued job from the pool assuming it hasn't been processed.
  39. static IQueuedWork* AddLambdaToQueue(TFunction< void()> InFunction);
  40. // Removes a lambda from the thread queue
  41. static bool RemoveLambdaFromQueue(IQueuedWork* Work);
  42. // Runs a short lambda on the game thread via task graph system
  43. static FGraphEventRef RunShortLambdaOnGameThread(TFunction< void()> InFunction);
  44. private:
  45. // This was yanked from Engine/Source/Runtime/Core/Public/Async/Async.h (originally called AsyncPool(..)). FQueuedThreadPool doesn't have
  46. // much documentation, so using the engine code as reference, pretty much everyone seems to use this templated function to queue up work.
  47. // It was modified to return an IQueuedWork instead of a TFuture to be more convenient for actually removing items from the queue.
  48. template<typename ResultType>
  49. static IQueuedWork* AsyncLambdaPool(FQueuedThreadPool& ThreadPool, TFunction<ResultType()> Function, TFunction<void()> CompletionCallback = TFunction<void()>())
  50. {
  51. TPromise<ResultType> Promise(MoveTemp(CompletionCallback));
  52. TFuture<ResultType> Future = Promise.GetFuture();
  53. IQueuedWork* Work = new TAsyncQueuedWork<ResultType>(MoveTemp(Function), MoveTemp(Promise));
  54. ThreadPool.AddQueuedWork(Work);
  55. return Work;
  56. }
  57. };