[vmkit-commits] [vmkit] r111775 - in /vmkit/trunk: include/mvm/GC/GC.h include/mvm/Threads/Locks.h include/mvm/VirtualMachine.h lib/J3/VMCore/JavaLocks.cpp lib/J3/VMCore/JavaLocks.h lib/J3/VMCore/JavaObject.cpp lib/J3/VMCore/JavaObject.h lib/J3/VMCore/Jnjvm.h lib/Mvm/CommonThread/ctlock.cpp mmtk/mmtk-j3/Memory.cpp mmtk/mmtk-j3/ObjectModel.cpp mmtk/mmtk-j3/VM.cpp

Nicolas Geoffray nicolas.geoffray at lip6.fr
Sun Aug 22 03:03:36 PDT 2010


Author: geoffray
Date: Sun Aug 22 05:03:36 2010
New Revision: 111775

URL: http://llvm.org/viewvc/llvm-project?rev=111775&view=rev
Log:
Put the thin lock implementation in a cpp file instead of a .h. Also remove the templated version. Now a thin lock can only be associated with a gc object.


Modified:
    vmkit/trunk/include/mvm/GC/GC.h
    vmkit/trunk/include/mvm/Threads/Locks.h
    vmkit/trunk/include/mvm/VirtualMachine.h
    vmkit/trunk/lib/J3/VMCore/JavaLocks.cpp
    vmkit/trunk/lib/J3/VMCore/JavaLocks.h
    vmkit/trunk/lib/J3/VMCore/JavaObject.cpp
    vmkit/trunk/lib/J3/VMCore/JavaObject.h
    vmkit/trunk/lib/J3/VMCore/Jnjvm.h
    vmkit/trunk/lib/Mvm/CommonThread/ctlock.cpp
    vmkit/trunk/mmtk/mmtk-j3/Memory.cpp
    vmkit/trunk/mmtk/mmtk-j3/ObjectModel.cpp
    vmkit/trunk/mmtk/mmtk-j3/VM.cpp

Modified: vmkit/trunk/include/mvm/GC/GC.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/GC/GC.h?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/include/mvm/GC/GC.h (original)
+++ vmkit/trunk/include/mvm/GC/GC.h Sun Aug 22 05:03:36 2010
@@ -19,6 +19,7 @@
 public:
   virtual           ~gcRoot() {}
   virtual void      tracer(uintptr_t closure) {}
+  uintptr_t header;
   
   /// getVirtualTable - Returns the virtual table of this object.
   ///

Modified: vmkit/trunk/include/mvm/Threads/Locks.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/Threads/Locks.h?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/include/mvm/Threads/Locks.h (original)
+++ vmkit/trunk/include/mvm/Threads/Locks.h Sun Aug 22 05:03:36 2010
@@ -24,6 +24,8 @@
 #define llvm_gcroot(a, b)
 #endif
 
