OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "src/v8.h" | 28 #include "src/v8.h" |
29 #include "test/cctest/cctest.h" | 29 #include "test/cctest/cctest.h" |
30 | 30 |
31 #include "src/platform.h" | 31 #include "src/base/platform/platform.h" |
32 | 32 |
33 | 33 |
34 v8::internal::Semaphore* semaphore = NULL; | 34 v8::base::Semaphore* semaphore = NULL; |
35 | 35 |
36 | 36 |
37 void Signal(const v8::FunctionCallbackInfo<v8::Value>& args) { | 37 void Signal(const v8::FunctionCallbackInfo<v8::Value>& args) { |
38 semaphore->Signal(); | 38 semaphore->Signal(); |
39 } | 39 } |
40 | 40 |
41 | 41 |
42 void TerminateCurrentThread(const v8::FunctionCallbackInfo<v8::Value>& args) { | 42 void TerminateCurrentThread(const v8::FunctionCallbackInfo<v8::Value>& args) { |
43 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 43 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); |
44 v8::V8::TerminateExecution(args.GetIsolate()); | 44 v8::V8::TerminateExecution(args.GetIsolate()); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 // Run a loop that will be infinite if thread termination does not work. | 152 // Run a loop that will be infinite if thread termination does not work. |
153 v8::Handle<v8::String> source = v8::String::NewFromUtf8( | 153 v8::Handle<v8::String> source = v8::String::NewFromUtf8( |
154 CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }"); | 154 CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }"); |
155 v8::Script::Compile(source)->Run(); | 155 v8::Script::Compile(source)->Run(); |
156 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); | 156 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); |
157 // Test that we can run the code again after thread termination. | 157 // Test that we can run the code again after thread termination. |
158 v8::Script::Compile(source)->Run(); | 158 v8::Script::Compile(source)->Run(); |
159 } | 159 } |
160 | 160 |
161 | 161 |
162 class TerminatorThread : public v8::internal::Thread { | 162 class TerminatorThread : public v8::base::Thread { |
163 public: | 163 public: |
164 explicit TerminatorThread(i::Isolate* isolate) | 164 explicit TerminatorThread(i::Isolate* isolate) |
165 : Thread("TerminatorThread"), | 165 : Thread("TerminatorThread"), |
166 isolate_(reinterpret_cast<v8::Isolate*>(isolate)) { } | 166 isolate_(reinterpret_cast<v8::Isolate*>(isolate)) { } |
167 void Run() { | 167 void Run() { |
168 semaphore->Wait(); | 168 semaphore->Wait(); |
169 CHECK(!v8::V8::IsExecutionTerminating(isolate_)); | 169 CHECK(!v8::V8::IsExecutionTerminating(isolate_)); |
170 v8::V8::TerminateExecution(isolate_); | 170 v8::V8::TerminateExecution(isolate_); |
171 } | 171 } |
172 | 172 |
173 private: | 173 private: |
174 v8::Isolate* isolate_; | 174 v8::Isolate* isolate_; |
175 }; | 175 }; |
176 | 176 |
177 | 177 |
178 // Test that a single thread of JavaScript execution can be terminated | 178 // Test that a single thread of JavaScript execution can be terminated |
179 // from the side by another thread. | 179 // from the side by another thread. |
180 TEST(TerminateOnlyV8ThreadFromOtherThread) { | 180 TEST(TerminateOnlyV8ThreadFromOtherThread) { |
181 semaphore = new v8::internal::Semaphore(0); | 181 semaphore = new v8::base::Semaphore(0); |
182 TerminatorThread thread(CcTest::i_isolate()); | 182 TerminatorThread thread(CcTest::i_isolate()); |
183 thread.Start(); | 183 thread.Start(); |
184 | 184 |
185 v8::HandleScope scope(CcTest::isolate()); | 185 v8::HandleScope scope(CcTest::isolate()); |
186 v8::Handle<v8::ObjectTemplate> global = | 186 v8::Handle<v8::ObjectTemplate> global = |
187 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop); | 187 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop); |
188 v8::Handle<v8::Context> context = | 188 v8::Handle<v8::Context> context = |
189 v8::Context::New(CcTest::isolate(), NULL, global); | 189 v8::Context::New(CcTest::isolate(), NULL, global); |
190 v8::Context::Scope context_scope(context); | 190 v8::Context::Scope context_scope(context); |
191 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); | 191 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 v8::HandleScope scope(isolate); | 371 v8::HandleScope scope(isolate); |
372 // Enqueue another should-not-run task to ensure we clean out the queue | 372 // Enqueue another should-not-run task to ensure we clean out the queue |
373 // when we terminate. | 373 // when we terminate. |
374 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun)); | 374 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun)); |
375 CompileRun("terminate(); while (true) { }"); | 375 CompileRun("terminate(); while (true) { }"); |
376 CHECK(v8::V8::IsExecutionTerminating()); | 376 CHECK(v8::V8::IsExecutionTerminating()); |
377 } | 377 } |
378 | 378 |
379 | 379 |
380 TEST(TerminateFromOtherThreadWhileMicrotaskRunning) { | 380 TEST(TerminateFromOtherThreadWhileMicrotaskRunning) { |
381 semaphore = new v8::internal::Semaphore(0); | 381 semaphore = new v8::base::Semaphore(0); |
382 TerminatorThread thread(CcTest::i_isolate()); | 382 TerminatorThread thread(CcTest::i_isolate()); |
383 thread.Start(); | 383 thread.Start(); |
384 | 384 |
385 v8::Isolate* isolate = CcTest::isolate(); | 385 v8::Isolate* isolate = CcTest::isolate(); |
386 isolate->SetAutorunMicrotasks(false); | 386 isolate->SetAutorunMicrotasks(false); |
387 v8::HandleScope scope(isolate); | 387 v8::HandleScope scope(isolate); |
388 v8::Handle<v8::ObjectTemplate> global = | 388 v8::Handle<v8::ObjectTemplate> global = |
389 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop); | 389 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop); |
390 v8::Handle<v8::Context> context = | 390 v8::Handle<v8::Context> context = |
391 v8::Context::New(CcTest::isolate(), NULL, global); | 391 v8::Context::New(CcTest::isolate(), NULL, global); |
392 v8::Context::Scope context_scope(context); | 392 v8::Context::Scope context_scope(context); |
393 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskLoopForever)); | 393 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskLoopForever)); |
394 // The second task should never be run because we bail out if we're | 394 // The second task should never be run because we bail out if we're |
395 // terminating. | 395 // terminating. |
396 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun)); | 396 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun)); |
397 isolate->RunMicrotasks(); | 397 isolate->RunMicrotasks(); |
398 | 398 |
399 v8::V8::CancelTerminateExecution(isolate); | 399 v8::V8::CancelTerminateExecution(isolate); |
400 isolate->RunMicrotasks(); // should not run MicrotaskShouldNotRun | 400 isolate->RunMicrotasks(); // should not run MicrotaskShouldNotRun |
401 | 401 |
402 thread.Join(); | 402 thread.Join(); |
403 delete semaphore; | 403 delete semaphore; |
404 semaphore = NULL; | 404 semaphore = NULL; |
405 } | 405 } |
OLD | NEW |