Line data Source code
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 : #include "testing/gtest/include/gtest/gtest.h"
8 :
9 : namespace v8 {
10 : namespace base {
11 :
12 15443 : TEST(OS, GetCurrentProcessId) {
13 : #if V8_OS_POSIX
14 2 : EXPECT_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId());
15 : #endif
16 :
17 : #if V8_OS_WIN
18 : EXPECT_EQ(static_cast<int>(::GetCurrentProcessId()),
19 : OS::GetCurrentProcessId());
20 : #endif
21 1 : }
22 :
23 :
24 : namespace {
25 :
26 : class ThreadLocalStorageTest : public Thread, public ::testing::Test {
27 : public:
28 2 : ThreadLocalStorageTest() : Thread(Options("ThreadLocalStorageTest")) {
29 65 : for (size_t i = 0; i < arraysize(keys_); ++i) {
30 32 : keys_[i] = Thread::CreateThreadLocalKey();
31 : }
32 1 : }
33 2 : ~ThreadLocalStorageTest() override {
34 65 : for (size_t i = 0; i < arraysize(keys_); ++i) {
35 32 : Thread::DeleteThreadLocalKey(keys_[i]);
36 : }
37 1 : }
38 :
39 2 : void Run() final {
40 130 : for (size_t i = 0; i < arraysize(keys_); i++) {
41 128 : CHECK(!Thread::HasThreadLocal(keys_[i]));
42 : }
43 64 : for (size_t i = 0; i < arraysize(keys_); i++) {
44 64 : Thread::SetThreadLocal(keys_[i], GetValue(i));
45 : }
46 130 : for (size_t i = 0; i < arraysize(keys_); i++) {
47 128 : CHECK(Thread::HasThreadLocal(keys_[i]));
48 : }
49 66 : for (size_t i = 0; i < arraysize(keys_); i++) {
50 128 : CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys_[i]));
51 128 : CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys_[i]));
52 : }
53 130 : for (size_t i = 0; i < arraysize(keys_); i++) {
54 64 : Thread::SetThreadLocal(keys_[i], GetValue(arraysize(keys_) - i - 1));
55 : }
56 130 : for (size_t i = 0; i < arraysize(keys_); i++) {
57 128 : CHECK(Thread::HasThreadLocal(keys_[i]));
58 : }
59 130 : for (size_t i = 0; i < arraysize(keys_); i++) {
60 128 : CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
61 : Thread::GetThreadLocal(keys_[i]));
62 128 : CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
63 : Thread::GetExistingThreadLocal(keys_[i]));
64 : }
65 2 : }
66 :
67 : private:
68 : static void* GetValue(size_t x) {
69 256 : return bit_cast<void*>(static_cast<uintptr_t>(x + 1));
70 : }
71 :
72 : // Older versions of Android have fewer TLS slots (nominally 64, but the
73 : // system uses "about 5 of them" itself).
74 : Thread::LocalStorageKey keys_[32];
75 : };
76 :
77 : } // namespace
78 :
79 :
80 15443 : TEST_F(ThreadLocalStorageTest, DoTest) {
81 1 : Run();
82 1 : Start();
83 1 : Join();
84 1 : }
85 :
86 : } // namespace base
87 9264 : } // namespace v8
|