#ifdef THREADS
#include "thread.h"
#endif
+#include "mlp_lock.h"
#ifdef D___Object______nativehashCode____
int CALL01(___Object______nativehashCode____, 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)
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
}
#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
#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);