From: Willy Tarreau Date: Wed, 6 Oct 2021 20:44:28 +0000 (+0200) Subject: REORG: thread: move ha_get_pthread_id() to thread.c X-Git-Tag: v2.5-dev9~50 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4eeb88363c6bc3e1b26b77e138afed2b289f6954;p=thirdparty%2Fhaproxy.git REORG: thread: move ha_get_pthread_id() to thread.c It's the last function which directly accesses the pthread_t, let's move it to thread.c and leave a static inline for non-thread. --- diff --git a/include/haproxy/thread.h b/include/haproxy/thread.h index a8b04784b7..1881fa98df 100644 --- a/include/haproxy/thread.h +++ b/include/haproxy/thread.h @@ -163,6 +163,11 @@ static inline void set_thread_cpu_affinity() { } +static inline unsigned long long ha_get_pthread_id(unsigned int thr) +{ + return 0; +} + #else /* !USE_THREAD */ /********************** THREADS ENABLED ************************/ @@ -181,6 +186,7 @@ void ha_rwlock_init(HA_RWLOCK_T *l); void setup_extra_threads(void *(*handler)(void *)); void wait_for_threads_completion(); void set_thread_cpu_affinity(); +unsigned long long ha_get_pthread_id(unsigned int thr); extern volatile unsigned long all_threads_mask; extern volatile unsigned long threads_harmless_mask; diff --git a/include/haproxy/tinfo.h b/include/haproxy/tinfo.h index 40b64b0e83..be6cddf171 100644 --- a/include/haproxy/tinfo.h +++ b/include/haproxy/tinfo.h @@ -29,38 +29,4 @@ extern struct thread_info ha_thread_info[MAX_THREADS]; extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */ -/* Retrieves the opaque pthread_t of thread cast to an unsigned long long - * since POSIX took great care of not specifying its representation, making it - * hard to export for post-mortem analysis. For this reason we copy it into a - * union and will use the smallest scalar type at least as large as its size, - * which will keep endianness and alignment for all regular sizes. As a last - * resort we end up with a long long ligned to the first bytes in memory, which - * will be endian-dependent if pthread_t is larger than a long long (not seen - * yet). - */ -static inline unsigned long long ha_get_pthread_id(unsigned int thr) -{ -#ifdef USE_THREAD - union { - pthread_t t; - unsigned long long ll; - unsigned int i; - unsigned short s; - unsigned char c; - } u = { 0 }; - - u.t = ha_thread_info[thr].pthread; - - if (sizeof(u.t) <= sizeof(u.c)) - return u.c; - else if (sizeof(u.t) <= sizeof(u.s)) - return u.s; - else if (sizeof(u.t) <= sizeof(u.i)) - return u.i; - return u.ll; -#else - return 0; -#endif -} - #endif /* _HAPROXY_TINFO_H */ diff --git a/src/thread.c b/src/thread.c index 25ee3ab947..c0b7bdbad1 100644 --- a/src/thread.c +++ b/src/thread.c @@ -254,6 +254,36 @@ void set_thread_cpu_affinity() #endif /* USE_CPU_AFFINITY */ } +/* Retrieves the opaque pthread_t of thread cast to an unsigned long long + * since POSIX took great care of not specifying its representation, making it + * hard to export for post-mortem analysis. For this reason we copy it into a + * union and will use the smallest scalar type at least as large as its size, + * which will keep endianness and alignment for all regular sizes. As a last + * resort we end up with a long long ligned to the first bytes in memory, which + * will be endian-dependent if pthread_t is larger than a long long (not seen + * yet). + */ +unsigned long long ha_get_pthread_id(unsigned int thr) +{ + union { + pthread_t t; + unsigned long long ll; + unsigned int i; + unsigned short s; + unsigned char c; + } u = { 0 }; + + u.t = ha_thread_info[thr].pthread; + + if (sizeof(u.t) <= sizeof(u.c)) + return u.c; + else if (sizeof(u.t) <= sizeof(u.s)) + return u.s; + else if (sizeof(u.t) <= sizeof(u.i)) + return u.i; + return u.ll; +} + /* send signal to thread */ void ha_tkill(unsigned int thr, int sig) {