remove debug printouts
[IRC.git] / Robust / src / Runtime / thread.c
index f851fd1e8fa992ff9f409ee33b3ab19818e33552..99439b6d307db6188dd98bd2258aae28c5a0b912 100644 (file)
@@ -1,16 +1,26 @@
 #include "runtime.h"
 #include <sys/types.h>
+#include <sys/mman.h>
 #include <unistd.h>
 #include <errno.h>
 #include <stdlib.h>
 #include "thread.h"
 #include "option.h"
 #include <signal.h>
+#include "methodheaders.h"
+#ifndef MULTICORE
+#include "mlp_lock.h"
+#endif
 
 #ifdef DSTM
+#ifdef RECOVERY
+#include <DSTM/interface_recovery/dstm.h>
+#include <DSTM/interface_recovery/llookup.h>
+#else
 #include <DSTM/interface/dstm.h>
 #include <DSTM/interface/llookup.h>
 #endif
+#endif
 
 #ifndef RAW
 #include <stdio.h>
@@ -19,6 +29,9 @@
 #include "tm.h"
 #endif
 #include <execinfo.h>
+#ifdef EVENTMONITOR
+#include "monitor.h"
+#endif
 
 
 int threadcount;
@@ -33,14 +46,17 @@ 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;
 
-#if defined(THREADS) || defined(DSTM) || defined(STM)
+#if defined(THREADS) || defined(DSTM) || defined(STM)||defined(MLP)
 #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;
@@ -54,23 +70,24 @@ void threadexit() {
   void *value;
 
 #ifdef THREADS
-  struct ___Object___ *ll=pthread_getspecific(threadlocks);
-  while(ll!=NULL) {
-    struct ___Object___ *llnext=ll->___nextlockobject___;
-    ll->___nextlockobject___=NULL;
-    ll->___prevlockobject___=NULL;
-    ll->lockcount=0;
-    ll->tid=0; //unlock it
-    ll=llnext;
+#ifdef MAC
+  struct lockvector *lptr=(struct lockvector *) pthread_getspecific(threadlocks);
+#else
+  struct lockvector *lptr=&lvector;
+#endif
+  for(lptr->index--; lptr->index>=0; lptr->index--) {
+    if (lptr->locks[lptr->index].islastlock) {
+      ObjectPtr ll=lptr->locks[lptr->index].object;
+      ll->tid=0;
+    }
   }
+
   pthread_mutex_lock(&objlock); //wake everyone up
   pthread_cond_broadcast(&objcond);
   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;
@@ -105,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;
     }
@@ -116,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;
@@ -136,14 +162,15 @@ 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
 
-struct primitivelist *pl;
+#define downpage(x) ((void *)(((INTPTR)x)&~((INTPTR)4095)))
 
 void initializethreads() {
   struct sigaction sig;
@@ -158,15 +185,26 @@ void initializethreads() {
   pthread_cond_init(&objcond,NULL);
   pthread_mutex_init(&joinlock,NULL);
   pthread_cond_init(&joincond,NULL);
-  pthread_key_create(&threadlocks, NULL);
 #ifdef MAC
-  pthread_key_create(&litem, NULL);
+  pthread_key_create(&macthreadid, NULL);
+  pthread_key_create(&threadlocks, NULL);
+  pthread_key_create(&memorybasekey, NULL);
+  pthread_key_create(&memorytopkey, NULL);
+  pthread_key_create(&litemkey, NULL);
 #endif
   processOptions();
   initializeexithandler();
+#ifdef AFFINITY
+  set_affinity();
+#endif
 
   //deprecated use of sighandler, but apparently still works
+#ifdef SANDBOX
+  sig.sa_handler=(void *)errorhandler;
+  abortenabled=0;
+#else
   sig.sa_handler=(void *)threadhandler;
+#endif
   sig.sa_flags=SA_RESTART;
   sigemptyset(&sig.sa_mask);
 
@@ -187,7 +225,24 @@ void initializethreads() {
   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
   ptrstack.count=0;
   primstack.count=0;
-  pl=&primstack;
+  branchstack.count=0;
+#if defined(STMARRAY)&&!defined(DUALVIEW)
+  arraystack.count=0;
+#endif
+  int a=mprotect((downpage(&ptrstack.buffer[1024])), 4096, PROT_NONE);
+  if (a==-1)
+    perror("ptrstack");
+  a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("primstack");
+  a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("branchstack");
+#if defined(STMARRAY)&&!defined(DUALVIEW)
+  a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("arraystack");
+#endif
 #endif
 #ifdef STMSTATS
   trec=calloc(1, sizeof(threadrec_t));
@@ -195,18 +250,31 @@ 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].numabort = 0;
       typesCausingAbort[i].numaccess = 0;
+      typesCausingAbort[i].numabort = 0;
+      typesCausingAbort[i].numtrans = 0;
     }
   }
 #endif
 #endif
 #ifdef MAC
   struct listitem *litem=malloc(sizeof(struct listitem));
+  struct lockvector *lvector=malloc(sizeof(struct lockvector));
+  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)
@@ -214,31 +282,78 @@ 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;
   list=&litem;
 #endif
