Make folly::detail::CacheLocality portable on apple
[folly.git] / folly / detail / CacheLocality.cpp
index e3364dd57a54f962c29df3ecc3428f71f9e5ca01..a6abec2ae1aabb6f454e601b34e22b1d1050dccc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include "folly/detail/CacheLocality.h"
+#include <folly/detail/CacheLocality.h>
 
+#ifndef _MSC_VER
 #define _GNU_SOURCE 1 // for RTLD_NOLOAD
 #include <dlfcn.h>
+#endif
 #include <fstream>
 
-#include "folly/Conv.h"
-#include "folly/Exception.h"
-#include "folly/FileUtil.h"
-#include "folly/Format.h"
-#include "folly/ScopeGuard.h"
+#include <folly/Conv.h>
+#include <folly/Exception.h>
+#include <folly/FileUtil.h>
+#include <folly/Format.h>
+#include <folly/ScopeGuard.h>
 
 namespace folly { namespace detail {
 
@@ -32,11 +34,13 @@ namespace folly { namespace detail {
 
 /// Returns the best real CacheLocality information available
 static CacheLocality getSystemLocalityInfo() {
+#ifdef __linux__
   try {
     return CacheLocality::readFromSysfs();
   } catch (...) {
     // keep trying
   }
+#endif
 
   long numCpus = sysconf(_SC_NPROCESSORS_CONF);
   if (numCpus <= 0) {
@@ -78,11 +82,11 @@ const CacheLocality& CacheLocality::system<std::atomic>() {
 /// Returns the first decimal number in the string, or throws an exception
 /// if the string does not start with a number terminated by ',', '-',
 /// '\n', or eos.
-static ssize_t parseLeadingNumber(const std::string& line) {
+static size_t parseLeadingNumber(const std::string& line) {
   auto raw = line.c_str();
   char *end;
-  unsigned val = strtoul(raw, &end, 10);
-  if (end == raw || (*end != ',' && *end != '-' && *end != '\n')) {
+  unsigned long val = strtoul(raw, &end, 10);
+  if (end == raw || (*end != ',' && *end != '-' && *end != '\n' && *end != 0)) {
     throw std::runtime_error(to<std::string>(
         "error parsing list '", line, "'").c_str());
   }
@@ -163,7 +167,7 @@ CacheLocality CacheLocality::readFromSysfsTree(
   // to each other than entries that are far away.  For striping we want
   // the inverse map, since we are starting with the cpu
   std::vector<size_t> indexes(cpus.size());
-  for (int i = 0; i < cpus.size(); ++i) {
+  for (size_t i = 0; i < cpus.size(); ++i) {
     indexes[cpus[i]] = i;
   }
 
@@ -202,6 +206,9 @@ CacheLocality CacheLocality::uniform(size_t numCpus) {
 /// Resolves the dynamically loaded symbol __vdso_getcpu, returning null
 /// on failure
 static Getcpu::Func loadVdsoGetcpu() {
+#ifdef _MSC_VER
+  return nullptr;
+#else
   void* h = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
   if (h == nullptr) {
     return nullptr;
@@ -217,6 +224,7 @@ static Getcpu::Func loadVdsoGetcpu() {
   }
 
   return func;
+#endif
 }
 
 Getcpu::Func Getcpu::vdsoFunc() {
@@ -224,13 +232,15 @@ Getcpu::Func Getcpu::vdsoFunc() {
   return func;
 }
 
+#ifdef FOLLY_TLS
 /////////////// SequentialThreadId
 
 template<>
 std::atomic<size_t> SequentialThreadId<std::atomic>::prevId(0);
 
 template<>
-__thread size_t SequentialThreadId<std::atomic>::currentId(0);
+FOLLY_TLS size_t SequentialThreadId<std::atomic>::currentId(0);
+#endif
 
 /////////////// AccessSpreader
 
@@ -269,7 +279,7 @@ Getcpu::Func AccessSpreader<std::atomic>::pickGetcpuFunc(size_t numStripes) {
     return &degenerateGetcpu;
   } else {
     auto best = Getcpu::vdsoFunc();
-    return best ? best : &SequentialThreadId<std::atomic>::getcpu;
+    return best ? best : &FallbackGetcpuType::getcpu;
   }
 }