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