thread_work_pool.hpp 6.1 KB

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