Change cctest/test-lockers to not copy persistent handles around.
authordcarney@chromium.org <dcarney@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 24 Apr 2013 14:23:46 +0000 (14:23 +0000)
committerdcarney@chromium.org <dcarney@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 24 Apr 2013 14:23:46 +0000 (14:23 +0000)
Instead, create Local handles to pass them around. This also means that the
code needs to be shifted around a bit such that a handle scope exists when
creating threads.

Review URL: https://codereview.chromium.org/14150017

Patch from Jochen Eisinger <jochen@chromium.org>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14420 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

include/v8.h
test/cctest/test-lockers.cc

index 8efb7c3..c85e71a 100644 (file)
@@ -377,6 +377,14 @@ template <class T> class Persistent : public Handle<T> {
   template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
 
   /**
+   * A constructor that creates a new global cell pointing to that. In contrast
+   * to the copy constructor, this creates a new persistent handle which needs
+   * to be separately disposed.
+   */
+  template <class S> V8_INLINE(Persistent(Isolate* isolate, Handle<S> that))
+      : Handle<T>(New(isolate, that)) { }
+
+  /**
    * "Casts" a plain handle which is known to be a persistent handle
    * to a persistent handle.
    */
index a54390b..a619c85 100644 (file)
@@ -33,6 +33,7 @@
 #include "isolate.h"
 #include "compilation-cache.h"
 #include "execution.h"
+#include "smart-pointers.h"
 #include "snapshot.h"
 #include "platform.h"
 #include "utils.h"
@@ -58,11 +59,10 @@ using ::v8::V8;
 // Migrating an isolate
 class KangarooThread : public v8::internal::Thread {
  public:
-  KangarooThread(v8::Isolate* isolate,
-                 v8::Handle<v8::Context> context)
+  KangarooThread(v8::Isolate* isolate, v8::Handle<v8::Context> context)
       : Thread("KangarooThread"),
-        isolate_(isolate), context_(context) {
-  }
+        isolate_(isolate),
+        context_(isolate, context) {}
 
   void Run() {
     {
@@ -95,19 +95,19 @@ class KangarooThread : public v8::internal::Thread {
 // Migrates an isolate from one thread to another
 TEST(KangarooIsolates) {
   v8::Isolate* isolate = v8::Isolate::New();
-  Persistent<v8::Context> context;
+  i::SmartPointer<KangarooThread> thread1;
   {
     v8::Locker locker(isolate);
     v8::Isolate::Scope isolate_scope(isolate);
     v8::HandleScope handle_scope(isolate);
-    context = v8::Context::New();
+    v8::Local<v8::Context> context = v8::Context::New(isolate);
     v8::Context::Scope context_scope(context);
     CHECK_EQ(isolate, v8::internal::Isolate::Current());
     CompileRun("function getValue() { return 30; }");
+    thread1.Reset(new KangarooThread(isolate, context));
   }
-  KangarooThread thread1(isolate, context);
-  thread1.Start();
-  thread1.Join();
+  thread1->Start();
+  thread1->Join();
 }
 
 static void CalcFibAndCheck() {
@@ -342,7 +342,7 @@ class LockIsolateAndCalculateFibSharedContextThread : public JoinableThread {
       v8::Isolate* isolate, v8::Handle<v8::Context> context)
     : JoinableThread("LockIsolateAndCalculateFibThread"),
       isolate_(isolate),
-      context_(context) {
+      context_(isolate, context) {
   }
 
   virtual void Run() {
@@ -368,15 +368,15 @@ class LockerUnlockerThread : public JoinableThread {
     v8::Locker lock(isolate_);
     v8::Isolate::Scope isolate_scope(isolate_);
     v8::HandleScope handle_scope(isolate_);
-    v8::Handle<v8::Context> context = v8::Context::New();
+    v8::Local<v8::Context> context = v8::Context::New(isolate_);
     {
       v8::Context::Scope context_scope(context);
       CalcFibAndCheck();
     }
     {
+      LockIsolateAndCalculateFibSharedContextThread thread(isolate_, context);
       isolate_->Exit();
       v8::Unlocker unlocker(isolate_);
-      LockIsolateAndCalculateFibSharedContextThread thread(isolate_, context);
       thread.Start();
       thread.Join();
     }
@@ -418,7 +418,7 @@ class LockTwiceAndUnlockThread : public JoinableThread {
     v8::Locker lock(isolate_);
     v8::Isolate::Scope isolate_scope(isolate_);
     v8::HandleScope handle_scope(isolate_);
-    v8::Handle<v8::Context> context = v8::Context::New();
+    v8::Local<v8::Context> context = v8::Context::New(isolate_);
     {
       v8::Context::Scope context_scope(context);
       CalcFibAndCheck();
@@ -426,9 +426,9 @@ class LockTwiceAndUnlockThread : public JoinableThread {
     {
       v8::Locker second_lock(isolate_);
       {
+        LockIsolateAndCalculateFibSharedContextThread thread(isolate_, context);
         isolate_->Exit();
         v8::Unlocker unlocker(isolate_);
-        LockIsolateAndCalculateFibSharedContextThread thread(isolate_, context);
         thread.Start();
         thread.Join();
       }
@@ -497,16 +497,23 @@ class LockAndUnlockDifferentIsolatesThread : public JoinableThread {
       }
     }
     {
+      i::SmartPointer<LockIsolateAndCalculateFibSharedContextThread> thread;
+      {
+        CHECK(v8::Locker::IsLocked(isolate1_));
+        v8::Isolate::Scope isolate_scope(isolate1_);
+        v8::HandleScope handle_scope(isolate1_);
+        thread.Reset(new LockIsolateAndCalculateFibSharedContextThread(
+            isolate1_, v8::Local<v8::Context>::New(isolate1_, context1)));
+      }
       v8::Unlocker unlock1(isolate1_);
       CHECK(!v8::Locker::IsLocked(isolate1_));
       CHECK(v8::Locker::IsLocked(isolate2_));
       v8::Isolate::Scope isolate_scope(isolate2_);
       v8::HandleScope handle_scope(isolate2_);
       v8::Context::Scope context_scope(context2);
-      LockIsolateAndCalculateFibSharedContextThread thread(isolate1_, context1);
-      thread.Start();
+      thread->Start();
       CalcFibAndCheck();
-      thread.Join();
+      thread->Join();
     }
   }
 
@@ -528,10 +535,10 @@ TEST(LockAndUnlockDifferentIsolates) {
 
 class LockUnlockLockThread : public JoinableThread {
  public:
-  LockUnlockLockThread(v8::Isolate* isolate, v8::Handle<v8::Context> context)
+  LockUnlockLockThread(v8::Isolate* isolate, v8::Local<v8::Context> context)
     : JoinableThread("LockUnlockLockThread"),
       isolate_(isolate),
-      context_(context) {
+      context_(isolate, context) {
   }
 
   virtual void Run() {
@@ -574,15 +581,16 @@ TEST(LockUnlockLockMultithreaded) {
 #endif
   v8::Isolate* isolate = v8::Isolate::New();
   Persistent<v8::Context> context;
+  i::List<JoinableThread*> threads(kNThreads);
   {
     v8::Locker locker_(isolate);
     v8::Isolate::Scope isolate_scope(isolate);
     v8::HandleScope handle_scope(isolate);
     context = v8::Context::New();
-  }
-  i::List<JoinableThread*> threads(kNThreads);
-  for (int i = 0; i < kNThreads; i++) {
-    threads.Add(new LockUnlockLockThread(isolate, context));
+    for (int i = 0; i < kNThreads; i++) {
+      threads.Add(new LockUnlockLockThread(
+          isolate, v8::Local<v8::Context>::New(isolate, context)));
+    }
   }
   StartJoinAndDeleteThreads(threads);
   isolate->Dispose();
@@ -590,10 +598,9 @@ TEST(LockUnlockLockMultithreaded) {
 
 class LockUnlockLockDefaultIsolateThread : public JoinableThread {
  public:
-  explicit LockUnlockLockDefaultIsolateThread(v8::Handle<v8::Context> context)
-    : JoinableThread("LockUnlockLockDefaultIsolateThread"),
-      context_(context) {
-  }
+  explicit LockUnlockLockDefaultIsolateThread(v8::Local<v8::Context> context)
+      : JoinableThread("LockUnlockLockDefaultIsolateThread"),
+        context_(CcTest::default_isolate(), context) {}
 
   virtual void Run() {
     v8::Locker lock1(CcTest::default_isolate());
@@ -625,14 +632,15 @@ TEST(LockUnlockLockDefaultIsolateMultithreaded) {
   const int kNThreads = 100;
 #endif
   Persistent<v8::Context> context;
+  i::List<JoinableThread*> threads(kNThreads);
   {
     v8::Locker locker_(CcTest::default_isolate());
     v8::HandleScope handle_scope(CcTest::default_isolate());
     context = v8::Context::New();
-  }
-  i::List<JoinableThread*> threads(kNThreads);
-  for (int i = 0; i < kNThreads; i++) {
-    threads.Add(new LockUnlockLockDefaultIsolateThread(context));
+    for (int i = 0; i < kNThreads; i++) {
+      threads.Add(new LockUnlockLockDefaultIsolateThread(
+          v8::Local<v8::Context>::New(CcTest::default_isolate(), context)));
+    }
   }
   StartJoinAndDeleteThreads(threads);
 }