[vmkit-commits] [vmkit] r121487 - in /vmkit/trunk: include/mvm/VirtualMachine.h lib/J3/Classpath/ClasspathReflect.h lib/J3/Classpath/JavaUpcalls.cpp lib/J3/Compiler/JavaAOTCompiler.cpp lib/J3/VMCore/Jnjvm.cpp lib/J3/VMCore/Jnjvm.h lib/Mvm/CommonThread/CollectionRV.cpp lib/Mvm/CommonThread/ctthread.cpp lib/Mvm/Runtime/Object.cpp

Nicolas Geoffray nicolas.geoffray at lip6.fr
Fri Dec 10 08:09:18 PST 2010


Author: geoffray
Date: Fri Dec 10 10:09:18 2010
New Revision: 121487

URL: http://llvm.org/viewvc/llvm-project?rev=121487&view=rev
Log:
Do not depend on finalizers to free thread stacks. Instead let the main native thread free them.


Modified:
    vmkit/trunk/include/mvm/VirtualMachine.h
    vmkit/trunk/lib/J3/Classpath/ClasspathReflect.h
    vmkit/trunk/lib/J3/Classpath/JavaUpcalls.cpp
    vmkit/trunk/lib/J3/Compiler/JavaAOTCompiler.cpp
    vmkit/trunk/lib/J3/VMCore/Jnjvm.cpp
    vmkit/trunk/lib/J3/VMCore/Jnjvm.h
    vmkit/trunk/lib/Mvm/CommonThread/CollectionRV.cpp
    vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp
    vmkit/trunk/lib/Mvm/Runtime/Object.cpp

Modified: vmkit/trunk/include/mvm/VirtualMachine.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/VirtualMachine.h?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/include/mvm/VirtualMachine.h (original)
+++ vmkit/trunk/include/mvm/VirtualMachine.h Fri Dec 10 10:09:18 2010
@@ -55,8 +55,10 @@
 protected:
   VirtualMachine(mvm::BumpPtrAllocator &Alloc) :
 		  allocator(Alloc) {
-    mainThread = 0;
-    NumberOfThreads = 0;
+    mainThread = NULL;
+    numberOfThreads = 0;
+    doExit = false;
+    exitingThread = NULL;
   }
 
   virtual ~VirtualMachine() {
@@ -79,11 +81,21 @@
 
   /// NumberOfThreads - The number of threads that currently run under this VM.
   ///
-  uint32_t NumberOfThreads;
+  uint32_t numberOfThreads;
 
   /// ThreadLock - Lock to create or destroy a new thread.
   ///
-  mvm::SpinLock ThreadLock;
+  mvm::LockNormal threadLock;
+
+  /// ThreadVar - Condition variable to wake up the thread manager.
+  mvm::Cond threadVar;
+
+  /// exitingThread - Thread that is currently exiting. Used by the thread
+  /// manager to free the resources (stack) used by a thread.
+  mvm::Thread* exitingThread;
+
+  /// doExit - Should the VM exit now?
+  bool doExit;
   
   /// setMainThread - Set the main thread of this VM.
   ///
@@ -96,26 +108,41 @@
   /// addThread - Add a new thread to the list of threads.
   ///
   void addThread(mvm::Thread* th) {
-    ThreadLock.lock();
-    NumberOfThreads++;
+    threadLock.lock();
+    numberOfThreads++;
     if (th != mainThread) {
       if (mainThread) th->append(mainThread);
       else mainThread = th;
     }
-    ThreadLock.unlock();
+    threadLock.unlock();
   }
   
   /// removeThread - Remove the thread from the list of threads.
   ///
   void removeThread(mvm::Thread* th) {
-    ThreadLock.lock();
-    NumberOfThreads--;
+    threadLock.lock();
+    while (exitingThread != NULL) {
+      // Make sure the thread manager had a chance to consume the previous
+      // dead thread.
+      threadLock.unlock();
+      Thread::yield();
+      threadLock.lock();
+    }
+    numberOfThreads--;
     if (mainThread == th) mainThread = (Thread*)th->next();
     th->remove();
-    if (!NumberOfThreads) mainThread = 0;
-    ThreadLock.unlock();
+    if (numberOfThreads == 0) mainThread = NULL;
+    exitingThread = th;
+    threadVar.signal();
+    threadLock.unlock();
   }
 
+  /// exit - Exit this virtual machine.
+  void exit();
+
+  /// waitForExit - Wait until the virtual machine stops its execution.
+  void waitForExit();
+
 //===----------------------------------------------------------------------===//
 // (2) GC-related methods.
 //===----------------------------------------------------------------------===//
@@ -193,9 +220,6 @@
   /// runApplication - Run an application. The application name is in
   /// the arguments, hence it is the virtual machine's job to parse them.
   virtual void runApplication(int argc, char** argv) = 0;
-  
-  /// waitForExit - Wait until the virtual machine stops its execution.
-  virtual void waitForExit() = 0;
 };
 
 } // end namespace mvm

