@@ -313,23 +313,20 @@ static scond_t *worker_cond = NULL;
313313static sthread_t * worker_thread = NULL ;
314314static bool worker_continue = true; /* use running_lock when touching it */
315315
316+ /* 'queue_lock' must be held for the duration of this function */
316317static void task_queue_remove (task_queue_t * queue , retro_task_t * task )
317318{
318319 retro_task_t * t = NULL ;
319320 retro_task_t * front = NULL ;
320321
321- slock_lock (queue_lock );
322322 front = queue -> front ;
323- slock_unlock (queue_lock );
324323
325324 /* Remove first element if needed */
326325 if (task == front )
327326 {
328- slock_lock (queue_lock );
329327 queue -> front = task -> next ;
330328 if (queue -> back == task ) /* if only element, also update back */
331329 queue -> back = NULL ;
332- slock_unlock (queue_lock );
333330 task -> next = NULL ;
334331 return ;
335332 }
@@ -348,10 +345,8 @@ static void task_queue_remove(task_queue_t *queue, retro_task_t *task)
348345 /* When removing the tail of the queue, update the tail pointer */
349346 if (queue -> back == task )
350347 {
351- slock_lock (queue_lock );
352348 if (queue -> back == task )
353349 queue -> back = t ;
354- slock_unlock (queue_lock );
355350 }
356351 break ;
357352 }
@@ -506,18 +501,31 @@ static void threaded_worker(void *userdata)
506501 finished = task -> finished ;
507502 slock_unlock (property_lock );
508503
509- slock_lock (running_lock );
510- task_queue_remove (& tasks_running , task );
511- slock_unlock (running_lock );
512-
513504 /* Update queue */
514505 if (!finished )
515506 {
516- /* Re-add task to running queue */
517- retro_task_threaded_push_running (task );
507+ /* Move the task to the back of the queue */
508+ /* mimics retro_task_threaded_push_running, but also includes a task_queue_remove */
509+ slock_lock (running_lock );
510+ slock_lock (queue_lock );
511+ if (task -> next != NULL ) /* do nothing if only item in queue */
512+ {
513+ task_queue_remove (& tasks_running , task );
514+ task_queue_put (& tasks_running , task );
515+ scond_signal (worker_cond );
516+ }
517+ slock_unlock (queue_lock );
518+ slock_unlock (running_lock );
518519 }
519520 else
520521 {
522+ /* Remove task from running queue */
523+ slock_lock (running_lock );
524+ slock_lock (queue_lock );
525+ task_queue_remove (& tasks_running , task );
526+ slock_unlock (queue_lock );
527+ slock_unlock (running_lock );
528+
521529 /* Add task to finished queue */
522530 slock_lock (finished_lock );
523531 task_queue_put (& tasks_finished , task );
0 commit comments