Add folly::getCurrentThreadID()
authorChristopher Dykes <cdykes@fb.com>
Thu, 17 Nov 2016 03:20:16 +0000 (19:20 -0800)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Thu, 17 Nov 2016 03:23:31 +0000 (19:23 -0800)
Summary: And also use it in a couple of tests, so that they can be compiled on Windows, where `pthread_t` is a struct rather than a pointer or integer.

Reviewed By: yfeldblum

Differential Revision: D4191560

fbshipit-source-id: 5bcf0a2952109b2a9bc5220c4640d42e2cdf8977

folly/Makefile.am
folly/ThreadId.h [new file with mode: 0644]
folly/test/ThreadCachedIntTest.cpp
folly/test/ThreadLocalTest.cpp

index 0ce666505b24a26a647b399b5ce70608d0c350a8..c965a587ddeaff4cb972150336cb7f30c53f59c3 100644 (file)
@@ -357,6 +357,7 @@ nobase_follyinclude_HEADERS = \
        test/TestUtils.h \
        ThreadCachedArena.h \
        ThreadCachedInt.h \
+       ThreadId.h \
        ThreadLocal.h \
        ThreadName.h \
        TimeoutQueue.h \
diff --git a/folly/ThreadId.h b/folly/ThreadId.h
new file mode 100644 (file)
index 0000000..104a53e
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <cstdint>
+
+#include <pthread.h>
+
+namespace folly {
+
+inline uint64_t getCurrentThreadID() {
+#ifdef _WIN32
+  // There's no need to force a Windows.h include, so grab the ID
+  // via pthread instead.
+  return uint64_t(pthread_getw32threadid_np(pthread_self()));
+#else
+  return uint64_t(pthread_self());
+#endif
+}
+}
index 1543bdaa11cb0966722bc83a3dfeadb940601dfd..aadefc98f0d726c07295d4c743e9bc93e65cb0e8 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <folly/Benchmark.h>
 #include <folly/Hash.h>
+#include <folly/ThreadId.h>
 #include <folly/portability/GFlags.h>
 #include <folly/portability/GTest.h>
 
@@ -298,9 +299,8 @@ struct ShardedAtomicInt {
   std::atomic<int64_t> ints_[kBuckets_];
 
   inline void inc(int64_t val = 1) {
-    int bucket = hash::twang_mix64(
-      uint64_t(pthread_self())) & (kBuckets_ - 1);
-    std::atomic_fetch_add(&ints_[bucket], val);
+    int buck = hash::twang_mix64(folly::getCurrentThreadID()) & (kBuckets_ - 1);
+    std::atomic_fetch_add(&ints_[buck], val);
   }
 
   // read the first few and extrapolate
index db163e5f3239a9c674fed3324f4e97391902744d..b669865ded921bb7c030e122f38d9cad8875cea2 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <folly/Baton.h>
 #include <folly/Memory.h>
+#include <folly/ThreadId.h>
 #include <folly/experimental/io/FsUtil.h>
 #include <folly/portability/GTest.h>
 #include <folly/portability/Unistd.h>
@@ -405,7 +406,7 @@ class FillObject {
 
  private:
   uint64_t val() const {
-    return (idx_ << 40) | uint64_t(pthread_self());
+    return (idx_ << 40) | folly::getCurrentThreadID();
   }
 
   uint64_t idx_;
@@ -584,7 +585,7 @@ TEST(ThreadLocal, Fork2) {
 
 TEST(ThreadLocal, SharedLibrary) {
   auto exe = fs::executable_path();
-  auto lib = exe.parent_path() / "lib_thread_local_test.so";
+  auto lib = exe.parent_path() / "thread_local_test_lib.so";
   auto handle = dlopen(lib.string().c_str(), RTLD_LAZY);
   EXPECT_NE(nullptr, handle);