Modified: vmkit/trunk/lib/J3/Classpath/ClasspathReflect.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/Classpath/ClasspathReflect.h?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/Classpath/ClasspathReflect.h (original)
+++ vmkit/trunk/lib/J3/Classpath/ClasspathReflect.h Fri Dec 10 10:09:18 2010
@@ -150,11 +150,6 @@
   JavaThread* vmdata;
 
 public:
-  static void staticDestructor(JavaObjectVMThread* obj) {
-    llvm_gcroot(obj, 0);
-    mvm::Thread::releaseThread(obj->vmdata);
-  }
-  
   static void staticTracer(JavaObjectVMThread* obj, uintptr_t closure) {
     mvm::Collector::markAndTrace(obj, &obj->thread, closure);
   }

Modified: vmkit/trunk/lib/J3/Classpath/JavaUpcalls.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/Classpath/JavaUpcalls.cpp?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/Classpath/JavaUpcalls.cpp (original)
+++ vmkit/trunk/lib/J3/Classpath/JavaUpcalls.cpp Fri Dec 10 10:09:18 2010
@@ -518,11 +518,6 @@
   JavaObjectVMThread::staticTracer(obj, closure);
 }
 
-extern "C" void nativeJavaObjectVMThreadDestructor(JavaObjectVMThread* obj) {
-  llvm_gcroot(obj, 0);
-  JavaObjectVMThread::staticDestructor(obj);
-}
-
 extern "C" JavaString* Java_java_lang_VMSystem_getenv__Ljava_lang_String_2(JavaString* str) {
   JavaString* ret = 0;
   llvm_gcroot(str, 0);
@@ -1018,11 +1013,6 @@
       (uintptr_t)nativeJavaObjectVMThreadTracer,
       "nativeJavaObjectVMThreadTracer");
  
-   newVMThread->getVirtualVT()->setNativeDestructor(
-      (uintptr_t)nativeJavaObjectVMThreadDestructor,
-      "nativeJavaObjectVMThreadDestructor");
-
-   
   newReference = UPCALL_CLASS(loader, "java/lang/ref/Reference");
     
   EnqueueReference = 

Modified: vmkit/trunk/lib/J3/Compiler/JavaAOTCompiler.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/Compiler/JavaAOTCompiler.cpp?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/Compiler/JavaAOTCompiler.cpp (original)
+++ vmkit/trunk/lib/J3/Compiler/JavaAOTCompiler.cpp Fri Dec 10 10:09:18 2010
@@ -2123,12 +2123,7 @@
 
 end:
 
-  vm->threadSystem.nonDaemonLock.lock();
-  --(vm->threadSystem.nonDaemonThreads);
-  if (vm->threadSystem.nonDaemonThreads == 0)
-      vm->threadSystem.nonDaemonVar.signal();
-  vm->threadSystem.nonDaemonLock.unlock();  
-  
+  vm->threadSystem.leave(); 
 }
 
 void JavaAOTCompiler::compileFile(Jnjvm* vm, const char* n) {

Modified: vmkit/trunk/lib/J3/VMCore/Jnjvm.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/VMCore/Jnjvm.cpp?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/VMCore/Jnjvm.cpp (original)
+++ vmkit/trunk/lib/J3/VMCore/Jnjvm.cpp Fri Dec 10 10:09:18 2010
@@ -1240,19 +1240,6 @@
   } IGNORE;
 }
 
