thread_work_pool.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* thread_work_pool.hpp */
  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 GODOT_THREAD_WORK_POOL_HPP
  31. #define GODOT_THREAD_WORK_POOL_HPP
  32. #include <godot_cpp/classes/os.hpp>
  33. #include <godot_cpp/classes/semaphore.hpp>
  34. #include <godot_cpp/core/error_macros.hpp>
  35. #include <godot_cpp/core/memory.hpp>
  36. #include <thread>
  37. #include <atomic>
  38. namespace godot {
  39. class ThreadWorkPool {
  40. std::atomic<uint32_t> index;
  41. struct BaseWork {
  42. std::atomic<uint32_t> *index = nullptr;
  43. uint32_t max_elements = 0;
  44. virtual void work() = 0;
  45. virtual ~BaseWork() = default;
  46. };
  47. template <typename C, typename M, typename U>
  48. struct Work : public BaseWork {
  49. C *instance;
  50. M method;
  51. U userdata;
  52. virtual void work() {
  53. while (true) {
  54. uint32_t work_index = index->fetch_add(1, std::memory_order_relaxed);
  55. if (work_index >= max_elements) {
  56. break;
  57. }
  58. (instance->*method)(work_index, userdata);
  59. }
  60. }
  61. };
  62. struct ThreadData {
  63. std::thread thread;
  64. Semaphore start;
  65. Semaphore completed;
  66. std::atomic<bool> exit;
  67. BaseWork *work;
  68. };
  69. ThreadData *threads = nullptr;
  70. uint32_t thread_count = 0;
  71. uint32_t threads_working = 0;
  72. BaseWork *current_work = nullptr;
  73. static void _thread_function(void *p_user) {
  74. ThreadData *thread = static_cast<ThreadData *>(p_user);
  75. while (true) {
  76. thread->start.wait();
  77. if (thread->exit.load()) {
  78. break;
  79. }
  80. thread->work->work();
  81. thread->completed.post();
  82. }
  83. }
  84. public:
  85. template <typename C, typename M, typename U>
  86. void begin_work(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  87. ERR_FAIL_NULL(threads); // Never initialized.
  88. ERR_FAIL_COND(current_work != nullptr);
  89. index.store(0, std::memory_order_release);
  90. Work<C, M, U> *w = new (Work<C, M, U>);
  91. w->instance = p_instance;
  92. w->userdata = p_userdata;
  93. w->method = p_method;
  94. w->index = &index;
  95. w->max_elements = p_elements;
  96. current_work = w;
  97. threads_working = Math::min(p_elements, thread_count);
  98. for (uint32_t i = 0; i < threads_working; i++) {
  99. threads[i].work = w;
  100. threads[i].start.post();
  101. }
  102. }
  103. bool is_working() const {
  104. return current_work != nullptr;
  105. }
  106. bool is_done_dispatching() const {
  107. ERR_FAIL_NULL_V(current_work, true);
  108. return index.load(std::memory_order_acquire) >= current_work->max_elements;
  109. }
  110. uint32_t get_work_index() const {
  111. ERR_FAIL_NULL_V(current_work, 0);
  112. uint32_t idx = index.load(std::memory_order_acquire);
  113. return Math::min(idx, current_work->max_elements);
  114. }
  115. void end_work() {
  116. ERR_FAIL_NULL(current_work);
  117. for (uint32_t i = 0; i < threads_working; i++) {
  118. threads[i].completed.wait();
  119. threads[i].work = nullptr;
  120. }
  121. threads_working = 0;
  122. delete current_work;
  123. current_work = nullptr;
  124. }
  125. template <typename C, typename M, typename U>
  126. void do_work(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  127. switch (p_elements) {
  128. case 0:
  129. // Nothing to do, so do nothing.
  130. break;
  131. case 1:
  132. // No value in pushing the work to another thread if it's a single job
  133. // and we're going to wait for it to finish. Just run it right here.
  134. (p_instance->*p_method)(0, p_userdata);
  135. break;
  136. default:
  137. // Multiple jobs to do; commence threaded business.
  138. begin_work(p_elements, p_instance, p_method, p_userdata);
  139. end_work();
  140. }
  141. }
  142. _FORCE_INLINE_ int get_thread_count() const { return thread_count; }
  143. void init(int p_thread_count = -1) {
  144. ERR_FAIL_COND(threads != nullptr);
  145. if (p_thread_count < 0) {
  146. p_thread_count = OS::get_singleton()->get_processor_count();
  147. }
  148. thread_count = p_thread_count;
  149. threads = new ThreadData[thread_count];
  150. for (uint32_t i = 0; i < thread_count; i++) {
  151. threads[i].exit.store(false);
  152. threads[i].thread = std::thread(&ThreadWorkPool::_thread_function, &threads[i]);
  153. }
  154. }
  155. void finish() {
  156. if (threads == nullptr) {
  157. return;
  158. }
  159. for (uint32_t i = 0; i < thread_count; i++) {
  160. threads[i].exit.store(true);
  161. threads[i].start.post();
  162. }
  163. for (uint32_t i = 0; i < thread_count; i++) {
  164. threads[i].thread.join();
  165. }
  166. delete[] (threads);
  167. threads = nullptr;
  168. }
  169. ~ThreadWorkPool() {
  170. finish();
  171. }
  172. };
  173. } // namespace godot
  174. #endif // GODOT_THREAD_WORK_POOL_HPP