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