changes
authorbdemsky <bdemsky>
Sun, 3 Apr 2011 08:56:04 +0000 (08:56 +0000)
committerbdemsky <bdemsky>
Sun, 3 Apr 2011 08:56:04 +0000 (08:56 +0000)
Robust/src/Runtime/mlp_lock.h
Robust/src/Runtime/object.c
Robust/src/Runtime/runtime.c

index 57bd18ad1f9335c123225eef5718973877a87431..d99a32705d72f32b3c3ddf428ce2e162a661bc91 100644 (file)
@@ -102,6 +102,15 @@ static inline INTPTR CAS(volatile void *ptr, unsigned INTPTR old, unsigned INTPT
                       : "memory");
   return prev;
 }
+
+static inline long CAS32(volatile void *ptr, unsigned long old, unsigned long new){
+  unsigned long prev;
+  __asm__ __volatile__("lock; cmpxchgl %k1,%2"
+                      : "=a"(prev)
+                      : "r"(new), "m"(*__xg(ptr)), "0"(old)
+                      : "memory");
+  return prev;
+}
 #else
 static inline long CAS(volatile void *ptr, unsigned long old, unsigned long new){
   unsigned long prev;
@@ -111,8 +120,10 @@ static inline long CAS(volatile void *ptr, unsigned long old, unsigned long new)
                       : "memory");
   return prev;
 }
+#define CAS32 CAS
 #endif
 
