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 namespace v8 {
17 namespace base {
18 
TEST(OS,GetCurrentProcessId)19 TEST(OS, GetCurrentProcessId) {
20 #if V8_OS_POSIX
21   EXPECT_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId());
22 #endif
23 
24 #if V8_OS_WIN
25   EXPECT_EQ(static_cast<int>(::GetCurrentProcessId()),
26             OS::GetCurrentProcessId());
27 #endif
28 }
29 
30 
31 namespace {
32 
33 class SelfJoinThread FINAL : public Thread {
34  public:
SelfJoinThread()35   SelfJoinThread() : Thread(Options("SelfJoinThread")) {}
Run()36   virtual void Run() OVERRIDE { Join(); }
37 };
38 
39 }  // namespace
40 
41 
TEST(Thread,SelfJoin)42 TEST(Thread, SelfJoin) {
43   SelfJoinThread thread;
44   thread.Start();
45   thread.Join();
46 }
47 
48 
49 namespace {
50 
51 class ThreadLocalStorageTest : public Thread, public ::testing::Test {
52  public:
ThreadLocalStorageTest()53   ThreadLocalStorageTest() : Thread(Options("ThreadLocalStorageTest")) {
54     for (size_t i = 0; i < arraysize(keys_); ++i) {
55       keys_[i] = Thread::CreateThreadLocalKey();
56     }
57   }
~ThreadLocalStorageTest()58   ~ThreadLocalStorageTest() {
59     for (size_t i = 0; i < arraysize(keys_); ++i) {
60       Thread::DeleteThreadLocalKey(keys_[i]);
61     }
62   }
63 
Run()64   virtual void Run() FINAL OVERRIDE {
65     for (size_t i = 0; i < arraysize(keys_); i++) {
66       CHECK(!Thread::HasThreadLocal(keys_[i]));
67     }
68     for (size_t i = 0; i < arraysize(keys_); i++) {
69       Thread::SetThreadLocal(keys_[i], GetValue(i));
70     }
71     for (size_t i = 0; i < arraysize(keys_); i++) {
72       CHECK(Thread::HasThreadLocal(keys_[i]));
73     }
74     for (size_t i = 0; i < arraysize(keys_); i++) {
75       CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys_[i]));
76       CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys_[i]));
77     }
78     for (size_t i = 0; i < arraysize(keys_); i++) {
79       Thread::SetThreadLocal(keys_[i], GetValue(arraysize(keys_) - i - 1));
80     }
81     for (size_t i = 0; i < arraysize(keys_); i++) {
82       CHECK(Thread::HasThreadLocal(keys_[i]));
83     }
84     for (size_t i = 0; i < arraysize(keys_); i++) {
85       CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
86                Thread::GetThreadLocal(keys_[i]));
87       CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
88                Thread::GetExistingThreadLocal(keys_[i]));
89     }
90   }
91 
92  private:
GetValue(size_t x)93   static void* GetValue(size_t x) {
94     return reinterpret_cast<void*>(static_cast<uintptr_t>(x + 1));
95   }
96 
97   Thread::LocalStorageKey keys_[256];
98 };
99 
100 }  // namespace
101 
102 
TEST_F(ThreadLocalStorageTest,DoTest)103 TEST_F(ThreadLocalStorageTest, DoTest) {
104   Run();
105   Start();
106   Join();
107 }
108 
109 }  // namespace base
110 }  // namespace v8
111