worker_thread_pool.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**************************************************************************/
  2. /* worker_thread_pool.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef WORKER_THREAD_POOL_H
  31. #define WORKER_THREAD_POOL_H
  32. #include "core/os/memory.h"
  33. #include "core/os/os.h"
  34. #include "core/os/semaphore.h"
  35. #include "core/os/thread.h"
  36. #include "core/templates/local_vector.h"
  37. #include "core/templates/paged_allocator.h"
  38. #include "core/templates/rid.h"
  39. #include "core/templates/safe_refcount.h"
  40. class WorkerThreadPool : public Object {
  41. GDCLASS(WorkerThreadPool, Object)
  42. public:
  43. enum {
  44. INVALID_TASK_ID = -1
  45. };
  46. typedef int64_t TaskID;
  47. typedef int64_t GroupID;
  48. private:
  49. struct Task;
  50. struct BaseTemplateUserdata {
  51. virtual void callback() {}
  52. virtual void callback_indexed(uint32_t p_index) {}
  53. virtual ~BaseTemplateUserdata() {}
  54. };
  55. struct Group {
  56. GroupID self;
  57. SafeNumeric<uint32_t> index;
  58. SafeNumeric<uint32_t> completed_index;
  59. uint32_t max = 0;
  60. Semaphore done_semaphore;
  61. SafeFlag completed;
  62. SafeNumeric<uint32_t> finished;
  63. uint32_t tasks_used = 0;
  64. TightLocalVector<Task *> low_priority_native_tasks;
  65. };
  66. struct Task {
  67. Callable callable;
  68. void (*native_func)(void *) = nullptr;
  69. void (*native_group_func)(void *, uint32_t) = nullptr;
  70. void *native_func_userdata = nullptr;
  71. String description;
  72. Semaphore done_semaphore;
  73. bool completed = false;
  74. Group *group = nullptr;
  75. SelfList<Task> task_elem;
  76. uint32_t waiting = 0;
  77. bool low_priority = false;
  78. BaseTemplateUserdata *template_userdata = nullptr;
  79. Thread *low_priority_thread = nullptr;
  80. int pool_thread_index = -1;
  81. void free_template_userdata();
  82. Task() :
  83. task_elem(this) {}
  84. };
  85. PagedAllocator<Task> task_allocator;
  86. PagedAllocator<Group> group_allocator;
  87. PagedAllocator<Thread> native_thread_allocator;
  88. SelfList<Task>::List low_priority_task_queue;
  89. SelfList<Task>::List task_queue;
  90. Mutex task_mutex;
  91. Semaphore task_available_semaphore;
  92. struct ThreadData {
  93. uint32_t index;
  94. Thread thread;
  95. Task *current_low_prio_task = nullptr;
  96. };
  97. TightLocalVector<ThreadData> threads;
  98. bool exit_threads = false;
  99. HashMap<Thread::ID, int> thread_ids;
  100. HashMap<TaskID, Task *> tasks;
  101. HashMap<GroupID, Group *> groups;
  102. bool use_native_low_priority_threads = false;
  103. uint32_t max_low_priority_threads = 0;
  104. uint32_t low_priority_threads_used = 0;
  105. uint32_t low_priority_tasks_running = 0;
  106. uint32_t low_priority_tasks_awaiting_others = 0;
  107. uint64_t last_task = 1;
  108. static void _thread_function(void *p_user);
  109. static void _native_low_priority_thread_function(void *p_user);
  110. void _process_task_queue();
  111. void _process_task(Task *task);
  112. void _post_task(Task *p_task, bool p_high_priority);
  113. bool _try_promote_low_priority_task();
  114. void _prevent_low_prio_saturation_deadlock();
  115. static WorkerThreadPool *singleton;
  116. TaskID _add_task(const Callable &p_callable, void (*p_func)(void *), void *p_userdata, BaseTemplateUserdata *p_template_userdata, bool p_high_priority, const String &p_description);
  117. GroupID _add_group_task(const Callable &p_callable, void (*p_func)(void *, uint32_t), void *p_userdata, BaseTemplateUserdata *p_template_userdata, int p_elements, int p_tasks, bool p_high_priority, const String &p_description);
  118. template <class C, class M, class U>
  119. struct TaskUserData : public BaseTemplateUserdata {
  120. C *instance;
  121. M method;
  122. U userdata;
  123. virtual void callback() override {
  124. (instance->*method)(userdata);
  125. }
  126. };
  127. template <class C, class M, class U>
  128. struct GroupUserData : public BaseTemplateUserdata {
  129. C *instance;
  130. M method;
  131. U userdata;
  132. virtual void callback_indexed(uint32_t p_index) override {
  133. (instance->*method)(p_index, userdata);
  134. }
  135. };
  136. protected:
  137. static void _bind_methods();
  138. public:
  139. template <class C, class M, class U>
  140. TaskID add_template_task(C *p_instance, M p_method, U p_userdata, bool p_high_priority = false, const String &p_description = String()) {
  141. typedef TaskUserData<C, M, U> TUD;
  142. TUD *ud = memnew(TUD);
  143. ud->instance = p_instance;
  144. ud->method = p_method;
  145. ud->userdata = p_userdata;
  146. return _add_task(Callable(), nullptr, nullptr, ud, p_high_priority, p_description);
  147. }
  148. TaskID add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority = false, const String &p_description = String());
  149. TaskID add_task(const Callable &p_action, bool p_high_priority = false, const String &p_description = String());
  150. bool is_task_completed(TaskID p_task_id) const;
  151. Error wait_for_task_completion(TaskID p_task_id);
  152. template <class C, class M, class U>
  153. GroupID add_template_group_task(C *p_instance, M p_method, U p_userdata, int p_elements, int p_tasks = -1, bool p_high_priority = false, const String &p_description = String()) {
  154. typedef GroupUserData<C, M, U> GroupUD;
  155. GroupUD *ud = memnew(GroupUD);
  156. ud->instance = p_instance;
  157. ud->method = p_method;
  158. ud->userdata = p_userdata;
  159. return _add_group_task(Callable(), nullptr, nullptr, ud, p_elements, p_tasks, p_high_priority, p_description);
  160. }
  161. GroupID add_native_group_task(void (*p_func)(void *, uint32_t), void *p_userdata, int p_elements, int p_tasks = -1, bool p_high_priority = false, const String &p_description = String());
  162. GroupID add_group_task(const Callable &p_action, int p_elements, int p_tasks = -1, bool p_high_priority = false, const String &p_description = String());
  163. uint32_t get_group_processed_element_count(GroupID p_group) const;
  164. bool is_group_task_completed(GroupID p_group) const;
  165. void wait_for_group_task_completion(GroupID p_group);
  166. _FORCE_INLINE_ int get_thread_count() const { return threads.size(); }
  167. static WorkerThreadPool *get_singleton() { return singleton; }
  168. void init(int p_thread_count = -1, bool p_use_native_threads_low_priority = true, float p_low_priority_task_ratio = 0.3);
  169. void finish();
  170. WorkerThreadPool();
  171. ~WorkerThreadPool();
  172. };
  173. #endif // WORKER_THREAD_POOL_H