[ovs-dev] [PATCH 1/1] include/openvswitch/compiler.h: check existence of __builtin_prefetch using __has_builtin

Sergey Madaminov sergey.madaminov at gmail.com
Wed Aug 11 00:17:56 UTC 2021


Checking if '__has_builtin' is defined and then defining OVS_PREFETCH to
be '__builtin_prefetch' if it is available. To preserve backwards
compatibility, the previous way to define OVS_PREFETCH macro is also
there.

Signed-off-by: Sergey Madaminov <sergey.madaminov at gmail.com>

---
 include/openvswitch/compiler.h | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/include/openvswitch/compiler.h b/include/openvswitch/compiler.h
index cf009f826..684d45d12 100644
--- a/include/openvswitch/compiler.h
+++ b/include/openvswitch/compiler.h
@@ -53,7 +53,7 @@
 #define OVS_UNLIKELY(CONDITION) (!!(CONDITION))
 #endif
 
-#if __has_feature(c_thread_safety_attributes)
+#if __has_feature(c_thread_safety_attributes) && defined(HAVE_THREAD_SAFETY)
 /* "clang" annotations for thread safety check.
  *
  * OVS_LOCKABLE indicates that the struct contains mutex element
@@ -229,13 +229,22 @@
  * OVS_PREFETCH_WRITE() should be used when the memory is going to be
  * written to.  Depending on the target CPU, this can generate the same
  * instruction as OVS_PREFETCH(), or bring the data into the cache in an
- * exclusive state. */
-#if __GNUC__
-#define OVS_PREFETCH(addr) __builtin_prefetch((addr))
-#define OVS_PREFETCH_WRITE(addr) __builtin_prefetch((addr), 1)
+ * exclusive state.
+ *
+ * GCC 10 introduced support for __has_builtin preprocessor operator,
+ * however, the old way to define OVS_PREFETCH remains to allow for backwards
+ * compatibility. */
+#if defined __has_builtin
+#  if __has_builtin (__builtin_prefetch)
+#    define OVS_PREFETCH(addr) __builtin_prefetch((addr))
+#    define OVS_PREFETCH_WRITE(addr) __builtin_prefetch((addr), 1)
+#  endif
+#elif __GNUC__
+#  define OVS_PREFETCH(addr) __builtin_prefetch((addr))
+#  define OVS_PREFETCH_WRITE(addr) __builtin_prefetch((addr), 1)
 #else
-#define OVS_PREFETCH(addr)
-#define OVS_PREFETCH_WRITE(addr)
+#  define OVS_PREFETCH(addr)
+#  define OVS_PREFETCH_WRITE(addr)
 #endif
 
 /* Since Visual Studio 2015 there has been an effort to make offsetof a
@@ -244,11 +253,13 @@
  * the C compiler.
  * e.g.: https://bit.ly/2UvWwti
  */
+ /*
 #if _MSC_VER >= 1900
 #undef offsetof
 #define offsetof(type, member) \
     ((size_t)((char *)&(((type *)0)->member) - (char *)0))
 #endif
+*/
 
 /* Build assertions.
  *
-- 
2.25.1



More information about the dev mailing list