2014-08-06 09:35:21 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/base/platform/platform.h"
|
|
|
|
|
|
|
|
#if V8_OS_POSIX
|
|
|
|
#include <unistd.h> // NOLINT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if V8_OS_WIN
|
|
|
|
#include "src/base/win32-headers.h"
|
|
|
|
#endif
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace base {
|
|
|
|
|
|
|
|
TEST(OS, GetCurrentProcessId) {
|
|
|
|
#if V8_OS_POSIX
|
|
|
|
EXPECT_EQ(static_cast<int>(getpid()), OS::GetCurrentProcessId());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if V8_OS_WIN
|
|
|
|
EXPECT_EQ(static_cast<int>(::GetCurrentProcessId()),
|
|
|
|
OS::GetCurrentProcessId());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-09-02 07:07:52 +00:00
|
|
|
class SelfJoinThread FINAL : public Thread {
|
2014-08-06 09:35:21 +00:00
|
|
|
public:
|
2014-08-12 13:33:35 +00:00
|
|
|
SelfJoinThread() : Thread(Options("SelfJoinThread")) {}
|
2014-09-02 07:07:52 +00:00
|
|
|
virtual void Run() OVERRIDE { Join(); }
|
2014-08-06 09:35:21 +00:00
|
|
|
};
|
|
|
|
|
2014-08-29 10:53:08 +00:00
|
|
|
} // namespace
|
2014-08-06 09:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(Thread, SelfJoin) {
|
|
|
|
SelfJoinThread thread;
|
|
|
|
thread.Start();
|
|
|
|
thread.Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ThreadLocalStorageTest : public Thread, public ::testing::Test {
|
|
|
|
public:
|
2014-08-12 13:33:35 +00:00
|
|
|
ThreadLocalStorageTest() : Thread(Options("ThreadLocalStorageTest")) {
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); ++i) {
|
2014-08-06 09:35:21 +00:00
|
|
|
keys_[i] = Thread::CreateThreadLocalKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~ThreadLocalStorageTest() {
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); ++i) {
|
2014-08-06 09:35:21 +00:00
|
|
|
Thread::DeleteThreadLocalKey(keys_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:07:52 +00:00
|
|
|
virtual void Run() FINAL OVERRIDE {
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); i++) {
|
2014-08-06 09:35:21 +00:00
|
|
|
CHECK(!Thread::HasThreadLocal(keys_[i]));
|
|
|
|
}
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); i++) {
|
2014-08-06 09:35:21 +00:00
|
|
|
Thread::SetThreadLocal(keys_[i], GetValue(i));
|
|
|
|
}
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); i++) {
|
2014-08-06 09:35:21 +00:00
|
|
|
CHECK(Thread::HasThreadLocal(keys_[i]));
|
|
|
|
}
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); i++) {
|
2014-08-06 09:35:21 +00:00
|
|
|
CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys_[i]));
|
|
|
|
CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys_[i]));
|
|
|
|
}
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); i++) {
|
|
|
|
Thread::SetThreadLocal(keys_[i], GetValue(arraysize(keys_) - i - 1));
|
2014-08-06 09:35:21 +00:00
|
|
|
}
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); i++) {
|
2014-08-06 09:35:21 +00:00
|
|
|
CHECK(Thread::HasThreadLocal(keys_[i]));
|
|
|
|
}
|
2014-08-26 09:19:24 +00:00
|
|
|
for (size_t i = 0; i < arraysize(keys_); i++) {
|
|
|
|
CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
|
2014-08-06 09:35:21 +00:00
|
|
|
Thread::GetThreadLocal(keys_[i]));
|
2014-08-26 09:19:24 +00:00
|
|
|
CHECK_EQ(GetValue(arraysize(keys_) - i - 1),
|
2014-08-06 09:35:21 +00:00
|
|
|
Thread::GetExistingThreadLocal(keys_[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void* GetValue(size_t x) {
|
|
|
|
return reinterpret_cast<void*>(static_cast<uintptr_t>(x + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread::LocalStorageKey keys_[256];
|
|
|
|
};
|
|
|
|
|
2014-08-29 10:53:08 +00:00
|
|
|
} // namespace
|
2014-08-06 09:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST_F(ThreadLocalStorageTest, DoTest) {
|
|
|
|
Run();
|
|
|
|
Start();
|
|
|
|
Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|
|
|
|
} // namespace v8
|