worker_thread_pool.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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/safe_binary_mutex.h"
  34. #include "core/os/thread_safe.h"
  35. WorkerThreadPool::Task *const WorkerThreadPool::ThreadData::YIELDING = (Task *)1;
  36. HashMap<StringName, WorkerThreadPool *> WorkerThreadPool::named_pools;
  37. void WorkerThreadPool::Task::free_template_userdata() {
  38. ERR_FAIL_NULL(template_userdata);
  39. ERR_FAIL_NULL(native_func_userdata);
  40. BaseTemplateUserdata *btu = (BaseTemplateUserdata *)native_func_userdata;
  41. memdelete(btu);
  42. }
  43. WorkerThreadPool *WorkerThreadPool::singleton = nullptr;
  44. #ifdef THREADS_ENABLED
  45. thread_local WorkerThreadPool::UnlockableLocks WorkerThreadPool::unlockable_locks[MAX_UNLOCKABLE_LOCKS];
  46. #endif
  47. void WorkerThreadPool::_process_task(Task *p_task) {
  48. #ifdef THREADS_ENABLED
  49. int pool_thread_index = thread_ids[Thread::get_caller_id()];
  50. ThreadData &curr_thread = threads[pool_thread_index];
  51. Task *prev_task = nullptr; // In case this is recursively called.
  52. bool safe_for_nodes_backup = is_current_thread_safe_for_nodes();
  53. CallQueue *call_queue_backup = MessageQueue::get_singleton() != MessageQueue::get_main_singleton() ? MessageQueue::get_singleton() : nullptr;
  54. {
  55. // Tasks must start with these at default values. They are free to set-and-forget otherwise.
  56. set_current_thread_safe_for_nodes(false);
  57. MessageQueue::set_thread_singleton_override(nullptr);
  58. // Since the WorkerThreadPool is started before the script server,
  59. // its pre-created threads can't have ScriptServer::thread_enter() called on them early.
  60. // Therefore, we do it late at the first opportunity, so in case the task
  61. // about to be run uses scripting, guarantees are held.
  62. ScriptServer::thread_enter();
  63. task_mutex.lock();
  64. p_task->pool_thread_index = pool_thread_index;
  65. prev_task = curr_thread.current_task;
  66. curr_thread.current_task = p_task;
  67. curr_thread.has_pump_task = p_task->is_pump_task;
  68. if (p_task->pending_notify_yield_over) {
  69. curr_thread.yield_is_over = true;
  70. }
  71. task_mutex.unlock();
  72. }
  73. #endif
  74. #ifdef THREADS_ENABLED
  75. bool low_priority = p_task->low_priority;
  76. #endif
  77. if (p_task->group) {
  78. // Handling a group
  79. bool do_post = false;
  80. while (true) {
  81. uint32_t work_index = p_task->group->index.postincrement();
  82. if (work_index >= p_task->group->max) {
  83. break;
  84. }
  85. if (p_task->native_group_func) {
  86. p_task->native_group_func(p_task->native_func_userdata, work_index);
  87. } else if (p_task->template_userdata) {
  88. p_task->template_userdata->callback_indexed(work_index);
  89. } else {
  90. p_task->callable.call(work_index);
  91. }
  92. // This is the only way to ensure posting is done when all tasks are really complete.
  93. uint32_t completed_amount = p_task->group->completed_index.increment();
  94. if (completed_amount == p_task->group->max) {
  95. do_post = true;
  96. }
  97. }
  98. if (do_post && p_task->template_userdata) {
  99. memdelete(p_task->template_userdata); // This is no longer needed at this point, so get rid of it.
  100. }
  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. MutexLock task_lock(task_mutex);
  110. group_allocator.free(p_task->group);
  111. }
  112. // For groups, tasks get rid of themselves.
  113. task_mutex.lock();
  114. task_allocator.free(p_task);
  115. } else {
  116. if (p_task->native_func) {
  117. p_task->native_func(p_task->native_func_userdata);
  118. } else if (p_task->template_userdata) {
  119. p_task->template_userdata->callback();
  120. memdelete(p_task->template_userdata);
  121. } else {
  122. p_task->callable.call();
  123. }
  124. task_mutex.lock();
  125. p_task->completed = true;
  126. p_task->pool_thread_index = -1;
  127. if (p_task->waiting_user) {
  128. p_task->done_semaphore.post(p_task->waiting_user);
  129. }
  130. // Let awaiters know.
  131. for (uint32_t i = 0; i < threads.size(); i++) {
  132. if (threads[i].awaited_task == p_task) {
  133. threads[i].cond_var.notify_one();
  134. threads[i].signaled = true;
  135. }
  136. }
  137. }
  138. #ifdef THREADS_ENABLED
  139. {
  140. curr_thread.current_task = prev_task;
  141. if (low_priority) {
  142. low_priority_threads_used--;
  143. if (_try_promote_low_priority_task()) {
  144. if (prev_task) { // Otherwise, this thread will catch it.
  145. _notify_threads(&curr_thread, 1, 0);
  146. }
  147. }
  148. }
  149. task_mutex.unlock();
  150. }
  151. set_current_thread_safe_for_nodes(safe_for_nodes_backup);
  152. MessageQueue::set_thread_singleton_override(call_queue_backup);
  153. #endif
  154. }
  155. void WorkerThreadPool::_thread_function(void *p_user) {
  156. ThreadData *thread_data = (ThreadData *)p_user;
  157. Thread::set_name(vformat("WorkerThread %d", thread_data->index));
  158. while (true) {
  159. Task *task_to_process = nullptr;
  160. {
  161. // Create the lock outside the inner loop so it isn't needlessly unlocked and relocked
  162. // when no task was found to process, and the loop is re-entered.
  163. MutexLock lock(thread_data->pool->task_mutex);
  164. while (true) {
  165. bool exit = thread_data->pool->_handle_runlevel(thread_data, lock);
  166. if (unlikely(exit)) {
  167. return;
  168. }
  169. thread_data->signaled = false;
  170. if (!thread_data->pool->task_queue.first()) {
  171. // There wasn't a task available yet.
  172. // Let's wait for the next notification, then recheck.
  173. thread_data->cond_var.wait(lock);
  174. continue;
  175. }
  176. // Got a task to process! Remove it from the queue, then break into the task handling section.
  177. task_to_process = thread_data->pool->task_queue.first()->self();
  178. thread_data->pool->task_queue.remove(thread_data->pool->task_queue.first());
  179. break;
  180. }
  181. }
  182. DEV_ASSERT(task_to_process);
  183. thread_data->pool->_process_task(task_to_process);
  184. }
  185. }
  186. void WorkerThreadPool::_post_tasks(Task **p_tasks, uint32_t p_count, bool p_high_priority, MutexLock<BinaryMutex> &p_lock, bool p_pump_task) {
  187. // Fall back to processing on the calling thread if there are no worker threads.
  188. // Separated into its own variable to make it easier to extend this logic
  189. // in custom builds.
  190. // Avoid calling pump tasks or low priority tasks from the calling thread.
  191. bool process_on_calling_thread = threads.is_empty() && !p_pump_task;
  192. if (process_on_calling_thread) {
  193. p_lock.temp_unlock();
  194. for (uint32_t i = 0; i < p_count; i++) {
  195. _process_task(p_tasks[i]);
  196. }
  197. p_lock.temp_relock();
  198. return;
  199. }
  200. while (runlevel == RUNLEVEL_EXIT_LANGUAGES) {
  201. control_cond_var.wait(p_lock);
  202. }
  203. uint32_t to_process = 0;
  204. uint32_t to_promote = 0;
  205. ThreadData *caller_pool_thread = thread_ids.has(Thread::get_caller_id()) ? &threads[thread_ids[Thread::get_caller_id()]] : nullptr;
  206. for (uint32_t i = 0; i < p_count; i++) {
  207. p_tasks[i]->low_priority = !p_high_priority;
  208. if (p_high_priority || low_priority_threads_used < max_low_priority_threads) {
  209. task_queue.add_last(&p_tasks[i]->task_elem);
  210. if (!p_high_priority) {
  211. low_priority_threads_used++;
  212. }
  213. to_process++;
  214. } else {
  215. // Too many threads using low priority, must go to queue.
  216. low_priority_task_queue.add_last(&p_tasks[i]->task_elem);
  217. to_promote++;
  218. }
  219. }
  220. _notify_threads(caller_pool_thread, to_process, to_promote);
  221. }
  222. void WorkerThreadPool::_notify_threads(const ThreadData *p_current_thread_data, uint32_t p_process_count, uint32_t p_promote_count) {
  223. uint32_t to_process = p_process_count;
  224. uint32_t to_promote = p_promote_count;
  225. // This is where which threads are awaken is decided according to the workload.
  226. // Threads that will anyway have a chance to check the situation and process/promote tasks
  227. // are excluded from being notified. Others will be tried anyway to try to distribute load.
  228. // The current thread, if is a pool thread, is also excluded depending on the promoting/processing
  229. // needs because it will anyway loop again. However, it will contribute to decreasing the count,
  230. // which helps reducing sync traffic.
  231. uint32_t thread_count = threads.size();
  232. // First round:
  233. // 1. For processing: notify threads that are not running tasks, to keep the stacks as shallow as possible.
  234. // 2. For promoting: since it's exclusive with processing, we fin threads able to promote low-prio tasks now.
  235. for (uint32_t i = 0;
  236. i < thread_count && (to_process || to_promote);
  237. i++, notify_index = (notify_index + 1) % thread_count) {
  238. ThreadData &th = threads[notify_index];
  239. if (th.signaled) {
  240. continue;
  241. }
  242. if (th.current_task) {
  243. // Good thread for promoting low-prio?
  244. if (to_promote && th.awaited_task && th.current_task->low_priority) {
  245. if (likely(&th != p_current_thread_data)) {
  246. th.cond_var.notify_one();
  247. }
  248. th.signaled = true;
  249. to_promote--;
  250. }
  251. } else {
  252. if (to_process) {
  253. if (likely(&th != p_current_thread_data)) {
  254. th.cond_var.notify_one();
  255. }
  256. th.signaled = true;
  257. to_process--;
  258. }
  259. }
  260. }
  261. // Second round:
  262. // For processing: if the first round wasn't enough, let's try now with threads processing tasks but currently awaiting.
  263. for (uint32_t i = 0;
  264. i < thread_count && to_process;
  265. i++, notify_index = (notify_index + 1) % thread_count) {
  266. ThreadData &th = threads[notify_index];
  267. if (th.signaled) {
  268. continue;
  269. }
  270. if (th.awaited_task) {
  271. if (likely(&th != p_current_thread_data)) {
  272. th.cond_var.notify_one();
  273. }
  274. th.signaled = true;
  275. to_process--;
  276. }
  277. }
  278. }
  279. bool WorkerThreadPool::_try_promote_low_priority_task() {
  280. if (low_priority_task_queue.first()) {
  281. Task *low_prio_task = low_priority_task_queue.first()->self();
  282. low_priority_task_queue.remove(low_priority_task_queue.first());
  283. task_queue.add_last(&low_prio_task->task_elem);
  284. low_priority_threads_used++;
  285. return true;
  286. } else {
  287. return false;
  288. }
  289. }
  290. WorkerThreadPool::TaskID WorkerThreadPool::add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority, const String &p_description) {
  291. return _add_task(Callable(), p_func, p_userdata, nullptr, p_high_priority, p_description);
  292. }
  293. 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, bool p_pump_task) {
  294. MutexLock<BinaryMutex> lock(task_mutex);
  295. // Get a free task
  296. Task *task = task_allocator.alloc();
  297. TaskID id = last_task++;
  298. task->self = id;
  299. task->callable = p_callable;
  300. task->native_func = p_func;
  301. task->native_func_userdata = p_userdata;
  302. task->description = p_description;
  303. task->template_userdata = p_template_userdata;
  304. task->is_pump_task = p_pump_task;
  305. tasks.insert(id, task);
  306. #ifdef THREADS_ENABLED
  307. if (p_pump_task) {
  308. pump_task_count++;
  309. int thread_count = get_thread_count();
  310. if (pump_task_count >= thread_count) {
  311. print_verbose(vformat("A greater number of dedicated threads were requested (%d) than threads available (%d). Please increase the number of available worker task threads. Recovering this session by spawning more worker task threads.", pump_task_count + 1, thread_count)); // +1 because we want to keep a Thread without any pump tasks free.
  312. // Re-sizing implies relocation, which is not supported for this array.
  313. CRASH_COND_MSG(thread_count + 1 > (int)threads.get_capacity(), "Reserve trick for worker thread pool failed. Crashing.");
  314. threads.resize_initialized(thread_count + 1);
  315. threads[thread_count].index = thread_count;
  316. threads[thread_count].pool = this;
  317. threads[thread_count].thread.start(&WorkerThreadPool::_thread_function, &threads[thread_count]);
  318. thread_ids.insert(threads[thread_count].thread.get_id(), thread_count);
  319. }
  320. }
  321. #endif
  322. _post_tasks(&task, 1, p_high_priority, lock, p_pump_task);
  323. return id;
  324. }
  325. WorkerThreadPool::TaskID WorkerThreadPool::add_task(const Callable &p_action, bool p_high_priority, const String &p_description, bool p_pump_task) {
  326. return _add_task(p_action, nullptr, nullptr, nullptr, p_high_priority, p_description, p_pump_task);
  327. }
  328. WorkerThreadPool::TaskID WorkerThreadPool::add_task_bind(const Callable &p_action, bool p_high_priority, const String &p_description) {
  329. return _add_task(p_action, nullptr, nullptr, nullptr, p_high_priority, p_description, false);
  330. }
  331. bool WorkerThreadPool::is_task_completed(TaskID p_task_id) const {
  332. MutexLock task_lock(task_mutex);
  333. const Task *const *taskp = tasks.getptr(p_task_id);
  334. if (!taskp) {
  335. ERR_FAIL_V_MSG(false, "Invalid Task ID"); // Invalid task
  336. }
  337. return (*taskp)->completed;
  338. }
  339. Error WorkerThreadPool::wait_for_task_completion(TaskID p_task_id) {
  340. task_mutex.lock();
  341. Task **taskp = tasks.getptr(p_task_id);
  342. if (!taskp) {
  343. task_mutex.unlock();
  344. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid Task ID"); // Invalid task
  345. }
  346. Task *task = *taskp;
  347. if (task->completed) {
  348. if (task->waiting_pool == 0 && task->waiting_user == 0) {
  349. tasks.erase(p_task_id);
  350. task_allocator.free(task);
  351. }
  352. task_mutex.unlock();
  353. return OK;
  354. }
  355. ThreadData *caller_pool_thread = thread_ids.has(Thread::get_caller_id()) ? &threads[thread_ids[Thread::get_caller_id()]] : nullptr;
  356. if (caller_pool_thread && p_task_id <= caller_pool_thread->current_task->self) {
  357. // Deadlock prevention:
  358. // When a pool thread wants to wait for an older task, the following situations can happen:
  359. // 1. Awaited task is deep in the stack of the awaiter.
  360. // 2. A group of awaiter threads end up depending on some tasks buried in the stack
  361. // of their worker threads in such a way that progress can't be made.
  362. // Both would entail a deadlock. Some may be handled here in the WorkerThreadPool
  363. // with some extra logic and bookkeeping. However, there would still be unavoidable
  364. // cases of deadlock because of the way waiting threads process outstanding tasks.
  365. // Taking into account there's no feasible solution for every possible case
  366. // with the current design, we just simply reject attempts to await on older tasks,
  367. // with a specific error code that signals the situation so the caller can handle it.
  368. task_mutex.unlock();
  369. return ERR_BUSY;
  370. }
  371. if (caller_pool_thread) {
  372. task->waiting_pool++;
  373. } else {
  374. task->waiting_user++;
  375. }
  376. if (caller_pool_thread) {
  377. task_mutex.unlock();
  378. _wait_collaboratively(caller_pool_thread, task);
  379. task_mutex.lock();
  380. task->waiting_pool--;
  381. if (task->waiting_pool == 0 && task->waiting_user == 0) {
  382. tasks.erase(p_task_id);
  383. task_allocator.free(task);
  384. }
  385. } else {
  386. task_mutex.unlock();
  387. task->done_semaphore.wait();
  388. task_mutex.lock();
  389. task->waiting_user--;
  390. if (task->waiting_pool == 0 && task->waiting_user == 0) {
  391. tasks.erase(p_task_id);
  392. task_allocator.free(task);
  393. }
  394. }
  395. task_mutex.unlock();
  396. return OK;
  397. }
  398. void WorkerThreadPool::_lock_unlockable_mutexes() {
  399. #ifdef THREADS_ENABLED
  400. for (uint32_t i = 0; i < MAX_UNLOCKABLE_LOCKS; i++) {
  401. if (unlockable_locks[i].ulock) {
  402. unlockable_locks[i].ulock->lock();
  403. }
  404. }
  405. #endif
  406. }
  407. void WorkerThreadPool::_unlock_unlockable_mutexes() {
  408. #ifdef THREADS_ENABLED
  409. for (uint32_t i = 0; i < MAX_UNLOCKABLE_LOCKS; i++) {
  410. if (unlockable_locks[i].ulock) {
  411. unlockable_locks[i].ulock->unlock();
  412. }
  413. }
  414. #endif
  415. }
  416. void WorkerThreadPool::_wait_collaboratively(ThreadData *p_caller_pool_thread, Task *p_task) {
  417. // Keep processing tasks until the condition to stop waiting is met.
  418. while (true) {
  419. Task *task_to_process = nullptr;
  420. bool relock_unlockables = false;
  421. {
  422. MutexLock lock(task_mutex);
  423. bool was_signaled = p_caller_pool_thread->signaled;
  424. p_caller_pool_thread->signaled = false;
  425. bool exit = _handle_runlevel(p_caller_pool_thread, lock);
  426. if (unlikely(exit)) {
  427. break;
  428. }
  429. bool wait_is_over = false;
  430. if (unlikely(p_task == ThreadData::YIELDING)) {
  431. if (p_caller_pool_thread->yield_is_over) {
  432. p_caller_pool_thread->yield_is_over = false;
  433. wait_is_over = true;
  434. }
  435. } else {
  436. if (p_task->completed) {
  437. wait_is_over = true;
  438. }
  439. }
  440. if (wait_is_over) {
  441. if (was_signaled) {
  442. // This thread was awaken for some additional reason, but it's about to exit.
  443. // Let's find out what may be pending and forward the requests.
  444. uint32_t to_process = task_queue.first() ? 1 : 0;
  445. uint32_t to_promote = p_caller_pool_thread->current_task->low_priority && low_priority_task_queue.first() ? 1 : 0;
  446. if (to_process || to_promote) {
  447. // This thread must be left alone since it won't loop again.
  448. p_caller_pool_thread->signaled = true;
  449. _notify_threads(p_caller_pool_thread, to_process, to_promote);
  450. }
  451. }
  452. break;
  453. }
  454. if (p_caller_pool_thread->current_task->low_priority && low_priority_task_queue.first()) {
  455. if (_try_promote_low_priority_task()) {
  456. _notify_threads(p_caller_pool_thread, 1, 0);
  457. }
  458. }
  459. if (p_caller_pool_thread->pool->task_queue.first()) {
  460. task_to_process = task_queue.first()->self();
  461. if ((p_task == ThreadData::YIELDING || p_caller_pool_thread->has_pump_task == true) && task_to_process->is_pump_task) {
  462. task_to_process = nullptr;
  463. _notify_threads(p_caller_pool_thread, 1, 0);
  464. } else {
  465. task_queue.remove(task_queue.first());
  466. }
  467. }
  468. if (!task_to_process) {
  469. p_caller_pool_thread->awaited_task = p_task;
  470. if (this == singleton) {
  471. _unlock_unlockable_mutexes();
  472. }
  473. relock_unlockables = true;
  474. p_caller_pool_thread->cond_var.wait(lock);
  475. p_caller_pool_thread->awaited_task = nullptr;
  476. }
  477. }
  478. if (relock_unlockables && this == singleton) {
  479. _lock_unlockable_mutexes();
  480. }
  481. if (task_to_process) {
  482. _process_task(task_to_process);
  483. }
  484. }
  485. }
  486. void WorkerThreadPool::_switch_runlevel(Runlevel p_runlevel) {
  487. DEV_ASSERT(p_runlevel > runlevel);
  488. runlevel = p_runlevel;
  489. memset(&runlevel_data, 0, sizeof(runlevel_data));
  490. for (uint32_t i = 0; i < threads.size(); i++) {
  491. threads[i].cond_var.notify_one();
  492. threads[i].signaled = true;
  493. }
  494. control_cond_var.notify_all();
  495. }
  496. // Returns whether threads have to exit. This may perform the check about handling needed.
  497. bool WorkerThreadPool::_handle_runlevel(ThreadData *p_thread_data, MutexLock<BinaryMutex> &p_lock) {
  498. bool exit = false;
  499. switch (runlevel) {
  500. case RUNLEVEL_NORMAL: {
  501. } break;
  502. case RUNLEVEL_PRE_EXIT_LANGUAGES: {
  503. if (!p_thread_data->pre_exited_languages) {
  504. if (!task_queue.first() && !low_priority_task_queue.first()) {
  505. p_thread_data->pre_exited_languages = true;
  506. runlevel_data.pre_exit_languages.num_idle_threads++;
  507. control_cond_var.notify_all();
  508. }
  509. }
  510. } break;
  511. case RUNLEVEL_EXIT_LANGUAGES: {
  512. if (!p_thread_data->exited_languages) {
  513. p_lock.temp_unlock();
  514. ScriptServer::thread_exit();
  515. p_lock.temp_relock();
  516. p_thread_data->exited_languages = true;
  517. runlevel_data.exit_languages.num_exited_threads++;
  518. control_cond_var.notify_all();
  519. }
  520. } break;
  521. case RUNLEVEL_EXIT: {
  522. exit = true;
  523. } break;
  524. }
  525. return exit;
  526. }
  527. void WorkerThreadPool::yield() {
  528. int th_index = get_thread_index();
  529. ERR_FAIL_COND_MSG(th_index == -1, "This function can only be called from a worker thread.");
  530. _wait_collaboratively(&threads[th_index], ThreadData::YIELDING);
  531. task_mutex.lock();
  532. if (runlevel < RUNLEVEL_EXIT_LANGUAGES) {
  533. // If this long-lived task started before the scripting server was initialized,
  534. // now is a good time to have scripting languages ready for the current thread.
  535. // Otherwise, such a piece of setup won't happen unless another task has been
  536. // run during the collaborative wait.
  537. task_mutex.unlock();
  538. ScriptServer::thread_enter();
  539. } else {
  540. task_mutex.unlock();
  541. }
  542. }
  543. void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {
  544. MutexLock task_lock(task_mutex);
  545. Task **taskp = tasks.getptr(p_task_id);
  546. if (!taskp) {
  547. ERR_FAIL_MSG("Invalid Task ID.");
  548. }
  549. Task *task = *taskp;
  550. if (task->pool_thread_index == -1) { // Completed or not started yet.
  551. if (!task->completed) {
  552. // This avoids a race condition where a task is created and yield-over called before it's processed.
  553. task->pending_notify_yield_over = true;
  554. }
  555. return;
  556. }
  557. ThreadData &td = threads[task->pool_thread_index];
  558. td.yield_is_over = true;
  559. td.signaled = true;
  560. td.cond_var.notify_one();
  561. }
  562. 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) {
  563. ERR_FAIL_COND_V(p_elements < 0, INVALID_TASK_ID);
  564. if (p_tasks < 0) {
  565. p_tasks = MAX(1u, threads.size());
  566. }
  567. MutexLock<BinaryMutex> lock(task_mutex);
  568. Group *group = group_allocator.alloc();
  569. GroupID id = last_task++;
  570. group->max = p_elements;
  571. group->self = id;
  572. Task **tasks_posted = nullptr;
  573. if (p_elements == 0) {
  574. // Should really not call it with zero Elements, but at least it should work.
  575. group->completed.set_to(true);
  576. group->done_semaphore.post();
  577. group->tasks_used = 0;
  578. p_tasks = 0;
  579. if (p_template_userdata) {
  580. memdelete(p_template_userdata);
  581. }
  582. } else {
  583. group->tasks_used = p_tasks;
  584. tasks_posted = (Task **)alloca(sizeof(Task *) * p_tasks);
  585. for (int i = 0; i < p_tasks; i++) {
  586. Task *task = task_allocator.alloc();
  587. task->native_group_func = p_func;
  588. task->native_func_userdata = p_userdata;
  589. task->description = p_description;
  590. task->group = group;
  591. task->callable = p_callable;
  592. task->template_userdata = p_template_userdata;
  593. tasks_posted[i] = task;
  594. // No task ID is used.
  595. }
  596. }
  597. groups[id] = group;
  598. _post_tasks(tasks_posted, p_tasks, p_high_priority, lock, false);
  599. return id;
  600. }
  601. 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) {
  602. return _add_group_task(Callable(), p_func, p_userdata, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  603. }
  604. WorkerThreadPool::GroupID WorkerThreadPool::add_group_task(const Callable &p_action, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  605. return _add_group_task(p_action, nullptr, nullptr, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  606. }
  607. uint32_t WorkerThreadPool::get_group_processed_element_count(GroupID p_group) const {
  608. MutexLock task_lock(task_mutex);
  609. const Group *const *groupp = groups.getptr(p_group);
  610. if (!groupp) {
  611. ERR_FAIL_V_MSG(0, "Invalid Group ID");
  612. }
  613. return (*groupp)->completed_index.get();
  614. }
  615. bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const {
  616. MutexLock task_lock(task_mutex);
  617. const Group *const *groupp = groups.getptr(p_group);
  618. if (!groupp) {
  619. ERR_FAIL_V_MSG(false, "Invalid Group ID");
  620. }
  621. return (*groupp)->completed.is_set();
  622. }
  623. void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) {
  624. #ifdef THREADS_ENABLED
  625. task_mutex.lock();
  626. Group **groupp = groups.getptr(p_group);
  627. task_mutex.unlock();
  628. if (!groupp) {
  629. ERR_FAIL_MSG("Invalid Group ID.");
  630. }
  631. {
  632. Group *group = *groupp;
  633. if (this == singleton) {
  634. _unlock_unlockable_mutexes();
  635. }
  636. group->done_semaphore.wait();
  637. if (this == singleton) {
  638. _lock_unlockable_mutexes();
  639. }
  640. 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.
  641. uint32_t finished_users = group->finished.increment(); // fetch happens before inc, so increment later.
  642. if (finished_users == max_users) {
  643. // All tasks using this group are gone (finished before the group), so clear the group too.
  644. MutexLock task_lock(task_mutex);
  645. group_allocator.free(group);
  646. }
  647. }
  648. MutexLock task_lock(task_mutex); // This mutex is needed when Physics 2D and/or 3D is selected to run on a separate thread.
  649. groups.erase(p_group);
  650. #endif
  651. }
  652. int WorkerThreadPool::get_thread_index() const {
  653. Thread::ID tid = Thread::get_caller_id();
  654. return thread_ids.has(tid) ? thread_ids[tid] : -1;
  655. }
  656. WorkerThreadPool::TaskID WorkerThreadPool::get_caller_task_id() const {
  657. int th_index = get_thread_index();
  658. if (th_index != -1 && threads[th_index].current_task) {
  659. return threads[th_index].current_task->self;
  660. } else {
  661. return INVALID_TASK_ID;
  662. }
  663. }
  664. WorkerThreadPool::GroupID WorkerThreadPool::get_caller_group_id() const {
  665. int th_index = get_thread_index();
  666. if (th_index != -1 && threads[th_index].current_task && threads[th_index].current_task->group) {
  667. return threads[th_index].current_task->group->self;
  668. } else {
  669. return INVALID_TASK_ID;
  670. }
  671. }
  672. #ifdef THREADS_ENABLED
  673. uint32_t WorkerThreadPool::_thread_enter_unlock_allowance_zone(THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &p_ulock) {
  674. for (uint32_t i = 0; i < MAX_UNLOCKABLE_LOCKS; i++) {
  675. DEV_ASSERT((bool)unlockable_locks[i].ulock == (bool)unlockable_locks[i].rc);
  676. if (unlockable_locks[i].ulock == &p_ulock) {
  677. // Already registered in the current thread.
  678. unlockable_locks[i].rc++;
  679. return i;
  680. } else if (!unlockable_locks[i].ulock) {
  681. unlockable_locks[i].ulock = &p_ulock;
  682. unlockable_locks[i].rc = 1;
  683. return i;
  684. }
  685. }
  686. ERR_FAIL_V_MSG(UINT32_MAX, "No more unlockable lock slots available. Engine bug.");
  687. }
  688. void WorkerThreadPool::thread_exit_unlock_allowance_zone(uint32_t p_zone_id) {
  689. DEV_ASSERT(unlockable_locks[p_zone_id].ulock && unlockable_locks[p_zone_id].rc);
  690. unlockable_locks[p_zone_id].rc--;
  691. if (unlockable_locks[p_zone_id].rc == 0) {
  692. unlockable_locks[p_zone_id].ulock = nullptr;
  693. }
  694. }
  695. #endif
  696. void WorkerThreadPool::init(int p_thread_count, float p_low_priority_task_ratio) {
  697. ERR_FAIL_COND(threads.size() > 0);
  698. runlevel = RUNLEVEL_NORMAL;
  699. if (p_thread_count < 0) {
  700. p_thread_count = OS::get_singleton()->get_default_thread_pool_size();
  701. }
  702. max_low_priority_threads = CLAMP(p_thread_count * p_low_priority_task_ratio, 1, p_thread_count - 1);
  703. print_verbose(vformat("WorkerThreadPool: %d threads, %d max low-priority.", p_thread_count, max_low_priority_threads));
  704. #ifdef THREADS_ENABLED
  705. // Reserve 5 threads in case we need separate threads for 1) 2D physics 2) 3D physics 3) rendering 4) GPU texture compression, 5) all other tasks.
  706. // We cannot safely increase the Vector size at runtime, so reserve enough up front, but only launch those needed.
  707. threads.reserve(5);
  708. #endif
  709. threads.resize(p_thread_count);
  710. for (uint32_t i = 0; i < threads.size(); i++) {
  711. threads[i].index = i;
  712. threads[i].pool = this;
  713. threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i]);
  714. thread_ids.insert(threads[i].thread.get_id(), i);
  715. }
  716. }
  717. void WorkerThreadPool::exit_languages_threads() {
  718. if (threads.is_empty()) {
  719. return;
  720. }
  721. MutexLock lock(task_mutex);
  722. // Wait until all threads are idle.
  723. _switch_runlevel(RUNLEVEL_PRE_EXIT_LANGUAGES);
  724. while (runlevel_data.pre_exit_languages.num_idle_threads != threads.size()) {
  725. control_cond_var.wait(lock);
  726. }
  727. // Wait until all threads have detached from scripting languages.
  728. _switch_runlevel(RUNLEVEL_EXIT_LANGUAGES);
  729. while (runlevel_data.exit_languages.num_exited_threads != threads.size()) {
  730. control_cond_var.wait(lock);
  731. }
  732. }
  733. void WorkerThreadPool::finish() {
  734. if (threads.is_empty()) {
  735. return;
  736. }
  737. {
  738. MutexLock lock(task_mutex);
  739. SelfList<Task> *E = low_priority_task_queue.first();
  740. while (E) {
  741. print_error("Task waiting was never re-claimed: " + E->self()->description);
  742. E = E->next();
  743. }
  744. _switch_runlevel(RUNLEVEL_EXIT);
  745. }
  746. for (ThreadData &data : threads) {
  747. data.thread.wait_to_finish();
  748. }
  749. {
  750. MutexLock lock(task_mutex);
  751. for (KeyValue<TaskID, Task *> &E : tasks) {
  752. task_allocator.free(E.value);
  753. }
  754. }
  755. threads.clear();
  756. }
  757. void WorkerThreadPool::_bind_methods() {
  758. ClassDB::bind_method(D_METHOD("add_task", "action", "high_priority", "description"), &WorkerThreadPool::add_task_bind, DEFVAL(false), DEFVAL(String()));
  759. ClassDB::bind_method(D_METHOD("is_task_completed", "task_id"), &WorkerThreadPool::is_task_completed);
  760. ClassDB::bind_method(D_METHOD("wait_for_task_completion", "task_id"), &WorkerThreadPool::wait_for_task_completion);
  761. ClassDB::bind_method(D_METHOD("get_caller_task_id"), &WorkerThreadPool::get_caller_task_id);
  762. 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()));
  763. ClassDB::bind_method(D_METHOD("is_group_task_completed", "group_id"), &WorkerThreadPool::is_group_task_completed);
  764. ClassDB::bind_method(D_METHOD("get_group_processed_element_count", "group_id"), &WorkerThreadPool::get_group_processed_element_count);
  765. ClassDB::bind_method(D_METHOD("wait_for_group_task_completion", "group_id"), &WorkerThreadPool::wait_for_group_task_completion);
  766. ClassDB::bind_method(D_METHOD("get_caller_group_id"), &WorkerThreadPool::get_caller_group_id);
  767. }
  768. WorkerThreadPool *WorkerThreadPool::get_named_pool(const StringName &p_name) {
  769. WorkerThreadPool **pool_ptr = named_pools.getptr(p_name);
  770. if (pool_ptr) {
  771. return *pool_ptr;
  772. } else {
  773. WorkerThreadPool *pool = memnew(WorkerThreadPool(false));
  774. pool->init();
  775. named_pools[p_name] = pool;
  776. return pool;
  777. }
  778. }
  779. WorkerThreadPool::WorkerThreadPool(bool p_singleton) {
  780. if (p_singleton) {
  781. singleton = this;
  782. }
  783. }
  784. WorkerThreadPool::~WorkerThreadPool() {
  785. finish();
  786. if (this == singleton) {
  787. singleton = nullptr;
  788. for (KeyValue<StringName, WorkerThreadPool *> &E : named_pools) {
  789. E.value->finish();
  790. memdelete(E.value);
  791. }
  792. named_pools.clear();
  793. }
  794. }