From eed3911a54aedeedb1193f6a3450cefe588f1375 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 15 Jun 2022 17:20:16 +0200 Subject: [PATCH] MINOR: task: replace task_set_affinity() with task_set_thread() The latter passes a thread ID instead of a mask, making the code simpler. --- doc/internals/api/scheduler.txt | 8 +++++--- include/haproxy/applet.h | 2 +- include/haproxy/task.h | 18 +++++++++--------- src/check.c | 4 ++-- src/extcheck.c | 4 ++-- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/doc/internals/api/scheduler.txt b/doc/internals/api/scheduler.txt index 34695436b..dd1ad5f07 100644 --- a/doc/internals/api/scheduler.txt +++ b/doc/internals/api/scheduler.txt @@ -91,10 +91,12 @@ void task_queue(t) function for local tasks or for shared tasks who have the calling thread in their thread mask. -void task_set_affinity(t, m) - Change task 's thread_mask to new value . This may only be +void task_set_thread(t, id) + Change task 's thread ID to new value . This may only be performed by the task itself while running. This is only used to let a - task voluntarily migrate to another thread. + task voluntarily migrate to another thread. Thread id -1 is used to + indicate "any thread". It's ignored and replaced by zero when threads + are disabled. void tasklet_wakeup(tl) Make sure that tasklet will wake up, that is, will execute at diff --git a/include/haproxy/applet.h b/include/haproxy/applet.h index 1fb3b260d..752a1a2b2 100644 --- a/include/haproxy/applet.h +++ b/include/haproxy/applet.h @@ -66,7 +66,7 @@ static inline int appctx_init(struct appctx *appctx) * eventually be created. The affinity must be set now ! */ BUG_ON(appctx->t->tid != tid); - task_set_affinity(appctx->t, tid_bit); + task_set_thread(appctx->t, tid); if (appctx->applet->init) return appctx->applet->init(appctx); diff --git a/include/haproxy/task.h b/include/haproxy/task.h index a753cd90b..d1613c047 100644 --- a/include/haproxy/task.h +++ b/include/haproxy/task.h @@ -324,22 +324,22 @@ static inline void task_queue(struct task *task) } } -/* change the thread affinity of a task to . +/* Change the thread affinity of a task to , which may either be a valid + * thread number from 0 to nbthread-1, or a negative value to allow the task + * to run on any thread. + * * This may only be done from within the running task itself or during its * initialization. It will unqueue and requeue the task from the wait queue * if it was in it. This is safe against a concurrent task_queue() call because * task_queue() itself will unlink again if needed after taking into account * the new thread_mask. */ -static inline void task_set_affinity(struct task *t, unsigned long thread_mask) +static inline void task_set_thread(struct task *t, int thr) { - int thr; - - if (atleast2(thread_mask)) - thr = -1; - else - thr = my_ffsl(thread_mask) - 1; - +#ifndef USE_THREAD + /* no shared queue without threads */ + thr = 0; +#endif if (unlikely(task_in_wq(t))) { task_unlink_wq(t); t->tid = thr; diff --git a/src/check.c b/src/check.c index ada79ed55..2366fc5ae 100644 --- a/src/check.c +++ b/src/check.c @@ -1118,7 +1118,7 @@ struct task *process_chk_conn(struct task *t, void *context, unsigned int state) check->state |= CHK_ST_INPROGRESS; TRACE_STATE("init new health-check", CHK_EV_TASK_WAKE|CHK_EV_HCHK_START, check); - task_set_affinity(t, tid_bit); + task_set_thread(t, tid); check->current_step = NULL; @@ -1217,7 +1217,7 @@ struct task *process_chk_conn(struct task *t, void *context, unsigned int state) if (LIST_INLIST(&check->buf_wait.list)) LIST_DEL_INIT(&check->buf_wait.list); - task_set_affinity(t, all_threads_mask); + task_set_thread(t, -1); check_release_buf(check, &check->bi); check_release_buf(check, &check->bo); check->state &= ~(CHK_ST_INPROGRESS|CHK_ST_IN_ALLOC|CHK_ST_OUT_ALLOC); diff --git a/src/extcheck.c b/src/extcheck.c index 7b3dbbaf1..6b5b2b685 100644 --- a/src/extcheck.c +++ b/src/extcheck.c @@ -525,7 +525,7 @@ struct task *process_chk_proc(struct task *t, void *context, unsigned int state) int t_con = tick_add(now_ms, s->proxy->timeout.connect); t->expire = tick_first(t->expire, t_con); } - task_set_affinity(t, tid_bit); + task_set_thread(t, tid); goto reschedule; } @@ -588,7 +588,7 @@ struct task *process_chk_proc(struct task *t, void *context, unsigned int state) /* a success was detected */ check_notify_success(check); } - task_set_affinity(t, 1); + task_set_thread(t, 0); check->state &= ~CHK_ST_INPROGRESS; pid_list_del(check->curpid);