worker_thread_pool.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /**************************************************************************/
  2. /* worker_thread_pool.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "worker_thread_pool.h"
  31. #include "core/object/script_language.h"
  32. #include "core/os/os.h"
  33. #include "core/os/thread_safe.h"
  34. #include "core/templates/command_queue_mt.h"
  35. WorkerThreadPool::Task *const WorkerThreadPool::ThreadData::YIELDING = (Task *)1;
  36. void WorkerThreadPool::Task::free_template_userdata() {
  37. ERR_FAIL_NULL(template_userdata);
  38. ERR_FAIL_NULL(native_func_userdata);
  39. BaseTemplateUserdata *btu = (BaseTemplateUserdata *)native_func_userdata;
  40. memdelete(btu);
  41. }
  42. WorkerThreadPool *WorkerThreadPool::singleton = nullptr;
  43. thread_local CommandQueueMT *WorkerThreadPool::flushing_cmd_queue = nullptr;
  44. void WorkerThreadPool::_process_task(Task *p_task) {
  45. #ifdef THREADS_ENABLED
  46. int pool_thread_index = thread_ids[Thread::get_caller_id()];
  47. ThreadData &curr_thread = threads[pool_thread_index];
  48. Task *prev_task = nullptr; // In case this is recursively called.
  49. bool safe_for_nodes_backup = is_current_thread_safe_for_nodes();
  50. {
  51. // Tasks must start with this unset. They are free to set-and-forget otherwise.
  52. set_current_thread_safe_for_nodes(false);
  53. // Since the WorkerThreadPool is started before the script server,
  54. // its pre-created threads can't have ScriptServer::thread_enter() called on them early.
  55. // Therefore, we do it late at the first opportunity, so in case the task
  56. // about to be run uses scripting, guarantees are held.
  57. task_mutex.lock();
  58. if (!curr_thread.ready_for_scripting && ScriptServer::are_languages_initialized()) {
  59. task_mutex.unlock();
  60. ScriptServer::thread_enter();
  61. task_mutex.lock();
  62. curr_thread.ready_for_scripting = true;
  63. }
  64. p_task->pool_thread_index = pool_thread_index;
  65. prev_task = curr_thread.current_task;
  66. curr_thread.current_task = p_task;
  67. if (p_task->pending_notify_yield_over) {
  68. curr_thread.yield_is_over = true;
  69. }
  70. task_mutex.unlock();
  71. }
  72. #endif
  73. if (p_task->group) {
  74. // Handling a group
  75. bool do_post = false;
  76. while (true) {
  77. uint32_t work_index = p_task->group->index.postincrement();
  78. if (work_index >= p_task->group->max) {
  79. break;
  80. }
  81. if (p_task->native_group_func) {
  82. p_task->native_group_func(p_task->native_func_userdata, work_index);
  83. } else if (p_task->template_userdata) {
  84. p_task->template_userdata->callback_indexed(work_index);
  85. } else {
  86. p_task->callable.call(work_index);
  87. }
  88. // This is the only way to ensure posting is done when all tasks are really complete.
  89. uint32_t completed_amount = p_task->group->completed_index.increment();
  90. if (completed_amount == p_task->group->max) {
  91. do_post = true;
  92. }
  93. }
  94. if (do_post && p_task->template_userdata) {
  95. memdelete(p_task->template_userdata); // This is no longer needed at this point, so get rid of it.
  96. }
  97. if (do_post) {
  98. p_task->group->done_semaphore.post();
  99. p_task->group->completed.set_to(true);
  100. }
  101. 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.
  102. uint32_t finished_users = p_task->group->finished.increment();
  103. if (finished_users == max_users) {
  104. // Get rid of the group, because nobody else is using it.
  105. task_mutex.lock();
  106. group_allocator.free(p_task->group);
  107. task_mutex.unlock();
  108. }
  109. // For groups, tasks get rid of themselves.
  110. task_mutex.lock();
  111. task_allocator.free(p_task);
  112. } else {
  113. if (p_task->native_func) {
  114. p_task->native_func(p_task->native_func_userdata);
  115. } else if (p_task->template_userdata) {
  116. p_task->template_userdata->callback();
  117. memdelete(p_task->template_userdata);
  118. } else {
  119. p_task->callable.call();
  120. }
  121. task_mutex.lock();
  122. p_task->completed = true;
  123. p_task->pool_thread_index = -1;
  124. if (p_task->waiting_user) {
  125. p_task->done_semaphore.post(p_task->waiting_user);
  126. }
  127. // Let awaiters know.
  128. for (uint32_t i = 0; i < threads.size(); i++) {
  129. if (threads[i].awaited_task == p_task) {
  130. threads[i].cond_var.notify_one();
  131. threads[i].signaled = true;
  132. }
  133. }
  134. }
  135. #ifdef THREADS_ENABLED
  136. {
  137. curr_thread.current_task = prev_task;
  138. if (p_task->low_priority) {
  139. low_priority_threads_used--;
  140. if (_try_promote_low_priority_task()) {
  141. if (prev_task) { // Otherwise, this thread will catch it.
  142. _notify_threads(&curr_thread, 1, 0);
  143. }
  144. }
  145. }
  146. task_mutex.unlock();
  147. }
  148. set_current_thread_safe_for_nodes(safe_for_nodes_backup);
  149. #endif
  150. }
  151. void WorkerThreadPool::_thread_function(void *p_user) {
  152. ThreadData *thread_data = (ThreadData *)p_user;
  153. while (true) {
  154. Task *task_to_process = nullptr;
  155. {
  156. MutexLock lock(singleton->task_mutex);
  157. if (singleton->exit_threads) {
  158. return;
  159. }
  160. thread_data->signaled = false;
  161. if (singleton->task_queue.first()) {
  162. task_to_process = singleton->task_queue.first()->self();
  163. singleton->task_queue.remove(singleton->task_queue.first());
  164. } else {
  165. thread_data->cond_var.wait(lock);
  166. DEV_ASSERT(singleton->exit_threads || thread_data->signaled);
  167. }
  168. }
  169. if (task_to_process) {
  170. singleton->_process_task(task_to_process);
  171. }
  172. }
  173. }
  174. void WorkerThreadPool::_post_tasks_and_unlock(Task **p_tasks, uint32_t p_count, bool p_high_priority) {
  175. // Fall back to processing on the calling thread if there are no worker threads.
  176. // Separated into its own variable to make it easier to extend this logic
  177. // in custom builds.
  178. bool process_on_calling_thread = threads.size() == 0;
  179. if (process_on_calling_thread) {
  180. task_mutex.unlock();
  181. for (uint32_t i = 0; i < p_count; i++) {
  182. _process_task(p_tasks[i]);
  183. }
  184. return;
  185. }
  186. uint32_t to_process = 0;
  187. uint32_t to_promote = 0;
  188. ThreadData *caller_pool_thread = thread_ids.has(Thread::get_caller_id()) ? &threads[thread_ids[Thread::get_caller_id()]] : nullptr;
  189. for (uint32_t i = 0; i < p_count; i++) {
  190. p_tasks[i]->low_priority = !p_high_priority;
  191. if (p_high_priority || low_priority_threads_used < max_low_priority_threads) {
  192. task_queue.add_last(&p_tasks[i]->task_elem);
  193. if (!p_high_priority) {
  194. low_priority_threads_used++;
  195. }
  196. to_process++;
  197. } else {
  198. // Too many threads using low priority, must go to queue.
  199. low_priority_task_queue.add_last(&p_tasks[i]->task_elem);
  200. to_promote++;
  201. }
  202. }
  203. _notify_threads(caller_pool_thread, to_process, to_promote);
  204. task_mutex.unlock();
  205. }
  206. void WorkerThreadPool::_notify_threads(const ThreadData *p_current_thread_data, uint32_t p_process_count, uint32_t p_promote_count) {
  207. uint32_t to_process = p_process_count;
  208. uint32_t to_promote = p_promote_count;
  209. // This is where which threads are awaken is decided according to the workload.
  210. // Threads that will anyway have a chance to check the situation and process/promote tasks
  211. // are excluded from being notified. Others will be tried anyway to try to distribute load.
  212. // The current thread, if is a pool thread, is also excluded depending on the promoting/processing
  213. // needs because it will anyway loop again. However, it will contribute to decreasing the count,
  214. // which helps reducing sync traffic.
  215. uint32_t thread_count = threads.size();
  216. // First round:
  217. // 1. For processing: notify threads that are not running tasks, to keep the stacks as shallow as possible.
  218. // 2. For promoting: since it's exclusive with processing, we fin threads able to promote low-prio tasks now.
  219. for (uint32_t i = 0;
  220. i < thread_count && (to_process || to_promote);
  221. i++, notify_index = (notify_index + 1) % thread_count) {
  222. ThreadData &th = threads[notify_index];
  223. if (th.signaled) {
  224. continue;
  225. }
  226. if (th.current_task) {
  227. // Good thread for promoting low-prio?
  228. if (to_promote && th.awaited_task && th.current_task->low_priority) {
  229. if (likely(&th != p_current_thread_data)) {
  230. th.cond_var.notify_one();
  231. }
  232. th.signaled = true;
  233. to_promote--;
  234. }
  235. } else {
  236. if (to_process) {
  237. if (likely(&th != p_current_thread_data)) {
  238. th.cond_var.notify_one();
  239. }
  240. th.signaled = true;
  241. to_process--;
  242. }
  243. }
  244. }
  245. // Second round:
  246. // For processing: if the first round wasn't enough, let's try now with threads processing tasks but currently awaiting.
  247. for (uint32_t i = 0;
  248. i < thread_count && to_process;
  249. i++, notify_index = (notify_index + 1) % thread_count) {
  250. ThreadData &th = threads[notify_index];
  251. if (th.signaled) {
  252. continue;
  253. }
  254. if (th.awaited_task) {
  255. if (likely(&th != p_current_thread_data)) {
  256. th.cond_var.notify_one();
  257. }
  258. th.signaled = true;
  259. to_process--;
  260. }
  261. }
  262. }
  263. bool WorkerThreadPool::_try_promote_low_priority_task() {
  264. if (low_priority_task_queue.first()) {
  265. Task *low_prio_task = low_priority_task_queue.first()->self();
  266. low_priority_task_queue.remove(low_priority_task_queue.first());
  267. task_queue.add_last(&low_prio_task->task_elem);
  268. low_priority_threads_used++;
  269. return true;
  270. } else {
  271. return false;
  272. }
  273. }
  274. WorkerThreadPool::TaskID WorkerThreadPool::add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority, const String &p_description) {
  275. return _add_task(Callable(), p_func, p_userdata, nullptr, p_high_priority, p_description);
  276. }
  277. 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) {
  278. task_mutex.lock();
  279. // Get a free task
  280. Task *task = task_allocator.alloc();
  281. TaskID id = last_task++;
  282. task->self = id;
  283. task->callable = p_callable;
  284. task->native_func = p_func;
  285. task->native_func_userdata = p_userdata;
  286. task->description = p_description;
  287. task->template_userdata = p_template_userdata;
  288. tasks.insert(id, task);
  289. _post_tasks_and_unlock(&task, 1, p_high_priority);
  290. return id;
  291. }
  292. WorkerThreadPool::TaskID WorkerThreadPool::add_task(const Callable &p_action, bool p_high_priority, const String &p_description) {
  293. return _add_task(p_action, nullptr, nullptr, nullptr, p_high_priority, p_description);
  294. }
  295. bool WorkerThreadPool::is_task_completed(TaskID p_task_id) const {
  296. task_mutex.lock();
  297. const Task *const *taskp = tasks.getptr(p_task_id);
  298. if (!taskp) {
  299. task_mutex.unlock();
  300. ERR_FAIL_V_MSG(false, "Invalid Task ID"); // Invalid task
  301. }
  302. bool completed = (*taskp)->completed;
  303. task_mutex.unlock();
  304. return completed;
  305. }
  306. Error WorkerThreadPool::wait_for_task_completion(TaskID p_task_id) {
  307. task_mutex.lock();
  308. Task **taskp = tasks.getptr(p_task_id);
  309. if (!taskp) {
  310. task_mutex.unlock();
  311. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid Task ID"); // Invalid task
  312. }
  313. Task *task = *taskp;
  314. if (task->completed) {
  315. if (task->waiting_pool == 0 && task->waiting_user == 0) {
  316. tasks.erase(p_task_id);
  317. task_allocator.free(task);
  318. }
  319. task_mutex.unlock();
  320. return OK;
  321. }
  322. ThreadData *caller_pool_thread = thread_ids.has(Thread::get_caller_id()) ? &threads[thread_ids[Thread::get_caller_id()]] : nullptr;
  323. if (caller_pool_thread && p_task_id <= caller_pool_thread->current_task->self) {
  324. // Deadlock prevention:
  325. // When a pool thread wants to wait for an older task, the following situations can happen:
  326. // 1. Awaited task is deep in the stack of the awaiter.
  327. // 2. A group of awaiter threads end up depending on some tasks buried in the stack
  328. // of their worker threads in such a way that progress can't be made.
  329. // Both would entail a deadlock. Some may be handled here in the WorkerThreadPool
  330. // with some extra logic and bookkeeping. However, there would still be unavoidable
  331. // cases of deadlock because of the way waiting threads process outstanding tasks.
  332. // Taking into account there's no feasible solution for every possible case
  333. // with the current design, we just simply reject attempts to await on older tasks,
  334. // with a specific error code that signals the situation so the caller can handle it.
  335. task_mutex.unlock();
  336. return ERR_BUSY;
  337. }
  338. if (caller_pool_thread) {
  339. task->waiting_pool++;
  340. } else {
  341. task->waiting_user++;
  342. }
  343. task_mutex.unlock();
  344. if (caller_pool_thread) {
  345. _wait_collaboratively(caller_pool_thread, task);
  346. task->waiting_pool--;
  347. if (task->waiting_pool == 0 && task->waiting_user == 0) {
  348. tasks.erase(p_task_id);
  349. task_allocator.free(task);
  350. }
  351. } else {
  352. task->done_semaphore.wait();
  353. task_mutex.lock();
  354. task->waiting_user--;
  355. if (task->waiting_pool == 0 && task->waiting_user == 0) {
  356. tasks.erase(p_task_id);
  357. task_allocator.free(task);
  358. }
  359. task_mutex.unlock();
  360. }
  361. return OK;
  362. }
  363. void WorkerThreadPool::_wait_collaboratively(ThreadData *p_caller_pool_thread, Task *p_task) {
  364. // Keep processing tasks until the condition to stop waiting is met.
  365. #define IS_WAIT_OVER (unlikely(p_task == ThreadData::YIELDING) ? p_caller_pool_thread->yield_is_over : p_task->completed)
  366. while (true) {
  367. Task *task_to_process = nullptr;
  368. {
  369. MutexLock lock(task_mutex);
  370. bool was_signaled = p_caller_pool_thread->signaled;
  371. p_caller_pool_thread->signaled = false;
  372. if (IS_WAIT_OVER) {
  373. p_caller_pool_thread->yield_is_over = false;
  374. if (!exit_threads && was_signaled) {
  375. // This thread was awaken for some additional reason, but it's about to exit.
  376. // Let's find out what may be pending and forward the requests.
  377. uint32_t to_process = task_queue.first() ? 1 : 0;
  378. uint32_t to_promote = p_caller_pool_thread->current_task->low_priority && low_priority_task_queue.first() ? 1 : 0;
  379. if (to_process || to_promote) {
  380. // This thread must be left alone since it won't loop again.
  381. p_caller_pool_thread->signaled = true;
  382. _notify_threads(p_caller_pool_thread, to_process, to_promote);
  383. }
  384. }
  385. break;
  386. }
  387. if (!exit_threads) {
  388. if (p_caller_pool_thread->current_task->low_priority && low_priority_task_queue.first()) {
  389. if (_try_promote_low_priority_task()) {
  390. _notify_threads(p_caller_pool_thread, 1, 0);
  391. }
  392. }
  393. if (singleton->task_queue.first()) {
  394. task_to_process = task_queue.first()->self();
  395. task_queue.remove(task_queue.first());
  396. }
  397. if (!task_to_process) {
  398. p_caller_pool_thread->awaited_task = p_task;
  399. if (flushing_cmd_queue) {
  400. flushing_cmd_queue->unlock();
  401. }
  402. p_caller_pool_thread->cond_var.wait(lock);
  403. if (flushing_cmd_queue) {
  404. flushing_cmd_queue->lock();
  405. }
  406. DEV_ASSERT(exit_threads || p_caller_pool_thread->signaled || IS_WAIT_OVER);
  407. p_caller_pool_thread->awaited_task = nullptr;
  408. }
  409. }
  410. }
  411. if (task_to_process) {
  412. _process_task(task_to_process);
  413. }
  414. }
  415. }
  416. void WorkerThreadPool::yield() {
  417. int th_index = get_thread_index();
  418. ERR_FAIL_COND_MSG(th_index == -1, "This function can only be called from a worker thread.");
  419. _wait_collaboratively(&threads[th_index], ThreadData::YIELDING);
  420. }
  421. void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {
  422. task_mutex.lock();
  423. Task **taskp = tasks.getptr(p_task_id);
  424. if (!taskp) {
  425. task_mutex.unlock();
  426. ERR_FAIL_MSG("Invalid Task ID.");
  427. }
  428. Task *task = *taskp;
  429. if (task->pool_thread_index == -1) { // Completed or not started yet.
  430. if (!task->completed) {
  431. // This avoids a race condition where a task is created and yield-over called before it's processed.
  432. task->pending_notify_yield_over = true;
  433. }
  434. task_mutex.unlock();
  435. return;
  436. }
  437. ThreadData &td = threads[task->pool_thread_index];
  438. td.yield_is_over = true;
  439. td.signaled = true;
  440. td.cond_var.notify_one();
  441. task_mutex.unlock();
  442. }
  443. 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) {
  444. ERR_FAIL_COND_V(p_elements < 0, INVALID_TASK_ID);
  445. if (p_tasks < 0) {
  446. p_tasks = MAX(1u, threads.size());
  447. }
  448. task_mutex.lock();
  449. Group *group = group_allocator.alloc();
  450. GroupID id = last_task++;
  451. group->max = p_elements;
  452. group->self = id;
  453. Task **tasks_posted = nullptr;
  454. if (p_elements == 0) {
  455. // Should really not call it with zero Elements, but at least it should work.
  456. group->completed.set_to(true);
  457. group->done_semaphore.post();
  458. group->tasks_used = 0;
  459. p_tasks = 0;
  460. if (p_template_userdata) {
  461. memdelete(p_template_userdata);
  462. }
  463. } else {
  464. group->tasks_used = p_tasks;
  465. tasks_posted = (Task **)alloca(sizeof(Task *) * p_tasks);
  466. for (int i = 0; i < p_tasks; i++) {
  467. Task *task = task_allocator.alloc();
  468. task->native_group_func = p_func;
  469. task->native_func_userdata = p_userdata;
  470. task->description = p_description;
  471. task->group = group;
  472. task->callable = p_callable;
  473. task->template_userdata = p_template_userdata;
  474. tasks_posted[i] = task;
  475. // No task ID is used.
  476. }
  477. }
  478. groups[id] = group;
  479. _post_tasks_and_unlock(tasks_posted, p_tasks, p_high_priority);
  480. return id;
  481. }
  482. 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) {
  483. return _add_group_task(Callable(), p_func, p_userdata, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  484. }
  485. WorkerThreadPool::GroupID WorkerThreadPool::add_group_task(const Callable &p_action, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  486. return _add_group_task(p_action, nullptr, nullptr, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  487. }
  488. uint32_t WorkerThreadPool::get_group_processed_element_count(GroupID p_group) const {
  489. task_mutex.lock();
  490. const Group *const *groupp = groups.getptr(p_group);
  491. if (!groupp) {
  492. task_mutex.unlock();
  493. ERR_FAIL_V_MSG(0, "Invalid Group ID");
  494. }
  495. uint32_t elements = (*groupp)->completed_index.get();
  496. task_mutex.unlock();
  497. return elements;
  498. }
  499. bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const {
  500. task_mutex.lock();
  501. const Group *const *groupp = groups.getptr(p_group);
  502. if (!groupp) {
  503. task_mutex.unlock();
  504. ERR_FAIL_V_MSG(false, "Invalid Group ID");
  505. }
  506. bool completed = (*groupp)->completed.is_set();
  507. task_mutex.unlock();
  508. return completed;
  509. }
  510. void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) {
  511. #ifdef THREADS_ENABLED
  512. task_mutex.lock();
  513. Group **groupp = groups.getptr(p_group);
  514. task_mutex.unlock();
  515. if (!groupp) {
  516. ERR_FAIL_MSG("Invalid Group ID.");
  517. }
  518. {
  519. Group *group = *groupp;
  520. if (flushing_cmd_queue) {
  521. flushing_cmd_queue->unlock();
  522. }
  523. group->done_semaphore.wait();
  524. if (flushing_cmd_queue) {
  525. flushing_cmd_queue->lock();
  526. }
  527. 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.
  528. uint32_t finished_users = group->finished.increment(); // fetch happens before inc, so increment later.
  529. if (finished_users == max_users) {
  530. // All tasks using this group are gone (finished before the group), so clear the group too.
  531. task_mutex.lock();
  532. group_allocator.free(group);
  533. task_mutex.unlock();
  534. }
  535. }
  536. task_mutex.lock(); // This mutex is needed when Physics 2D and/or 3D is selected to run on a separate thread.
  537. groups.erase(p_group);
  538. task_mutex.unlock();
  539. #endif
  540. }
  541. int WorkerThreadPool::get_thread_index() {
  542. Thread::ID tid = Thread::get_caller_id();
  543. return singleton->thread_ids.has(tid) ? singleton->thread_ids[tid] : -1;
  544. }
  545. void WorkerThreadPool::thread_enter_command_queue_mt_flush(CommandQueueMT *p_queue) {
  546. ERR_FAIL_COND(flushing_cmd_queue != nullptr);
  547. flushing_cmd_queue = p_queue;
  548. }
  549. void WorkerThreadPool::thread_exit_command_queue_mt_flush() {
  550. ERR_FAIL_NULL(flushing_cmd_queue);
  551. flushing_cmd_queue = nullptr;
  552. }
  553. void WorkerThreadPool::init(int p_thread_count, float p_low_priority_task_ratio) {
  554. ERR_FAIL_COND(threads.size() > 0);
  555. if (p_thread_count < 0) {
  556. p_thread_count = OS::get_singleton()->get_default_thread_pool_size();
  557. }
  558. max_low_priority_threads = CLAMP(p_thread_count * p_low_priority_task_ratio, 1, p_thread_count - 1);
  559. threads.resize(p_thread_count);
  560. for (uint32_t i = 0; i < threads.size(); i++) {
  561. threads[i].index = i;
  562. threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i]);
  563. thread_ids.insert(threads[i].thread.get_id(), i);
  564. }
  565. }
  566. void WorkerThreadPool::finish() {
  567. if (threads.size() == 0) {
  568. return;
  569. }
  570. {
  571. MutexLock lock(task_mutex);
  572. SelfList<Task> *E = low_priority_task_queue.first();
  573. while (E) {
  574. print_error("Task waiting was never re-claimed: " + E->self()->description);
  575. E = E->next();
  576. }
  577. }
  578. {
  579. MutexLock lock(task_mutex);
  580. exit_threads = true;
  581. }
  582. for (ThreadData &data : threads) {
  583. data.cond_var.notify_one();
  584. }
  585. for (ThreadData &data : threads) {
  586. data.thread.wait_to_finish();
  587. }
  588. {
  589. MutexLock lock(task_mutex);
  590. for (KeyValue<TaskID, Task *> &E : tasks) {
  591. task_allocator.free(E.value);
  592. }
  593. }
  594. threads.clear();
  595. }
  596. void WorkerThreadPool::_bind_methods() {
  597. ClassDB::bind_method(D_METHOD("add_task", "action", "high_priority", "description"), &WorkerThreadPool::add_task, DEFVAL(false), DEFVAL(String()));
  598. ClassDB::bind_method(D_METHOD("is_task_completed", "task_id"), &WorkerThreadPool::is_task_completed);
  599. ClassDB::bind_method(D_METHOD("wait_for_task_completion", "task_id"), &WorkerThreadPool::wait_for_task_completion);
  600. 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()));
  601. ClassDB::bind_method(D_METHOD("is_group_task_completed", "group_id"), &WorkerThreadPool::is_group_task_completed);
  602. ClassDB::bind_method(D_METHOD("get_group_processed_element_count", "group_id"), &WorkerThreadPool::get_group_processed_element_count);
  603. ClassDB::bind_method(D_METHOD("wait_for_group_task_completion", "group_id"), &WorkerThreadPool::wait_for_group_task_completion);
  604. }
  605. WorkerThreadPool::WorkerThreadPool() {
  606. singleton = this;
  607. }
  608. WorkerThreadPool::~WorkerThreadPool() {
  609. finish();
  610. }