worker_thread_pool.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. bool waiting = false; // Waiting for completion
  77. bool low_priority = false;
  78. BaseTemplateUserdata *template_userdata = nullptr;
  79. Thread *low_priority_thread = nullptr;
  80. void free_template_userdata();
  81. Task() :
  82. task_elem(this) {}
  83. };
  84. PagedAllocator<Task> task_allocator;
  85. PagedAllocator<Group> group_allocator;
  86. PagedAllocator<Thread> native_thread_allocator;
  87. SelfList<Task>::List low_priority_task_queue;
  88. SelfList<Task>::List task_queue;
  89. Mutex task_mutex;
  90. Semaphore task_available_semaphore;
  91. struct ThreadData {
  92. uint32_t index;
  93. Thread thread;
  94. };
  95. TightLocalVector<ThreadData> threads;
  96. SafeFlag exit_threads;
  97. HashMap<Thread::ID, int> thread_ids;
  98. HashMap<TaskID, Task *> tasks;
  99. HashMap<GroupID, Group *> groups;
  100. bool use_native_low_priority_threads = false;
  101. uint32_t max_low_priority_threads = 0;
  102. SafeNumeric<uint32_t> low_priority_threads_used;
  103. uint64_t last_task = 1;
  104. static void _thread_function(void *p_user);
  105. static void _native_low_priority_thread_function(void *p_user);
  106. void _process_task_queue();
  107. void _process_task(Task *task);
  108. void _post_task(Task *p_task, bool p_high_priority);
  109. static WorkerThreadPool *singleton;
  110. 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);
  111. 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);
  112. template <class C, class M, class U>
  113. struct TaskUserData : public BaseTemplateUserdata {
  114. C *instance;
  115. M method;
  116. U userdata;
  117. virtual void callback() override {
  118. (instance->*method)(userdata);
  119. }
  120. };
  121. template <class C, class M, class U>
  122. struct GroupUserData : public BaseTemplateUserdata {
  123. C *instance;
  124. M method;
  125. U userdata;
  126. virtual void callback_indexed(uint32_t p_index) override {
  127. (instance->*method)(p_index, userdata);
  128. }
  129. };
  130. protected:
  131. static void _bind_methods();
  132. public:
  133. template <class C, class M, class U>
  134. TaskID add_template_task(C *p_instance, M p_method, U p_userdata, bool p_high_priority = false, const String &p_description = String()) {
  135. typedef TaskUserData<C, M, U> TUD;
  136. TUD *ud = memnew(TUD);
  137. ud->instance = p_instance;
  138. ud->method = p_method;
  139. ud->userdata = p_userdata;
  140. return _add_task(Callable(), nullptr, nullptr, ud, p_high_priority, p_description);
  141. }
  142. TaskID add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority = false, const String &p_description = String());
  143. TaskID add_task(const Callable &p_action, bool p_high_priority = false, const String &p_description = String());
  144. bool is_task_completed(TaskID p_task_id) const;
  145. void wait_for_task_completion(TaskID p_task_id);
  146. template <class C, class M, class U>
  147. 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()) {
  148. typedef GroupUserData<C, M, U> GroupUD;
  149. GroupUD *ud = memnew(GroupUD);
  150. ud->instance = p_instance;
  151. ud->method = p_method;
  152. ud->userdata = p_userdata;
  153. return _add_group_task(Callable(), nullptr, nullptr, ud, p_elements, p_tasks, p_high_priority, p_description);
  154. }
  155. 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());
  156. 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());
  157. uint32_t get_group_processed_element_count(GroupID p_group) const;
  158. bool is_group_task_completed(GroupID p_group) const;
  159. void wait_for_group_task_completion(GroupID p_group);
  160. _FORCE_INLINE_ int get_thread_count() const { return threads.size(); }
  161. static WorkerThreadPool *get_singleton() { return singleton; }
  162. void init(int p_thread_count = -1, bool p_use_native_threads_low_priority = true, float p_low_priority_task_ratio = 0.3);
  163. void finish();
  164. WorkerThreadPool();
  165. ~WorkerThreadPool();
  166. };
  167. #endif // WORKER_THREAD_POOL_H