+class gc;
+
 namespace mvm {
 
 extern "C" uint8  llvm_atomic_cmp_swap_i8(uint8* ptr,  uint8 cmp,
@@ -71,6 +73,7 @@
 #endif
 
 class Cond;
+class FatLock;
 class LockNormal;
 class LockRecursive;
 class Thread;
@@ -185,195 +188,35 @@
   void lockAll(int count);
 };
 
-class FatLockNoGC {
-public:
-  static void gcroot(void* val, void* unused) 
-    __attribute__ ((always_inline)) {}
-
-  static uintptr_t mask() {
-    return 0;
-  }
-};
-  
-class FatLockWithGC {
-public:
-  static void gcroot(void* val, void* unused) 
-    __attribute__ ((always_inline)) {
-    llvm_gcroot(val, unused);
-  }
-    
-  static uintptr_t mask() {
-    return NonLockBitsMask;
-  }
-};
-
-/// ThinLock - This class is an implementation of thin locks. The template class
-/// TFatLock is a virtual machine specific fat lock.
-///
-template <class TFatLock, class Owner, class IsGC>
 class ThinLock {
 public:
-  uintptr_t lock;
 
-  /// overflowThinlock - Change the lock of this object to a fat lock because
-  /// we have reached 0xFF locks.
-  void overflowThinLock(Owner* O) {
-    IsGC::gcroot(O, 0);
-    TFatLock* obj = TFatLock::allocate(O);
-    obj->acquireAll((ThinCountMask >> ThinCountShift) + 1);
-    uintptr_t oldLock = lock;
-    lock = obj->getID() | (oldLock & IsGC::mask());
-  }
- 
   /// initialise - Initialise the value of the lock.
   ///
-  void initialise() {
-    lock = lock & IsGC::mask();
-  }
-  
-  /// ThinLock - Calls initialize.
-  ThinLock() {
-    initialise();
-  }
+  static void initialise(gc* object);
+
+  /// overflowThinlock - Change the lock of this object to a fat lock because
+  /// we have reached 0xFF locks.
+  static void overflowThinLock(gc* object);
  
   /// changeToFatlock - Change the lock of this object to a fat lock. The lock
   /// may be in a thin lock or fat lock state.
-  TFatLock* changeToFatlock(Owner* O) {
-    IsGC::gcroot(O, 0);
-    if (!(lock & FatMask)) {
-      TFatLock* obj = TFatLock::allocate(O);
-      uint32 count = (lock & ThinCountMask) >> ThinCountShift;
-      obj->acquireAll(count + 1);
-      uintptr_t oldLock = lock;
-      lock = obj->getID() | (oldLock & IsGC::mask());
-      return obj;
-    } else {
-      TFatLock* res = TFatLock::getFromID(lock);
-      assert(res && "Lock deallocated while held.");
-      return res;
-    }
-  }
+  static FatLock* changeToFatlock(gc* object);
 
   /// acquire - Acquire the lock.
-  void acquire(Owner* O) {
-    IsGC::gcroot(O, 0);
-start:
-    uint64_t id = mvm::Thread::get()->getThreadID();
-    uintptr_t oldValue = lock;
-    uintptr_t newValue = id | (lock & IsGC::mask());
-    uintptr_t val = __sync_val_compare_and_swap(&lock, oldValue & IsGC::mask(),
-                                                newValue);
-
-    if (val != (oldValue & IsGC::mask())) {
-      //fat!
-      if (!(val & FatMask)) {
-        if ((val & Thread::IDMask) == id) {
-          if ((val & ThinCountMask) != ThinCountMask) {
-            lock += ThinCountAdd;
-          } else {
-            overflowThinLock(O);
-          }
-        } else {
-          TFatLock* obj = TFatLock::allocate(O);
-          uintptr_t val = obj->getID();
-loop:
-          while (lock & ~IsGC::mask()) {
-            if (lock & FatMask) {
-              obj->deallocate();
-              goto end;
-            }
-            else mvm::Thread::yield();
-          }
-        
-          oldValue = lock;
-          newValue = val | (lock & IsGC::mask());
-          uintptr_t test = __sync_val_compare_and_swap(&lock,
-                                                       oldValue & IsGC::mask(),
-                                                       newValue);
-          if (test != (oldValue & IsGC::mask())) goto loop;
-          if (!obj->acquire(O)) goto start;
-        }
-      } else {
-
-end:
-        TFatLock* obj = TFatLock::getFromID(lock);
-        if (obj) {
-          if (!obj->acquire(O)) goto start;
-        } else {
-          goto start;
-        }
-      }
-    }
-
-    assert(owner() && "Not owner after quitting acquire!");
-  }
+  static void acquire(gc* object);
 
   /// release - Release the lock.
-  void release(Owner* O) {
-    IsGC::gcroot(O, 0);
-    assert(owner() && "Not owner when entering release!");
-    uint64 id = mvm::Thread::get()->getThreadID();
-    if ((lock & ~IsGC::mask()) == id) {
-      lock = lock & IsGC::mask();
-    } else if (lock & FatMask) {
-      TFatLock* obj = TFatLock::getFromID(lock);
-      assert(obj && "Lock deallocated while held.");
-      obj->release(O);
-    } else {
-      lock -= ThinCountAdd;
-    }
-  }
+  static void release(gc* object);
 
-  /// broadcast - Wakes up all threads waiting for this lock.
-  ///
-  void broadcast() {
-    if (lock & FatMask) {
-      TFatLock* obj = TFatLock::getFromID(lock);
-      assert(obj && "Lock deallocated while held.");
-      obj->broadcast();
-    }
-  }
-  
-  /// signal - Wakes up one thread waiting for this lock.
-  void signal() {
-    if (lock & FatMask) {
-      TFatLock* obj = TFatLock::getFromID(lock);
-      assert(obj && "Lock deallocated while held.");
-      obj->signal();
-    }
-  }
-  
   /// owner - Returns true if the curren thread is the owner of this object's
   /// lock.
-  bool owner() {
-    if (lock & FatMask) {
-      TFatLock* obj = TFatLock::getFromID(lock);
-      if (obj) return obj->owner();
-    } else {
-      uint64 id = mvm::Thread::get()->getThreadID();
-      if ((lock & Thread::IDMask) == id) return true;
-    }
-    return false;
-  }
+  static bool owner(gc* object);
 
-  mvm::Thread* getOwner() {
-    if (lock & FatMask) {
-      TFatLock* obj = TFatLock::getFromID(lock);
-      if (obj) return obj->getOwner();
-      return 0;
-    } else {
-      return (mvm::Thread*)(lock & mvm::Thread::IDMask);
-    }
-  }
+  static mvm::Thread* getOwner(gc* object);
 
   /// getFatLock - Get the fat lock is the lock is a fat lock, 0 otherwise.
-  TFatLock* getFatLock() {
-    if (lock & FatMask) {
-      return TFatLock::getFromID(lock);
-    } else {
-      return 0;
-    }
-  }
+  static FatLock* getFatLock(gc* object);
 };
 
 

Modified: vmkit/trunk/include/mvm/VirtualMachine.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/include/mvm/VirtualMachine.h?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/include/mvm/VirtualMachine.h (original)
+++ vmkit/trunk/include/mvm/VirtualMachine.h Sun Aug 22 05:03:36 2010
@@ -185,6 +185,16 @@
   void scan(VirtualMachine* vm, uintptr_t closure);
 };
 
