worker_thread_pool.cpp 21 KB

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