[vmkit-commits] [vmkit] r120906 - in /vmkit/branches/multi-vm: include/debug.h include/mvm/Threads/CollectionRV.h include/mvm/Threads/Thread.h include/mvm/VMKit.h lib/J3/VMCore/Jnjvm.cpp lib/J3/VMCore/Jnjvm.h lib/J3/VMCore/ReferenceQueue.cpp lib/Mvm/CommonThread/CollectionRV.cpp lib/Mvm/CommonThread/ctthread.cpp lib/Mvm/Compiler/mvm-runtime.ll lib/Mvm/Runtime/VMKit.cpp mmtk/mmtk-j3/ActivePlan.cpp

Gael Thomas gael.thomas at lip6.fr
Sat Dec 4 04:46:27 PST 2010


Author: gthomas
Date: Sat Dec  4 06:46:27 2010
New Revision: 120906

URL: http://llvm.org/viewvc/llvm-project?rev=120906&view=rev
Log:
Add a lock in vmkit (vmkitLock)
The list of virtual machine inside vmkit is an array instead of a vactor. Synchronize with vmkitLock.
Move the list of threads in vmkit. Synchronized with vmkitLock.
Add a list of thread specific data inside each thread.
The finalizer thread uses to vm to finalize the thread. For the moment, the vm specific data is not changed. Will use the array of vm thread specific data.


Modified:
    vmkit/branches/multi-vm/include/debug.h
    vmkit/branches/multi-vm/include/mvm/Threads/CollectionRV.h
    vmkit/branches/multi-vm/include/mvm/Threads/Thread.h
    vmkit/branches/multi-vm/include/mvm/VMKit.h
    vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.cpp
    vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.h
    vmkit/branches/multi-vm/lib/J3/VMCore/ReferenceQueue.cpp
    vmkit/branches/multi-vm/lib/Mvm/CommonThread/CollectionRV.cpp
    vmkit/branches/multi-vm/lib/Mvm/CommonThread/ctthread.cpp
    vmkit/branches/multi-vm/lib/Mvm/Compiler/mvm-runtime.ll
    vmkit/branches/multi-vm/lib/Mvm/Runtime/VMKit.cpp
    vmkit/branches/multi-vm/mmtk/mmtk-j3/ActivePlan.cpp

Modified: vmkit/branches/multi-vm/include/debug.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/include/debug.h?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/include/debug.h (original)
+++ vmkit/branches/multi-vm/include/debug.h Sat Dec  4 06:46:27 2010
@@ -73,7 +73,7 @@
 #define UNREACHABLE() ABORT()
 
 #define ASSERT(cond) {  \
