The deferred handes list belongs to the Isolate and not to the
authorsanjoy@chromium.org <sanjoy@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 18 Jul 2012 14:15:02 +0000 (14:15 +0000)
committersanjoy@chromium.org <sanjoy@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 18 Jul 2012 14:15:02 +0000 (14:15 +0000)
HandleScopeImplementer (since it isn't something specific to a
particular thread executing on an Isolate). This patch ensures correct
traversal of the handles when when multiple compilations are queued
up.

BUG=
TEST=

Review URL: https://chromiumcodereview.appspot.com/10778036

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

src/api.cc
src/api.h
src/handles.h
src/heap.cc
src/isolate.cc
src/isolate.h

index 8218ad43c3a6c7dcfeb8b448a0d4b15aaaf0ccf1..e1d57e3cb2e0397dae0854cbf4ddbbe61f03acd5 100644 (file)
@@ -6422,12 +6422,6 @@ void HandleScopeImplementer::IterateThis(ObjectVisitor* v) {
     Object** start = reinterpret_cast<Object**>(&saved_contexts_.first());
     v->VisitPointers(start, start + saved_contexts_.length());
   }
-
-  for (DeferredHandles* deferred = deferred_handles_head_;
-       deferred != NULL;
-       deferred = deferred->next_) {
-    deferred->Iterate(v);
-  }
 }
 
 
