From: adash Date: Thu, 7 May 2009 19:26:22 +0000 (+0000) Subject: race condition fixed: race due to thread rec becoming NULL before accessing its field... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e1026930b587bd6dd313190f857d08547158809e;p=IRC.git race condition fixed: race due to thread rec becoming NULL before accessing its field/element --- diff --git a/Robust/src/Runtime/STM/stm.c b/Robust/src/Runtime/STM/stm.c index 71bb1214..c731895b 100644 --- a/Robust/src/Runtime/STM/stm.c +++ b/Robust/src/Runtime/STM/stm.c @@ -12,6 +12,8 @@ #include "tm.h" #include "garbage.h" + +/*** Globals *****/ /* Thread transaction variables */ __thread objstr_t *t_cache; __thread objstr_t *t_reserve; @@ -27,8 +29,9 @@ int nSoftAbortAbort = 0; #ifdef STMSTATS /* Thread variable for locking/unlocking */ -__thread threadrec_t trec; +__thread threadrec_t *trec; __thread struct objlist * lockedobjs; +/** Global lock **/ int typesCausingAbort[TOTALNUMCLASSANDARRAY]; /******Keep track of objects and types causing aborts******/ /*TODO @@ -787,8 +790,36 @@ void getTotalAbortCount(int start, int stop, void *startptr, void *checkptr, cha * Locks an object that causes aborts **/ void needLock(objheader_t *header) { + int lockstatus; + threadrec_t *ptr; + while((lockstatus = pthread_mutex_trylock(&(header->objlock))) + && ((ptr = header->trec) == NULL)) { //retry + ; + } + if(!lockstatus) { //acquired lock + //TODO printf("%s() Got lock on type= %d in first try\n", __func__, TYPE(header)); + /* Reset blocked field */ + trec->blocked = 0; + /* Set trec */ + header->trec = trec; + } else { //failed to get lock + if(ptr->blocked == 1) { //ignore locking + return; + } else { //lock that blocks + pthread_mutex_lock(&(header->objlock)); + //TODO printf("%s() Got lock on type= %d in second try\n", __func__, TYPE(header)); + /* Reset blocked field */ + trec->blocked = 0; + /* Set trec */ + header->trec = trec; + } + } + + +#if 0 if(pthread_mutex_trylock(&(header->objlock))) { //busy and failed to get locked - trec.blocked = 1; //set blocked flag + trec->blocked = 1; //set blocked flag + while( while(header->trec == NULL) { //retry ; } @@ -798,17 +829,18 @@ void needLock(objheader_t *header) { pthread_mutex_lock(&(header->objlock)); //TODO printf("%s() Got lock on type= %d in second try\n", __func__, TYPE(header)); /* Reset blocked field */ - trec.blocked = 0; + trec->blocked = 0; /* Set trec */ - header->trec = &trec; + header->trec = trec; } } else { //acquired lock //TODO printf("%s() Got lock on type= %d in first try\n", __func__, TYPE(header)); /* Reset blocked field */ - trec.blocked = 0; + trec->blocked = 0; /* Set trec */ - header->trec = &trec; + header->trec = trec; } +#endif /* Save the locked object */ if (lockedobjs->offsetobjs[lockedobjs->offset++]=OID(header); diff --git a/Robust/src/Runtime/STM/tm.h b/Robust/src/Runtime/STM/tm.h index a1668206..2c98fc6a 100644 --- a/Robust/src/Runtime/STM/tm.h +++ b/Robust/src/Runtime/STM/tm.h @@ -20,7 +20,6 @@ #include #include #include -//#include "threadnotify.h" #include "stmlookup.h" #include "dsmlock.h" @@ -126,7 +125,7 @@ extern __thread struct objlist * newobjs; extern __thread objstr_t *t_cache; extern __thread objstr_t *t_reserve; #ifdef STMSTATS -extern __thread threadrec_t trec; +extern __thread threadrec_t *trec; extern __thread struct objlist * lockedobjs; #endif diff --git a/Robust/src/Runtime/thread.c b/Robust/src/Runtime/thread.c index 3f3a5898..18bb2281 100644 --- a/Robust/src/Runtime/thread.c +++ b/Robust/src/Runtime/thread.c @@ -115,7 +115,8 @@ void initializethreads() { t_reserve=NULL; t_chashCreate(CHASH_SIZE, CLOADFACTOR); #ifdef STMSTATS - trec.blocked = 0; + trec=calloc(1, sizeof(threadrec_t)); + trec->blocked = 0; lockedobjs=calloc(1, sizeof(struct objlist)); #endif #endif @@ -130,7 +131,8 @@ void initthread(struct ___Thread___ * ___this___) { #else newobjs=calloc(1, sizeof(struct objlist)); #ifdef STMSTATS - trec.blocked = 0; + trec=calloc(1, sizeof(threadrec_t)); + trec->blocked = 0; lockedobjs=calloc(1, sizeof(struct objlist)); #endif t_cache = objstrCreate(1048576);