-void Jnjvm::waitForExit() { 
-  
-  threadSystem.nonDaemonLock.lock();
-  
-  while (threadSystem.nonDaemonThreads) {
-    threadSystem.nonDaemonVar.wait(&threadSystem.nonDaemonLock);
-  }
-  
-  threadSystem.nonDaemonLock.unlock();
-
-  return;
-}
-
 void Jnjvm::mainJavaStart(JavaThread* thread) {
 
   JavaString* str = NULL;
@@ -1315,7 +1302,7 @@
 void ThreadSystem::leave() {
   nonDaemonLock.lock();
   --nonDaemonThreads;
-  if (nonDaemonThreads == 0) nonDaemonVar.signal();
+  if (nonDaemonThreads == 0) mvm::Thread::get()->MyVM->exit();
   nonDaemonLock.unlock();  
 }
 

Modified: vmkit/trunk/lib/J3/VMCore/Jnjvm.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/VMCore/Jnjvm.h?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/VMCore/Jnjvm.h (original)
+++ vmkit/trunk/lib/J3/VMCore/Jnjvm.h Fri Dec 10 10:09:18 2010
@@ -59,15 +59,8 @@
 
   /// nonDaemonLock - Protection lock for the nonDaemonThreads variable.
   ///
-  mvm::LockNormal nonDaemonLock;
+  mvm::SpinLock nonDaemonLock;
 
-  /// nonDaemonVar - Condition variable to wake up the initial thread when it
-  /// waits for other non-daemon threads to end. The non-daemon thread that
-  /// decrements the nonDaemonThreads variable to zero wakes up the initial
-  /// thread.
-  ///
-  mvm::Cond nonDaemonVar;
-  
   /// ThreadSystem - Allocates a thread system management, initializing the
   /// lock, the condition variable and setting the initial number of non
   /// daemon threads to one, for the initial thread.
@@ -349,10 +342,6 @@
   ///
   virtual void runApplication(int argc, char** argv);
 
-  /// waitForExit - Waits that there are no more non-daemon threads in this JVM.
-  ///
-  virtual void waitForExit();
-
   /// loadBootstrap - Bootstraps the JVM, getting the class loader, initializing
   /// bootstrap classes (e.g. java/lang/Class, java/lang/Exception) and
   /// mapping the initial thread.

Modified: vmkit/trunk/lib/Mvm/CommonThread/CollectionRV.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/Mvm/CommonThread/CollectionRV.cpp?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/lib/Mvm/CommonThread/CollectionRV.cpp (original)
+++ vmkit/trunk/lib/Mvm/CommonThread/CollectionRV.cpp Fri Dec 10 10:09:18 2010
@@ -20,9 +20,9 @@
 void CollectionRV::another_mark() {
   mvm::Thread* th = mvm::Thread::get();
   assert(th->getLastSP() != NULL);
-  assert(nbJoined < th->MyVM->NumberOfThreads);
+  assert(nbJoined < th->MyVM->numberOfThreads);
   nbJoined++;
-  if (nbJoined == th->MyVM->NumberOfThreads) {
+  if (nbJoined == th->MyVM->numberOfThreads) {
     condInitiator.broadcast();
   }
 }
@@ -41,7 +41,7 @@
   // Add myself.
   nbJoined++;
 
-  while (nbJoined != self->MyVM->NumberOfThreads) {
+  while (nbJoined != self->MyVM->numberOfThreads) {
     condInitiator.wait(&_lockRV);
   } 
 }
@@ -51,7 +51,7 @@
   mvm::Thread* self = mvm::Thread::get();
   // Lock thread lock, so that we can traverse the thread list safely. This will
   // be released on finishRV.
-  self->MyVM->ThreadLock.lock();
+  self->MyVM->threadLock.lock();
 
   mvm::Thread* cur = self;
   assert(initiator == NULL);
@@ -94,7 +94,7 @@
   mvm::Thread* self = mvm::Thread::get();
   // Lock thread lock, so that we can traverse the thread list safely. This will
   // be released on finishRV.
-  self->MyVM->ThreadLock.lock();
+  self->MyVM->threadLock.lock();
   
   for (mvm::Thread* cur = (mvm::Thread*)self->next(); cur != self; 
        cur = (mvm::Thread*)cur->next()) {
@@ -204,9 +204,9 @@
     cur = (mvm::Thread*)cur->next();
   } while (cur != initiator);
 
-  assert(nbJoined == initiator->MyVM->NumberOfThreads && "Inconsistent state");
+  assert(nbJoined == initiator->MyVM->numberOfThreads && "Inconsistent state");
   nbJoined = 0;
-  initiator->MyVM->ThreadLock.unlock();
+  initiator->MyVM->threadLock.unlock();
   condEndRV.broadcast();
   initiator = NULL;
   unlockRV();
@@ -216,9 +216,9 @@
 void UncooperativeCollectionRV::finishRV() {
   lockRV();
   mvm::Thread* initiator = mvm::Thread::get();
-  assert(nbJoined == initiator->MyVM->NumberOfThreads && "Inconsistent state");
+  assert(nbJoined == initiator->MyVM->numberOfThreads && "Inconsistent state");
   nbJoined = 0;
-  initiator->MyVM->ThreadLock.unlock();
+  initiator->MyVM->threadLock.unlock();
   condEndRV.broadcast();
   unlockRV();
   initiator->inRV = false;

Modified: vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp (original)
+++ vmkit/trunk/lib/Mvm/CommonThread/ctthread.cpp Fri Dec 10 10:09:18 2010
@@ -446,17 +446,8 @@
 void* Thread::operator new(size_t sz) {
   assert(sz < (size_t)getpagesize() && "Thread local data too big");
   void* res = (void*)TheStackManager.allocate();
-  // Give it a second chance.
-  if (res == NULL) {
-    Collector::collect();
-    // Wait for the finalizer to have cleaned up the threads.
-    while (res == NULL) {
-      mvm::Thread::yield();
-      res = (void*)TheStackManager.allocate();
-    }
-  }
   // Make sure the thread information is cleared.
-  memset(res, 0, sz);
+  if (res != NULL) memset(res, 0, sz);
   return res;
 }
 

Modified: vmkit/trunk/lib/Mvm/Runtime/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/Mvm/Runtime/Object.cpp?rev=121487&r1=121486&r2=121487&view=diff
==============================================================================
--- vmkit/trunk/lib/Mvm/Runtime/Object.cpp (original)
+++ vmkit/trunk/lib/Mvm/Runtime/Object.cpp Fri Dec 10 10:09:18 2010
@@ -13,6 +13,7 @@
 #include "MvmGC.h"
 #include "mvm/Object.h"
 #include "mvm/PrintBuffer.h"
+#include "mvm/VirtualMachine.h"
 
 using namespace mvm;
 
@@ -54,3 +55,25 @@
   buf->writePtr((void*)o);
   buf->write(">");
 }
+
+void VirtualMachine::waitForExit() {   
+  threadLock.lock();
+  
+  while (!doExit) {
+    threadVar.wait(&threadLock);
+    if (exitingThread != NULL) {
+      Thread* th = exitingThread;
+      exitingThread = NULL;
+      mvm::Thread::releaseThread(th);
+    }
+  }
+  
+  threadLock.unlock();
+}
+
+void VirtualMachine::exit() { 
+  doExit = true;
+  threadLock.lock();
+  threadVar.signal();
+  threadLock.unlock();
+}





More information about the vmkit-commits mailing list