From b81c9390f44289225aadebdf0f13ef80cb05eda4 Mon Sep 17 00:00:00 2001 From: Olivier Houchard Date: Tue, 29 Apr 2025 15:46:20 +0200 Subject: [PATCH] MEDIUM: tasks: Mutualize the TASK_KILLED code between tasks and tasklets The code to handle a task/tasklet when it's been killed before it were to run is mostly identical, so move it outside of task and tasklet specific code, and inside the common code. This commit is just cosmetic, and should have no impact. --- src/task.c | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/task.c b/src/task.c index c7abcdbcc..664742704 100644 --- a/src/task.c +++ b/src/task.c @@ -613,22 +613,26 @@ unsigned int run_tasks_from_lists(unsigned int budgets[]) } } while (!_HA_ATOMIC_CAS(&t->state, &state, (state & TASK_PERSISTENT) | TASK_RUNNING)); + if (unlikely((state & TASK_KILLED) || process == NULL)) { + /* Task or tasklet has been killed, let's remove it */ + if (t->state & TASK_F_TASKLET) + pool_free(pool_head_tasklet, t); + else { + task_unlink_wq(t); + __task_free(t); + } + /* We don't want max_processed to be decremented if + * we're just freeing a destroyed task, we should only + * do so if we really ran a task. + */ + goto next; + } if (t->state & TASK_F_TASKLET) { /* this is a tasklet */ - if (likely(!(state & TASK_KILLED))) { - t = process(t, ctx, state); - if (t != NULL) - _HA_ATOMIC_AND(&t->state, ~TASK_RUNNING); - } - else { - pool_free(pool_head_tasklet, t); - /* We don't want max_processed to be decremented if - * we're just freeing a destroyed tasklet, we should - * only do so if we really ran a tasklet. - */ - goto next; - } + t = process(t, ctx, state); + if (t != NULL) + _HA_ATOMIC_AND(&t->state, ~TASK_RUNNING); } else { /* This is a regular task */ __ha_barrier_atomic_store(); @@ -639,19 +643,10 @@ unsigned int run_tasks_from_lists(unsigned int budgets[]) * directly free the task. Otherwise it will be seen after processing and * it's freed on the exit path. */ - if (likely(!(state & TASK_KILLED) && process == process_stream)) + if (process == process_stream) t = process_stream(t, ctx, state); - else if (!(state & TASK_KILLED) && process != NULL) + else t = process(t, ctx, state); - else { - task_unlink_wq(t); - __task_free(t); - /* We don't want max_processed to be decremented if - * we're just freeing a destroyed task, we should only - * do so if we really ran a task. - */ - goto next; - } /* If there is a pending state we have to wake up the task * immediately, else we defer it into wait queue -- 2.39.5