From 23ba478f821fc895b43766d0be5a8d96e371c6cf Mon Sep 17 00:00:00 2001 From: Jamiras Date: Sat, 4 Apr 2020 09:37:55 -0600 Subject: [PATCH] fix race condition where task could momentarily not be in the queue when reordering --- libretro-common/queues/task_queue.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libretro-common/queues/task_queue.c b/libretro-common/queues/task_queue.c index 6833f1bfbf..792339b11a 100644 --- a/libretro-common/queues/task_queue.c +++ b/libretro-common/queues/task_queue.c @@ -506,18 +506,28 @@ static void threaded_worker(void *userdata) finished = task->finished; slock_unlock(property_lock); - slock_lock(running_lock); - task_queue_remove(&tasks_running, task); - slock_unlock(running_lock); - /* Update queue */ if (!finished) { - /* Re-add task to running queue */ - retro_task_threaded_push_running(task); + /* Move the task to the back of the queue */ + /* mimics retro_task_threaded_push_running, but also includes a task_queue_remove */ + slock_lock(running_lock); + slock_lock(queue_lock); + if (task->next != NULL) /* do nothing if only item in queue */ + { + task_queue_remove(&tasks_running, task); + task_queue_put(&tasks_running, task); + } + slock_unlock(queue_lock); + slock_unlock(running_lock); } else { + /* Remove task from running queue */ + slock_lock(running_lock); + task_queue_remove(&tasks_running, task); + slock_unlock(running_lock); + /* Add task to finished queue */ slock_lock(finished_lock); task_queue_put(&tasks_finished, task);