1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // 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.
27
28 #include "src/v8.h"
29 #include "test/cctest/cctest.h"
30
31 #include "src/base/platform/platform.h"
32 #include "src/isolate.h"
33
34
35 enum Turn {
36 FILL_CACHE,
37 CLEAN_CACHE,
38 SECOND_TIME_FILL_CACHE,
39 DONE
40 };
41
42 static Turn turn = FILL_CACHE;
43
44
45 class ThreadA : public v8::base::Thread {
46 public:
ThreadA()47 ThreadA() : Thread(Options("ThreadA")) {}
Run()48 void Run() {
49 v8::Isolate* isolate = CcTest::isolate();
50 v8::Locker locker(isolate);
51 v8::Isolate::Scope isolate_scope(isolate);
52 v8::HandleScope scope(isolate);
53 v8::Handle<v8::Context> context = v8::Context::New(isolate);
54 v8::Context::Scope context_scope(context);
55
56 CHECK_EQ(FILL_CACHE, turn);
57
58 // Fill String.search cache.
59 v8::Handle<v8::Script> script = v8::Script::Compile(
60 v8::String::NewFromUtf8(
61 isolate,
62 "for (var i = 0; i < 3; i++) {"
63 " var result = \"a\".search(\"a\");"
64 " if (result != 0) throw \"result: \" + result + \" @\" + i;"
65 "};"
66 "true"));
67 CHECK(script->Run()->IsTrue());
68
69 turn = CLEAN_CACHE;
70 do {
71 {
72 v8::Unlocker unlocker(CcTest::isolate());
73 Thread::YieldCPU();
74 }
75 } while (turn != SECOND_TIME_FILL_CACHE);
76
77 // Rerun the script.
78 CHECK(script->Run()->IsTrue());
79
80 turn = DONE;
81 }
82 };
83
84
85 class ThreadB : public v8::base::Thread {
86 public:
ThreadB()87 ThreadB() : Thread(Options("ThreadB")) {}
Run()88 void Run() {
89 do {
90 {
91 v8::Isolate* isolate = CcTest::isolate();
92 v8::Locker locker(isolate);
93 v8::Isolate::Scope isolate_scope(isolate);
94 if (turn == CLEAN_CACHE) {
95 v8::HandleScope scope(isolate);
96 v8::Handle<v8::Context> context = v8::Context::New(isolate);
97 v8::Context::Scope context_scope(context);
98
99 // Clear the caches by forcing major GC.
100 CcTest::heap()->CollectAllGarbage(v8::internal::Heap::kNoGCFlags);
101 turn = SECOND_TIME_FILL_CACHE;
102 break;
103 }
104 }
105
106 Thread::YieldCPU();
107 } while (true);
108 }
109 };
110
111
TEST(JSFunctionResultCachesInTwoThreads)112 TEST(JSFunctionResultCachesInTwoThreads) {
113 ThreadA threadA;
114 ThreadB threadB;
115
116 threadA.Start();
117 threadB.Start();
118
119 threadA.Join();
120 threadB.Join();
121
122 CHECK_EQ(DONE, turn);
123 }
124
125 class ThreadIdValidationThread : public v8::base::Thread {
126 public:
ThreadIdValidationThread(v8::base::Thread * thread_to_start,i::List<i::ThreadId> * refs,unsigned int thread_no,v8::base::Semaphore * semaphore)127 ThreadIdValidationThread(v8::base::Thread* thread_to_start,
128 i::List<i::ThreadId>* refs, unsigned int thread_no,
129 v8::base::Semaphore* semaphore)
130 : Thread(Options("ThreadRefValidationThread")),
131 refs_(refs),
132 thread_no_(thread_no),
133 thread_to_start_(thread_to_start),
134 semaphore_(semaphore) {}
135
Run()136 void Run() {
137 i::ThreadId thread_id = i::ThreadId::Current();
138 for (int i = 0; i < thread_no_; i++) {
139 CHECK(!(*refs_)[i].Equals(thread_id));
140 }
141 CHECK(thread_id.IsValid());
142 (*refs_)[thread_no_] = thread_id;
143 if (thread_to_start_ != NULL) {
144 thread_to_start_->Start();
145 }
146 semaphore_->Signal();
147 }
148
149 private:
150 i::List<i::ThreadId>* refs_;
151 int thread_no_;
152 v8::base::Thread* thread_to_start_;
153 v8::base::Semaphore* semaphore_;
154 };
155
156
TEST(ThreadIdValidation)157 TEST(ThreadIdValidation) {
158 const int kNThreads = 100;
159 i::List<ThreadIdValidationThread*> threads(kNThreads);
160 i::List<i::ThreadId> refs(kNThreads);
161 v8::base::Semaphore semaphore(0);
162 ThreadIdValidationThread* prev = NULL;
163 for (int i = kNThreads - 1; i >= 0; i--) {
164 ThreadIdValidationThread* newThread =
165 new ThreadIdValidationThread(prev, &refs, i, &semaphore);
166 threads.Add(newThread);
167 prev = newThread;
168 refs.Add(i::ThreadId::Invalid());
169 }
170 prev->Start();
171 for (int i = 0; i < kNThreads; i++) {
172 semaphore.Wait();
173 }
174 for (int i = 0; i < kNThreads; i++) {
175 delete threads[i];
176 }
177 }
178