thread_work_pool.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* thread_work_pool.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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() {
  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;
  65. };
  66. ThreadData *threads = nullptr;
  67. uint32_t thread_count = 0;
  68. BaseWork *current_work = nullptr;
  69. static void _thread_function(void *p_user);
  70. public:
  71. template <class C, class M, class U>
  72. void begin_work(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  73. ERR_FAIL_COND(!threads); //never initialized
  74. ERR_FAIL_COND(current_work != nullptr);
  75. index.store(0);
  76. Work<C, M, U> *w = memnew((Work<C, M, U>));
  77. w->instance = p_instance;
  78. w->userdata = p_userdata;
  79. w->method = p_method;
  80. w->index = &index;
  81. w->max_elements = p_elements;
  82. current_work = w;
  83. for (uint32_t i = 0; i < thread_count; i++) {
  84. threads[i].work = w;
  85. threads[i].start.post();
  86. }
  87. }
  88. bool is_working() const {
  89. return current_work != nullptr;
  90. }
  91. uint32_t get_work_index() const {
  92. return index;
  93. }
  94. void end_work() {
  95. ERR_FAIL_COND(current_work == nullptr);
  96. for (uint32_t i = 0; i < thread_count; i++) {
  97. threads[i].completed.wait();
  98. threads[i].work = nullptr;
  99. }
  100. memdelete(current_work);
  101. current_work = nullptr;
  102. }
  103. template <class C, class M, class U>
  104. void do_work(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  105. begin_work(p_elements, p_instance, p_method, p_userdata);
  106. end_work();
  107. }
  108. _FORCE_INLINE_ int get_thread_count() const { return thread_count; }
  109. void init(int p_thread_count = -1);
  110. void finish();
  111. ~ThreadWorkPool();
  112. };
  113. #endif // THREAD_POOL_H