Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: test/cctest/test-thread-termination.cc

Issue 7003108: "Deiceolate" Thread classes. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
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: 162 public:
163 explicit TerminatorThread(i::Isolate* isolate) 163 explicit TerminatorThread(i::Isolate* isolate)
164 : Thread(isolate, "TerminatorThread") { } 164 : Thread("TerminatorThread"),
165 isolate_(reinterpret_cast<v8::Isolate*>(isolate)) { }
165 void Run() { 166 void Run() {
166 semaphore->Wait(); 167 semaphore->Wait();
167 CHECK(!v8::V8::IsExecutionTerminating()); 168 CHECK(!v8::V8::IsExecutionTerminating(isolate_));
168 v8::V8::TerminateExecution(); 169 v8::V8::TerminateExecution(isolate_);
169 } 170 }
171
172 private:
173 v8::Isolate* isolate_;
170 }; 174 };
171 175
172 176
173 // Test that a single thread of JavaScript execution can be terminated 177 // Test that a single thread of JavaScript execution can be terminated
174 // from the side by another thread. 178 // from the side by another thread.
175 TEST(TerminateOnlyV8ThreadFromOtherThread) { 179 TEST(TerminateOnlyV8ThreadFromOtherThread) {
176 semaphore = v8::internal::OS::CreateSemaphore(0); 180 semaphore = v8::internal::OS::CreateSemaphore(0);
177 TerminatorThread thread(i::Isolate::Current()); 181 TerminatorThread thread(i::Isolate::Current());
178 thread.Start(); 182 thread.Start();
179 183
180 v8::HandleScope scope; 184 v8::HandleScope scope;
181 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal, DoLoop); 185 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal, DoLoop);
182 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); 186 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
183 v8::Context::Scope context_scope(context); 187 v8::Context::Scope context_scope(context);
184 CHECK(!v8::V8::IsExecutionTerminating()); 188 CHECK(!v8::V8::IsExecutionTerminating());
185 // Run a loop that will be infinite if thread termination does not work. 189 // Run a loop that will be infinite if thread termination does not work.
186 v8::Handle<v8::String> source = 190 v8::Handle<v8::String> source =
187 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); 191 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
188 v8::Script::Compile(source)->Run(); 192 v8::Script::Compile(source)->Run();
189 193
190 thread.Join(); 194 thread.Join();
191 delete semaphore; 195 delete semaphore;
192 semaphore = NULL; 196 semaphore = NULL;
193 context.Dispose(); 197 context.Dispose();
194 } 198 }
195 199
196 200
197 class LoopingThread : public v8::internal::Thread { 201 class LoopingThread : public v8::internal::Thread {
198 public: 202 public:
199 explicit LoopingThread(i::Isolate* isolate) 203 LoopingThread() : Thread("LoopingThread") { }
200 : Thread(isolate, "LoopingThread") { }
201 void Run() { 204 void Run() {
202 v8::Locker locker; 205 v8::Locker locker;
203 v8::HandleScope scope; 206 v8::HandleScope scope;
204 v8_thread_id_ = v8::V8::GetCurrentThreadId(); 207 v8_thread_id_ = v8::V8::GetCurrentThreadId();
205 v8::Handle<v8::ObjectTemplate> global = 208 v8::Handle<v8::ObjectTemplate> global =
206 CreateGlobalTemplate(Signal, DoLoop); 209 CreateGlobalTemplate(Signal, DoLoop);
207 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); 210 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
208 v8::Context::Scope context_scope(context); 211 v8::Context::Scope context_scope(context);
209 CHECK(!v8::V8::IsExecutionTerminating()); 212 CHECK(!v8::V8::IsExecutionTerminating());
210 // Run a loop that will be infinite if thread termination does not work. 213 // Run a loop that will be infinite if thread termination does not work.
(...skipping 15 matching lines...) Expand all
226 TEST(TerminateMultipleV8ThreadsDefaultIsolate) { 229 TEST(TerminateMultipleV8ThreadsDefaultIsolate) {
227 { 230 {
228 v8::Locker locker; 231 v8::Locker locker;
229 v8::V8::Initialize(); 232 v8::V8::Initialize();
230 v8::Locker::StartPreemption(1); 233 v8::Locker::StartPreemption(1);
231 semaphore = v8::internal::OS::CreateSemaphore(0); 234 semaphore = v8::internal::OS::CreateSemaphore(0);
232 } 235 }
233 const int kThreads = 2; 236 const int kThreads = 2;
234 i::List<LoopingThread*> threads(kThreads); 237 i::List<LoopingThread*> threads(kThreads);
235 for (int i = 0; i < kThreads; i++) { 238 for (int i = 0; i < kThreads; i++) {
236 threads.Add(new LoopingThread(i::Isolate::Current())); 239 threads.Add(new LoopingThread());
237 } 240 }
238 for (int i = 0; i < kThreads; i++) { 241 for (int i = 0; i < kThreads; i++) {
239 threads[i]->Start(); 242 threads[i]->Start();
240 } 243 }
241 // Wait until all threads have signaled the semaphore. 244 // Wait until all threads have signaled the semaphore.
242 for (int i = 0; i < kThreads; i++) { 245 for (int i = 0; i < kThreads; i++) {
243 semaphore->Wait(); 246 semaphore->Wait();
244 } 247 }
245 { 248 {
246 v8::Locker locker; 249 v8::Locker locker;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 v8::Handle<v8::String> source = 362 v8::Handle<v8::String> source =
360 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); 363 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
361 v8::Script::Compile(source)->Run(); 364 v8::Script::Compile(source)->Run();
362 CHECK(!v8::V8::IsExecutionTerminating()); 365 CHECK(!v8::V8::IsExecutionTerminating());
363 // Check we can run JS again after termination. 366 // Check we can run JS again after termination.
364 CHECK(v8::Script::Compile(v8::String::New("function f() { return true; }" 367 CHECK(v8::Script::Compile(v8::String::New("function f() { return true; }"
365 "f()"))->Run()->IsTrue()); 368 "f()"))->Run()->IsTrue());
366 context.Dispose(); 369 context.Dispose();
367 } 370 }
368 371
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698