From 4e42eb8b9730276b500d4b868bc4e47b7a2c0ec6 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Wed, 16 Nov 2016 19:20:16 -0800 Subject: [PATCH] Add folly::getCurrentThreadID() 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 | 1 + folly/ThreadId.h | 34 ++++++++++++++++++++++++++++++ folly/test/ThreadCachedIntTest.cpp | 6 +++--- folly/test/ThreadLocalTest.cpp | 5 +++-- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 folly/ThreadId.h diff --git a/folly/Makefile.am b/folly/Makefile.am index 0ce66650..c965a587 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -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 index 00000000..104a53e0 --- /dev/null +++ b/folly/ThreadId.h @@ -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 + +#include + +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 +} +} diff --git a/folly/test/ThreadCachedIntTest.cpp b/folly/test/ThreadCachedIntTest.cpp index 1543bdaa..aadefc98 100644 --- a/folly/test/ThreadCachedIntTest.cpp +++ b/folly/test/ThreadCachedIntTest.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -298,9 +299,8 @@ struct ShardedAtomicInt { std::atomic 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 diff --git a/folly/test/ThreadLocalTest.cpp b/folly/test/ThreadLocalTest.cpp index db163e5f..b669865d 100644 --- a/folly/test/ThreadLocalTest.cpp +++ b/folly/test/ThreadLocalTest.cpp @@ -38,6 +38,7 @@ #include #include +#include #include #include #include @@ -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); -- 2.34.1