blob: cc9ffebe38ad9261df77c43e49af9f6896b50f6f [file] [log] [blame]
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000027//
28// Tests of fast TLS support.
29
30#include "v8.h"
31
32#include "cctest.h"
33#include "checks.h"
34#include "platform.h"
35
36using v8::internal::Thread;
37
38static const int kValueCount = 128;
39
40static Thread::LocalStorageKey keys[kValueCount];
41
42static void* GetValue(int num) {
43 return reinterpret_cast<void*>(static_cast<intptr_t>(num + 1));
44}
45
46static void DoTest() {
47 for (int i = 0; i < kValueCount; i++) {
48 CHECK(!Thread::HasThreadLocal(keys[i]));
49 }
50 for (int i = 0; i < kValueCount; i++) {
51 Thread::SetThreadLocal(keys[i], GetValue(i));
52 }
53 for (int i = 0; i < kValueCount; i++) {
54 CHECK(Thread::HasThreadLocal(keys[i]));
55 }
56 for (int i = 0; i < kValueCount; i++) {
57 CHECK_EQ(GetValue(i), Thread::GetThreadLocal(keys[i]));
58 CHECK_EQ(GetValue(i), Thread::GetExistingThreadLocal(keys[i]));
59 }
60 for (int i = 0; i < kValueCount; i++) {
61 Thread::SetThreadLocal(keys[i], GetValue(kValueCount - i - 1));
62 }
63 for (int i = 0; i < kValueCount; i++) {
64 CHECK(Thread::HasThreadLocal(keys[i]));
65 }
66 for (int i = 0; i < kValueCount; i++) {
67 CHECK_EQ(GetValue(kValueCount - i - 1),
68 Thread::GetThreadLocal(keys[i]));
69 CHECK_EQ(GetValue(kValueCount - i - 1),
70 Thread::GetExistingThreadLocal(keys[i]));
71 }
72}
73
74class TestThread : public Thread {
75 public:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000076 TestThread() : Thread("TestThread") {}
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000077
78 virtual void Run() {
79 DoTest();
80 }
81};
82
83TEST(FastTLS) {
84 for (int i = 0; i < kValueCount; i++) {
85 keys[i] = Thread::CreateThreadLocalKey();
86 }
87 DoTest();
88 TestThread thread;
89 thread.Start();
90 thread.Join();
91}