BsTaskScheduler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsModule.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Task priority. Tasks with higher priority will get executed sooner.
  8. */
  9. enum class TaskPriority
  10. {
  11. VeryLow = 98,
  12. Low = 99,
  13. Normal = 100,
  14. High = 101,
  15. VeryHigh = 102
  16. };
  17. /**
  18. * @brief Represents a single task that may be queued in the TaskScheduler.
  19. *
  20. * @note Thread safe.
  21. */
  22. class BS_UTILITY_EXPORT Task
  23. {
  24. struct PrivatelyConstruct {};
  25. public:
  26. Task(const PrivatelyConstruct& dummy, const String& name, std::function<void()> taskWorker,
  27. TaskPriority priority, TaskPtr dependency);
  28. /**
  29. * @brief Creates a new task. Task should be provided to TaskScheduler in order for it
  30. * to start.
  31. *
  32. * @param name Name you can use to more easily identify the task.
  33. * @param taskWorker Worker method that does all of the work in the task.
  34. * @param priority (optional) Higher priority means the tasks will be executed sooner.
  35. * @param dependency (optional) Task dependency if one exists. If provided the task will
  36. * not be executed until its dependency is complete.
  37. */
  38. static TaskPtr create(const String& name, std::function<void()> taskWorker, TaskPriority priority = TaskPriority::Normal,
  39. TaskPtr dependency = nullptr);
  40. /**
  41. * @brief Returns true if the task has completed.
  42. */
  43. bool isComplete() const;
  44. /**
  45. * @brief Returns true if the task has been canceled.
  46. */
  47. bool isCanceled() const;
  48. /**
  49. * @brief Blocks the current thread until the task has completed.
  50. *
  51. * @note While waiting adds a new worker thread, so that the blocking threads core can be utilized.
  52. */
  53. void wait();
  54. /**
  55. * @brief Cancels the task and removes it from the TaskSchedulers queue.
  56. */
  57. void cancel();
  58. private:
  59. friend class TaskScheduler;
  60. String mName;
  61. TaskPriority mPriority;
  62. UINT32 mTaskId;
  63. std::function<void()> mTaskWorker;
  64. TaskPtr mTaskDependency;
  65. std::atomic<UINT32> mState; /**< 0 - Inactive, 1 - In progress, 2 - Completed, 3 - Canceled */
  66. TaskScheduler* mParent;
  67. };
  68. /**
  69. * @brief Represents a task scheduler running on multiple threads. You may queue
  70. * tasks on it from any thread and they will be executed in user specified order
  71. * on any available thread.
  72. *
  73. * @note Thread safe.
  74. *
  75. * This type of task scheduler uses a global queue and is best used for coarse granularity of tasks.
  76. * (Number of tasks in the order of hundreds. Higher number of tasks might require different queuing and
  77. * locking mechanism, potentially at the cost of flexibility.)
  78. *
  79. * By default the task scheduler will create as many threads as there are physical CPU cores. You may add or remove
  80. * threads using addWorker/removeWorker methods.
  81. */
  82. class BS_UTILITY_EXPORT TaskScheduler : public Module<TaskScheduler>
  83. {
  84. public:
  85. TaskScheduler();
  86. ~TaskScheduler();
  87. /**
  88. * @brief Queues a new task.
  89. */
  90. void addTask(const TaskPtr& task);
  91. /**
  92. * @brief Adds a new worker thread which will be used for executing queued tasks.
  93. */
  94. void addWorker();
  95. /**
  96. * @brief Removes a worker thread (as soon as its current task is finished).
  97. */
  98. void removeWorker();
  99. protected:
  100. friend class Task;
  101. /**
  102. * @brief Main task scheduler method that dispatches tasks to other threads.
  103. */
  104. void runMain();
  105. /**
  106. * @brief Worker method that runs a single task.
  107. */
  108. void runTask(const TaskPtr& task);
  109. /**
  110. * @brief Blocks the calling thread until the specified task has completed.
  111. */
  112. void waitUntilComplete(const Task* task);
  113. /**
  114. * @brief Method used for sorting tasks.
  115. */
  116. static bool taskCompare(const TaskPtr& lhs, const TaskPtr& rhs);
  117. Set<TaskPtr, std::function<bool(const TaskPtr&, const TaskPtr&)>> mTaskQueue;
  118. UINT32 mNumActiveTasks;
  119. UINT32 mMaxActiveTasks;
  120. UINT32 mNextTaskId;
  121. bool mShutdown;
  122. BS_MUTEX(mReadyMutex);
  123. BS_MUTEX(mCompleteMutex);
  124. BS_THREAD_SYNCHRONISER(mTaskReadyCond);
  125. BS_THREAD_SYNCHRONISER(mTaskCompleteCond);
  126. };
  127. }