+
 static inline int BARRIER(){
   CFENCE;
   return 1;
index cdf75048084c6cdf6b71c6e5e209bbc0b903e41f..607ff32de31c137a87b5fa683f7e9314a0c2201b 100644 (file)
@@ -9,6 +9,7 @@
 #ifdef THREADS
 #include "thread.h"
 #endif
+#include "mlp_lock.h"
 
 #ifdef D___Object______nativehashCode____
 int CALL01(___Object______nativehashCode____, struct ___Object___ * ___this___) {
@@ -35,17 +36,10 @@ int CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___) {
 #ifndef NOLOCK
   pthread_t self=pthread_self();
   if (self==VAR(___this___)->tid) {
-    VAR(___this___)->lockcount++;
+    atomic_inc(&VAR(___this___)->lockcount);
   } else {
-#ifdef PRECISE_GC
-    stopforgc((struct garbagelist *)___params___);
-#endif
-    pthread_mutex_lock(&objlock);
-#ifdef PRECISE_GC
-    restartaftergc();
-#endif
     while(1) {
-      if (VAR(___this___)->tid==0) {
+      if (CAS32(&VAR(___this___)->lockcount, 0, 1)==0) {
        VAR(___this___)->___prevlockobject___=NULL;
        VAR(___this___)->___nextlockobject___=(struct ___Object___ *)pthread_getspecific(threadlocks);
        if (VAR(___this___)->___nextlockobject___!=NULL)
@@ -54,13 +48,13 @@ int CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___) {
        VAR(___this___)->lockcount=1;
        VAR(___this___)->tid=self;
        pthread_mutex_unlock(&objlock);
-       break;
+       BARRIER();
+       return;
       }
       {
 #ifdef PRECISE_GC
        stopforgc((struct garbagelist *)___params___);
 #endif
-       pthread_cond_wait(&objcond, &objlock);
 #ifdef PRECISE_GC
        restartaftergc();
 #endif
@@ -69,62 +63,60 @@ int CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___) {
   }
 #endif
 }
+
 #ifdef D___Object______notify____
 void CALL01(___Object______notify____, struct ___Object___ * ___this___) {
-  pthread_mutex_lock(&objlock);
-  pthread_cond_broadcast(&objcond);
-  pthread_mutex_unlock(&objlock);
 }
 #endif
 #ifdef D___Object______notifyAll____
 void CALL01(___Object______notifyAll____, struct ___Object___ * ___this___) {
-  pthread_mutex_lock(&objlock);
-  pthread_cond_broadcast(&objcond);
-  pthread_mutex_unlock(&objlock);
 }
 #endif
 #ifdef D___Object______wait____
 void CALL01(___Object______wait____, struct ___Object___ * ___this___) {
-  //release lock
   pthread_t self=pthread_self();
   int lockcount=VAR(___this___)->lockcount;
+  //release lock
   if (VAR(___this___)->___prevlockobject___==NULL) {
     pthread_setspecific(threadlocks, VAR(___this___)->___nextlockobject___);
   } else
     VAR(___this___)->___prevlockobject___->___nextlockobject___=VAR(___this___)->___nextlockobject___;
   if (VAR(___this___)->___nextlockobject___!=NULL)
     VAR(___this___)->___nextlockobject___->___prevlockobject___=VAR(___this___)->___prevlockobject___;
+  VAR(___this___)->___nextlockobject___=NULL;
+  VAR(___this___)->___prevlockobject___=NULL;
   VAR(___this___)->lockentry=NULL;
-  VAR(___this___)->tid=0;  
-  //lock released
-  //wait
+  VAR(___this___)->tid=0;
+  //release lock
+  BARRIER();
+  VAR(___this___)->lockcount=0;
+  
+  //allow gc
 #ifdef PRECISE_GC
   stopforgc((struct garbagelist *)___params___);
 #endif
-  pthread_cond_wait(&objcond, &objlock);
-
-  //grab lock
-  pthread_mutex_lock(&objlock);
+  sched_yield();
 #ifdef PRECISE_GC
   restartaftergc();
 #endif
+
   while(1) {
-    if (VAR(___this___)->tid==0) {
+    if (CAS32(&VAR(___this___)->lockcount, 0, lockcount)==0) {
       VAR(___this___)->___prevlockobject___=NULL;
       VAR(___this___)->___nextlockobject___=(struct ___Object___ *)pthread_getspecific(threadlocks);
       if (VAR(___this___)->___nextlockobject___!=NULL)
        VAR(___this___)->___nextlockobject___->___prevlockobject___=VAR(___this___);
       pthread_setspecific(threadlocks, VAR(___this___));
-      VAR(___this___)->lockcount=lockcount;
+      VAR(___this___)->lockcount=1;
       VAR(___this___)->tid=self;
       pthread_mutex_unlock(&objlock);
-      break;
+      BARRIER();
+      return;
     }
     {
 #ifdef PRECISE_GC
       stopforgc((struct garbagelist *)___params___);
 #endif
-      pthread_cond_wait(&objcond, &objlock);
 #ifdef PRECISE_GC
       restartaftergc();
 #endif
@@ -137,20 +129,21 @@ int CALL01(___Object______MonitorExit____, struct ___Object___ * ___this___) {
 #ifndef NOLOCK
   pthread_t self=pthread_self();
   if (self==VAR(___this___)->tid) {
-    VAR(___this___)->lockcount--;
-    if (VAR(___this___)->lockcount==0) {
+    //release one lock...
+    BARRIER();
+    if (VAR(___this___)->lockcount==1) {
       if (VAR(___this___)->___prevlockobject___==NULL) {
        pthread_setspecific(threadlocks, VAR(___this___)->___nextlockobject___);
       } else
        VAR(___this___)->___prevlockobject___->___nextlockobject___=VAR(___this___)->___nextlockobject___;
       if (VAR(___this___)->___nextlockobject___!=NULL)
        VAR(___this___)->___nextlockobject___->___prevlockobject___=VAR(___this___)->___prevlockobject___;
+      VAR(___this___)->___nextlockobject___=NULL;
+      VAR(___this___)->___prevlockobject___=NULL;
       VAR(___this___)->lockentry=NULL;
       VAR(___this___)->tid=0;
     }
-    pthread_mutex_lock(&objlock);
-    pthread_cond_broadcast(&objcond);
-    pthread_mutex_unlock(&objlock);
+    atomic_dec(&VAR(___this___)->lockcount);
   } else {
 #ifdef MULTICORE
     BAMBOO_EXIT(0xf201);
index 7adccfe40c50614920c37a353f01556aa53fc92f..cffd1c375ebe2d377ea290f98008b4a692971db3 100644 (file)
@@ -1,3 +1,4 @@
+
 #include "runtime.h"
 #include "structdefs.h"
 #include <signal.h>
@@ -335,7 +336,7 @@ void CALL35(___System______arraycopy____L___Object____I_L___Object____I_I, int _
 #ifdef D___Runtime______availableProcessors____
 int CALL01(___Runtime______availableProcessors____, struct ___Runtime___ * ___this___) {
   printf("Unimplemented Runtime.availableProcessors\n");
-  return 2;
+  return 24;
 }
 #endif