1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/base/platform/platform.h"
6
7 #if V8_OS_POSIX
8 #include <unistd.h> // NOLINT
9 #endif
10
11 #if V8_OS_WIN
12 #include "src/base/win32-headers.h"
13 #endif
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 #if V8_OS_ANDROID
17 #define DISABLE_ON_ANDROID(Name) DISABLED_##Name
18 #else
19 #define DISABLE_ON_ANDROID(Name) Name
20 #endif
21
22 namespace v8 {
23 namespace base {
24
TEST(OS,GetCurrentProcessId)25 TEST(OS, GetCurrentProcessId) {
26 #if V8_OS_POSIX
27 EXPECT_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId());
28 #endif
29
30 #if V8_OS_WIN
31 EXPECT_EQ(static_cast<int>(::GetCurrentProcessId()),
32 OS::GetCurrentProcessId());
33 #endif
34 }
35
36
37 namespace {
38
39 class ThreadLocalStorageTest : public Thread, public ::testing::Test {
40 public:
ThreadLocalStorageTest()41 ThreadLocalStorageTest() : Thread(Options("ThreadLocalStorageTest")) {
42 for (size_t i = 0; i < arraysize(keys_); ++i) {
43 keys_[i] = Thread::CreateThreadLocalKey();
44 }
45 }
~ThreadLocalStorageTest()46 ~ThreadLocalStorageTest() {
47 for (size_t i = 0; i < arraysize(keys_); ++i) {
48 Thread::DeleteThreadLocalKey(keys_[i]);
49 }
50 }
51
Run()52 void Run() final {
53 for (size_t i = 0; i < arraysize(keys_); i++) {
54 CHECK(!Thread::HasThreadLocal(keys_[i]));
55 }
56 for (size_t i = 0; i < arraysize(keys_); i++) {
57 Thread::SetThreadLocal(keys_[i], GetValue(i));
58 }
59 for (size_t i = 0; i < arraysize(keys_); i++) {
60 CHECK(Thread::HasThreadLocal(keys_[i]));
61 }
62 for (size_t i = 0; i < arraysize(keys_); i++) {
63 CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys_[i]));
64 CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys_[i]));
65 }
66 for (size_t i = 0; i < arraysize(keys_); i++) {
67 Thread::SetThreadLocal(keys_[i], GetValue(arraysize(keys_) - i - 1));
68 }
69 for (size_t i = 0; i < arraysize(keys_); i++) {
70 CHECK(Thread::HasThreadLocal(keys_[i]));
71 }
72 for (size_t i = 0; i < arraysize(keys_); i++) {
73 CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
74 Thread::GetThreadLocal(keys_[i]));
75 CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
76 Thread::GetExistingThreadLocal(keys_[i]));
77 }
78 }
79
80 private:
GetValue(size_t x)81 static void* GetValue(size_t x) {
82 return bit_cast<void*>(static_cast<uintptr_t>(x + 1));
83 }
84
85 // Older versions of Android have fewer TLS slots (nominally 64, but the
86 // system uses "about 5 of them" itself).
87 Thread::LocalStorageKey keys_[32];
88 };
89
90 } // namespace
91
92
TEST_F(ThreadLocalStorageTest,DoTest)93 TEST_F(ThreadLocalStorageTest, DoTest) {
94 Run();
95 Start();
96 Join();
97 }
98
99 } // namespace base
100 } // namespace v8
101