• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
31 ABSL_NAMESPACE_BEGIN
32 namespace base_internal {
33 namespace {
34 
TEST(SysinfoTest,NumCPUs)35 TEST(SysinfoTest, NumCPUs) {
36   EXPECT_NE(NumCPUs(), 0)
37       << "NumCPUs() should not have the default value of 0";
38 }
39 
TEST(SysinfoTest,GetTID)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__
TEST(SysinfoTest,LinuxGetTID)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
87 ABSL_NAMESPACE_END
88 }  // namespace absl
89