From 5b3ade3da574764efcfeacab8285bdc292ab4639 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Sun, 3 Apr 2011 08:56:04 +0000 Subject: [PATCH 1/1] changes --- Robust/src/Runtime/mlp_lock.h | 11 +++++++ Robust/src/Runtime/object.c | 61 ++++++++++++++++------------------- Robust/src/Runtime/runtime.c | 3 +- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Robust/src/Runtime/mlp_lock.h b/Robust/src/Runtime/mlp_lock.h index 57bd18ad..d99a3270 100644 --- a/Robust/src/Runtime/mlp_lock.h +++ b/Robust/src/Runtime/mlp_lock.h @@ -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; diff --git a/Robust/src/Runtime/object.c b/Robust/src/Runtime/object.c index cdf75048..607ff32d 100644 --- a/Robust/src/Runtime/object.c +++ b/Robust/src/Runtime/object.c @@ -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); diff --git a/Robust/src/Runtime/runtime.c b/Robust/src/Runtime/runtime.c index 7adccfe4..cffd1c37 100644 --- a/Robust/src/Runtime/runtime.c +++ b/Robust/src/Runtime/runtime.c @@ -1,3 +1,4 @@ + #include "runtime.h" #include "structdefs.h" #include @@ -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 -- 2.34.1