static intptr_t AdjustAmountOfExternalAllocatedMemory(
intptr_t change_in_bytes);
- /**
- * Retrieve the V8 thread id of the calling thread.
- *
- * The thread id for a thread should only be retrieved after the V8
- * lock has been acquired with a Locker object with that thread.
- */
- static int GetCurrentThreadId();
-
- /**
- * Forcefully terminate execution of a JavaScript thread. This can
- * be used to terminate long-running scripts.
- *
- * TerminateExecution should only be called when then V8 lock has
- * been acquired with a Locker object. Therefore, in order to be
- * able to terminate long-running threads, preemption must be
- * enabled to allow the user of TerminateExecution to acquire the
- * lock.
- *
- * The termination is achieved by throwing an exception that is
- * uncatchable by JavaScript exception handlers. Termination
- * exceptions act as if they were caught by a C++ TryCatch exception
- * handler. If forceful termination is used, any C++ TryCatch
- * exception handler that catches an exception should check if that
- * exception is a termination exception and immediately return if
- * that is the case. Returning immediately in that case will
- * continue the propagation of the termination exception if needed.
- *
- * The thread id passed to TerminateExecution must have been
- * obtained by calling GetCurrentThreadId on the thread in question.
- *
- * \param thread_id The thread id of the thread to terminate.
- */
- static void TerminateExecution(int thread_id);
-
/**
* Forcefully terminate the current thread of JavaScript execution
* in the given isolate. If no isolate is provided, the default
}
-int V8::GetCurrentThreadId() {
- i::Isolate* isolate = i::Isolate::Current();
- EnsureInitializedForIsolate(isolate, "V8::GetCurrentThreadId()");
- return isolate->thread_id().ToInteger();
-}
-
-
-void V8::TerminateExecution(int thread_id) {
- i::Isolate* isolate = i::Isolate::Current();
- if (!isolate->IsInitialized()) return;
- API_ENTRY_CHECK(isolate, "V8::TerminateExecution()");
- // If the thread_id identifies the current thread just terminate
- // execution right away. Otherwise, ask the thread manager to
- // terminate the thread with the given id if any.
- i::ThreadId internal_tid = i::ThreadId::FromInteger(thread_id);
- if (isolate->thread_id().Equals(internal_tid)) {
- isolate->stack_guard()->TerminateExecution();
- } else {
- isolate->thread_manager()->TerminateExecution(internal_tid);
- }
-}
-
-
void V8::TerminateExecution(Isolate* isolate) {
// If no isolate is supplied, use the default isolate.
if (isolate != NULL) {
}
-class LoopingThread : public v8::internal::Thread {
- public:
- LoopingThread() : Thread("LoopingThread") { }
- void Run() {
- v8::Locker locker(CcTest::default_isolate());
- v8::HandleScope scope(CcTest::default_isolate());
- v8_thread_id_ = v8::V8::GetCurrentThreadId();
- v8::Handle<v8::ObjectTemplate> global =
- CreateGlobalTemplate(Signal, DoLoop);
- v8::Handle<v8::Context> context =
- v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
- v8::Context::Scope context_scope(context);
- CHECK(!v8::V8::IsExecutionTerminating());
- // Run a loop that will be infinite if thread termination does not work.
- v8::Handle<v8::String> source =
- v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
- v8::Script::Compile(source)->Run();
- }
-
- int GetV8ThreadId() { return v8_thread_id_; }
-
- private:
- int v8_thread_id_;
-};
-
-
-// Test that multiple threads using default isolate can be terminated
-// from another thread when using Lockers and preemption.
-TEST(TerminateMultipleV8ThreadsDefaultIsolate) {
- {
- v8::Locker locker(CcTest::default_isolate());
- v8::V8::Initialize();
- v8::Locker::StartPreemption(1);
- semaphore = new v8::internal::Semaphore(0);
- }
- const int kThreads = 2;
- i::List<LoopingThread*> threads(kThreads);
- for (int i = 0; i < kThreads; i++) {
- threads.Add(new LoopingThread());
- }
- for (int i = 0; i < kThreads; i++) {
- threads[i]->Start();
- }
- // Wait until all threads have signaled the semaphore.
- for (int i = 0; i < kThreads; i++) {
- semaphore->Wait();
- }
- {
- v8::Locker locker(CcTest::default_isolate());
- for (int i = 0; i < kThreads; i++) {
- v8::V8::TerminateExecution(threads[i]->GetV8ThreadId());
- }
- }
- for (int i = 0; i < kThreads; i++) {
- threads[i]->Join();
- delete threads[i];
- }
- {
- v8::Locker locker(CcTest::default_isolate());
- v8::Locker::StopPreemption();
- }
-
- delete semaphore;
- semaphore = NULL;
-}
-
-
int call_count = 0;