+class FatLock : public mvm::PermanentObject {
+public:
+  virtual void deallocate() = 0;
+  virtual uintptr_t getID() = 0;
+  virtual bool acquire(gc* object) = 0;
+  virtual void acquireAll(gc* object, uint32_t count) = 0;
+  virtual void release(gc* object) = 0;
+  virtual mvm::Thread* getOwner() = 0;
+  virtual bool owner() = 0;
+};
 
 /// VirtualMachine - This class is the root of virtual machine classes. It
 /// defines what a VM should be.
@@ -291,6 +301,9 @@
   /// waitForExit - Wait until the virtual machine stops its execution.
   virtual void waitForExit() = 0;
 
+  virtual FatLock* allocateFatLock(gc* object) = 0;
+  virtual FatLock* getFatLockFromID(uintptr_t header) = 0;
+
   static j3::JnjvmClassLoader* initialiseJVM(j3::JavaCompiler* C,
                                                 bool dlLoad = true);
   static VirtualMachine* createJVM(j3::JnjvmClassLoader* C = 0);

Modified: vmkit/trunk/lib/J3/VMCore/JavaLocks.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/VMCore/JavaLocks.cpp?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/VMCore/JavaLocks.cpp (original)
+++ vmkit/trunk/lib/J3/VMCore/JavaLocks.cpp Sun Aug 22 05:03:36 2010
@@ -13,10 +13,9 @@
 
 using namespace j3;
 