-  if (!cond) ABORT(); } \
+		if (!cond) { fprintf(stderr, "fatal: assert("#cond")\n"); ABORT(); } } \
 
 #undef ALWAYS_INLINE
 #define ALWAYS_INLINE __attribute__ ((always_inline))

Modified: vmkit/branches/multi-vm/include/mvm/Threads/CollectionRV.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/include/mvm/Threads/CollectionRV.h?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/include/mvm/Threads/CollectionRV.h (original)
+++ vmkit/branches/multi-vm/include/mvm/Threads/CollectionRV.h Sat Dec  4 06:46:27 2010
@@ -18,16 +18,6 @@
 
 class CollectionRV {
 public:
-  /// oneThread - One of the threads
-  mvm::Thread* oneThread;
-
-protected: 
-  /// numberOfThreads - The number of threads that currently run under this VM.
-  uint32_t numberOfThreads;
-
-  /// threadLock - Lock to create or destroy a new thread.
-  mvm::SpinLock threadLock;
-
   /// _lockRV - Lock for synchronization.
   LockNormal _lockRV;         
   
@@ -43,8 +33,6 @@
 public: 
   CollectionRV() {
     nbJoined = 0;
-    oneThread = 0;
-    numberOfThreads = 0;
   }
 
   void lockRV() { _lockRV.lock(); }
@@ -72,12 +60,6 @@
   virtual void joinAfterUncooperative(void* SP) = 0;
   virtual void joinBeforeUncooperative() = 0;
 
-  /// addThread - Add a new thread to the list of threads.
-  void addThread(mvm::Thread* th);
-  
-  /// removeThread - Remove the thread from the list of threads.
-  void removeThread(mvm::Thread* th);
-
   /// prepareForJoin - for uncooperative gc, prepare the SIGGC handler
 	virtual void prepareForJoin() = 0;
 };

Modified: vmkit/branches/multi-vm/include/mvm/Threads/Thread.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/include/mvm/Threads/Thread.h?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/include/mvm/Threads/Thread.h (original)
+++ vmkit/branches/multi-vm/include/mvm/Threads/Thread.h Sat Dec  4 06:46:27 2010
@@ -149,6 +149,7 @@
 public:
   /// mut - The associated thread mutator
 	MutatorThread*  mut;
+  /// vm - The associated virtual machine
 	VirtualMachine* vm;
 
 	VMThreadData(MutatorThread* m, VirtualMachine *v) {
@@ -178,6 +179,7 @@
 		_vmkit = vmk;
 		lastKnownFrame = 0;
 		pendingException = 0;
+		allVmsData = 0;
   }
 
   /// yield - Yield the processor to another thread.
@@ -389,6 +391,15 @@
 
 public:
 	mvm::VMKit* vmkit();
+
+  /// allVmsData - the array of thread specific data.
+  ///
+	VMThreadData** allVmsData;
+
+  /// reallocAllVmsData - realloc the allVmsData from old to n or 0 to n if allVmsData=0
+	///   must be protected by rendezvous.threadLock
+  ///
+	void reallocAllVmsData(int old, int n);
 };
 
 #ifndef RUNTIME_DWARF_EXCEPTIONS

Modified: vmkit/branches/multi-vm/include/mvm/VMKit.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/include/mvm/VMKit.h?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/include/mvm/VMKit.h (original)
+++ vmkit/branches/multi-vm/include/mvm/VMKit.h Sat Dec  4 06:46:27 2010
@@ -42,16 +42,17 @@
   /// allocator - Bump pointer allocator to allocate permanent memory of VMKit
   mvm::BumpPtrAllocator& allocator;
 
-  VMKit(mvm::BumpPtrAllocator &Alloc) : allocator(Alloc) {
-	}
+  VMKit(mvm::BumpPtrAllocator &Alloc);
+
+	SpinLock                     vmkitLock;
 
 	/// ------------------------------------------------- ///
 	/// ---             vm managment                  --- ///
 	/// ------------------------------------------------- ///
-	// locksvms - a lock for vms
-	mvm::SpinLock                lockvms;
 	// vms - the list of vms. Could be directly an array and we could also directly use the vmID as index in this array.
-	std::vector<VirtualMachine*> vms;
+	// synchronize with vmkitLock
+	VirtualMachine**             vms;
+	size_t                       numberOfVms;
 
 	size_t addVM(VirtualMachine* vm);
 	void   removeVM(size_t id);
@@ -59,6 +60,13 @@
 	/// ------------------------------------------------- ///
 	/// ---             thread managment              --- ///
 	/// ------------------------------------------------- ///
+  /// oneThread - the linked list of thread. 
+  ///
+	Thread*                      oneThread;
+
+  /// numberOfThreads - The number of threads that currently run under this VM.
+  size_t                       numberOfThreads;
+
   /// rendezvous - The rendezvous implementation for garbage collection.
   ///
 #ifdef WITH_LLVM_GCC
@@ -67,6 +75,9 @@
   UncooperativeCollectionRV rendezvous;
 #endif
 
+	void addThread(mvm::Thread* th);  
+	void removeThread(mvm::Thread* th);
+
 	/// ------------------------------------------------- ///
 	/// ---    backtrace related methods              --- ///
 	/// ------------------------------------------------- ///

Modified: vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.cpp?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.cpp (original)
+++ vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.cpp Sat Dec  4 06:46:27 2010
@@ -1393,6 +1393,17 @@
   return tmp;
 }
 
