[vmkit-commits] [vmkit] r73144 - in /vmkit/trunk/lib/JnJVM/VMCore: JavaObject.cpp JavaObject.h JavaThread.h

Nicolas Geoffray nicolas.geoffray at lip6.fr
Tue Jun 9 14:02:17 PDT 2009


Author: geoffray
Date: Tue Jun  9 16:02:16 2009
New Revision: 73144

URL: http://llvm.org/viewvc/llvm-project?rev=73144&view=rev
Log:
Inline the list of threads doing a wait in the JavaThread structure.


Modified:
    vmkit/trunk/lib/JnJVM/VMCore/JavaObject.cpp
    vmkit/trunk/lib/JnJVM/VMCore/JavaObject.h
    vmkit/trunk/lib/JnJVM/VMCore/JavaThread.h

Modified: vmkit/trunk/lib/JnJVM/VMCore/JavaObject.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JavaObject.cpp?rev=73144&r1=73143&r2=73144&view=diff

==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JavaObject.cpp (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JavaObject.cpp Tue Jun  9 16:02:16 2009
@@ -7,8 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <vector>
-
 #include "mvm/Threads/Locks.h"
 
 #include "JavaClass.h"
@@ -20,52 +18,6 @@
 
 using namespace jnjvm;
 
-void JavaCond::notify() {
-  for (std::vector<JavaThread*>::iterator i = threads.begin(), 
-            e = threads.end(); i!= e;) {
-    JavaThread* cur = *i;
-    cur->lock.lock();
-    if (cur->interruptFlag != 0) {
-      cur->lock.unlock();
-      ++i;
-      continue;
-    } else if (cur->javaThread != 0) {
-      cur->varcond.signal();
-      cur->lock.unlock();
-      threads.erase(i);
-      break;
-    } else { // dead thread
-      ++i;
-      threads.erase(i - 1);
-    }
-  }
-}
-
-void JavaCond::notifyAll() {
-  for (std::vector<JavaThread*>::iterator i = threads.begin(),
-            e = threads.end(); i!= e; ++i) {
-    JavaThread* cur = *i;
-    cur->lock.lock();
-    cur->varcond.signal();
-    cur->lock.unlock();
-  }
-  threads.clear();
-}
-
-void JavaCond::wait(JavaThread* th) {
-  threads.push_back(th);
-}
-
-void JavaCond::remove(JavaThread* th) {
-  for (std::vector<JavaThread*>::iterator i = threads.begin(),
-            e = threads.end(); i!= e; ++i) {
-    if (*i == th) {
-      threads.erase(i);
-      break;
-    }
-  }
-}
-
 LockObj* LockObj::allocate(JavaObject* owner) {
 #ifdef USE_GC_BOEHM
   LockObj* res = new LockObj();
@@ -94,9 +46,22 @@
       thread->interruptFlag = 0;
       thread->getJVM()->interruptedException(this);
     } else {
-      JavaCond* cond = l->getCond();
-      cond->wait(thread);
       thread->state = JavaThread::StateWaiting;
+      if (l->firstThread) {
+        l->firstThread->prevWaiting->nextWaiting = thread;
+        thread->prevWaiting = l->firstThread->prevWaiting;
+        thread->nextWaiting = l->firstThread;
+        l->firstThread->prevWaiting = thread;
+        if (l->firstThread->nextWaiting == l->firstThread) 
+          l->firstThread->nextWaiting = thread;
+      } else {
+        l->firstThread = thread;
+        thread->nextWaiting = thread;
+        thread->prevWaiting = thread;
+      }
+      assert(thread->prevWaiting && thread->nextWaiting && "Inconsistent list");
+      assert(l->firstThread->prevWaiting && l->firstThread->nextWaiting &&
+             "Inconsistent list");
       
       bool timeout = false;
       uint32 recur = l->lock.unlockAll();
@@ -112,7 +77,24 @@
       l->lock.lockAll(recur);
 
       if (interrupted || timeout) {
-        cond->remove(thread);
+        if (thread->nextWaiting) {
+          if (l->firstThread != thread) {
+            thread->nextWaiting->prevWaiting = thread->prevWaiting;
+            thread->prevWaiting->nextWaiting = thread->nextWaiting;
+            assert(l->firstThread->prevWaiting && 
+                   l->firstThread->nextWaiting && "Inconsistent list");
+          } else if (thread->nextWaiting == thread) {
+            l->firstThread = 0;
+          } else {
+            l->firstThread = thread->nextWaiting;
+            l->firstThread->prevWaiting = thread->prevWaiting;
+            thread->prevWaiting->nextWaiting = l->firstThread;
+            assert(l->firstThread->prevWaiting && 
+                   l->firstThread->nextWaiting && "Inconsistent list");
+          }
+          thread->nextWaiting = 0;
+          thread->prevWaiting = 0;
+        }
       }
 
       thread->state = JavaThread::StateRunning;
@@ -139,7 +121,42 @@
 void JavaObject::notify() {
   if (owner()) {
     LockObj * l = lock.getFatLock();
-    if (l) l->getCond()->notify();
+    if (l) {
+      JavaThread* cur = l->firstThread;
+      if (cur) {
+        do {
+          cur->lock.lock();
+          if (cur->interruptFlag != 0) {
+            cur->lock.unlock();
+            cur = cur->nextWaiting;
+          } else if (cur->javaThread != 0) {
+            assert(cur->prevWaiting && cur->nextWaiting &&
+                   "Inconsistent list");
+            if (cur != l->firstThread) {
+              cur->prevWaiting->nextWaiting = cur->nextWaiting;
+              cur->nextWaiting->prevWaiting = cur->prevWaiting;
+              assert(l->firstThread->prevWaiting &&
+                     l->firstThread->nextWaiting && "Inconsistent list");
+            } else if (cur->nextWaiting == cur) {
+              l->firstThread = 0;
+            } else {
+              l->firstThread = cur->nextWaiting;
+              l->firstThread->prevWaiting = cur->prevWaiting;
+              cur->prevWaiting->nextWaiting = l->firstThread;
+              assert(l->firstThread->prevWaiting && 
+                     l->firstThread->nextWaiting && "Inconsistent list");
+            }
+            cur->prevWaiting = 0;
+            cur->nextWaiting = 0;
+            cur->varcond.signal();
+            cur->lock.unlock();
+            break;
+          } else {
+            cur->lock.unlock();
+          }
+        } while (cur != l->firstThread);
+      }
+    }
   } else {
     JavaThread::get()->getJVM()->illegalMonitorStateException(this);
   }
@@ -149,7 +166,21 @@
 void JavaObject::notifyAll() {
   if (owner()) {
     LockObj * l = lock.getFatLock();
-    if (l) l->getCond()->notifyAll();
+    if (l) {
+      JavaThread* cur = l->firstThread;
+      if (cur) {
+        do {
+          cur->lock.lock();
+          JavaThread* temp = cur->nextWaiting;
+          cur->prevWaiting = 0;
+          cur->nextWaiting = 0;
+          cur->varcond.signal();
+          cur->lock.unlock();
+          cur = temp;
+        } while (cur != l->firstThread);
+        l->firstThread = 0;
+      }
+    }
   } else {
     JavaThread::get()->getJVM()->illegalMonitorStateException(this);
   }

Modified: vmkit/trunk/lib/JnJVM/VMCore/JavaObject.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JavaObject.h?rev=73144&r1=73143&r2=73144&view=diff

==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JavaObject.h (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JavaObject.h Tue Jun  9 16:02:16 2009
@@ -10,8 +10,6 @@
 #ifndef JNJVM_JAVA_OBJECT_H
 #define JNJVM_JAVA_OBJECT_H
 
-#include <vector>
-
 #include "mvm/Object.h"
 #include "mvm/Threads/Locks.h"
 #include "mvm/Threads/Thread.h"
@@ -28,36 +26,6 @@
 class Typedef;
 class UserCommonClass;
 
-/// JavaCond - This class maintains a list of threads blocked on a wait. 
-/// notify and notifyAll will change the state of one or more of these threads.
-///
-class JavaCond {
-private:
-  
-  /// threads - The list of threads currently waiting.
-  ///
-  std::vector<JavaThread*> threads;
-
-public:
-  
-  /// notify - The Java notify: takes the first thread in the list and wakes
-  /// it up.
-  void notify();
-  
-  /// notifyAll - The Java notifyAll: wakes all threads in the list.
-  ///
-  void notifyAll();
-
-  /// wait - The Java wait: the thread blocks waiting to be notified.
-  ///
-  void wait(JavaThread* th);
-
-  /// remove - Remove the thread from the list. This is called by threads being
-  /// interrupted or timed out on a wait.
-  void remove(JavaThread* th);
-};
-
-
 /// LockObj - This class represents a Java monitor.
 ///
 class LockObj : public gc {
@@ -69,9 +37,9 @@
   ///
   mvm::LockRecursive lock;
 
-  /// varcond - The condition variable for wait, notify and notifyAll calls.
+  /// firstThread - The first thread doing a wait.
   ///
-  JavaCond varcond;
+  JavaThread* firstThread;
 
 public:
   /// allocate - Allocates a lock object. Only the internal lock is allocated.
@@ -107,13 +75,6 @@
     return lock.getOwner();
   }
   
-  /// getCond - Returns the conditation variable of this lock, allocating it
-  /// if non-existant.
-  ///
-  JavaCond* getCond() {
-    return &varcond;
-  }
-
   /// VT - LockObj is GC-allocated, so we must make the VT explicit.
   ///
   static VirtualTable VT;
@@ -122,11 +83,10 @@
   ///
   static void staticDestructor(LockObj* obj) {
     obj->lock.~LockRecursive();
-    obj->varcond.~JavaCond();
   }
 
   /// LockObj - Empty constructor.
-  LockObj() {}
+  LockObj() { firstThread = 0; }
 };
 
 /// JavaVirtualTable - This class is the virtual table of instances of

Modified: vmkit/trunk/lib/JnJVM/VMCore/JavaThread.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/JnJVM/VMCore/JavaThread.h?rev=73144&r1=73143&r2=73144&view=diff

==============================================================================
--- vmkit/trunk/lib/JnJVM/VMCore/JavaThread.h (original)
+++ vmkit/trunk/lib/JnJVM/VMCore/JavaThread.h Tue Jun  9 16:02:16 2009
@@ -88,7 +88,15 @@
   /// interruptFlag - Has this thread been interrupted?
   ///
   uint32 interruptFlag;
+
+  /// nextWaiting - Next thread waiting on the same monitor.
+  ///
+  JavaThread* nextWaiting;
   
+  /// prevWaiting - Previous thread waiting on the same monitor.
+  ///
+  JavaThread* prevWaiting;
+
   static const unsigned int StateRunning;
   static const unsigned int StateWaiting;
   static const unsigned int StateInterrupted;





More information about the vmkit-commits mailing list