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.
diff --git a/include/haproxy/compiler.h b/include/haproxy/compiler.h
index a0d4569..0e594fa 100644
--- a/include/haproxy/compiler.h
+++ b/include/haproxy/compiler.h
@@ -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 */
diff --git a/include/haproxy/fd-t.h b/include/haproxy/fd-t.h
index 5f1c173..1bffb26 100644
--- a/include/haproxy/fd-t.h
+++ b/include/haproxy/fd-t.h
@@ -114,7 +114,9 @@
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 @@
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 {