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