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.
This commit is contained in:
Willy Tarreau 2021-10-06 22:44:28 +02:00
parent d10385ac4b
commit 4eeb88363c
3 changed files with 36 additions and 34 deletions

View File

@ -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;

View File

@ -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 <thr> 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 */

View File

@ -254,6 +254,36 @@ void set_thread_cpu_affinity()
#endif /* USE_CPU_AFFINITY */
}
/* Retrieves the opaque pthread_t of thread <thr> 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 <sig> to thread <thr> */
void ha_tkill(unsigned int thr, int sig)
{