worker_thread_pool.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /**************************************************************************/
  2. /* worker_thread_pool.cpp */
  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. #include "worker_thread_pool.h"
  31. #include "core/object/script_language.h"
  32. #include "core/os/os.h"
  33. #include "core/os/thread_safe.h"
  34. void WorkerThreadPool::Task::free_template_userdata() {
  35. ERR_FAIL_NULL(template_userdata);
  36. ERR_FAIL_NULL(native_func_userdata);
  37. BaseTemplateUserdata *btu = (BaseTemplateUserdata *)native_func_userdata;
  38. memdelete(btu);
  39. }
  40. WorkerThreadPool *WorkerThreadPool::singleton = nullptr;
  41. void WorkerThreadPool::_process_task_queue() {
  42. task_mutex.lock();
  43. Task *task = task_queue.first()->self();
  44. task_queue.remove(task_queue.first());
  45. task_mutex.unlock();
  46. _process_task(task);
  47. }
  48. void WorkerThreadPool::_process_task(Task *p_task) {
  49. bool low_priority = p_task->low_priority;
  50. int pool_thread_index = -1;
  51. Task *prev_low_prio_task = nullptr; // In case this is recursively called.
  52. if (!use_native_low_priority_threads) {
  53. // Tasks must start with this unset. They are free to set-and-forget otherwise.
  54. set_current_thread_safe_for_nodes(false);
  55. pool_thread_index = thread_ids[Thread::get_caller_id()];
  56. ThreadData &curr_thread = threads[pool_thread_index];
  57. // Since the WorkerThreadPool is started before the script server,
  58. // its pre-created threads can't have ScriptServer::thread_enter() called on them early.
  59. // Therefore, we do it late at the first opportunity, so in case the task
  60. // about to be run uses scripting, guarantees are held.
  61. if (!curr_thread.ready_for_scripting && ScriptServer::are_languages_initialized()) {
  62. ScriptServer::thread_enter();
  63. curr_thread.ready_for_scripting = true;
  64. }
  65. task_mutex.lock();
  66. p_task->pool_thread_index = pool_thread_index;
  67. if (low_priority) {
  68. low_priority_tasks_running++;
  69. prev_low_prio_task = curr_thread.current_low_prio_task;
  70. curr_thread.current_low_prio_task = p_task;
  71. } else {
  72. curr_thread.current_low_prio_task = nullptr;
  73. }
  74. task_mutex.unlock();
  75. }
  76. if (p_task->group) {
  77. // Handling a group
  78. bool do_post = false;
  79. while (true) {
  80. uint32_t work_index = p_task->group->index.postincrement();
  81. if (work_index >= p_task->group->max) {
  82. break;
  83. }
  84. if (p_task->native_group_func) {
  85. p_task->native_group_func(p_task->native_func_userdata, work_index);
  86. } else if (p_task->template_userdata) {
  87. p_task->template_userdata->callback_indexed(work_index);
  88. } else {
  89. p_task->callable.call(work_index);
  90. }
  91. // This is the only way to ensure posting is done when all tasks are really complete.
  92. uint32_t completed_amount = p_task->group->completed_index.increment();
  93. if (completed_amount == p_task->group->max) {
  94. do_post = true;
  95. }
  96. }
  97. if (do_post && p_task->template_userdata) {
  98. memdelete(p_task->template_userdata); // This is no longer needed at this point, so get rid of it.
  99. }
  100. if (low_priority && use_native_low_priority_threads) {
  101. p_task->completed = true;
  102. p_task->done_semaphore.post();
  103. if (do_post) {
  104. p_task->group->completed.set_to(true);
  105. }
  106. } else {
  107. if (do_post) {
  108. p_task->group->done_semaphore.post();
  109. p_task->group->completed.set_to(true);
  110. }
  111. uint32_t max_users = p_task->group->tasks_used + 1; // Add 1 because the thread waiting for it is also user. Read before to avoid another thread freeing task after increment.
  112. uint32_t finished_users = p_task->group->finished.increment();
  113. if (finished_users == max_users) {
  114. // Get rid of the group, because nobody else is using it.
  115. task_mutex.lock();
  116. group_allocator.free(p_task->group);
  117. task_mutex.unlock();
  118. }
  119. // For groups, tasks get rid of themselves.
  120. task_mutex.lock();
  121. task_allocator.free(p_task);
  122. task_mutex.unlock();
  123. }
  124. } else {
  125. if (p_task->native_func) {
  126. p_task->native_func(p_task->native_func_userdata);
  127. } else if (p_task->template_userdata) {
  128. p_task->template_userdata->callback();
  129. memdelete(p_task->template_userdata);
  130. } else {
  131. p_task->callable.call();
  132. }
  133. task_mutex.lock();
  134. p_task->completed = true;
  135. for (uint8_t i = 0; i < p_task->waiting; i++) {
  136. p_task->done_semaphore.post();
  137. }
  138. if (!use_native_low_priority_threads) {
  139. p_task->pool_thread_index = -1;
  140. }
  141. task_mutex.unlock(); // Keep mutex down to here since on unlock the task may be freed.
  142. }
  143. // Task may have been freed by now (all callers notified).
  144. p_task = nullptr;
  145. if (!use_native_low_priority_threads) {
  146. bool post = false;
  147. task_mutex.lock();
  148. ThreadData &curr_thread = threads[pool_thread_index];
  149. curr_thread.current_low_prio_task = prev_low_prio_task;
  150. if (low_priority) {
  151. low_priority_threads_used--;
  152. low_priority_tasks_running--;
  153. // A low prioriry task was freed, so see if we can move a pending one to the high priority queue.
  154. if (_try_promote_low_priority_task()) {
  155. post = true;
  156. }
  157. if (low_priority_tasks_awaiting_others == low_priority_tasks_running) {
  158. _prevent_low_prio_saturation_deadlock();
  159. }
  160. }
  161. task_mutex.unlock();
  162. if (post) {
  163. task_available_semaphore.post();
  164. }
  165. }
  166. }
  167. void WorkerThreadPool::_thread_function(void *p_user) {
  168. while (true) {
  169. singleton->task_available_semaphore.wait();
  170. if (singleton->exit_threads) {
  171. break;
  172. }
  173. singleton->_process_task_queue();
  174. }
  175. }
  176. void WorkerThreadPool::_native_low_priority_thread_function(void *p_user) {
  177. Task *task = (Task *)p_user;
  178. singleton->_process_task(task);
  179. }
  180. void WorkerThreadPool::_post_task(Task *p_task, bool p_high_priority) {
  181. // Fall back to processing on the calling thread if there are no worker threads.
  182. // Separated into its own variable to make it easier to extend this logic
  183. // in custom builds.
  184. bool process_on_calling_thread = threads.size() == 0;
  185. if (process_on_calling_thread) {
  186. _process_task(p_task);
  187. return;
  188. }
  189. task_mutex.lock();
  190. p_task->low_priority = !p_high_priority;
  191. if (!p_high_priority && use_native_low_priority_threads) {
  192. p_task->low_priority_thread = native_thread_allocator.alloc();
  193. task_mutex.unlock();
  194. if (p_task->group) {
  195. p_task->group->low_priority_native_tasks.push_back(p_task);
  196. }
  197. p_task->low_priority_thread->start(_native_low_priority_thread_function, p_task); // Pask task directly to thread.
  198. } else if (p_high_priority || low_priority_threads_used < max_low_priority_threads) {
  199. task_queue.add_last(&p_task->task_elem);
  200. if (!p_high_priority) {
  201. low_priority_threads_used++;
  202. }
  203. task_mutex.unlock();
  204. task_available_semaphore.post();
  205. } else {
  206. // Too many threads using low priority, must go to queue.
  207. low_priority_task_queue.add_last(&p_task->task_elem);
  208. task_mutex.unlock();
  209. }
  210. }
  211. bool WorkerThreadPool::_try_promote_low_priority_task() {
  212. if (low_priority_task_queue.first()) {
  213. Task *low_prio_task = low_priority_task_queue.first()->self();
  214. low_priority_task_queue.remove(low_priority_task_queue.first());
  215. task_queue.add_last(&low_prio_task->task_elem);
  216. low_priority_threads_used++;
  217. return true;
  218. } else {
  219. return false;
  220. }
  221. }
  222. void WorkerThreadPool::_prevent_low_prio_saturation_deadlock() {
  223. if (low_priority_tasks_awaiting_others == low_priority_tasks_running) {
  224. #ifdef DEV_ENABLED
  225. print_verbose("WorkerThreadPool: Low-prio slots saturated with tasks all waiting for other low-prio tasks. Attempting to avoid deadlock by scheduling one extra task.");
  226. #endif
  227. // In order not to create dependency cycles, we can only schedule the next one.
  228. // We'll keep doing the same until the deadlock is broken,
  229. SelfList<Task> *to_promote = low_priority_task_queue.first();
  230. if (to_promote) {
  231. low_priority_task_queue.remove(to_promote);
  232. task_queue.add_last(to_promote);
  233. low_priority_threads_used++;
  234. task_available_semaphore.post();
  235. }
  236. }
  237. }
  238. WorkerThreadPool::TaskID WorkerThreadPool::add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority, const String &p_description) {
  239. return _add_task(Callable(), p_func, p_userdata, nullptr, p_high_priority, p_description);
  240. }
  241. WorkerThreadPool::TaskID WorkerThreadPool::_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) {
  242. task_mutex.lock();
  243. // Get a free task
  244. Task *task = task_allocator.alloc();
  245. TaskID id = last_task++;
  246. task->callable = p_callable;
  247. task->native_func = p_func;
  248. task->native_func_userdata = p_userdata;
  249. task->description = p_description;
  250. task->template_userdata = p_template_userdata;
  251. tasks.insert(id, task);
  252. task_mutex.unlock();
  253. _post_task(task, p_high_priority);
  254. return id;
  255. }
  256. WorkerThreadPool::TaskID WorkerThreadPool::add_task(const Callable &p_action, bool p_high_priority, const String &p_description) {
  257. return _add_task(p_action, nullptr, nullptr, nullptr, p_high_priority, p_description);
  258. }
  259. bool WorkerThreadPool::is_task_completed(TaskID p_task_id) const {
  260. task_mutex.lock();
  261. const Task *const *taskp = tasks.getptr(p_task_id);
  262. if (!taskp) {
  263. task_mutex.unlock();
  264. ERR_FAIL_V_MSG(false, "Invalid Task ID"); // Invalid task
  265. }
  266. bool completed = (*taskp)->completed;
  267. task_mutex.unlock();
  268. return completed;
  269. }
  270. Error WorkerThreadPool::wait_for_task_completion(TaskID p_task_id) {
  271. task_mutex.lock();
  272. Task **taskp = tasks.getptr(p_task_id);
  273. if (!taskp) {
  274. task_mutex.unlock();
  275. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid Task ID"); // Invalid task
  276. }
  277. Task *task = *taskp;
  278. if (!task->completed) {
  279. if (!use_native_low_priority_threads && task->pool_thread_index != -1) { // Otherwise, it's not running yet.
  280. int caller_pool_th_index = thread_ids.has(Thread::get_caller_id()) ? thread_ids[Thread::get_caller_id()] : -1;
  281. if (caller_pool_th_index == task->pool_thread_index) {
  282. // Deadlock prevention.
  283. // Waiting for a task run on this same thread? That means the task to be awaited started waiting as well
  284. // and another task was run to make use of the thread in the meantime, with enough bad luck as to
  285. // the need to wait for the original task arose in turn.
  286. // In other words, the task we want to wait for is buried in the stack.
  287. // Let's report the caller about the issue to it handles as it sees fit.
  288. task_mutex.unlock();
  289. return ERR_BUSY;
  290. }
  291. }
  292. task->waiting++;
  293. bool is_low_prio_waiting_for_another = false;
  294. if (!use_native_low_priority_threads) {
  295. // Deadlock prevention:
  296. // If all low-prio tasks are waiting for other low-prio tasks and there are no more free low-prio slots,
  297. // we have a no progressable situation. We can apply a workaround, consisting in promoting an awaited queued
  298. // low-prio task to the schedule queue so it can run and break the "impasse".
  299. // NOTE: A similar reasoning could be made about high priority tasks, but there are usually much more
  300. // than low-prio. Therefore, a deadlock there would only happen when dealing with a very complex task graph
  301. // or when there are too few worker threads (limited platforms or exotic settings). If that turns out to be
  302. // an issue in the real world, a further fix can be applied against that.
  303. if (task->low_priority) {
  304. bool awaiter_is_a_low_prio_task = thread_ids.has(Thread::get_caller_id()) && threads[thread_ids[Thread::get_caller_id()]].current_low_prio_task;
  305. if (awaiter_is_a_low_prio_task) {
  306. is_low_prio_waiting_for_another = true;
  307. low_priority_tasks_awaiting_others++;
  308. if (low_priority_tasks_awaiting_others == low_priority_tasks_running) {
  309. _prevent_low_prio_saturation_deadlock();
  310. }
  311. }
  312. }
  313. }
  314. task_mutex.unlock();
  315. if (use_native_low_priority_threads && task->low_priority) {
  316. task->done_semaphore.wait();
  317. } else {
  318. bool current_is_pool_thread = thread_ids.has(Thread::get_caller_id());
  319. if (current_is_pool_thread) {
  320. // We are an actual process thread, we must not be blocked so continue processing stuff if available.
  321. bool must_exit = false;
  322. while (true) {
  323. if (task->done_semaphore.try_wait()) {
  324. // If done, exit
  325. break;
  326. }
  327. if (!must_exit) {
  328. if (task_available_semaphore.try_wait()) {
  329. if (exit_threads) {
  330. must_exit = true;
  331. } else {
  332. // Solve tasks while they are around.
  333. bool safe_for_nodes_backup = is_current_thread_safe_for_nodes();
  334. _process_task_queue();
  335. set_current_thread_safe_for_nodes(safe_for_nodes_backup);
  336. continue;
  337. }
  338. } else if (!use_native_low_priority_threads && task->low_priority) {
  339. // A low prioriry task started waiting, so see if we can move a pending one to the high priority queue.
  340. task_mutex.lock();
  341. bool post = _try_promote_low_priority_task();
  342. task_mutex.unlock();
  343. if (post) {
  344. task_available_semaphore.post();
  345. }
  346. }
  347. }
  348. OS::get_singleton()->delay_usec(1); // Microsleep, this could be converted to waiting for multiple objects in supported platforms for a bit more performance.
  349. }
  350. } else {
  351. task->done_semaphore.wait();
  352. }
  353. }
  354. task_mutex.lock();
  355. if (is_low_prio_waiting_for_another) {
  356. low_priority_tasks_awaiting_others--;
  357. }
  358. task->waiting--;
  359. }
  360. if (task->waiting == 0) {
  361. if (use_native_low_priority_threads && task->low_priority) {
  362. task->low_priority_thread->wait_to_finish();
  363. native_thread_allocator.free(task->low_priority_thread);
  364. }
  365. tasks.erase(p_task_id);
  366. task_allocator.free(task);
  367. }
  368. task_mutex.unlock();
  369. return OK;
  370. }
  371. WorkerThreadPool::GroupID WorkerThreadPool::_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) {
  372. ERR_FAIL_COND_V(p_elements < 0, INVALID_TASK_ID);
  373. if (p_tasks < 0) {
  374. p_tasks = MAX(1u, threads.size());
  375. }
  376. task_mutex.lock();
  377. Group *group = group_allocator.alloc();
  378. GroupID id = last_task++;
  379. group->max = p_elements;
  380. group->self = id;
  381. Task **tasks_posted = nullptr;
  382. if (p_elements == 0) {
  383. // Should really not call it with zero Elements, but at least it should work.
  384. group->completed.set_to(true);
  385. group->done_semaphore.post();
  386. group->tasks_used = 0;
  387. p_tasks = 0;
  388. if (p_template_userdata) {
  389. memdelete(p_template_userdata);
  390. }
  391. } else {
  392. group->tasks_used = p_tasks;
  393. tasks_posted = (Task **)alloca(sizeof(Task *) * p_tasks);
  394. for (int i = 0; i < p_tasks; i++) {
  395. Task *task = task_allocator.alloc();
  396. task->native_group_func = p_func;
  397. task->native_func_userdata = p_userdata;
  398. task->description = p_description;
  399. task->group = group;
  400. task->callable = p_callable;
  401. task->template_userdata = p_template_userdata;
  402. tasks_posted[i] = task;
  403. // No task ID is used.
  404. }
  405. }
  406. groups[id] = group;
  407. task_mutex.unlock();
  408. for (int i = 0; i < p_tasks; i++) {
  409. _post_task(tasks_posted[i], p_high_priority);
  410. }
  411. return id;
  412. }
  413. WorkerThreadPool::GroupID WorkerThreadPool::add_native_group_task(void (*p_func)(void *, uint32_t), void *p_userdata, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  414. return _add_group_task(Callable(), p_func, p_userdata, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  415. }
  416. WorkerThreadPool::GroupID WorkerThreadPool::add_group_task(const Callable &p_action, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  417. return _add_group_task(p_action, nullptr, nullptr, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  418. }
  419. uint32_t WorkerThreadPool::get_group_processed_element_count(GroupID p_group) const {
  420. task_mutex.lock();
  421. const Group *const *groupp = groups.getptr(p_group);
  422. if (!groupp) {
  423. task_mutex.unlock();
  424. ERR_FAIL_V_MSG(0, "Invalid Group ID");
  425. }
  426. uint32_t elements = (*groupp)->completed_index.get();
  427. task_mutex.unlock();
  428. return elements;
  429. }
  430. bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const {
  431. task_mutex.lock();
  432. const Group *const *groupp = groups.getptr(p_group);
  433. if (!groupp) {
  434. task_mutex.unlock();
  435. ERR_FAIL_V_MSG(false, "Invalid Group ID");
  436. }
  437. bool completed = (*groupp)->completed.is_set();
  438. task_mutex.unlock();
  439. return completed;
  440. }
  441. void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) {
  442. task_mutex.lock();
  443. Group **groupp = groups.getptr(p_group);
  444. task_mutex.unlock();
  445. if (!groupp) {
  446. ERR_FAIL_MSG("Invalid Group ID");
  447. }
  448. Group *group = *groupp;
  449. if (group->low_priority_native_tasks.size() > 0) {
  450. for (Task *task : group->low_priority_native_tasks) {
  451. task->low_priority_thread->wait_to_finish();
  452. task_mutex.lock();
  453. native_thread_allocator.free(task->low_priority_thread);
  454. task_allocator.free(task);
  455. task_mutex.unlock();
  456. }
  457. task_mutex.lock();
  458. group_allocator.free(group);
  459. task_mutex.unlock();
  460. } else {
  461. group->done_semaphore.wait();
  462. uint32_t max_users = group->tasks_used + 1; // Add 1 because the thread waiting for it is also user. Read before to avoid another thread freeing task after increment.
  463. uint32_t finished_users = group->finished.increment(); // fetch happens before inc, so increment later.
  464. if (finished_users == max_users) {
  465. // All tasks using this group are gone (finished before the group), so clear the group too.
  466. task_mutex.lock();
  467. group_allocator.free(group);
  468. task_mutex.unlock();
  469. }
  470. }
  471. task_mutex.lock(); // This mutex is needed when Physics 2D and/or 3D is selected to run on a separate thread.
  472. groups.erase(p_group);
  473. task_mutex.unlock();
  474. }
  475. void WorkerThreadPool::init(int p_thread_count, bool p_use_native_threads_low_priority, float p_low_priority_task_ratio) {
  476. ERR_FAIL_COND(threads.size() > 0);
  477. if (p_thread_count < 0) {
  478. p_thread_count = OS::get_singleton()->get_default_thread_pool_size();
  479. }
  480. if (p_use_native_threads_low_priority) {
  481. max_low_priority_threads = 0;
  482. } else {
  483. max_low_priority_threads = CLAMP(p_thread_count * p_low_priority_task_ratio, 1, p_thread_count - 1);
  484. }
  485. use_native_low_priority_threads = p_use_native_threads_low_priority;
  486. threads.resize(p_thread_count);
  487. for (uint32_t i = 0; i < threads.size(); i++) {
  488. threads[i].index = i;
  489. threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i]);
  490. thread_ids.insert(threads[i].thread.get_id(), i);
  491. }
  492. }
  493. void WorkerThreadPool::finish() {
  494. if (threads.size() == 0) {
  495. return;
  496. }
  497. task_mutex.lock();
  498. SelfList<Task> *E = low_priority_task_queue.first();
  499. while (E) {
  500. print_error("Task waiting was never re-claimed: " + E->self()->description);
  501. E = E->next();
  502. }
  503. task_mutex.unlock();
  504. exit_threads = true;
  505. for (uint32_t i = 0; i < threads.size(); i++) {
  506. task_available_semaphore.post();
  507. }
  508. for (ThreadData &data : threads) {
  509. data.thread.wait_to_finish();
  510. }
  511. threads.clear();
  512. }
  513. void WorkerThreadPool::_bind_methods() {
  514. ClassDB::bind_method(D_METHOD("add_task", "action", "high_priority", "description"), &WorkerThreadPool::add_task, DEFVAL(false), DEFVAL(String()));
  515. ClassDB::bind_method(D_METHOD("is_task_completed", "task_id"), &WorkerThreadPool::is_task_completed);
  516. ClassDB::bind_method(D_METHOD("wait_for_task_completion", "task_id"), &WorkerThreadPool::wait_for_task_completion);
  517. ClassDB::bind_method(D_METHOD("add_group_task", "action", "elements", "tasks_needed", "high_priority", "description"), &WorkerThreadPool::add_group_task, DEFVAL(-1), DEFVAL(false), DEFVAL(String()));
  518. ClassDB::bind_method(D_METHOD("is_group_task_completed", "group_id"), &WorkerThreadPool::is_group_task_completed);
  519. ClassDB::bind_method(D_METHOD("get_group_processed_element_count", "group_id"), &WorkerThreadPool::get_group_processed_element_count);
  520. ClassDB::bind_method(D_METHOD("wait_for_group_task_completion", "group_id"), &WorkerThreadPool::wait_for_group_task_completion);
  521. }
  522. WorkerThreadPool::WorkerThreadPool() {
  523. singleton = this;
  524. }
  525. WorkerThreadPool::~WorkerThreadPool() {
  526. finish();
  527. }