From 6036342f58bb350e4d95cb63d1567439b71165b7 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 1 Oct 2021 16:29:27 +0200 Subject: [PATCH] MINOR: thread: make "ti" a const pointer and clean up thread_info a bit We want to make sure that the current thread_info accessed via "ti" will remain constant, so that we don't accidentally place new variable parts there and so that the compiler knows that info retrieved from there is not expected to have changed between two function calls. Only a few init locations had to be adjusted to use the array and the rest is unaffected. --- include/haproxy/tinfo-t.h | 6 ++++-- include/haproxy/tinfo.h | 2 +- src/thread.c | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/haproxy/tinfo-t.h b/include/haproxy/tinfo-t.h index 737431f7a..fe9ef9bd0 100644 --- a/include/haproxy/tinfo-t.h +++ b/include/haproxy/tinfo-t.h @@ -39,11 +39,13 @@ enum { #define TH_FL_STUCK 0x00000001 /* This structure describes all the per-thread info we need. When threads are - * disabled, it contains the same info for the single running thread. + * disabled, it contains the same info for the single running thread. This is + * stable across all of a thread's life, and is being pointed to by the + * thread-local "ti" pointer. */ struct thread_info { /* pad to cache line (64B) */ - char __pad[0]; /* unused except to check remaining room */ + char __pad[0]; /* unused except to check remaining room */ char __end[0] __attribute__((aligned(64))); }; diff --git a/include/haproxy/tinfo.h b/include/haproxy/tinfo.h index 6e73824cb..a1b73463f 100644 --- a/include/haproxy/tinfo.h +++ b/include/haproxy/tinfo.h @@ -27,7 +27,7 @@ /* the structs are in thread.c */ extern struct thread_info ha_thread_info[MAX_THREADS]; -extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */ +extern THREAD_LOCAL const struct thread_info *ti; /* thread_info for the current thread */ extern struct thread_ctx ha_thread_ctx[MAX_THREADS]; extern THREAD_LOCAL struct thread_ctx *th_ctx; /* ha_thread_ctx for the current thread */ diff --git a/src/thread.c b/src/thread.c index ab9ef90b6..14bf21eba 100644 --- a/src/thread.c +++ b/src/thread.c @@ -51,7 +51,7 @@ #include struct thread_info ha_thread_info[MAX_THREADS] = { }; -THREAD_LOCAL struct thread_info *ti = &ha_thread_info[0]; +THREAD_LOCAL const struct thread_info *ti = &ha_thread_info[0]; struct thread_ctx ha_thread_ctx[MAX_THREADS] = { }; THREAD_LOCAL struct thread_ctx *th_ctx = &ha_thread_ctx[0];