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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); | 152 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
153 v8::Script::Compile(source)->Run(); | 153 v8::Script::Compile(source)->Run(); |
154 CHECK(!v8::V8::IsExecutionTerminating()); | 154 CHECK(!v8::V8::IsExecutionTerminating()); |
155 // Test that we can run the code again after thread termination. | 155 // Test that we can run the code again after thread termination. |
156 v8::Script::Compile(source)->Run(); | 156 v8::Script::Compile(source)->Run(); |
157 context.Dispose(); | 157 context.Dispose(); |
158 } | 158 } |
159 | 159 |
160 | 160 |
161 class TerminatorThread : public v8::internal::Thread { | 161 class TerminatorThread : public v8::internal::Thread { |
| 162 public: |
| 163 explicit TerminatorThread(i::Isolate* isolate) : Thread(isolate) { } |
162 void Run() { | 164 void Run() { |
163 semaphore->Wait(); | 165 semaphore->Wait(); |
164 CHECK(!v8::V8::IsExecutionTerminating()); | 166 CHECK(!v8::V8::IsExecutionTerminating()); |
165 v8::V8::TerminateExecution(); | 167 v8::V8::TerminateExecution(); |
166 } | 168 } |
167 }; | 169 }; |
168 | 170 |
169 | 171 |
170 // Test that a single thread of JavaScript execution can be terminated | 172 // Test that a single thread of JavaScript execution can be terminated |
171 // from the side by another thread. | 173 // from the side by another thread. |
172 TEST(TerminateOnlyV8ThreadFromOtherThread) { | 174 TEST(TerminateOnlyV8ThreadFromOtherThread) { |
173 semaphore = v8::internal::OS::CreateSemaphore(0); | 175 semaphore = v8::internal::OS::CreateSemaphore(0); |
174 TerminatorThread thread; | 176 TerminatorThread thread(i::Isolate::Current()); |
175 thread.Start(); | 177 thread.Start(); |
176 | 178 |
177 v8::HandleScope scope; | 179 v8::HandleScope scope; |
178 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal, DoLoop); | 180 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal, DoLoop); |
179 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); | 181 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
180 v8::Context::Scope context_scope(context); | 182 v8::Context::Scope context_scope(context); |
181 CHECK(!v8::V8::IsExecutionTerminating()); | 183 CHECK(!v8::V8::IsExecutionTerminating()); |
182 // Run a loop that will be infinite if thread termination does not work. | 184 // Run a loop that will be infinite if thread termination does not work. |
183 v8::Handle<v8::String> source = | 185 v8::Handle<v8::String> source = |
184 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); | 186 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
185 v8::Script::Compile(source)->Run(); | 187 v8::Script::Compile(source)->Run(); |
186 | 188 |
187 thread.Join(); | 189 thread.Join(); |
188 delete semaphore; | 190 delete semaphore; |
189 semaphore = NULL; | 191 semaphore = NULL; |
190 context.Dispose(); | 192 context.Dispose(); |
191 } | 193 } |
192 | 194 |
193 | 195 |
194 class LoopingThread : public v8::internal::Thread { | 196 class LoopingThread : public v8::internal::Thread { |
195 public: | 197 public: |
| 198 explicit LoopingThread(i::Isolate* isolate) : Thread(isolate) { } |
196 void Run() { | 199 void Run() { |
197 v8::Locker locker; | 200 v8::Locker locker; |
198 v8::HandleScope scope; | 201 v8::HandleScope scope; |
199 v8_thread_id_ = v8::V8::GetCurrentThreadId(); | 202 v8_thread_id_ = v8::V8::GetCurrentThreadId(); |
200 v8::Handle<v8::ObjectTemplate> global = | 203 v8::Handle<v8::ObjectTemplate> global = |
201 CreateGlobalTemplate(Signal, DoLoop); | 204 CreateGlobalTemplate(Signal, DoLoop); |
202 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); | 205 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
203 v8::Context::Scope context_scope(context); | 206 v8::Context::Scope context_scope(context); |
204 CHECK(!v8::V8::IsExecutionTerminating()); | 207 CHECK(!v8::V8::IsExecutionTerminating()); |
205 // Run a loop that will be infinite if thread termination does not work. | 208 // Run a loop that will be infinite if thread termination does not work. |
(...skipping 12 matching lines...) Expand all Loading... |
218 | 221 |
219 // Test that multiple threads using V8 can be terminated from another | 222 // Test that multiple threads using V8 can be terminated from another |
220 // thread when using Lockers and preemption. | 223 // thread when using Lockers and preemption. |
221 TEST(TerminateMultipleV8Threads) { | 224 TEST(TerminateMultipleV8Threads) { |
222 { | 225 { |
223 v8::Locker locker; | 226 v8::Locker locker; |
224 v8::V8::Initialize(); | 227 v8::V8::Initialize(); |
225 v8::Locker::StartPreemption(1); | 228 v8::Locker::StartPreemption(1); |
226 semaphore = v8::internal::OS::CreateSemaphore(0); | 229 semaphore = v8::internal::OS::CreateSemaphore(0); |
227 } | 230 } |
228 LoopingThread thread1; | 231 LoopingThread thread1(i::Isolate::Current()); |
229 thread1.Start(); | 232 thread1.Start(); |
230 LoopingThread thread2; | 233 LoopingThread thread2(i::Isolate::Current()); |
231 thread2.Start(); | 234 thread2.Start(); |
232 // Wait until both threads have signaled the semaphore. | 235 // Wait until both threads have signaled the semaphore. |
233 semaphore->Wait(); | 236 semaphore->Wait(); |
234 semaphore->Wait(); | 237 semaphore->Wait(); |
235 { | 238 { |
236 v8::Locker locker; | 239 v8::Locker locker; |
237 v8::V8::TerminateExecution(thread1.GetV8ThreadId()); | 240 v8::V8::TerminateExecution(thread1.GetV8ThreadId()); |
238 v8::V8::TerminateExecution(thread2.GetV8ThreadId()); | 241 v8::V8::TerminateExecution(thread2.GetV8ThreadId()); |
239 } | 242 } |
240 thread1.Join(); | 243 thread1.Join(); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 v8::Handle<v8::String> source = | 349 v8::Handle<v8::String> source = |
347 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); | 350 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
348 v8::Script::Compile(source)->Run(); | 351 v8::Script::Compile(source)->Run(); |
349 CHECK(!v8::V8::IsExecutionTerminating()); | 352 CHECK(!v8::V8::IsExecutionTerminating()); |
350 // Check we can run JS again after termination. | 353 // Check we can run JS again after termination. |
351 CHECK(v8::Script::Compile(v8::String::New("function f() { return true; }" | 354 CHECK(v8::Script::Compile(v8::String::New("function f() { return true; }" |
352 "f()"))->Run()->IsTrue()); | 355 "f()"))->Run()->IsTrue()); |
353 context.Dispose(); | 356 context.Dispose(); |
354 } | 357 } |
355 | 358 |
OLD | NEW |