remove debug printouts
[IRC.git] / Robust / src / Runtime / thread.c
index 30a97db6ee844344cab8ebe5b847362ead809aa6..99439b6d307db6188dd98bd2258aae28c5a0b912 100644 (file)
@@ -46,6 +46,7 @@ pthread_mutex_t atomiclock;
 pthread_mutex_t joinlock;
 pthread_cond_t joincond;
 pthread_key_t threadlocks;
+pthread_key_t macthreadid;
 pthread_mutex_t threadnotifylock;
 pthread_cond_t threadnotifycond;
 pthread_key_t oidval;
@@ -54,6 +55,8 @@ pthread_key_t oidval;
 #ifndef MAC
 extern __thread struct listitem litem;
 #else
+pthread_key_t memorybasekey;
+pthread_key_t memorytopkey;
 pthread_key_t litemkey;
 #endif
 extern struct listitem * list;
@@ -72,12 +75,10 @@ void threadexit() {
 #else
   struct lockvector *lptr=&lvector;
 #endif
-  for(lptr->index--;lptr->index>=0;lptr->index--) {
+  for(lptr->index--; lptr->index>=0; lptr->index--) {
     if (lptr->locks[lptr->index].islastlock) {
-      struct ___Object___ *ll=lptr->locks[lptr->index].object;
+      ObjectPtr ll=lptr->locks[lptr->index].object;
       ll->tid=0;
-      BARRIER();
-      ll->lockcount=0;
     }
   }
 
@@ -121,9 +122,9 @@ transstart:
   {
     transStart();
     ptr = transRead(oidvalue);
-    struct ___Thread___ *p = (struct ___Thread___ *) ptr;
+    ThreadPtr p = (ThreadPtr) ptr;
     p->___threadDone___ = 1;
-    *((unsigned int *)&((struct ___Object___ *) p)->___localcopy___) |=DIRTY;
+    *((unsigned int *)&((ObjectPtr) p)->___localcopy___) |=DIRTY;
     if(transCommit() != 0) {
       goto transstart;
     }
@@ -132,6 +133,15 @@ transstart:
   pthread_exit(NULL);
 }
 
+#ifdef MAC
+void threadhandler(int sig) {
+  printf("We just took sig=%d\n",sig);
+  printf("signal\n");
+  printf("To get stack trace, set breakpoint in threadhandler in gdb\n");
+
+  threadexit();
+}
+#else
 void threadhandler(int sig, struct sigcontext ctx) {
   void *buffer[100];
   char **strings;
@@ -152,12 +162,13 @@ void threadhandler(int sig, struct sigcontext ctx) {
     perror("backtrace_symbols");
     exit(EXIT_FAILURE);
   }
-  
+
   for (j = 0; j < nptrs; j++)
     printf("%s\n", strings[j]);
-  
+
   threadexit();
 }
+#endif
 
 #define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
 
@@ -175,8 +186,11 @@ void initializethreads() {
   pthread_mutex_init(&joinlock,NULL);
   pthread_cond_init(&joincond,NULL);
 #ifdef MAC
+  pthread_key_create(&macthreadid, NULL);
   pthread_key_create(&threadlocks, NULL);
-  pthread_key_create(&litem, NULL);
+  pthread_key_create(&memorybasekey, NULL);
+  pthread_key_create(&memorytopkey, NULL);
+  pthread_key_create(&litemkey, NULL);
 #endif
   processOptions();
   initializeexithandler();
@@ -236,7 +250,7 @@ void initializethreads() {
   lockedobjs=calloc(1, sizeof(struct objlist));
   objlockscope = calloc(1, sizeof(objlockstate_t));
   pthread_mutex_init(&lockedobjstore, NULL);
-  { 
+  {
     int i;
     for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
       typesCausingAbort[i].numaccess = 0;
@@ -249,10 +263,18 @@ void initializethreads() {
 #ifdef MAC
   struct listitem *litem=malloc(sizeof(struct listitem));
   struct lockvector *lvector=malloc(sizeof(struct lockvector));
-  litem->lockvector=lvector;
+  litem->lvector=lvector;
   lvector->index=0;
   pthread_setspecific(threadlocks, lvector);
+  pthread_setspecific(macthreadid, (void *)0);
   pthread_setspecific(litemkey, litem);
+  char ** memorybase=malloc(sizeof(char *));
+  *memorybase=NULL;
+  pthread_setspecific(memorybasekey, memorybase);
+  char ** memorytop=malloc(sizeof(char *));
+  *memorytop=NULL;
+  pthread_setspecific(memorytopkey, memorytop);
+
   litem->prev=NULL;
   litem->next=list;
   if(list!=NULL)
@@ -260,9 +282,14 @@ void initializethreads() {
   list=litem;
 #else
   //Add our litem to list of threads
+  mythreadid=0;
   litem.prev=NULL;
   litem.next=list;
   litem.lvector=&lvector;
+#ifdef JNI
+  litem.jnirefs=&jnirefs;
+  jnirefs=NULL;
+#endif
   lvector.index=0;
   if(list!=NULL)
     list->prev=&litem;
@@ -274,7 +301,10 @@ void initializethreads() {
 }
 
 #if defined(THREADS)||defined(STM)
-void initthread(struct ___Thread___ * ___this___) {
+int threadcounter=0;
+
+#ifdef D___Thread______nativeCreate____
+void initthread(ThreadPtr ___this___) {
 #ifdef AFFINITY
   set_affinity();
 #endif
@@ -298,14 +328,27 @@ void initthread(struct ___Thread___ * ___this___) {
   //Add our litem to list of threads
 #ifdef MAC
   struct listitem litem;
-  pthread_setspecific(litemkey, &litem);
   struct lockvector lvector;
+  char * memorybase=NULL;
+  char * memorytop=NULL;
+  pthread_setspecific(litemkey, &litem);
+  pthread_setspecific(memorybasekey, &memorybase);
+  pthread_setspecific(memorytopkey, &memorytop);
   pthread_setspecific(threadlocks, &lvector);
+#endif
+#ifdef JNI
+  litem.jnirefs=&jnirefs;
+  jnirefs=NULL;
 #endif
   litem.lvector=&lvector;
   lvector.index=0;
   litem.prev=NULL;
   pthread_mutex_lock(&gclistlock);
+#ifdef MAC
+  pthread_setspecific(macthreadid, (void *)((long)(++threadcounter)));
+#else
+  mythreadid=++threadcounter;
+#endif
   litem.next=list;
   if(list!=NULL)
     list->prev=&litem;
@@ -349,16 +392,16 @@ void initthread(struct ___Thread___ * ___this___) {
     perror("arraystack");
 #endif
 #endif
- ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
- objstrDelete(t_cache);
- objstrDelete(t_reserve);
- t_chashDelete();
- free(newobjs);
 ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
 objstrDelete(t_cache);
 objstrDelete(t_reserve);
 t_chashDelete();
 free(newobjs);
 #ifdef STMSTATS
- free(lockedobjs);
 free(lockedobjs);
 #endif
 #endif
-  ___this___=(struct ___Thread___ *) p[2];
+  ___this___=(ThreadPtr) p[2];
 #else
   ___Thread______staticStart____L___Thread___(___this___);
 #endif
@@ -381,6 +424,7 @@ void initthread(struct ___Thread___ * ___this___) {
   pthread_mutex_unlock(&gclistlock);
 }
 #endif
+#endif
 
 #ifdef D___Thread______sleep____J
 void CALL11(___Thread______sleep____J, long long ___millis___, long long ___millis___) {
@@ -416,13 +460,13 @@ void CALL00(___Thread______abort____) {
 #ifdef RECOVERY
 // return if the machine is dead
 #ifdef D___Thread______nativeGetStatus____I
-int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
+int CALL12(___Thread______nativeGetStatus____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
   return getStatus(___mid___);
 }
 #endif
-#else 
+#else
 #ifdef D___Thread______nativeGetStatus____I
-int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
+int CALL12(___Thread______nativeGetStatus____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
   return 0;
 }
 #endif
@@ -431,7 +475,7 @@ int CALL12(___Thread______nativeGetStatus____I, int ___mid___, struct ___Thread_
 #ifdef DSTM
 /* Add thread join capability */
 #ifdef D___Thread______join____
-void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
+void CALL01(___Thread______join____, ThreadPtr ___this___) {
   unsigned int *oidarray;
   unsigned short *versionarray, version;
   objheader_t *ptr;
@@ -439,7 +483,7 @@ void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
 transstart:
   transStart();
   ptr = transRead((unsigned int) VAR(___this___));
-  struct ___Thread___ *p = (struct ___Thread___ *) ptr;
+  ThreadPtr p = (ThreadPtr) ptr;
 #ifdef THREADJOINDEBUG
   printf("Start join process for Oid = %x\n", (unsigned int) VAR(___this___));
 #endif
@@ -497,11 +541,11 @@ transstart:
 
 #if defined(THREADS)||defined(STM)
 #ifdef D___Thread______nativeJoin____
-void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
+void CALL01(___Thread______nativeJoin____, ThreadPtr ___this___) {
   pthread_mutex_lock(&joinlock);
   while(!VAR(___this___)->___finished___) {
 #ifdef PRECISE_GC
-  stopforgc((struct garbagelist *)___params___);
+    stopforgc((struct garbagelist *)___params___);
 #endif
     pthread_cond_wait(&joincond, &joinlock);
 #ifdef PRECISE_GC
@@ -513,7 +557,7 @@ void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
 #endif
 
 #ifdef D___Thread______nativeCreate____
-void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
+void CALL01(___Thread______nativeCreate____, ThreadPtr ___this___) {
   pthread_t thread;
   int retval;
   pthread_attr_t nattr;
@@ -539,7 +583,7 @@ void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
 
 #ifdef DSTM
 #ifdef D___Thread______start____I
-void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
+void CALL12(___Thread______start____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
 }
 #endif
@@ -573,9 +617,9 @@ void initDSMthread(int *ptr) {
   list=&litem;
   pthread_mutex_unlock(&gclistlock);
 
-  ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
+  ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(p);
 #else
-  ((void(*) (void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
+  ((void (*)(void *))virtualtable[type*MAXCOUNT+RUNMETHOD])(oid);
 #endif
   threadData = calloc(1, sizeof(unsigned int));
   *((unsigned int *) threadData) = oid;
@@ -602,7 +646,7 @@ transstart:
   {
     transStart();
     tmp  = transRead((unsigned int) oid);
-    ((struct ___Thread___ *)tmp)->___threadDone___ = 1;
+    ((ThreadPtr)tmp)->___threadDone___ = 1;
     *((unsigned int *)&((struct ___Object___ *) tmp)->___localcopy___) |=DIRTY;
     if(transCommit()!= 0) {
       goto transstart;
@@ -627,7 +671,7 @@ void startDSMthread(int oid, int objType) {
   ptr[0]=oid;
   ptr[1]=objType;
   pthread_key_create(&oidval, globalDestructor);
-  
+
   do {
     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initDSMthread,  ptr);
     if (retval!=0)