@@ -6448,8 +6442,8 @@ char* HandleScopeImplementer::Iterate(ObjectVisitor* v, char* storage) {
 
 
 DeferredHandles* HandleScopeImplementer::Detach(Object** prev_limit) {
-  DeferredHandles* deferred = new DeferredHandles(
-      deferred_handles_head_, isolate()->handle_scope_data()->next, this);
+  DeferredHandles* deferred =
+      new DeferredHandles(isolate()->handle_scope_data()->next, isolate());
 
   while (!blocks_.is_empty()) {
     Object** block_start = blocks_.last();
@@ -6470,41 +6464,12 @@ DeferredHandles* HandleScopeImplementer::Detach(Object** prev_limit) {
   ASSERT(prev_limit == NULL || !blocks_.is_empty());
 
   ASSERT(!blocks_.is_empty() && prev_limit != NULL);
-  deferred_handles_head_ = deferred;
   ASSERT(last_handle_before_deferred_block_ != NULL);
   last_handle_before_deferred_block_ = NULL;
   return deferred;
 }
 
 
-void HandleScopeImplementer::DestroyDeferredHandles(DeferredHandles* deferred) {
-#ifdef DEBUG
-  DeferredHandles* deferred_iterator = deferred;
-  while (deferred_iterator->previous_ != NULL) {
-    deferred_iterator = deferred_iterator->previous_;
-  }
-  ASSERT(deferred_handles_head_ == deferred_iterator);
-#endif
-  if (deferred_handles_head_ == deferred) {
-    deferred_handles_head_ = deferred_handles_head_->next_;
-  }
-  if (deferred->next_ != NULL) {
-    deferred->next_->previous_ = deferred->previous_;
-  }
-  if (deferred->previous_ != NULL) {
-    deferred->previous_->next_ = deferred->next_;
-  }
-  for (int i = 0; i < deferred->blocks_.length(); i++) {
-#ifdef DEBUG
-    HandleScope::ZapRange(deferred->blocks_[i],
-                          &deferred->blocks_[i][kHandleBlockSize]);
-#endif
-    if (spare_ != NULL) DeleteArray(spare_);
-    spare_ = deferred->blocks_[i];
-  }
-}
-
-
 void HandleScopeImplementer::BeginDeferredScope() {
   ASSERT(last_handle_before_deferred_block_ == NULL);
   last_handle_before_deferred_block_ = isolate()->handle_scope_data()->next;
@@ -6512,7 +6477,14 @@ void HandleScopeImplementer::BeginDeferredScope() {
 
 
 DeferredHandles::~DeferredHandles() {
-  impl_->DestroyDeferredHandles(this);
+  isolate_->UnlinkDeferredHandles(this);
+
+  for (int i = 0; i < blocks_.length(); i++) {
+#ifdef DEBUG
+    HandleScope::ZapRange(blocks_[i], &blocks_[i][kHandleBlockSize]);
+#endif
+    isolate_->handle_scope_implementer()->ReturnBlock(blocks_[i]);
+  }
 }
 
 
index 6be447424687a51812c4126edd333ddb6b9cfb88..ea70dca8a5fbe27a2af63d93206ab88fcd72d238 100644 (file)
--- a/src/api.h
+++ b/src/api.h
@@ -397,13 +397,12 @@ class DeferredHandles {
   ~DeferredHandles();
 
  private:
-  DeferredHandles(DeferredHandles* next, Object** first_block_limit,
-                  HandleScopeImplementer* impl)
-      : next_(next),
+  DeferredHandles(Object** first_block_limit, Isolate* isolate)
+      : next_(NULL),
         previous_(NULL),
         first_block_limit_(first_block_limit),
-        impl_(impl) {
-    if (next != NULL) next->previous_ = this;
+        isolate_(isolate) {
+    isolate->LinkDeferredHandles(this);
   }
 
   void Iterate(ObjectVisitor* v);
@@ -412,9 +411,10 @@ class DeferredHandles {
   DeferredHandles* next_;
   DeferredHandles* previous_;
   Object** first_block_limit_;
-  HandleScopeImplementer* impl_;
+  Isolate* isolate_;
 
   friend class HandleScopeImplementer;
+  friend class Isolate;
 };
 
 
@@ -436,8 +436,7 @@ class HandleScopeImplementer {
         saved_contexts_(0),
         spare_(NULL),
         call_depth_(0),
-        last_handle_before_deferred_block_(NULL),
-        deferred_handles_head_(NULL) { }
+        last_handle_before_deferred_block_(NULL) { }
 
   ~HandleScopeImplementer() {
     DeleteArray(spare_);
@@ -475,13 +474,18 @@ class HandleScopeImplementer {
   inline List<internal::Object**>* blocks() { return &blocks_; }
   Isolate* isolate() const { return isolate_; }
 
+  void ReturnBlock(Object** block) {
+    ASSERT(block != NULL);
+    if (spare_ != NULL) DeleteArray(spare_);
+    spare_ = block;
+  }
+
  private:
   void ResetAfterArchive() {
     blocks_.Initialize(0);
     entered_contexts_.Initialize(0);
     saved_contexts_.Initialize(0);
     spare_ = NULL;
-    deferred_handles_head_ = NULL;
     last_handle_before_deferred_block_ = NULL;
     call_depth_ = 0;
   }
@@ -490,7 +494,6 @@ class HandleScopeImplementer {
     ASSERT(blocks_.length() == 0);
     ASSERT(entered_contexts_.length() == 0);
     ASSERT(saved_contexts_.length() == 0);
-    ASSERT(deferred_handles_head_ == NULL);
     blocks_.Free();
     entered_contexts_.Free();
     saved_contexts_.Free();
@@ -503,7 +506,6 @@ class HandleScopeImplementer {
 
   void BeginDeferredScope();
   DeferredHandles* Detach(Object** prev_limit);
-  void DestroyDeferredHandles(DeferredHandles* handles);
 
   Isolate* isolate_;
   List<internal::Object**> blocks_;
@@ -514,7 +516,6 @@ class HandleScopeImplementer {
   Object** spare_;
   int call_depth_;
   Object** last_handle_before_deferred_block_;
-  DeferredHandles* deferred_handles_head_;
   // This is only used for threading support.
   v8::ImplementationUtilities::HandleScopeData handle_scope_data_;
 
index 6f3044cb8a8ff19e1328c1cba2e215a32b5e5938..c4180d57c0e49455383839d7c10d3721d37e869c 100644 (file)
@@ -95,6 +95,7 @@ class Handle {
 };
 
 
+class DeferredHandles;
 class HandleScopeImplementer;
 
 
@@ -159,9 +160,11 @@ class HandleScope {
   // Zaps the handles in the half-open interval [start, end).
   static void ZapRange(internal::Object** start, internal::Object** end);
 
+  friend class v8::internal::DeferredHandles;
   friend class v8::HandleScope;
   friend class v8::internal::HandleScopeImplementer;
   friend class v8::ImplementationUtilities;
+  friend class v8::internal::Isolate;
 };
 
 
index 15fd430e49409f99327abe605bad2efef6d7c8f6..15ac5c31ae944a12a209db2e5206b91a70c01503 100644 (file)
@@ -5698,6 +5698,7 @@ void Heap::IterateStrongRoots(ObjectVisitor* v, VisitMode mode) {
 
   // Iterate over local handles in handle scopes.
   isolate_->handle_scope_implementer()->Iterate(v);
+  isolate_->IterateDeferredHandles(v);
   v->Synchronize(VisitorSynchronization::kHandleScope);
 
   // Iterate over the builtin code objects and code stubs in the
index cb73c26b98e8849d3ee1048a47e05e5f1d0b1d43..e5d46cd903b8354d08391470d9f5d46564123d22 100644 (file)
@@ -477,6 +477,14 @@ void Isolate::Iterate(ObjectVisitor* v) {
   Iterate(v, current_t);
 }
 
+void Isolate::IterateDeferredHandles(ObjectVisitor* visitor) {
+  for (DeferredHandles* deferred = deferred_handles_head_;
+       deferred != NULL;
+       deferred = deferred->next_) {
+    deferred->Iterate(visitor);
+  }
+}
+
 
 void Isolate::RegisterTryCatchHandler(v8::TryCatch* that) {
   // The ARM simulator has a separate JS stack.  We therefore register
@@ -1484,7 +1492,8 @@ Isolate::Isolate()
       string_tracker_(NULL),
       regexp_stack_(NULL),
       date_cache_(NULL),
-      context_exit_happened_(false) {
+      context_exit_happened_(false),
+      deferred_handles_head_(NULL) {
   TRACE_ISOLATE(constructor);
 
   memset(isolate_addresses_, 0,
@@ -1989,6 +1998,36 @@ void Isolate::Exit() {
 }
 
 
+void Isolate::LinkDeferredHandles(DeferredHandles* deferred) {
+  deferred->next_ = deferred_handles_head_;
+  if (deferred_handles_head_ != NULL) {
+    deferred_handles_head_->previous_ = deferred;
+  }
+  deferred_handles_head_ = deferred;
+}
+
+
+void Isolate::UnlinkDeferredHandles(DeferredHandles* deferred) {
+#ifdef DEBUG
+  // In debug mode assert that the linked list is well-formed.
+  DeferredHandles* deferred_iterator = deferred;
+  while (deferred_iterator->previous_ != NULL) {
+    deferred_iterator = deferred_iterator->previous_;
+  }
+  ASSERT(deferred_handles_head_ == deferred_iterator);
+#endif
+  if (deferred_handles_head_ == deferred) {
+    deferred_handles_head_ = deferred_handles_head_->next_;
+  }
+  if (deferred->next_ != NULL) {
+    deferred->next_->previous_ = deferred->previous_;
+  }
+  if (deferred->previous_ != NULL) {
+    deferred->previous_->next_ = deferred->next_;
+  }
+}
+
+
 #ifdef DEBUG
 #define ISOLATE_FIELD_OFFSET(type, name, ignored)                       \
 const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_);
index 8b4e12bc0d38aea0acf9498e49258c7c0964995e..ef33dcb3c0b79493adc7e13941e20dc9d122b49b 100644 (file)
@@ -1055,6 +1055,10 @@ class Isolate {
     date_cache_ = date_cache;
   }
 
+  void IterateDeferredHandles(ObjectVisitor* visitor);
+  void LinkDeferredHandles(DeferredHandles* deferred_handles);
+  void UnlinkDeferredHandles(DeferredHandles* deferred_handles);
+
  private:
   Isolate();
 
@@ -1278,6 +1282,8 @@ class Isolate {
 #undef ISOLATE_FIELD_OFFSET
 #endif
 
+  DeferredHandles* deferred_handles_head_;
+
   friend class ExecutionAccess;
   friend class IsolateInitializer;
   friend class ThreadManager;