CLEANUP: compiler: add a THREAD_ALIGNED macro and use it where appropriate

Sometimes we need to align a struct member or a struct's size only when
threads are enabled. This is the case on fdtab for example. Instead of
using ugly ifdefs in the code itself, let's have a THREAD_ALIGNED() macro
performing the alignment only when threads are enabled. For now this was
only applied to fd-t.h as it was the only place found.
This commit is contained in:
Willy Tarreau 2020-06-11 08:22:01 +02:00
parent 920214e8c4
commit 8e3f5c6661
2 changed files with 27 additions and 9 deletions

View File

@ -173,6 +173,17 @@
#endif
#endif
/* sets alignment for current field or variable only when threads are enabled.
* Typically used to respect cache line alignment to avoid false sharing.
*/
#ifndef THREAD_ALIGNED
#ifdef USE_THREAD
#define THREAD_ALIGNED(x) __attribute__((aligned(x)))
#else
#define THREAD_ALIGNED(x)
#endif
#endif
/* add a mandatory alignment for next fields in a structure */
#ifndef ALWAYS_ALIGN
#define ALWAYS_ALIGN(x) union { } ALIGNED(x)
@ -200,4 +211,16 @@
#endif
#endif
/* add an optional alignment for next fields in a structure, only when threads
* are enabled. Typically used to respect cache line alignment to avoid false
* sharing.
*/
#ifndef THREAD_ALIGN
#ifdef USE_THREAD
#define THREAD_ALIGN(x) union { } ALIGNED(x)
#else
#define THREAD_ALIGN(x)
#endif
#endif
#endif /* _HAPROXY_COMPILER_H */

View File

@ -114,7 +114,9 @@ struct fdlist {
int last;
} ALIGNED(8);
/* info about one given fd */
/* info about one given fd. Note: only align on cache lines when using threads;
* 32-bit small archs can put everything in 32-bytes when threads are disabled.
*/
struct fdtab {
unsigned long running_mask; /* mask of thread IDs currently using the fd */
unsigned long thread_mask; /* mask of thread IDs authorized to process the fd */
@ -127,14 +129,7 @@ struct fdtab {
unsigned char linger_risk:1; /* 1 if we must kill lingering before closing */
unsigned char cloned:1; /* 1 if a cloned socket, requires EPOLL_CTL_DEL on close */
unsigned char initialized:1; /* 1 if init phase was done on this fd (e.g. set non-blocking) */
}
#ifdef USE_THREAD
/* only align on cache lines when using threads; 32-bit small archs
* can put everything in 32-bytes when threads are disabled.
*/
ALIGNED(64)
#endif
;
} THREAD_ALIGNED(64);
/* polled mask, one bit per thread and per direction for each FD */
struct polled_mask {