Partial fixes for threading/worker_pool/max_threads = 0

This commit is contained in:
myaaaaaaaaa 2023-02-03 23:41:43 -05:00
parent 65778525bf
commit fcd8ce52e9
1 changed files with 13 additions and 7 deletions

View File

@ -165,11 +165,24 @@ void WorkerThreadPool::_native_low_priority_thread_function(void *p_user) {
} }
void WorkerThreadPool::_post_task(Task *p_task, bool p_high_priority) { void WorkerThreadPool::_post_task(Task *p_task, bool p_high_priority) {
// Fall back to processing on the calling thread if there are no worker threads.
// Separated into its own variable to make it easier to extend this logic
// in custom builds.
bool process_on_calling_thread = threads.size() == 0;
if (process_on_calling_thread) {
_process_task(p_task);
return;
}
task_mutex.lock(); task_mutex.lock();
p_task->low_priority = !p_high_priority; p_task->low_priority = !p_high_priority;
if (!p_high_priority && use_native_low_priority_threads) { if (!p_high_priority && use_native_low_priority_threads) {
p_task->low_priority_thread = native_thread_allocator.alloc(); p_task->low_priority_thread = native_thread_allocator.alloc();
task_mutex.unlock(); task_mutex.unlock();
if (p_task->group) {
p_task->group->low_priority_native_tasks.push_back(p_task);
}
p_task->low_priority_thread->start(_native_low_priority_thread_function, p_task); // Pask task directly to thread. p_task->low_priority_thread->start(_native_low_priority_thread_function, p_task); // Pask task directly to thread.
} else if (p_high_priority || low_priority_threads_used < max_low_priority_threads) { } else if (p_high_priority || low_priority_threads_used < max_low_priority_threads) {
task_queue.add_last(&p_task->task_elem); task_queue.add_last(&p_task->task_elem);
@ -324,15 +337,8 @@ WorkerThreadPool::GroupID WorkerThreadPool::_add_group_task(const Callable &p_ca
groups[id] = group; groups[id] = group;
task_mutex.unlock(); task_mutex.unlock();
if (!p_high_priority && use_native_low_priority_threads) {
group->low_priority_native_tasks.resize(p_tasks);
}
for (int i = 0; i < p_tasks; i++) { for (int i = 0; i < p_tasks; i++) {
_post_task(tasks_posted[i], p_high_priority); _post_task(tasks_posted[i], p_high_priority);
if (!p_high_priority && use_native_low_priority_threads) {
group->low_priority_native_tasks[i] = tasks_posted[i];
}
} }
return id; return id;