worker_thread_pool.cpp 21 KB

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