folly: fix sysMembarrier() with newer kernel headers
[folly.git] / folly / portability / SysMembarrier.cpp
index 7801000fad2a60f70fbc76d92229ec0b716bbac8..9b3735c965603e8cbfa985aa7f4566522191b820 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <mutex>
 
 #include <folly/Portability.h>
 #include <mutex>
 
 #include <folly/Portability.h>
+#include <folly/portability/SysSyscall.h>
 #include <folly/portability/Unistd.h>
 
 #include <folly/portability/Unistd.h>
 
-#if !defined(__NR_membarrier) && FOLLY_X64 && !FOLLY_MOBILE && \
-    defined(__linux__)
+#if FOLLY_X64 && !FOLLY_MOBILE && defined(__linux__)
+#define FOLLY_USE_SYS_MEMBARRIER 1
+#if !defined(__NR_membarrier)
 #define __NR_membarrier 324
 #define __NR_membarrier 324
+#endif
+#if FOLLY_HAVE_LINUX_MEMBARRIER_H
+#include <linux/membarrier.h> // @manual
+#else
 #define MEMBARRIER_CMD_QUERY 0
 #define MEMBARRIER_CMD_SHARED 1
 #endif
 #define MEMBARRIER_CMD_QUERY 0
 #define MEMBARRIER_CMD_SHARED 1
 #endif
+#endif
 
 namespace folly {
 namespace detail {
 
 namespace folly {
 namespace detail {
@@ -36,7 +43,7 @@ bool sysMembarrierAvailable() {
     return false;
   }
 
     return false;
   }
 
-#ifdef __NR_membarrier
+#if FOLLY_USE_SYS_MEMBARRIER
   auto r = syscall(__NR_membarrier, MEMBARRIER_CMD_QUERY, /* flags = */ 0);
   if (r == -1) {
     return false;
   auto r = syscall(__NR_membarrier, MEMBARRIER_CMD_QUERY, /* flags = */ 0);
   if (r == -1) {
     return false;
@@ -49,11 +56,11 @@ bool sysMembarrierAvailable() {
 }
 
 int sysMembarrier() {
 }
 
 int sysMembarrier() {
-#ifdef __NR_membarrier
+#if FOLLY_USE_SYS_MEMBARRIER
   return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, /* flags = */ 0);
 #else
   return -1;
 #endif
 }
   return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, /* flags = */ 0);
 #else
   return -1;
 #endif
 }
-}
-}
+} // namespace detail
+} // namespace folly