Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WorkerThreadPool: Fix yield-over for not-yet-started tasks #90865

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion core/object/worker_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ void WorkerThreadPool::_process_task(Task *p_task) {
p_task->pool_thread_index = pool_thread_index;
prev_task = curr_thread.current_task;
curr_thread.current_task = p_task;
if (p_task->pending_notify_yield_over) {
curr_thread.yield_is_over = true;
}
task_mutex.unlock();
}
#endif
Expand Down Expand Up @@ -491,7 +494,11 @@ void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {
ERR_FAIL_MSG("Invalid Task ID.");
}
Task *task = *taskp;
if (task->completed) {
if (task->pool_thread_index == -1) { // Completed or not started yet.
if (!task->completed) {
// This avoids a race condition where a task is created and yield-over called before it's processed.
task->pending_notify_yield_over = true;
}
task_mutex.unlock();
return;
}
Expand Down
5 changes: 4 additions & 1 deletion core/object/worker_thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class WorkerThreadPool : public Object {
void *native_func_userdata = nullptr;
String description;
Semaphore done_semaphore; // For user threads awaiting.
bool completed = false;
bool completed : 1;
bool pending_notify_yield_over : 1;
Group *group = nullptr;
SelfList<Task> task_elem;
uint32_t waiting_pool = 0;
Expand All @@ -92,6 +93,8 @@ class WorkerThreadPool : public Object {

void free_template_userdata();
Task() :
completed(false),
pending_notify_yield_over(false),
task_elem(this) {}
};

Expand Down
Loading