Commit 4e42eb8b authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

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
parent a64f5cb7
...@@ -357,6 +357,7 @@ nobase_follyinclude_HEADERS = \ ...@@ -357,6 +357,7 @@ nobase_follyinclude_HEADERS = \
test/TestUtils.h \ test/TestUtils.h \
ThreadCachedArena.h \ ThreadCachedArena.h \
ThreadCachedInt.h \ ThreadCachedInt.h \
ThreadId.h \
ThreadLocal.h \ ThreadLocal.h \
ThreadName.h \ ThreadName.h \
TimeoutQueue.h \ TimeoutQueue.h \
......
/*
* 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
}
}
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <folly/Benchmark.h> #include <folly/Benchmark.h>
#include <folly/Hash.h> #include <folly/Hash.h>
#include <folly/ThreadId.h>
#include <folly/portability/GFlags.h> #include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
...@@ -298,9 +299,8 @@ struct ShardedAtomicInt { ...@@ -298,9 +299,8 @@ struct ShardedAtomicInt {
std::atomic<int64_t> ints_[kBuckets_]; std::atomic<int64_t> ints_[kBuckets_];
inline void inc(int64_t val = 1) { inline void inc(int64_t val = 1) {
int bucket = hash::twang_mix64( int buck = hash::twang_mix64(folly::getCurrentThreadID()) & (kBuckets_ - 1);
uint64_t(pthread_self())) & (kBuckets_ - 1); std::atomic_fetch_add(&ints_[buck], val);
std::atomic_fetch_add(&ints_[bucket], val);
} }
// read the first few and extrapolate // read the first few and extrapolate
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <folly/Baton.h> #include <folly/Baton.h>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/ThreadId.h>
#include <folly/experimental/io/FsUtil.h> #include <folly/experimental/io/FsUtil.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#include <folly/portability/Unistd.h> #include <folly/portability/Unistd.h>
...@@ -405,7 +406,7 @@ class FillObject { ...@@ -405,7 +406,7 @@ class FillObject {
private: private:
uint64_t val() const { uint64_t val() const {
return (idx_ << 40) | uint64_t(pthread_self()); return (idx_ << 40) | folly::getCurrentThreadID();
} }
uint64_t idx_; uint64_t idx_;
...@@ -584,7 +585,7 @@ TEST(ThreadLocal, Fork2) { ...@@ -584,7 +585,7 @@ TEST(ThreadLocal, Fork2) {
TEST(ThreadLocal, SharedLibrary) { TEST(ThreadLocal, SharedLibrary) {
auto exe = fs::executable_path(); 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); auto handle = dlopen(lib.string().c_str(), RTLD_LAZY);
EXPECT_NE(nullptr, handle); EXPECT_NE(nullptr, handle);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment