abseil-cpp/absl/base/internal/sysinfo_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/base/internal/sysinfo.h"
16 
17 #ifndef _WIN32
18 #include <sys/types.h>
19 #include <unistd.h>
20 #endif
21 
22 #include <thread> // NOLINT(build/c++11)
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "gtest/gtest.h"
27 #include "absl/synchronization/barrier.h"
28 #include "absl/synchronization/mutex.h"
29 
30 namespace absl {
32 namespace base_internal {
33 namespace {
34 
35 TEST(SysinfoTest, NumCPUs) {
36  EXPECT_NE(NumCPUs(), 0)
37  << "NumCPUs() should not have the default value of 0";
38 }
39 
40 TEST(SysinfoTest, GetTID) {
41  EXPECT_EQ(GetTID(), GetTID()); // Basic compile and equality test.
42 #ifdef __native_client__
43  // Native Client has a race condition bug that leads to memory
44  // exaustion when repeatedly creating and joining threads.
45  // https://bugs.chromium.org/p/nativeclient/issues/detail?id=1027
46  return;
47 #endif
48  // Test that TIDs are unique to each thread.
49  // Uses a few loops to exercise implementations that reallocate IDs.
50  for (int i = 0; i < 10; ++i) {
51  constexpr int kNumThreads = 10;
52  Barrier all_threads_done(kNumThreads);
53  std::vector<std::thread> threads;
54 
55  Mutex mutex;
56  std::unordered_set<pid_t> tids;
57 
58  for (int j = 0; j < kNumThreads; ++j) {
59  threads.push_back(std::thread([&]() {
60  pid_t id = GetTID();
61  {
62  MutexLock lock(&mutex);
63  ASSERT_TRUE(tids.find(id) == tids.end());
64  tids.insert(id);
65  }
66  // We can't simply join the threads here. The threads need to
67  // be alive otherwise the TID might have been reallocated to
68  // another live thread.
69  all_threads_done.Block();
70  }));
71  }
72  for (auto& thread : threads) {
73  thread.join();
74  }
75  }
76 }
77 
78 #ifdef __linux__
79 TEST(SysinfoTest, LinuxGetTID) {
80  // On Linux, for the main thread, GetTID()==getpid() is guaranteed by the API.
81  EXPECT_EQ(GetTID(), getpid());
82 }
83 #endif
84 
85 } // namespace
86 } // namespace base_internal
88 } // namespace absl
absl::str_format_internal::LengthMod::j
@ j
MutexLock
#define MutexLock(x)
Definition: bloaty/third_party/re2/util/mutex.h:125
mutex
static uv_mutex_t mutex
Definition: threadpool.c:34
absl::base_internal::NumCPUs
int NumCPUs()
Definition: abseil-cpp/absl/base/internal/sysinfo.cc:347
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
threads
static uv_thread_t * threads
Definition: threadpool.c:38
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
absl::base_internal::GetTID
pid_t GetTID()
Definition: abseil-cpp/absl/base/internal/sysinfo.cc:453
absl::base_internal::TEST
TEST(ExponentialBiasedTest, CoinTossDemoWithGetSkipCount)
Definition: bloaty/third_party/abseil-cpp/absl/base/internal/exponential_biased_test.cc:117
kNumThreads
const int kNumThreads
Definition: thread_stress_test.cc:46
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:24