+void Jnjvm::finalizeObject(mvm::gc* _o) {
+	JavaObject *obj = (JavaObject*)_o;
+
+	llvm_gcroot(_o, 0);
+	llvm_gcroot(obj, 0);
+
+  JavaMethod* meth = upcalls->FinalizeObject;
+  UserClass* cl = JavaObject::getClass(obj)->asClass();
+  meth->invokeIntVirtualBuf(this, cl, obj, 0);
+}
+
 void Jnjvm::startCollection() {
   finalizerThread->FinalizationQueueLock.acquire();
   referenceThread->ToEnqueueLock.acquire();

Modified: vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.h?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.h (original)
+++ vmkit/branches/multi-vm/lib/J3/VMCore/Jnjvm.h Sat Dec  4 06:46:27 2010
@@ -320,6 +320,10 @@
   /// asciizToUTF8 - Constructs an UTF8 out of the asciiz.
   ///
   ArrayUInt16* asciizToArray(const char* asciiz);
+
+  /// finalizeObject - invoke the finalizer of a java object
+  ///
+	void finalizeObject(mvm::gc* obj);
   
   /// setFinalizerThread - Set the finalizer thread of this VM.
   ///

Modified: vmkit/branches/multi-vm/lib/J3/VMCore/ReferenceQueue.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/lib/J3/VMCore/ReferenceQueue.cpp?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/lib/J3/VMCore/ReferenceQueue.cpp (original)
+++ vmkit/branches/multi-vm/lib/J3/VMCore/ReferenceQueue.cpp Sat Dec  4 06:46:27 2010
@@ -239,12 +239,10 @@
 typedef void (*destructor_t)(void*);
 
 void invokeFinalizer(mvm::gc* _obj) {
-  Jnjvm* vm = JavaThread::get()->getJVM();
   JavaObject* obj = (JavaObject*)_obj;
   llvm_gcroot(obj, 0);
-  JavaMethod* meth = vm->upcalls->FinalizeObject;
-  UserClass* cl = JavaObject::getClass(obj)->asClass();
-  meth->invokeIntVirtualBuf(vm, cl, obj, 0);
+  Jnjvm* vm = (Jnjvm*)obj->getVirtualTable()->vm; //JavaThread::get()->getJVM();
+	vm->finalizeObject(obj);
 }
 
 void invokeFinalize(mvm::gc* res) {
@@ -276,6 +274,7 @@
       if (!res) break;
 
 			mvm::VirtualTable* VT = res->getVirtualTable();
+			ASSERT(VT->vm);
       if (VT->operatorDelete) {
         destructor_t dest = (destructor_t)VT->destructor;
         dest(res);

Modified: vmkit/branches/multi-vm/lib/Mvm/CommonThread/CollectionRV.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/lib/Mvm/CommonThread/CollectionRV.cpp?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/lib/Mvm/CommonThread/CollectionRV.cpp (original)
+++ vmkit/branches/multi-vm/lib/Mvm/CommonThread/CollectionRV.cpp Sat Dec  4 06:46:27 2010
@@ -18,30 +18,12 @@
 
 using namespace mvm;
 
-void CollectionRV::addThread(mvm::Thread* th) {
-	threadLock.lock();
-	numberOfThreads++;
-	if (th != oneThread) {
-		if (oneThread) th->append(oneThread);
-		else oneThread = th;
-	}
-	threadLock.unlock();
-}
-  
-void CollectionRV::removeThread(mvm::Thread* th) {
-	threadLock.lock();
-	numberOfThreads--;
-	if (oneThread == th) oneThread = (Thread*)th->next();
-	th->remove();
-	if (!numberOfThreads) oneThread = 0;
-	threadLock.unlock();
-}
-
 void CollectionRV::another_mark() {
+	VMKit *vmkit = mvm::Thread::get()->vmkit();
   assert(th->getLastSP() != NULL);
-  assert(nbJoined < th->MyVM->NumberOfThreads);
+  assert(nbJoined < vmkit->NumberOfThreads);
   nbJoined++;
-  if (nbJoined == numberOfThreads) {
+  if (nbJoined == vmkit->numberOfThreads) {
     condInitiator.broadcast();
   }
 }
@@ -59,7 +41,7 @@
   // Add myself.
   nbJoined++;
 
-  while (nbJoined != numberOfThreads) {
+  while (nbJoined != mvm::Thread::get()->vmkit()->numberOfThreads) {
     condInitiator.wait(&_lockRV);
   } 
 }
@@ -69,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.
- threadLock.lock();
+	self->vmkit()->vmkitLock.lock();
 
   mvm::Thread* cur = self;
   do {
@@ -111,7 +93,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.
-  threadLock.lock();
+  self->vmkit()->vmkitLock.lock();
   
   for (mvm::Thread* cur = (mvm::Thread*)self->next(); cur != self; 
        cur = (mvm::Thread*)cur->next()) {
@@ -223,7 +205,7 @@
 
   assert(nbJoined == initiator->MyVM->NumberOfThreads && "Inconsistent state");
   nbJoined = 0;
-  threadLock.unlock();
+  initiator->vmkit()->vmkitLock.unlock();
   condEndRV.broadcast();
   unlockRV();
   initiator->inRV = false;
@@ -236,9 +218,9 @@
 void UncooperativeCollectionRV::finishRV() {
   lockRV();
   mvm::Thread* initiator = mvm::Thread::get();
-  assert(nbJoined == initiator->MyVM->NumberOfThreads && "Inconsistent state");
+  assert(nbJoined == initiator->vmkit()->NumberOfThreads && "Inconsistent state");
   nbJoined = 0;
-  threadLock.unlock();
+  initiator->vmkit()->vmkitLock.unlock();
   condEndRV.broadcast();
   unlockRV();
   initiator->inRV = false;

Modified: vmkit/branches/multi-vm/lib/Mvm/CommonThread/ctthread.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/lib/Mvm/CommonThread/ctthread.cpp?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/lib/Mvm/CommonThread/ctthread.cpp (original)
+++ vmkit/branches/multi-vm/lib/Mvm/CommonThread/ctthread.cpp Sat Dec  4 06:46:27 2010
@@ -31,6 +31,18 @@
 
 using namespace mvm;
 
+// must be protected by rendezvous.threadLock
+void Thread::reallocAllVmsData(int old, int n) {
+	VMThreadData **newData = new VMThreadData*[n];
+	if(old) {
+		memcpy(newData, allVmsData, old*sizeof(VMThreadData*));
+		VMThreadData **oldData = allVmsData;
+		allVmsData = newData;
+		delete oldData;
+	} else
+		allVmsData = newData;
+}
+
 mvm::VMKit* Thread::vmkit() {
 	return vmData->vm->vmkit; 
 }
@@ -435,7 +447,7 @@
 #endif
   th->vmkit()->rendezvous.prepareForJoin();
   th->routine(th);
-  th->vmkit()->rendezvous.removeThread(th);
+  th->vmkit()->removeThread(th);
 }
 
 
@@ -450,7 +462,7 @@
   routine = fct;
   // Make sure to add it in the list of threads before leaving this function:
   // the garbage collector wants to trace this thread.
-  vmkit()->rendezvous.addThread(this);
+  vmkit()->addThread(this);
   int res = pthread_create((pthread_t*)(void*)(&internalThreadID), &attributs,
                            (void* (*)(void *))internalThreadStart, this);
   pthread_detach((pthread_t)internalThreadID);

Modified: vmkit/branches/multi-vm/lib/Mvm/Compiler/mvm-runtime.ll
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/lib/Mvm/Compiler/mvm-runtime.ll?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/lib/Mvm/Compiler/mvm-runtime.ll (original)
+++ vmkit/branches/multi-vm/lib/Mvm/Compiler/mvm-runtime.ll Sat Dec  4 06:46:27 2010
@@ -22,7 +22,8 @@
 ;;; field 12: void*  vmData
 ;;; field 13: gc*    pendingException
 ;;; field 14: VMkit* vmkit
-%Thread       = type { %CircularBase, i32, i8*, i8*, i1, i1, i1, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8* }
+;;; field 15: void*  allVMDatas
+%Thread       = type { %CircularBase, i32, i8*, i8*, i1, i1, i1, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8* }
 
 ;;; field 0: VT
 ;;; field 1: mvm::MutatorThread*  mut

Modified: vmkit/branches/multi-vm/lib/Mvm/Runtime/VMKit.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/lib/Mvm/Runtime/VMKit.cpp?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/lib/Mvm/Runtime/VMKit.cpp (original)
+++ vmkit/branches/multi-vm/lib/Mvm/Runtime/VMKit.cpp Sat Dec  4 06:46:27 2010
@@ -3,19 +3,66 @@
 
 using namespace mvm;
 
+VMKit::VMKit(mvm::BumpPtrAllocator &Alloc) : allocator(Alloc) {
+	vms         = 0;
+	numberOfVms = 0;
+}
+
 size_t VMKit::addVM(VirtualMachine* vm) {
-	lockvms.lock();
-	for(size_t i=0; i<vms.size(); i++)
+	vmkitLock.lock();
+
+	for(size_t i=0; i<numberOfVms; i++)
 		if(!vms[i]) {
 			vms[i] = vm;
+			vmkitLock.unlock();
 			return i;
 		}
-	int res = vms.size();
-	vms.push_back(vm);
-	lockvms.unlock();
+
+	int res = numberOfVms;
+	numberOfVms = numberOfVms ? (numberOfVms<<1) : 1;
+	// reallocate the vms
+	VirtualMachine **newVms = new VirtualMachine*[numberOfVms];
+	memcpy(newVms, vms, res*sizeof(VirtualMachine*));
+	VirtualMachine **oldVms = vms;
+	vms = newVms; // vms must always contain valid data
+	delete[] oldVms;
+	
+ 	// reallocate the allVMDatas
+ 	Thread* cur=oneThread;
+	if(cur) {
+		do {
+			cur->reallocAllVmsData(res, numberOfVms);
+			cur = (Thread*)cur->next();
+		} while(cur!=oneThread);
+	}
+
+	vmkitLock.unlock();
+
 	return res;
 }
 
 void VMKit::removeVM(size_t id) {
 	vms[id] = 0;
 }
+
+void VMKit::addThread(mvm::Thread* th) {
+	vmkitLock.lock();
+	numberOfThreads++;
+	if (th != oneThread) {
+		if (oneThread) th->append(oneThread);
+		else oneThread = th;
+	}
+	th->reallocAllVmsData(0, numberOfVms);
+	vmkitLock.unlock();
+}
+  
+void VMKit::removeThread(mvm::Thread* th) {
+	vmkitLock.lock();
+	numberOfThreads--;
+	if (oneThread == th) oneThread = (Thread*)th->next();
+	th->remove();
+	if (!numberOfThreads) oneThread = 0;
+	delete th->allVmsData;
+	th->allVmsData = 0;
+	vmkitLock.unlock();
+}

Modified: vmkit/branches/multi-vm/mmtk/mmtk-j3/ActivePlan.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/multi-vm/mmtk/mmtk-j3/ActivePlan.cpp?rev=120906&r1=120905&r2=120906&view=diff
==============================================================================
--- vmkit/branches/multi-vm/mmtk/mmtk-j3/ActivePlan.cpp (original)
+++ vmkit/branches/multi-vm/mmtk/mmtk-j3/ActivePlan.cpp Sat Dec  4 06:46:27 2010
@@ -19,8 +19,8 @@
   assert(A && "No active plan");
   
   if (A->current == NULL) {
-    A->current = (mvm::MutatorThread*)mvm::Thread::get()->vmkit()->rendezvous.oneThread;
-  } else if (A->current->next() == mvm::Thread::get()->vmkit()->rendezvous.oneThread) {
+    A->current = (mvm::MutatorThread*)mvm::Thread::get()->vmkit()->oneThread;
+  } else if (A->current->next() == mvm::Thread::get()->vmkit()->oneThread) {
     A->current = NULL;
     return NULL;
   } else {





More information about the vmkit-commits mailing list