Skip to content

Commit

Permalink
task: Fix deadlock between thread and iio_task_stop()
Browse files Browse the repository at this point in the history
It is possible that iio_task_stop() will be called even before the
iio_task_run() thread enters the while() loop, where it waits for tasks;
in that case, both threads would be waiting on the condition variable
without anyone signaling it.

Address that issue by signaling the condition variable at the beginning
of the for() loop, and not at the end.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
  • Loading branch information
pcercuei committed Dec 5, 2023
1 parent 4292df5 commit 40d5514
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions task.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ static int iio_task_run(void *d)
iio_mutex_lock(task->lock);

for (;;) {
/* Signal that we're idle */
iio_cond_signal(task->cond);

while (!task->stop && !(task->list && task->running)) {
iio_cond_wait(task->cond, task->lock, 0);

Expand Down Expand Up @@ -77,9 +80,7 @@ static int iio_task_run(void *d)
if (autoclear)
iio_task_token_destroy(entry);

/* Signal that we're done with the previous entry */
iio_mutex_lock(task->lock);
iio_cond_signal(task->cond);
}

iio_mutex_unlock(task->lock);
Expand Down

0 comments on commit 40d5514

Please sign in to comment.