worker_thread_pool.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. // 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. Callable::CallError ce;
  71. Variant ret;
  72. Variant arg;
  73. Variant *argptr = &arg;
  74. while (true) {
  75. uint32_t work_index = p_task->group->index.postincrement();
  76. if (work_index >= p_task->group->max) {
  77. break;
  78. }
  79. if (p_task->native_group_func) {
  80. p_task->native_group_func(p_task->native_func_userdata, work_index);
  81. } else if (p_task->template_userdata) {
  82. p_task->template_userdata->callback_indexed(work_index);
  83. } else {
  84. arg = work_index;
  85. p_task->callable.callp((const Variant **)&argptr, 1, ret, ce);
  86. }
  87. // This is the only way to ensure posting is done when all tasks are really complete.
  88. uint32_t completed_amount = p_task->group->completed_index.increment();
  89. if (completed_amount == p_task->group->max) {
  90. do_post = true;
  91. }
  92. }
  93. if (do_post && p_task->template_userdata) {
  94. memdelete(p_task->template_userdata); // This is no longer needed at this point, so get rid of it.
  95. }
  96. if (low_priority && use_native_low_priority_threads) {
  97. p_task->completed = true;
  98. p_task->done_semaphore.post();
  99. if (do_post) {
  100. p_task->group->completed.set_to(true);
  101. }
  102. } else {
  103. if (do_post) {
  104. p_task->group->done_semaphore.post();
  105. p_task->group->completed.set_to(true);
  106. }
  107. 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.
  108. uint32_t finished_users = p_task->group->finished.increment();
  109. if (finished_users == max_users) {
  110. // Get rid of the group, because nobody else is using it.
  111. task_mutex.lock();
  112. group_allocator.free(p_task->group);
  113. task_mutex.unlock();
  114. }
  115. // For groups, tasks get rid of themselves.
  116. task_mutex.lock();
  117. task_allocator.free(p_task);
  118. task_mutex.unlock();
  119. }
  120. } else {
  121. if (p_task->native_func) {
  122. p_task->native_func(p_task->native_func_userdata);
  123. } else if (p_task->template_userdata) {
  124. p_task->template_userdata->callback();
  125. memdelete(p_task->template_userdata);
  126. } else {
  127. Callable::CallError ce;
  128. Variant ret;
  129. p_task->callable.callp(nullptr, 0, ret, ce);
  130. }
  131. task_mutex.lock();
  132. p_task->completed = true;
  133. for (uint8_t i = 0; i < p_task->waiting; i++) {
  134. p_task->done_semaphore.post();
  135. }
  136. if (!use_native_low_priority_threads) {
  137. p_task->pool_thread_index = -1;
  138. }
  139. task_mutex.unlock(); // Keep mutex down to here since on unlock the task may be freed.
  140. }
  141. // Task may have been freed by now (all callers notified).
  142. p_task = nullptr;
  143. if (!use_native_low_priority_threads) {
  144. bool post = false;
  145. task_mutex.lock();
  146. ThreadData &curr_thread = threads[pool_thread_index];
  147. curr_thread.current_low_prio_task = prev_low_prio_task;
  148. if (low_priority) {
  149. low_priority_threads_used--;
  150. low_priority_tasks_running--;
  151. // A low prioriry task was freed, so see if we can move a pending one to the high priority queue.
  152. if (_try_promote_low_priority_task()) {
  153. post = true;
  154. }
  155. if (low_priority_tasks_awaiting_others == low_priority_tasks_running) {
  156. _prevent_low_prio_saturation_deadlock();
  157. }
  158. }
  159. task_mutex.unlock();
  160. if (post) {
  161. task_available_semaphore.post();
  162. }
  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. bool safe_for_nodes_backup = is_current_thread_safe_for_nodes();
  332. _process_task_queue();
  333. set_current_thread_safe_for_nodes(safe_for_nodes_backup);
  334. continue;
  335. }
  336. } else if (!use_native_low_priority_threads && task->low_priority) {
  337. // A low prioriry task started waiting, so see if we can move a pending one to the high priority queue.
  338. task_mutex.lock();
  339. bool post = _try_promote_low_priority_task();
  340. task_mutex.unlock();
  341. if (post) {
  342. task_available_semaphore.post();
  343. }
  344. }
  345. }
  346. OS::get_singleton()->delay_usec(1); // Microsleep, this could be converted to waiting for multiple objects in supported platforms for a bit more performance.
  347. }
  348. } else {
  349. task->done_semaphore.wait();
  350. }
  351. }
  352. task_mutex.lock();
  353. if (is_low_prio_waiting_for_another) {
  354. low_priority_tasks_awaiting_others--;
  355. }
  356. task->waiting--;
  357. }
  358. if (task->waiting == 0) {
  359. if (use_native_low_priority_threads && task->low_priority) {
  360. task->low_priority_thread->wait_to_finish();
  361. native_thread_allocator.free(task->low_priority_thread);
  362. }
  363. tasks.erase(p_task_id);
  364. task_allocator.free(task);
  365. }
  366. task_mutex.unlock();
  367. return OK;
  368. }
  369. 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) {
  370. ERR_FAIL_COND_V(p_elements < 0, INVALID_TASK_ID);
  371. if (p_tasks < 0) {
  372. p_tasks = MAX(1u, threads.size());
  373. }
  374. task_mutex.lock();
  375. Group *group = group_allocator.alloc();
  376. GroupID id = last_task++;
  377. group->max = p_elements;
  378. group->self = id;
  379. Task **tasks_posted = nullptr;
  380. if (p_elements == 0) {
  381. // Should really not call it with zero Elements, but at least it should work.
  382. group->completed.set_to(true);
  383. group->done_semaphore.post();
  384. group->tasks_used = 0;
  385. p_tasks = 0;
  386. if (p_template_userdata) {
  387. memdelete(p_template_userdata);
  388. }
  389. } else {
  390. group->tasks_used = p_tasks;
  391. tasks_posted = (Task **)alloca(sizeof(Task *) * p_tasks);
  392. for (int i = 0; i < p_tasks; i++) {
  393. Task *task = task_allocator.alloc();
  394. task->native_group_func = p_func;
  395. task->native_func_userdata = p_userdata;
  396. task->description = p_description;
  397. task->group = group;
  398. task->callable = p_callable;
  399. task->template_userdata = p_template_userdata;
  400. tasks_posted[i] = task;
  401. // No task ID is used.
  402. }
  403. }
  404. groups[id] = group;
  405. task_mutex.unlock();
  406. for (int i = 0; i < p_tasks; i++) {
  407. _post_task(tasks_posted[i], p_high_priority);
  408. }
  409. return id;
  410. }
  411. 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) {
  412. return _add_group_task(Callable(), p_func, p_userdata, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  413. }
  414. WorkerThreadPool::GroupID WorkerThreadPool::add_group_task(const Callable &p_action, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  415. return _add_group_task(p_action, nullptr, nullptr, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  416. }
  417. uint32_t WorkerThreadPool::get_group_processed_element_count(GroupID p_group) const {
  418. task_mutex.lock();
  419. const Group *const *groupp = groups.getptr(p_group);
  420. if (!groupp) {
  421. task_mutex.unlock();
  422. ERR_FAIL_V_MSG(0, "Invalid Group ID");
  423. }
  424. uint32_t elements = (*groupp)->completed_index.get();
  425. task_mutex.unlock();
  426. return elements;
  427. }
  428. bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const {
  429. task_mutex.lock();
  430. const Group *const *groupp = groups.getptr(p_group);
  431. if (!groupp) {
  432. task_mutex.unlock();
  433. ERR_FAIL_V_MSG(false, "Invalid Group ID");
  434. }
  435. bool completed = (*groupp)->completed.is_set();
  436. task_mutex.unlock();
  437. return completed;
  438. }
  439. void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) {
  440. task_mutex.lock();
  441. Group **groupp = groups.getptr(p_group);
  442. task_mutex.unlock();
  443. if (!groupp) {
  444. ERR_FAIL_MSG("Invalid Group ID");
  445. }
  446. Group *group = *groupp;
  447. if (group->low_priority_native_tasks.size() > 0) {
  448. for (Task *task : group->low_priority_native_tasks) {
  449. task->low_priority_thread->wait_to_finish();
  450. task_mutex.lock();
  451. native_thread_allocator.free(task->low_priority_thread);
  452. task_allocator.free(task);
  453. task_mutex.unlock();
  454. }
  455. task_mutex.lock();
  456. group_allocator.free(group);
  457. task_mutex.unlock();
  458. } else {
  459. group->done_semaphore.wait();
  460. 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.
  461. uint32_t finished_users = group->finished.increment(); // fetch happens before inc, so increment later.
  462. if (finished_users == max_users) {
  463. // All tasks using this group are gone (finished before the group), so clear the group too.
  464. task_mutex.lock();
  465. group_allocator.free(group);
  466. task_mutex.unlock();
  467. }
  468. }
  469. task_mutex.lock(); // This mutex is needed when Physics 2D and/or 3D is selected to run on a separate thread.
  470. groups.erase(p_group);
  471. task_mutex.unlock();
  472. }
  473. void WorkerThreadPool::init(int p_thread_count, bool p_use_native_threads_low_priority, float p_low_priority_task_ratio) {
  474. ERR_FAIL_COND(threads.size() > 0);
  475. if (p_thread_count < 0) {
  476. p_thread_count = OS::get_singleton()->get_default_thread_pool_size();
  477. }
  478. if (p_use_native_threads_low_priority) {
  479. max_low_priority_threads = 0;
  480. } else {
  481. max_low_priority_threads = CLAMP(p_thread_count * p_low_priority_task_ratio, 1, p_thread_count - 1);
  482. }
  483. use_native_low_priority_threads = p_use_native_threads_low_priority;
  484. threads.resize(p_thread_count);
  485. for (uint32_t i = 0; i < threads.size(); i++) {
  486. threads[i].index = i;
  487. threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i]);
  488. thread_ids.insert(threads[i].thread.get_id(), i);
  489. }
  490. }
  491. void WorkerThreadPool::finish() {
  492. if (threads.size() == 0) {
  493. return;
  494. }
  495. task_mutex.lock();
  496. SelfList<Task> *E = low_priority_task_queue.first();
  497. while (E) {
  498. print_error("Task waiting was never re-claimed: " + E->self()->description);
  499. E = E->next();
  500. }
  501. task_mutex.unlock();
  502. exit_threads = true;
  503. for (uint32_t i = 0; i < threads.size(); i++) {
  504. task_available_semaphore.post();
  505. }
  506. for (ThreadData &data : threads) {
  507. data.thread.wait_to_finish();
  508. }
  509. threads.clear();
  510. }
  511. void WorkerThreadPool::_bind_methods() {
  512. ClassDB::bind_method(D_METHOD("add_task", "action", "high_priority", "description"), &WorkerThreadPool::add_task, DEFVAL(false), DEFVAL(String()));
  513. ClassDB::bind_method(D_METHOD("is_task_completed", "task_id"), &WorkerThreadPool::is_task_completed);
  514. ClassDB::bind_method(D_METHOD("wait_for_task_completion", "task_id"), &WorkerThreadPool::wait_for_task_completion);
  515. 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()));
  516. ClassDB::bind_method(D_METHOD("is_group_task_completed", "group_id"), &WorkerThreadPool::is_group_task_completed);
  517. ClassDB::bind_method(D_METHOD("get_group_processed_element_count", "group_id"), &WorkerThreadPool::get_group_processed_element_count);
  518. ClassDB::bind_method(D_METHOD("wait_for_group_task_completion", "group_id"), &WorkerThreadPool::wait_for_group_task_completion);
  519. }
  520. WorkerThreadPool::WorkerThreadPool() {
  521. singleton = this;
  522. }
  523. WorkerThreadPool::~WorkerThreadPool() {
  524. finish();
  525. }