Revision: 4887
Author: lukezarko
Date: Wed Jun 16 18:24:32 2010
Log: [Isolates] Add a preprocessor flag to guard using TLS for the global isolate.

- Define V8_USE_TLS_FOR_GLOBAL_ISOLATE to use TLS to look up the global isolate.

Review URL: http://codereview.chromium.org/2860009
http://code.google.com/p/v8/source/detail?r=4887

Modified:
 /branches/experimental/isolates/src/isolate.cc
 /branches/experimental/isolates/src/isolate.h

=======================================
--- /branches/experimental/isolates/src/isolate.cc      Wed Jun 16 17:13:33 2010
+++ /branches/experimental/isolates/src/isolate.cc      Wed Jun 16 18:24:32 2010
@@ -44,7 +44,18 @@
 namespace internal {


-Isolate* Isolate::global_isolate = NULL;
+#ifdef V8_USE_TLS_FOR_GLOBAL_ISOLATE
+Thread::LocalStorageKey Isolate::global_isolate_key_;
+
+
+Isolate* Isolate::InitThreadForGlobalIsolate() {
+  ASSERT(global_isolate_ != NULL);
+  Thread::SetThreadLocal(global_isolate_key_, global_isolate_);
+  return global_isolate_;
+}
+#endif
+
+Isolate* Isolate::global_isolate_ = NULL;
 int Isolate::number_of_isolates_ = 0;


@@ -60,9 +71,12 @@


 void Isolate::InitOnce() {
-  ASSERT(global_isolate == NULL);
-  global_isolate = new Isolate();
-  CHECK(global_isolate->PreInit());
+  ASSERT(global_isolate_ == NULL);
+#ifdef V8_USE_TLS_FOR_GLOBAL_ISOLATE
+  global_isolate_key_ = Thread::CreateThreadLocalKey();
+#endif
+  global_isolate_ = new Isolate();
+  CHECK(global_isolate_->PreInit());
 }


@@ -70,19 +84,22 @@
   // While we're still building out support for isolates, only support
   // one single global isolate.

-  if (global_isolate != NULL) {
+  if (global_isolate_ != NULL) {
     // Allow for two-phase initialization.
-    ASSERT(global_isolate->state_ != INITIALIZED);
+    ASSERT(global_isolate_->state_ != INITIALIZED);
   } else {
-    global_isolate = new Isolate();
+    global_isolate_ = new Isolate();
   }

-  if (global_isolate->Init(des)) {
+  if (global_isolate_->Init(des)) {
     ++number_of_isolates_;
-    return global_isolate;
+    return global_isolate_;
   } else {
-    delete global_isolate;
-    global_isolate = NULL;
+    delete global_isolate_;
+    global_isolate_ = NULL;
+#ifdef V8_USE_TLS_FOR_GLOBAL_ISOLATE
+    Thread::SetThreadLocal(global_isolate_key_, NULL);
+#endif
     return NULL;
   }
 }
@@ -139,7 +156,11 @@

 bool Isolate::PreInit() {
   if (state_ != UNINITIALIZED) return true;
-  ASSERT(global_isolate == this);
+  ASSERT(global_isolate_ == this);
+
+#ifdef V8_USE_TLS_FOR_GLOBAL_ISOLATE
+  InitThreadForGlobalIsolate();
+#endif

   // Safe after setting Heap::isolate_, initializing StackGuard and
   // ensuring that Isolate::Current() == this.
@@ -158,7 +179,7 @@


 bool Isolate::Init(Deserializer* des) {
-  ASSERT(global_isolate == this);
+  ASSERT(global_isolate_ == this);

   bool create_heap_objects = des == NULL;

=======================================
--- /branches/experimental/isolates/src/isolate.h       Wed Jun 16 09:30:02 2010
+++ /branches/experimental/isolates/src/isolate.h       Wed Jun 16 18:24:32 2010
@@ -28,6 +28,8 @@
 #ifndef V8_ISOLATE_H_
 #define V8_ISOLATE_H_

+// #define V8_USE_TLS_FOR_GLOBAL_ISOLATE
+
 #include "apiutils.h"
 #include "heap.h"
 #include "execution.h"
@@ -152,9 +154,23 @@

   // Returns the single global isolate.
   static Isolate* Current() {
-    ASSERT(global_isolate != NULL);
-    return global_isolate;
-  }
+#ifdef V8_USE_TLS_FOR_GLOBAL_ISOLATE
+    Isolate* isolate = reinterpret_cast<Isolate*>(
+        Thread::GetThreadLocal(global_isolate_key_));
+    if (isolate == NULL) {
+      isolate = InitThreadForGlobalIsolate();
+      ASSERT(isolate != NULL);
+    }
+    return isolate;
+#else
+    ASSERT(global_isolate_ != NULL);
+    return global_isolate_;
+#endif
+  }
+
+#ifdef V8_USE_TLS_FOR_GLOBAL_ISOLATE
+  static Isolate* InitThreadForGlobalIsolate();
+#endif

   // Creates a new isolate (perhaps using a deserializer). Returns null
   // on failure.
@@ -216,7 +232,11 @@
  private:
   Isolate();

-  static Isolate* global_isolate;
+#ifdef V8_USE_TLS_FOR_GLOBAL_ISOLATE
+  static Thread::LocalStorageKey global_isolate_key_;
+#endif
+  static Isolate* global_isolate_;
+
   // TODO(isolates): Access to this global counter should be serialized.
   static int number_of_isolates_;

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to