+#ifdef EVENTMONITOR
+  createmonitor();
+#endif
 }
 
 #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
+#ifdef EVENTMONITOR
+  createmonitor();
+#endif
+#ifdef SANDBOX
+  struct sigaction sig;
+  abortenabled=0;
+  sig.sa_handler=(void *)errorhandler;
+  sig.sa_flags=SA_RESTART;
+  sigemptyset(&sig.sa_mask);
+
+  /* Catch bus errors, segmentation faults, and floating point exceptions*/
+  sigaction(SIGBUS,&sig,0);
+  sigaction(SIGSEGV,&sig,0);
+  sigaction(SIGFPE,&sig,0);
+#endif
 #ifdef PRECISE_GC
   INTPTR p[]={1, (INTPTR) NULL, (INTPTR) ___this___};
   //Add our litem to list of threads
 #ifdef MAC
   struct listitem 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;
   list=&litem;
   pthread_mutex_unlock(&gclistlock);
-  
 #ifdef THREADS
   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
 #else
@@ -258,17 +373,35 @@ void initthread(struct ___Thread___ * ___this___) {
   dc_t_chashCreate(CHASH_SIZE, CLOADFACTOR);
   ptrstack.count=0;
   primstack.count=0;
-#endif
- ___Thread____NNR____staticStart____L___Thread___((struct ___Thread____NNR____staticStart____L___Thread____params *)p);
- objstrDelete(t_cache);
- objstrDelete(t_reserve);
- t_chashDelete();
- free(newobjs);
+  branchstack.count=0;
+#if defined(STMARRAY)&&!defined(DUALVIEW)
+  arraystack.count=0;
+#endif
+  int a=mprotect(downpage(&ptrstack.buffer[1024]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("ptrstack");
+  a=mprotect(downpage(&primstack.array[MAXVALUES]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("primstack");
+  a=mprotect(downpage(&branchstack.array[MAXBRANCHES]), 4096, PROT_NONE);
+  if (a==-1)
+    perror("branchstack");
+#if defined(STMARRAY)&!defined(DUALVIEW)
+  a=mprotect(downpage(&arraystack.index[MAXARRAY]), 4096, PROT_NONE);
+  if (a==-1)
+    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);
 #ifdef STMSTATS
- free(lockedobjs);
 free(lockedobjs);
 #endif
 #endif
-  ___this___=(struct ___Thread___ *) p[2];
+  ___this___=(ThreadPtr) p[2];
 #else
   ___Thread______staticStart____L___Thread___(___this___);
 #endif
@@ -278,9 +411,6 @@ 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 {
@@ -294,30 +424,58 @@ 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___) {
 #if defined(THREADS)||defined(STM)
 #ifdef PRECISE_GC
   stopforgc((struct garbagelist *)___params___);
 #endif
 #endif
-  usleep(___millis___);
+  usleep(___millis___*1000);
 #if defined(THREADS)||defined(STM)
 #ifdef PRECISE_GC
   restartaftergc();
 #endif
 #endif
 }
+#endif
 
-#if defined(DSTM)|| defined(THREADS)||defined(STM)
+#ifdef D___Thread______yield____
 void CALL00(___Thread______yield____) {
   pthread_yield();
 }
 #endif
 
+#ifdef D___Thread______abort____
+void CALL00(___Thread______abort____) {
+#ifdef SANDBOX
+  _longjmp(aborttrans,1);
+#endif
+}
+#endif
+
+#ifdef DSTM
+#ifdef RECOVERY
+// return if the machine is dead
+#ifdef D___Thread______nativeGetStatus____I
+int CALL12(___Thread______nativeGetStatus____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
+  return getStatus(___mid___);
+}
+#endif
+#else
+#ifdef D___Thread______nativeGetStatus____I
+int CALL12(___Thread______nativeGetStatus____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
+  return 0;
+}
+#endif
+#endif
+#endif
 #ifdef DSTM
 /* Add thread join capability */
-void CALL01(___Thread______join____, struct ___Thread___ * ___this___) {
+#ifdef D___Thread______join____
+void CALL01(___Thread______join____, ThreadPtr ___this___) {
   unsigned int *oidarray;
   unsigned short *versionarray, version;
   objheader_t *ptr;
@@ -325,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
@@ -335,8 +493,15 @@ transstart:
 #endif
     transAbort();
     return;
-  } else {
-
+  }
+#ifdef RECOVERY
+  else if( checkiftheMachineDead(p->___mid___) == 0) {
+    printf("Thread oid = %x is dead\n", (unsigned int) VAR(___this___));
+    transAbort();
+    return;
+  }
+#endif
+  else {
     version = (ptr-1)->version;
     if((oidarray = calloc(1, sizeof(unsigned int))) == NULL) {
       printf("Calloc error %s, %d\n", __FILE__, __LINE__);
@@ -355,7 +520,12 @@ transstart:
 #ifdef PRECISE_GC
     stopforgc((struct garbagelist *)___params___);
 #endif
+
+#ifdef RECOVERY
+    reqNotify(oidarray, versionarray, 1,p->___mid___);
+#else
     reqNotify(oidarray, versionarray, 1);
+#endif
 #ifdef PRECISE_GC
     restartaftergc();
 #endif
@@ -367,13 +537,15 @@ transstart:
   return;
 }
 #endif
+#endif
 
 #if defined(THREADS)||defined(STM)
-void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
+#ifdef D___Thread______nativeJoin____
+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
@@ -382,8 +554,10 @@ void CALL01(___Thread______nativeJoin____, struct ___Thread___ * ___this___) {
   }
   pthread_mutex_unlock(&joinlock);
 }
+#endif
 
-void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
+#ifdef D___Thread______nativeCreate____
+void CALL01(___Thread______nativeCreate____, ThreadPtr ___this___) {
   pthread_t thread;
   int retval;
   pthread_attr_t nattr;
@@ -395,7 +569,6 @@ void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
   pthread_attr_setdetachstate(&nattr, PTHREAD_CREATE_DETACHED);
   INTPTR stacksize;
   pthread_attr_getstacksize(&nattr, &stacksize);
-  printf("STACKSIZE=%u\n",stacksize);
   do {
     retval=pthread_create(&thread, &nattr, (void * (*)(void *)) &initthread, VAR(___this___));
     if (retval!=0)
@@ -406,12 +579,15 @@ void CALL01(___Thread______nativeCreate____, struct ___Thread___ * ___this___) {
   pthread_attr_destroy(&nattr);
 }
 #endif
+#endif
 
 #ifdef DSTM
-void CALL12(___Thread______start____I, int ___mid___, struct ___Thread___ * ___this___, int ___mid___) {
+#ifdef D___Thread______start____I
+void CALL12(___Thread______start____I, int ___mid___, ThreadPtr ___this___, int ___mid___) {
   startRemoteThread((unsigned int)VAR(___this___), ___mid___);
 }
 #endif
+#endif
 
 #ifdef DSTM
 void globalDestructor(void *value) {
@@ -441,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;
@@ -470,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;
@@ -484,6 +660,8 @@ void startDSMthread(int oid, int objType) {
   int retval;
   pthread_attr_t nattr;
 
+//  printf("%s -> oid : %u\n",__func__,oid);
+
   pthread_mutex_lock(&gclistlock);
   threadcount++;
   pthread_mutex_unlock(&gclistlock);
@@ -493,6 +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)