v8/test/cctest/test-platform-tls.cc
mikhail.naganov@gmail.com 47248cc5fe Revert accidental r8254..r8256
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8257 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2011-06-10 09:42:08 +00:00

67 lines
1.6 KiB
C++

// Copyright 2011 the V8 project authors. All rights reserved.
//
// Tests of fast TLS support.
#include "v8.h"
#include "cctest.h"
#include "checks.h"
#include "platform.h"
using v8::internal::Thread;
static const int kValueCount = 128;
static Thread::LocalStorageKey keys[kValueCount];
static void* GetValue(int num) {
return reinterpret_cast<void*>(static_cast<intptr_t>(num + 1));
}
static void DoTest() {
for (int i = 0; i < kValueCount; i++) {
CHECK(!Thread::HasThreadLocal(keys[i]));
}
for (int i = 0; i < kValueCount; i++) {
Thread::SetThreadLocal(keys[i], GetValue(i));
}
for (int i = 0; i < kValueCount; i++) {
CHECK(Thread::HasThreadLocal(keys[i]));
}
for (int i = 0; i < kValueCount; i++) {
CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys[i]));
CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys[i]));
}
for (int i = 0; i < kValueCount; i++) {
Thread::SetThreadLocal(keys[i], GetValue(kValueCount - i - 1));
}
for (int i = 0; i < kValueCount; i++) {
CHECK(Thread::HasThreadLocal(keys[i]));
}
for (int i = 0; i < kValueCount; i++) {
CHECK_EQ(GetValue(kValueCount - i - 1),
Thread::GetThreadLocal(keys[i]));
CHECK_EQ(GetValue(kValueCount - i - 1),
Thread::GetExistingThreadLocal(keys[i]));
}
}
class TestThread : public Thread {
public:
TestThread() : Thread(NULL, "TestThread") {}
virtual void Run() {
DoTest();
}
};
TEST(FastTLS) {
for (int i = 0; i < kValueCount; i++) {
keys[i] = Thread::CreateThreadLocalKey();
}
DoTest();
TestThread thread;
thread.Start();
thread.Join();
}