diff options
| author | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2024-04-18 19:05:44 +0200 |
|---|---|---|
| committer | Pedro J. Estébanez <pedrojrulez@gmail.com> | 2024-04-18 19:25:19 +0200 |
| commit | 8192d1cbc7cb2c079ae07da692e0b7447c319b37 (patch) | |
| tree | ee1d65d5f1e989cbd0475785f027727701acc654 /core/object | |
| parent | 2efbc6bfb3b4f49a6bc75b3d367cfe81eeddbf3a (diff) | |
| download | redot-engine-8192d1cbc7cb2c079ae07da692e0b7447c319b37.tar.gz | |
WorkerThreadPool: Fix yield-over for not-yet-started tasks
Diffstat (limited to 'core/object')
| -rw-r--r-- | core/object/worker_thread_pool.cpp | 9 | ||||
| -rw-r--r-- | core/object/worker_thread_pool.h | 5 |
2 files changed, 12 insertions, 2 deletions
diff --git a/core/object/worker_thread_pool.cpp b/core/object/worker_thread_pool.cpp index e0b8730a67..9c9e0fa899 100644 --- a/core/object/worker_thread_pool.cpp +++ b/core/object/worker_thread_pool.cpp @@ -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 @@ -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; } diff --git a/core/object/worker_thread_pool.h b/core/object/worker_thread_pool.h index 64f24df79f..a9cf260a0f 100644 --- a/core/object/worker_thread_pool.h +++ b/core/object/worker_thread_pool.h @@ -81,7 +81,8 @@ private: 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; @@ -92,6 +93,8 @@ private: void free_template_userdata(); Task() : + completed(false), + pending_notify_yield_over(false), task_elem(this) {} }; |