-JavaLock* JavaLock::allocate(JavaObject* obj) {
+JavaLock* Jnjvm::allocateFatLock(gc* obj) {
   llvm_gcroot(obj, 0);
-  Jnjvm* vm = JavaThread::get()->getJVM();
-  JavaLock* res = vm->lockSystem.allocate(obj);  
+  JavaLock* res = lockSystem.allocate((JavaObject*)obj);  
   return res;
 }
 
@@ -89,24 +88,23 @@
   return (index << mvm::NonLockBits) | mvm::FatMask;
 }
 
-JavaLock* JavaLock::getFromID(uintptr_t ID) {
-  Jnjvm* vm = JavaThread::get()->getJVM();
+JavaLock* Jnjvm::getFatLockFromID(uintptr_t ID) {
   if (ID & mvm::FatMask) {
     uint32_t index = (ID & ~mvm::FatMask) >> mvm::NonLockBits;
-    JavaLock* res = vm->lockSystem.getLock(index);
+    JavaLock* res = lockSystem.getLock(index);
     return res;
   } else {
-    return 0;
+    return NULL;
   }
 }
 
-void JavaLock::release(JavaObject* obj) {
+void JavaLock::release(gc* obj) {
   llvm_gcroot(obj, 0);
   assert(associatedObject && "No associated object when releasing");
   assert(associatedObject == obj && "Mismatch object in lock");
   if (!waitingThreads && !lockingThreads &&
       internalLock.recursionCount() == 1) {
-    associatedObject->lock.initialise();
+    mvm::ThinLock::initialise(associatedObject);
     deallocate();
   }
   internalLock.unlock();
@@ -114,7 +112,7 @@
 
 /// acquire - Acquires the internalLock.
 ///
-bool JavaLock::acquire(JavaObject* obj) {
+bool JavaLock::acquire(gc* obj) {
   llvm_gcroot(obj, 0);
     
   spinLock.lock();
@@ -131,5 +129,5 @@
     internalLock.unlock();
     return false;
   }
-    return true;
-  }
+  return true;
+}

Modified: vmkit/trunk/lib/J3/VMCore/JavaLocks.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/VMCore/JavaLocks.h?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/VMCore/JavaLocks.h (original)
+++ vmkit/trunk/lib/J3/VMCore/JavaLocks.h Sun Aug 22 05:03:36 2010
@@ -12,6 +12,7 @@
 
 #include "mvm/Allocator.h"
 #include "mvm/Threads/Locks.h"
+#include "mvm/VirtualMachine.h"
 
 namespace mvm {
   class Thread;
@@ -23,7 +24,7 @@
 class JavaThread;
 class Jnjvm;
 
-class JavaLock : public mvm::PermanentObject {
+class JavaLock : public mvm::FatLock {
 
 friend class JavaObject;
 friend class LockSystem;
@@ -50,7 +51,7 @@
 
   /// acquire - Acquires the internalLock.
   ///
-  bool acquire(JavaObject* obj);
+  bool acquire(gc* obj);
  
   /// tryAcquire - Tries to acquire the lock.
   ///
@@ -60,13 +61,13 @@
 
  
   /// acquireAll - Acquires the lock nb times.
-  void acquireAll(uint32 nb) {
+  void acquireAll(gc* obj, uint32 nb) {
     internalLock.lockAll(nb);
   }
 
   /// release - Releases the internalLock.
   ///
-  void release(JavaObject* obj);
+  void release(gc* obj);
   
   /// owner - Returns if the current thread owns this internalLock.
   ///
@@ -91,11 +92,9 @@
     nextFreeLock = NULL;
   }
 
-  static JavaLock* allocate(JavaObject*);
   void deallocate();
   
   uintptr_t getID();
-  static JavaLock* getFromID(uintptr_t val);
 };
 
 /// LockSystem - This class manages all Java locks used by the applications.

Modified: vmkit/trunk/lib/J3/VMCore/JavaObject.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/VMCore/JavaObject.cpp?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/VMCore/JavaObject.cpp (original)
+++ vmkit/trunk/lib/J3/VMCore/JavaObject.cpp Sun Aug 22 05:03:36 2010
@@ -28,7 +28,7 @@
   llvm_gcroot(self, 0);
   if (!mvm::MovesObject) return (uint32_t)(long)self;
 
-  uintptr_t header = self->lock.lock;
+  uintptr_t header = self->header;
   uintptr_t GCBits = header & mvm::GCBitMask;
   uintptr_t val = header & mvm::HashMask;
   if (val != 0) {
@@ -48,16 +48,16 @@
   assert(val != hashCodeGenerator);
 
   do {
-    header = self->lock.lock;
+    header = self->header;
     if ((header & mvm::HashMask) != 0) break;
     uintptr_t newHeader = header | val;
     assert((newHeader & ~mvm::HashMask) == header);
-    __sync_val_compare_and_swap(&(self->lock.lock), header, newHeader);
+    __sync_val_compare_and_swap(&(self->header), header, newHeader);
   } while (true);
 
-  assert((self->lock.lock & mvm::HashMask) != 0);
-  assert(GCBits == (self->lock.lock & mvm::GCBitMask));
-  return (self->lock.lock & mvm::HashMask) ^ (uintptr_t)getClass(self);
+  assert((self->header & mvm::HashMask) != 0);
+  assert(GCBits == (self->header & mvm::GCBitMask));
+  return (self->header & mvm::HashMask) ^ (uintptr_t)getClass(self);
 }
 
 
@@ -67,7 +67,7 @@
   JavaLock* l = 0;
 
   if (owner(self)) {
-    l = self->lock.changeToFatlock(self);
+    l = (JavaLock*)mvm::ThinLock::changeToFatlock(self);
     JavaThread* thread = JavaThread::get();
     thread->waitsOn = l;
     mvm::Cond& varcondThread = thread->varcond;
@@ -178,7 +178,7 @@
   JavaLock* l = 0;
 
   if (owner(self)) {
-    l = self->lock.getFatLock();
+    l = (JavaLock*)mvm::ThinLock::getFatLock(self);
     if (l) {
       JavaThread* cur = l->firstThread;
       if (cur) {
@@ -222,7 +222,7 @@
   JavaLock* l = 0;
   
   if (owner(self)) {
-    l = self->lock.getFatLock();
+    l = (JavaLock*)mvm::ThinLock::getFatLock(self);
     if (l) {
       JavaThread* cur = l->firstThread;
       if (cur) {

Modified: vmkit/trunk/lib/J3/VMCore/JavaObject.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/VMCore/JavaObject.h?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/VMCore/JavaObject.h (original)
+++ vmkit/trunk/lib/J3/VMCore/JavaObject.h Sun Aug 22 05:03:36 2010
@@ -245,10 +245,6 @@
     return ((JavaVirtualTable*)self->getVirtualTable())->cl;
   }
 
-  /// lock - The monitor of this object. Most of the time null.
-  ///
-  mvm::ThinLock<JavaLock, JavaObject, mvm::FatLockWithGC> lock;
-
   /// wait - Java wait. Makes the current thread waiting on a monitor.
   ///
   static void wait(JavaObject* self);
@@ -268,11 +264,11 @@
   ///
   static void notifyAll(JavaObject* self);
  
-  /// overflowThinLock - Notify that the thin lock has overflowed.
+  /// overflowmvm::ThinLock - Notify that the thin lock has overflowed.
   ///
   static void overflowThinLock(JavaObject* self) {
     llvm_gcroot(self, 0);
-    self->lock.overflowThinLock(self);
+    mvm::ThinLock::overflowThinLock(self);
   }
 
   /// instanceOf - Is this object's class of type the given class?
@@ -282,20 +278,20 @@
   /// acquire - Acquire the lock on this object.
   static void acquire(JavaObject* self) {
     llvm_gcroot(self, 0);
-    self->lock.acquire(self);
+    mvm::ThinLock::acquire(self);
   }
 
   /// release - Release the lock on this object
   static void release(JavaObject* self) {
     llvm_gcroot(self, 0);
-    self->lock.release(self);
+    mvm::ThinLock::release(self);
   }
 
   /// owner - Returns true if the current thread is the owner of this object's
   /// lock.
   static bool owner(JavaObject* self) {
     llvm_gcroot(self, 0);
-    return self->lock.owner();
+    return mvm::ThinLock::owner(self);
   }
 
 #ifdef SIGSEGV_THROW_NULL
@@ -308,7 +304,7 @@
   /// lockObj - Get the LockObj if the lock is a fat lock.
   static JavaLock* lockObj(JavaObject* self) {
     llvm_gcroot(self, 0);
-    return self->lock.getFatLock();
+    return (JavaLock*)mvm::ThinLock::getFatLock(self);
   }
 
   /// decapsulePrimitive - Based on the signature argument, decapsule

Modified: vmkit/trunk/lib/J3/VMCore/Jnjvm.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/J3/VMCore/Jnjvm.h?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/lib/J3/VMCore/Jnjvm.h (original)
+++ vmkit/trunk/lib/J3/VMCore/Jnjvm.h Sun Aug 22 05:03:36 2010
@@ -341,6 +341,9 @@
   ///
   virtual void waitForExit();
 
+  virtual JavaLock* allocateFatLock(gc*);
+  virtual JavaLock* getFatLockFromID(uintptr_t val);
+
 private:
   /// internalRemoveMethodsInFunctionMap - Removes all methods compiled by this
   /// class loader from the function map.

Modified: vmkit/trunk/lib/Mvm/CommonThread/ctlock.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/lib/Mvm/CommonThread/ctlock.cpp?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/lib/Mvm/CommonThread/ctlock.cpp (original)
+++ vmkit/trunk/lib/Mvm/CommonThread/ctlock.cpp Sun Aug 22 05:03:36 2010
@@ -12,6 +12,8 @@
 #include "mvm/Threads/Cond.h"
 #include "mvm/Threads/Locks.h"
 #include "mvm/Threads/Thread.h"
+#include "mvm/VirtualMachine.h"
+#include "MvmGC.h"
 #include "cterror.h"
 #include <cerrno>
 #include <sys/time.h>
@@ -186,3 +188,145 @@
 
   return res;
 }
+
+
+void ThinLock::overflowThinLock(gc* object) {
+  llvm_gcroot(object, 0);
+  FatLock* obj = Thread::get()->MyVM->allocateFatLock(object);
+  obj->acquireAll(object, (ThinCountMask >> ThinCountShift) + 1);
+  uintptr_t oldLock = object->header;
+  object->header = obj->getID() | (oldLock & NonLockBitsMask);
+}
+ 
+/// initialise - Initialise the value of the lock.
+///
+void ThinLock::initialise(gc* object) {
+  llvm_gcroot(object, 0);
+  uintptr_t oldValue = 0;
+  uintptr_t newValue = 0;
+  uintptr_t yieldedValue = 0;
+  do {
+    oldValue = object->header;
+    newValue = oldValue & NonLockBitsMask;
+    yieldedValue = __sync_val_compare_and_swap(&object->header, oldValue, newValue);
+  } while (yieldedValue != oldValue);
+}
+  
+FatLock* ThinLock::changeToFatlock(gc* object) {
+  llvm_gcroot(object, 0);
+  if (!(object->header & FatMask)) {
+    FatLock* obj = Thread::get()->MyVM->allocateFatLock(object);
+    uint32 count = (object->header & ThinCountMask) >> ThinCountShift;
+    obj->acquireAll(object, count + 1);
+    uintptr_t oldLock = object->header;
+    object->header = obj->getID() | (oldLock & NonLockBitsMask);
+    return obj;
+  } else {
+    FatLock* res = Thread::get()->MyVM->getFatLockFromID(object->header);
+    assert(res && "Lock deallocated while held.");
+    return res;
+  }
+}
+
+void ThinLock::acquire(gc* object) {
+  llvm_gcroot(object, 0);
+start:
+  uint64_t id = mvm::Thread::get()->getThreadID();
+  uintptr_t oldValue = object->header;
+  uintptr_t newValue = id | (oldValue & NonLockBitsMask);
+  uintptr_t val = __sync_val_compare_and_swap(&object->header, oldValue & NonLockBitsMask,
+                                              newValue);
+
+  if (val != (oldValue & NonLockBitsMask)) {
+    //fat!
+    if (!(val & FatMask)) {
+      if ((val & Thread::IDMask) == id) {
+        if ((val & ThinCountMask) != ThinCountMask) {
+          object->header += ThinCountAdd;
+        } else {
+          overflowThinLock(object);
+        }
+      } else {
+        FatLock* obj = Thread::get()->MyVM->allocateFatLock(object);
+        uintptr_t val = obj->getID();
+loop:
+        while (object->header & ~NonLockBitsMask) {
+          if (object->header & FatMask) {
+            obj->deallocate();
+            goto end;
+          }
+          else mvm::Thread::yield();
+        }
+        
+        oldValue = object->header;
+        newValue = val | (oldValue & NonLockBitsMask);
+        uintptr_t test = __sync_val_compare_and_swap(&object->header,
+                                                     oldValue & NonLockBitsMask,
+                                                      newValue);
+        if (test != (oldValue & NonLockBitsMask)) goto loop;
+        if (!obj->acquire(object)) goto start;
+      }
+    } else {
+end:
+      FatLock* obj = Thread::get()->MyVM->getFatLockFromID(object->header);
+      if (obj) {
+        if (!obj->acquire(object)) goto start;
+      } else {
+        goto start;
+      }
+    }
+  }
+
+  assert(owner(object) && "Not owner after quitting acquire!");
+}
+
+/// release - Release the lock.
+void ThinLock::release(gc* object) {
+  llvm_gcroot(object, 0);
+  assert(owner(object) && "Not owner when entering release!");
+  uint64 id = mvm::Thread::get()->getThreadID();
+  if ((object->header & ~NonLockBitsMask) == id) {
+    object->header = object->header & NonLockBitsMask;
+  } else if (object->header & FatMask) {
+    FatLock* obj = Thread::get()->MyVM->getFatLockFromID(object->header);
+    assert(obj && "Lock deallocated while held.");
+    obj->release(object);
+  } else {
+    object->header -= ThinCountAdd;
+  }
+}
+
+/// owner - Returns true if the curren thread is the owner of this object's
+/// lock.
+bool ThinLock::owner(gc* object) {
+  llvm_gcroot(object, 0);
+  if (object->header & FatMask) {
+    FatLock* obj = Thread::get()->MyVM->getFatLockFromID(object->header);
+    if (obj) return obj->owner();
+  } else {
+    uint64 id = mvm::Thread::get()->getThreadID();
+    if ((object->header & Thread::IDMask) == id) return true;
+  }
+  return false;
+}
+
+mvm::Thread* ThinLock::getOwner(gc* object) {
+  llvm_gcroot(object, 0);
+  if (object->header & FatMask) {
+    FatLock* obj = Thread::get()->MyVM->getFatLockFromID(object->header);
+    if (obj) return obj->getOwner();
+    return 0;
+  } else {
+    return (mvm::Thread*)(object->header & mvm::Thread::IDMask);
+  }
+}
+
+/// getFatLock - Get the fat lock is the lock is a fat lock, 0 otherwise.
+FatLock* ThinLock::getFatLock(gc* object) {
+  llvm_gcroot(object, 0);
+  if (object->header & FatMask) {
+    return Thread::get()->MyVM->getFatLockFromID(object->header);
+  } else {
+    return NULL;
+  }
+}

Modified: vmkit/trunk/mmtk/mmtk-j3/Memory.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/mmtk/mmtk-j3/Memory.cpp?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/mmtk/mmtk-j3/Memory.cpp (original)
+++ vmkit/trunk/mmtk/mmtk-j3/Memory.cpp Sun Aug 22 05:03:36 2010
@@ -43,12 +43,12 @@
 
 extern "C" void
 Java_org_j3_mmtk_Memory_mprotect__Lorg_vmmagic_unboxed_Address_2I (JavaObject* M, uintptr_t address, sint32 size) {
-  UNIMPLEMENTED();
+  mprotect((void*)address, size, PROT_NONE);
 }
 
 extern "C" void
 Java_org_j3_mmtk_Memory_munprotect__Lorg_vmmagic_unboxed_Address_2I (JavaObject* M, uintptr_t address, sint32 size) {
-  UNIMPLEMENTED();
+  mprotect((void*)address, size, PROT_READ | PROT_WRITE);
 }
 
 extern "C" void

Modified: vmkit/trunk/mmtk/mmtk-j3/ObjectModel.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/mmtk/mmtk-j3/ObjectModel.cpp?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/mmtk/mmtk-j3/ObjectModel.cpp (original)
+++ vmkit/trunk/mmtk/mmtk-j3/ObjectModel.cpp Sun Aug 22 05:03:36 2010
@@ -25,12 +25,12 @@
 }
 
 extern "C" uintptr_t Java_org_j3_mmtk_ObjectModel_readAvailableBitsWord__Lorg_vmmagic_unboxed_ObjectReference_2 (JavaObject* OM, JavaObject* obj) {
-  return obj->lock.lock;
+  return obj->header;
 }
 
 extern "C" void Java_org_j3_mmtk_ObjectModel_writeAvailableBitsWord__Lorg_vmmagic_unboxed_ObjectReference_2Lorg_vmmagic_unboxed_Word_2 (
     JavaObject* OM, JavaObject* obj, uintptr_t val) {
-  obj->lock.lock = val;
+  obj->header = val;
 }
 
 extern "C" JavaObject* Java_org_j3_mmtk_ObjectModel_objectStartRef__Lorg_vmmagic_unboxed_ObjectReference_2 (JavaObject* OM, JavaObject* obj) {
@@ -62,13 +62,14 @@
 }
 
 extern "C" uintptr_t Java_org_j3_mmtk_ObjectModel_prepareAvailableBits__Lorg_vmmagic_unboxed_ObjectReference_2 (JavaObject* OM, JavaObject* obj) {
-  return obj->lock.lock;
+  return obj->header;
 }
 
 extern "C" uint8_t
 Java_org_j3_mmtk_ObjectModel_attemptAvailableBits__Lorg_vmmagic_unboxed_ObjectReference_2Lorg_vmmagic_unboxed_Word_2Lorg_vmmagic_unboxed_Word_2(
     JavaObject* OM, JavaObject* obj, intptr_t oldValue, intptr_t newValue) { 
-  return __sync_bool_compare_and_swap(&(obj->lock.lock), oldValue, newValue);
+  intptr_t val = __sync_val_compare_and_swap(&(obj->header), oldValue, newValue);
+  return (val == oldValue);
 }
 
 extern "C" void Java_org_j3_bindings_Bindings_memcpy__Lorg_vmmagic_unboxed_ObjectReference_2Lorg_vmmagic_unboxed_ObjectReference_2I(

Modified: vmkit/trunk/mmtk/mmtk-j3/VM.cpp
URL: http://llvm.org/viewvc/llvm-project/vmkit/trunk/mmtk/mmtk-j3/VM.cpp?rev=111775&r1=111774&r2=111775&view=diff
==============================================================================
--- vmkit/trunk/mmtk/mmtk-j3/VM.cpp (original)
+++ vmkit/trunk/mmtk/mmtk-j3/VM.cpp Sun Aug 22 05:03:36 2010
@@ -58,5 +58,9 @@
 }
 
 extern "C" bool Java_org_j3_runtime_VM_verifyAssertions__ () {
+#ifdef DEBUG
   return true;
+#else
+  return false;
+#endif
 }





More information about the vmkit-commits mailing list