worker_thread_pool.cpp 21 KB

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