thread_work_pool.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**************************************************************************/
  2. /* thread_work_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 THREAD_WORK_POOL_H
  31. #define THREAD_WORK_POOL_H
  32. #include "core/os/memory.h"
  33. #include "core/os/semaphore.h"
  34. #include "core/os/thread.h"
  35. #include <atomic>
  36. class ThreadWorkPool {
  37. std::atomic<uint32_t> index;
  38. struct BaseWork {
  39. std::atomic<uint32_t> *index = nullptr;
  40. uint32_t max_elements = 0;
  41. virtual void work() = 0;
  42. virtual ~BaseWork() = default;
  43. };
  44. template <class C, class M, class U>
  45. struct Work : public BaseWork {
  46. C *instance;
  47. M method;
  48. U userdata;
  49. virtual void work() override {
  50. while (true) {
  51. uint32_t work_index = index->fetch_add(1, std::memory_order_relaxed);
  52. if (work_index >= max_elements) {
  53. break;
  54. }
  55. (instance->*method)(work_index, userdata);
  56. }
  57. }
  58. };
  59. struct ThreadData {
  60. Thread thread;
  61. Semaphore start;
  62. Semaphore completed;
  63. std::atomic<bool> exit;
  64. BaseWork *work = nullptr;
  65. };
  66. ThreadData *threads = nullptr;
  67. uint32_t thread_count = 0;
  68. uint32_t threads_working = 0;
  69. BaseWork *current_work = nullptr;
  70. static void _thread_function(void *p_user);
  71. public:
  72. template <class C, class M, class U>
  73. void begin_work(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  74. ERR_FAIL_COND(!threads); //never initialized
  75. ERR_FAIL_COND(current_work != nullptr);
  76. index.store(0, std::memory_order_release);
  77. Work<C, M, U> *w = memnew((Work<C, M, U>));
  78. w->instance = p_instance;
  79. w->userdata = p_userdata;
  80. w->method = p_method;
  81. w->index = &index;
  82. w->max_elements = p_elements;
  83. current_work = w;
  84. threads_working = MIN(p_elements, thread_count);
  85. for (uint32_t i = 0; i < threads_working; i++) {
  86. threads[i].work = w;
  87. threads[i].start.post();
  88. }
  89. }
  90. bool is_working() const {
  91. return current_work != nullptr;
  92. }
  93. bool is_done_dispatching() const {
  94. ERR_FAIL_COND_V(current_work == nullptr, true);
  95. return index.load(std::memory_order_acquire) >= current_work->max_elements;
  96. }
  97. uint32_t get_work_index() const {
  98. ERR_FAIL_COND_V(current_work == nullptr, 0);
  99. uint32_t idx = index.load(std::memory_order_acquire);
  100. return MIN(idx, current_work->max_elements);
  101. }
  102. void end_work() {
  103. ERR_FAIL_COND(current_work == nullptr);
  104. for (uint32_t i = 0; i < threads_working; i++) {
  105. threads[i].completed.wait();
  106. threads[i].work = nullptr;
  107. }
  108. threads_working = 0;
  109. memdelete(current_work);
  110. current_work = nullptr;
  111. }
  112. template <class C, class M, class U>
  113. void do_work(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  114. switch (p_elements) {
  115. case 0:
  116. // Nothing to do, so do nothing.
  117. break;
  118. case 1:
  119. // No value in pushing the work to another thread if it's a single job
  120. // and we're going to wait for it to finish. Just run it right here.
  121. (p_instance->*p_method)(0, p_userdata);
  122. break;
  123. default:
  124. // Multiple jobs to do; commence threaded business.
  125. begin_work(p_elements, p_instance, p_method, p_userdata);
  126. end_work();
  127. }
  128. }
  129. _FORCE_INLINE_ int get_thread_count() const { return thread_count; }
  130. void init(int p_thread_count = -1);
  131. void finish();
  132. ~ThreadWorkPool();
  133. };
  134. #endif // THREAD_WORK_POOL_H