From: bdemsky Date: Wed, 13 May 2009 01:22:50 +0000 (+0000) Subject: try to simplify garbage collector lock X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=52c061c8ea193831f19e0220137a6255f23de1b1;p=IRC.git try to simplify garbage collector lock --- diff --git a/Robust/src/Runtime/garbage.c b/Robust/src/Runtime/garbage.c index 42fc413e..a30cb5f8 100644 --- a/Robust/src/Runtime/garbage.c +++ b/Robust/src/Runtime/garbage.c @@ -43,6 +43,9 @@ extern struct RuntimeHash *fdtoobject; int needtocollect=0; struct listitem * list=NULL; int listcount=0; +#ifndef MAC +__thread struct listitem litem; +#endif #endif //Need to check if pointers are transaction pointers @@ -581,68 +584,58 @@ void * tomalloc(int size) { #if defined(THREADS)||defined(DSTM)||defined(STM) void checkcollect(void * ptr) { - struct listitem * tmp=stopforgc((struct garbagelist *)ptr); + stopforgc((struct garbagelist *)ptr); pthread_mutex_lock(&gclock); // Wait for GC - restartaftergc(tmp); + restartaftergc(); pthread_mutex_unlock(&gclock); } #ifdef DSTM void checkcollect2(void * ptr) { int ptrarray[]={1, (int)ptr, (int) revertlist}; - struct listitem * tmp=stopforgc((struct garbagelist *)ptrarray); + stopforgc((struct garbagelist *)ptrarray); pthread_mutex_lock(&gclock); // Wait for GC - restartaftergc(tmp); + restartaftergc(); pthread_mutex_unlock(&gclock); revertlist=(struct ___Object___*)ptrarray[2]; } #endif -struct listitem * stopforgc(struct garbagelist * ptr) { - struct listitem * litem=malloc(sizeof(struct listitem)); - litem->stackptr=ptr; +void stopforgc(struct garbagelist * ptr) { +#ifndef MAC + litem.stackptr=ptr; #ifdef THREADS - litem->locklist=pthread_getspecific(threadlocks); + litem.locklist=pthread_getspecific(threadlocks); #endif #ifdef STM - litem->tc_size=c_size; - litem->tc_table=&c_table; - litem->tc_list=&c_list; - litem->tc_structs=&c_structs; - litem->objlist=newobjs; + litem.tc_size=c_size; + litem.tc_table=&c_table; + litem.tc_list=&c_list; + litem.tc_structs=&c_structs; + litem.objlist=newobjs; #ifdef STMSTATS - litem->lockedlist=lockedobjs; + litem.lockedlist=lockedobjs; +#endif + litem.base=&memorybase; +#endif +#else + //handle MAC + struct listitem *litem=pthread_getspecific(litemkey); + litem->stackptr=ptr; +#ifdef THREADS + litem->locklist=pthread_getspecific(threadlocks); #endif - litem->base=&memorybase; #endif - litem->prev=NULL; pthread_mutex_lock(&gclistlock); - litem->next=list; - if(list!=NULL) - list->prev=litem; - list=litem; listcount++; pthread_cond_signal(&gccond); pthread_mutex_unlock(&gclistlock); - return litem; } -void restartaftergc(struct listitem * litem) { +void restartaftergc() { pthread_mutex_lock(&gclistlock); -#ifdef THREADS - pthread_setspecific(threadlocks, litem->locklist); -#endif - if (litem->prev==NULL) { - list=litem->next; - } else { - litem->prev->next=litem->next; - } - if (litem->next!=NULL) { - litem->next->prev=litem->prev; - } listcount--; pthread_mutex_unlock(&gclistlock); - free(litem); } #endif @@ -669,9 +662,9 @@ void * mygcmalloc(struct garbagelist * stackptr, int size) { void *ptr; #if defined(THREADS)||defined(DSTM)||defined(STM) if (pthread_mutex_trylock(&gclock)!=0) { - struct listitem *tmp=stopforgc(stackptr); + stopforgc(stackptr); pthread_mutex_lock(&gclock); - restartaftergc(tmp); + restartaftergc(); } #endif ptr=curr_heapptr; diff --git a/Robust/src/Runtime/garbage.h b/Robust/src/Runtime/garbage.h index aa3451e3..f3428c2e 100644 --- a/Robust/src/Runtime/garbage.h +++ b/Robust/src/Runtime/garbage.h @@ -36,8 +36,8 @@ void fixtags(); #if defined(THREADS)||defined(DSTM)||defined(STM) extern int needtocollect; void checkcollect(void * ptr); -struct listitem * stopforgc(struct garbagelist * ptr); -void restartaftergc(struct listitem * litem); +void stopforgc(struct garbagelist * ptr); +void restartaftergc(); #endif void * tomalloc(int size); void collect(struct garbagelist *stackptr); diff --git a/Robust/src/Runtime/object.c b/Robust/src/Runtime/object.c index a935927d..01037384 100644 --- a/Robust/src/Runtime/object.c +++ b/Robust/src/Runtime/object.c @@ -25,11 +25,11 @@ int CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___) { VAR(___this___)->lockcount++; } else { #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif pthread_mutex_lock(&objlock); #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif while(1) { if (VAR(___this___)->tid==0) { @@ -45,11 +45,11 @@ int CALL01(___Object______MonitorEnter____, struct ___Object___ * ___this___) { } { #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif pthread_cond_wait(&objcond, &objlock); #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif } } diff --git a/Robust/src/Runtime/runtime.c b/Robust/src/Runtime/runtime.c index 11b45474..c8161aa4 100644 --- a/Robust/src/Runtime/runtime.c +++ b/Robust/src/Runtime/runtime.c @@ -210,11 +210,11 @@ void CALL00(___Barrier______enterBarrier____) { // Synchronization point int ret; #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif ret = pthread_barrier_wait(&barrier); #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) { printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__); diff --git a/Robust/src/Runtime/socket.c b/Robust/src/Runtime/socket.c index cb7b77e9..0b855036 100644 --- a/Robust/src/Runtime/socket.c +++ b/Robust/src/Runtime/socket.c @@ -28,7 +28,7 @@ int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port_ sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int))); #if defined(THREADS)||defined(DSTM)||defined(STM) #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif #endif do { @@ -36,7 +36,7 @@ int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port_ } while (rc<0 && errno==EINTR); /* repeat if interrupted */ #if defined(THREADS)||defined(DSTM)||defined(STM) #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif #endif @@ -299,7 +299,7 @@ int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSo int newfd; #if defined(THREADS)||defined(DSTM)||defined(STM) #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif #endif newfd=accept(fd, (struct sockaddr *)&sin, &sinlen); @@ -307,7 +307,7 @@ int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSo setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag)); #if defined(THREADS)||defined(DSTM)||defined(STM) #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif #endif if (newfd<0) { @@ -377,7 +377,7 @@ int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, #if defined(THREADS)||defined(DSTM)||defined(STM) #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif #endif int byteread=-1; @@ -387,7 +387,7 @@ int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, } while(byteread==-1&&errno==EINTR); #if defined(THREADS)||defined(DSTM)||defined(STM) #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif #endif diff --git a/Robust/src/Runtime/thread.c b/Robust/src/Runtime/thread.c index 678e2847..83a08a9b 100644 --- a/Robust/src/Runtime/thread.c +++ b/Robust/src/Runtime/thread.c @@ -33,6 +33,14 @@ pthread_mutex_t threadnotifylock; pthread_cond_t threadnotifycond; pthread_key_t oidval; +#if defined(THREADS) || defined(DSTM) || defined(STM) +#ifndef MAC +extern __thread struct listitem litem; +#else +pthread_key_t litemkey; +#endif +#endif + void threadexit() { #ifdef DSTM objheader_t* ptr; @@ -55,6 +63,31 @@ void threadexit() { pthread_mutex_unlock(&objlock); #endif pthread_mutex_lock(&gclistlock); +#ifdef THREADS + pthread_setspecific(threadlocks, litem->locklist); +#endif +#ifndef MAC + if (litem.prev==NULL) { + list=litem.next; + } else { + litem.prev->next=litem.next; + } + if (litem.next!=NULL) { + litem.next->prev=litem.prev; + } +#else + { + struct listitem *litem=pthread_getspecific(litemkey); + if (litem->prev==NULL) { + list=litem->next; + } else { + litem->prev->next=litem->next; + } + if (litem->next!=NULL) { + litem->next->prev=litem->prev; + } + } +#endif threadcount--; pthread_cond_signal(&gccond); pthread_mutex_unlock(&gclistlock); @@ -97,6 +130,9 @@ void initializethreads() { pthread_mutex_init(&joinlock,NULL); pthread_cond_init(&joincond,NULL); pthread_key_create(&threadlocks, NULL); +#ifdef MAC + pthread_key_create(&litem, NULL); +#endif processOptions(); initializeexithandler(); @@ -134,6 +170,19 @@ void initializethreads() { void initthread(struct ___Thread___ * ___this___) { #ifdef PRECISE_GC INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___}; + //Add our litem to list of threads +#ifdef MAC + struct listitem litem; + pthread_setspecific(litemkey, &litem); +#endif + litem.prev=NULL; + pthread_mutex_lock(&gclistlock); + litem.next=list; + if(list!=NULL) + list->prev=&litem; + list=&litem; + pthread_mutex_unlock(&gclistlock); + #ifdef THREADS ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p); #else @@ -165,6 +214,17 @@ void initthread(struct ___Thread___ * ___this___) { pthread_mutex_unlock(&joinlock); pthread_mutex_lock(&gclistlock); +#ifdef THREADS + pthread_setspecific(threadlocks, litem->locklist); +#endif + if (litem.prev==NULL) { + list=litem.next; + } else { + litem.prev->next=litem.next; + } + if (litem.next!=NULL) { + litem.next->prev=litem.prev; + } threadcount--; pthread_cond_signal(&gccond); pthread_mutex_unlock(&gclistlock); @@ -174,13 +234,13 @@ void initthread(struct ___Thread___ * ___this___) { void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) { #if defined(THREADS)||defined(STM) #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif #endif usleep(___millis___); #if defined(THREADS)||defined(STM) #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif #endif } @@ -229,11 +289,11 @@ transstart: versionarray[0] = version; /* Request Notification */ #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif reqNotify(oidarray, versionarray, 1); #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif free(oidarray); free(versionarray); @@ -247,14 +307,14 @@ transstart: #if defined(THREADS)||defined(STM) void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) { #ifdef PRECISE_GC - struct listitem *tmp=stopforgc((struct garbagelist *)___params___); + stopforgc((struct garbagelist *)___params___); #endif pthread_mutex_lock(&joinlock); while(!VAR(___this___)->___finished___) pthread_cond_wait(&joincond, &joinlock); pthread_mutex_unlock(&joinlock); #ifdef PRECISE_GC - restartaftergc(tmp); + restartaftergc(); #endif } @@ -301,6 +361,20 @@ void initDSMthread(int *ptr) { free(ptr); #ifdef PRECISE_GC int p[]={1, 0 /* NULL */, oid}; +#ifdef MAC + struct listitem litem; + pthread_setspecific(litemkey, &litem); +#endif + + //Add our litem to list of threads + litem.prev=NULL; + pthread_mutex_lock(&gclistlock); + litem.next=list; + if(list!=NULL) + list->prev=&litem; + list=&litem; + pthread_mutex_unlock(&gclistlock); + ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p); #else ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid); @@ -309,6 +383,18 @@ void initDSMthread(int *ptr) { *((unsigned int *) threadData) = oid; pthread_setspecific(oidval, threadData); pthread_mutex_lock(&gclistlock); + +#ifdef THREADS + pthread_setspecific(threadlocks, litem->locklist); +#endif + if (litem.prev==NULL) { + list=litem.next; + } else { + litem.prev->next=litem.next; + } + if (litem.next!=NULL) { + litem.next->prev=litem.prev; + } threadcount--; pthread_cond_signal(&gccond); pthread_mutex_unlock(&gclistlock);