Home | History | Annotate | Download | only in cctest
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 //
      3 // Tests of fast TLS support.
      4 
      5 #include "v8.h"
      6 
      7 #include "cctest.h"
      8 #include "checks.h"
      9 #include "platform.h"
     10 
     11 using v8::internal::Thread;
     12 
     13 static const int kValueCount = 128;
     14 
     15 static Thread::LocalStorageKey keys[kValueCount];
     16 
     17 static void* GetValue(int num) {
     18   return reinterpret_cast<void*>(static_cast<intptr_t>(num + 1));
     19 }
     20 
     21 static void DoTest() {
     22   for (int i = 0; i < kValueCount; i++) {
     23     CHECK(!Thread::HasThreadLocal(keys[i]));
     24   }
     25   for (int i = 0; i < kValueCount; i++) {
     26     Thread::SetThreadLocal(keys[i], GetValue(i));
     27   }
     28   for (int i = 0; i < kValueCount; i++) {
     29     CHECK(Thread::HasThreadLocal(keys[i]));
     30   }
     31   for (int i = 0; i < kValueCount; i++) {
     32     CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys[i]));
     33     CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys[i]));
     34   }
     35   for (int i = 0; i < kValueCount; i++) {
     36     Thread::SetThreadLocal(keys[i], GetValue(kValueCount - i - 1));
     37   }
     38   for (int i = 0; i < kValueCount; i++) {
     39     CHECK(Thread::HasThreadLocal(keys[i]));
     40   }
     41   for (int i = 0; i < kValueCount; i++) {
     42     CHECK_EQ(GetValue(kValueCount - i - 1),
     43              Thread::GetThreadLocal(keys[i]));
     44     CHECK_EQ(GetValue(kValueCount - i - 1),
     45              Thread::GetExistingThreadLocal(keys[i]));
     46   }
     47 }
     48 
     49 class TestThread : public Thread {
     50  public:
     51   TestThread() : Thread("TestThread") {}
     52 
     53   virtual void Run() {
     54     DoTest();
     55   }
     56 };
     57 
     58 TEST(FastTLS) {
     59   for (int i = 0; i < kValueCount; i++) {
     60     keys[i] = Thread::CreateThreadLocalKey();
     61   }
     62   DoTest();
     63   TestThread thread;
     64   thread.Start();
     65   thread.Join();
     66 }
     67