Skip to content

Commit 9bc49a6

Browse files
authored
Merge pull request godotengine#90865 from RandomShaper/wtp_re_fix_yield
WorkerThreadPool: Fix yield-over for not-yet-started tasks
2 parents 2efbc6b + 8192d1c commit 9bc49a6

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

core/object/worker_thread_pool.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ void WorkerThreadPool::_process_task(Task *p_task) {
7272
p_task->pool_thread_index = pool_thread_index;
7373
prev_task = curr_thread.current_task;
7474
curr_thread.current_task = p_task;
75+
if (p_task->pending_notify_yield_over) {
76+
curr_thread.yield_is_over = true;
77+
}
7578
task_mutex.unlock();
7679
}
7780
#endif
@@ -491,7 +494,11 @@ void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {
491494
ERR_FAIL_MSG("Invalid Task ID.");
492495
}
493496
Task *task = *taskp;
494-
if (task->completed) {
497+
if (task->pool_thread_index == -1) { // Completed or not started yet.
498+
if (!task->completed) {
499+
// This avoids a race condition where a task is created and yield-over called before it's processed.
500+
task->pending_notify_yield_over = true;
501+
}
495502
task_mutex.unlock();
496503
return;
497504
}

core/object/worker_thread_pool.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ class WorkerThreadPool : public Object {
8181
void *native_func_userdata = nullptr;
8282
String description;
8383
Semaphore done_semaphore; // For user threads awaiting.
84-
bool completed = false;
84+
bool completed : 1;
85+
bool pending_notify_yield_over : 1;
8586
Group *group = nullptr;
8687
SelfList<Task> task_elem;
8788
uint32_t waiting_pool = 0;
@@ -92,6 +93,8 @@ class WorkerThreadPool : public Object {
9293

9394
void free_template_userdata();
9495
Task() :
96+
completed(false),
97+
pending_notify_yield_over(false),
9598
task_elem(this) {}
9699
};
97100

0 commit comments

Comments
 (0)